forked from asranand7/Adverserial_Attack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcifar10_vgg.py
302 lines (247 loc) · 11.1 KB
/
cifar10_vgg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# -*- coding: utf-8 -*-
"""CIFAR10_VGG
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1XKcTW1rUrPTYy1fm8MrpfUCIHaiWnsuE
"""
# ================================================= GOOGLE COLAB SETTINGS ==============================================================================
!apt-get install -y -qq software-properties-common python-software-properties module-init-tools
!wget https://launchpad.net/~alessandro-strada/+archive/ubuntu/google-drive-ocamlfuse-beta/+build/15331130/+files/google-drive-ocamlfuse_0.7.0-0ubuntu1_amd64.deb
!dpkg -i google-drive-ocamlfuse_0.7.0-0ubuntu1_amd64.deb
!apt-get install -f
!apt-get -y install -qq fuse
from google.colab import auth
auth.authenticate_user()
from oauth2client.client import GoogleCredentials
creds = GoogleCredentials.get_application_default()
import getpass
!google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret} < /dev/null 2>&1 | grep URL
vcode = getpass.getpass()
!echo {vcode} | google-drive-ocamlfuse -headless -id={creds.client_id} -secret={creds.client_secret}
!mkdir -p drive
!google-drive-ocamlfuse drive
cd drive/Adverserial_Attacks
from os import path
from wheel.pep425tags import get_abbr_impl, get_impl_ver, get_abi_tag
platform = '{}{}-{}'.format(get_abbr_impl(), get_impl_ver(), get_abi_tag())
accelerator = 'cu80' #'cu80' if path.exists('/opt/bin/nvidia-smi') else 'cpu'
print('Platform:', platform, 'Accelerator:', accelerator)
!pip install --upgrade --force-reinstall -q http://download.pytorch.org/whl/{accelerator}/torch-0.4.0-{platform}-linux_x86_64.whl torchvision
import torch
print('Torch', torch.__version__, 'CUDA', torch.version.cuda)
print('Device:', torch.device('cuda:0'))
torch.cuda.is_available()
#===================================================== Import libraries ================================================================================
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import torch.backends.cudnn as cudnn
from torch.autograd import Variable
import torchvision
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
import numpy as np
# =================================================== Prepare the dataset ===============================================================================
mean_cifar10 = [0.485, 0.456, 0.406] # Mean and Std value hase been taken from a github implmentation online.
std_cifar10 = [0.229, 0.224, 0.225]
batch_size = 100
transform_train = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(mean_cifar10,std_cifar10),
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean_cifar10,std_cifar10),
])
trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download= True, transform=transform_train)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=batch_size, shuffle=True, num_workers=2)
testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transform_test)
testloader = torch.utils.data.DataLoader(testset, batch_size=batch_size, shuffle=False, num_workers=2)
classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck') # 10 Classes of the cifar-10
# ========================================== Visualising the dataset ==========================================================================
std= torch.FloatTensor(std_cifar10)
mean = torch.FloatTensor(mean_cifar10)
mean = mean[:,None,None]
std = std[:,None,None]
def imshow(img):
print(img.size())
img = img*std + mean # unnormalize
npimg = img.numpy()
plt.imshow(np.transpose(npimg, (1, 2, 0)))
# get some random training images
dataiter = iter(trainloader)
images, labels = dataiter.next()
# show images
imshow(torchvision.utils.make_grid(images[:4]))
# print labels
print(' '.join('%5s' % classes[labels[j]] for j in range(4)))
# ================================================= VGG-16 Network ================================================================================
class VGG16(nn.Module):
def __init__(self):
super(VGG16,self).__init__()
self.block1 = nn.Sequential(
nn.Conv2d(in_channels = 3,out_channels = 64,kernel_size = 3,padding = 1),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.Conv2d(in_channels = 64,out_channels = 64,kernel_size = 3, padding =1),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Dropout2d(0.3))
self.block2 = nn.Sequential(
nn.Conv2d(in_channels = 64,out_channels = 128,kernel_size = 3,padding = 1),
nn.BatchNorm2d(128),
nn.ReLU(),
nn.Conv2d(in_channels = 128,out_channels = 128,kernel_size = 3, padding =1),
nn.BatchNorm2d(128),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Dropout2d(0.4))
self.block3 = nn.Sequential(
nn.Conv2d(in_channels = 128,out_channels = 256,kernel_size = 3,padding = 1),
nn.BatchNorm2d(256),
nn.ReLU(),
nn.Conv2d(in_channels = 256,out_channels = 256,kernel_size = 3,padding = 1),
nn.BatchNorm2d(256),
nn.ReLU(),
nn.Conv2d(in_channels = 256,out_channels = 256,kernel_size = 3, padding =1),
nn.BatchNorm2d(256),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Dropout2d(0.4))
self.block4 = nn.Sequential(
nn.Conv2d(in_channels = 256,out_channels = 512,kernel_size = 3,padding = 1),
nn.BatchNorm2d(512),
nn.ReLU(),
nn.Conv2d(in_channels = 512,out_channels = 512,kernel_size = 3,padding = 1),
nn.BatchNorm2d(512),
nn.ReLU(),
nn.Conv2d(in_channels = 512,out_channels = 512,kernel_size = 3, padding =1),
nn.BatchNorm2d(512),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2) ,
nn.Dropout2d(0.4))
self.block5 = nn.Sequential(
nn.Conv2d(in_channels = 512,out_channels = 512,kernel_size = 3,padding = 1),
nn.BatchNorm2d(512),
nn.ReLU(),
nn.Conv2d(in_channels = 512,out_channels = 512,kernel_size = 3,padding = 1),
nn.BatchNorm2d(512),
nn.ReLU(),
nn.Conv2d(in_channels = 512,out_channels = 512,kernel_size = 3, padding =1),
nn.BatchNorm2d(512),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.Dropout2d(0.5) )
self.fc = nn.Sequential(
nn.Linear(512,100),
nn.Dropout(0.5),
nn.BatchNorm1d(100),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(100,10), )
def forward(self,x):
out = self.block1(x)
out = self.block2(out)
out = self.block3(out)
out = self.block4(out)
out = self.block5(out)
out = out.view(out.size(0),-1)
out = self.fc(out)
return out
# =============================================================== Model initialisation, Loss function and Optimizer =====================================
model = VGG16()
if torch.cuda.is_available():
model.cuda()
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(),lr = 0.001,momentum = 0.9,weight_decay = 0.006)
schedule = torch.optim.lr_scheduler.StepLR(optimizer,step_size=20,gamma = 0.7)
# ======================== Function to get the test accuracy ===============================================================================
def test():
correct = 0
total = 0
model.train(False)
with torch.no_grad():
for i,(images,labels)in enumerate(testloader):
if torch.cuda.is_available():
images = images.cuda()
labels = labels.cuda()
outputs = model(Variable(images))
labels = Variable(labels)
_,predicted = outputs.max(1)
total += labels.size(0)
correct += (predicted.eq(labels)).sum().item()
print('Test accuracy: %d %%' % (
100 * correct / total))
return 100*(correct/total)
#======================================================= Training =========================================================================
num_epochs = 200 # Train for 200 epochs
start_epoch = 0
total_step = len(trainloader)
train_loss = [] # Store the train_loss per epoch
test_accuracy = [] # Store the test_accuracy per epoch
for epoch in range(start_epoch,num_epochs):
model.train(True)
schedule.step()
epoch_loss = 0
i_count = 0
acc_total = 0
for i,(images,labels) in enumerate(trainloader):
if torch.cuda.is_available():
images = images.cuda()
labels = labels.cuda()
labels = Variable(labels)
optimizer.zero_grad()
outputs = model(Variable(images))
loss = criterion(outputs,labels)
epoch_loss += loss.item()
loss.backward()
optimizer.step()
_,predicted = outputs.max(1)
denom = labels.size(0)
correct = predicted.eq(labels).sum().item()
acc = 100*(correct/denom)
acc_total += acc
i_count = i_count + 1
if(i%20 == 0): # Print the loss per 20 iterations
print("Epoch: ",epoch," ","Iteration: ",i," loss: ",loss.item()," Train_iter Accuracy: ",acc)
train_loss.append(epoch_loss)
print("Epoch: ",epoch," ","Loss: ",epoch_loss," ","Train Accuracy :",acc_total/i_count) # Print train accuracy per epoch
print('\n')
test_acc = test() # Print the test accuracy per epoch
test_accuracy.append(test_acc)
if(epoch%10 == 0): # Save the model every 10 epoch
state = {
'model': model.state_dict(),
'acc' : test_acc,
'optim':optimizer.state_dict(),
'epoch' : epoch
}
path = './' + 'model_' + str(int(epoch)) +'_' + str(int(test_acc))+'.pth'
torch.save(state,path)
#======================================= Testing ===================================================================================================
test_acc = test() # Test error
print(test_acc)
# Per class accuracy
class_correct = list(0. for i in range(10)) # Individual class error
class_total = list(0. for i in range(10))
with torch.no_grad():
for data in testloader:
images, labels = data
if torch.cuda.is_available():
images = images.cuda()
labels = labels.cuda()
images = Variable(images)
labels = Variable(labels)
outputs = model(images)
_, predicted = torch.max(outputs, 1)
c = (predicted == labels).squeeze()
for i in range(4):
label = labels[i]
class_correct[label] += c[i].item()
class_total[label] += 1
for i in range(10):
print('Accuracy of %5s : %2d %%' % (
classes[i], 100 * class_correct[i] / class_total[i]))