-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathmodels.py
473 lines (402 loc) · 15.3 KB
/
models.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
import time
import random
import numpy as np
import torch
import torch.nn as nn
import torchvision.datasets as dsets
import torchvision.transforms as transforms
from torch.autograd import Variable
from torch.utils.data.dataset import Dataset
import os
from PIL import Image
#import pretrainedmodels
#import pretrainedmodels.utils as utils
import torchvision.models as models
# Hyperparameters
num_epochs = 50
batch_size = 128
learning_rate = 0.001
class IMAGENET():
def __init__(self, arch):
self.model = models.__dict__[arch](pretrained=True)
self.model.eval()
if torch.cuda.is_available():
self.model = self.model.cuda()
self.model = torch.nn.DataParallel(self.model, device_ids=[0])
def predict(self, image):
image = torch.clamp(image, -1, 1)
image = Variable(image, volatile=True).view(1,3,224,224)
output = self.model(image)
_, predict = torch.max(output.data, 1)
return predict[0]
def predict_batch(self, image):
image = torch.clamp(image, -1 ,1)
image = Variable(image, volatile=True)
if torch.cuda.is_available():
image = image.cuda()
output = self.model(image)
_, predict = torch.max(output.data, 1)
return predict
class CIFAR10(nn.Module):
def __init__(self):
super(CIFAR10, self).__init__()
self.features = self._make_layers()
self.fc1 = nn.Linear(3200,256)
self.relu = nn.ReLU()
self.fc2 = nn.Linear(256,256)
self.dropout = nn.Dropout(p=0.5)
self.fc3 = nn.Linear(256,10)
def forward(self, x):
out = self.features(x)
out = out.view(out.size(0), -1)
out = self.fc1(out)
out = self.relu(out)
out = self.dropout(out)
out = self.fc2(out)
out = self.relu(out)
out = self.dropout(out)
out = self.fc3(out)
return out
def _make_layers(self):
layers=[]
in_channels= 3
layers += [nn.Conv2d(in_channels, 64, kernel_size=3),
nn.BatchNorm2d(64),
nn.ReLU()]
layers += [nn.Conv2d(64, 64, kernel_size=3),
nn.BatchNorm2d(64),
nn.ReLU()]
layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
layers += [nn.Conv2d(64, 128, kernel_size=3),
nn.BatchNorm2d(128),
nn.ReLU()]
layers += [nn.Conv2d(128, 128, kernel_size=3),
nn.BatchNorm2d(128),
nn.ReLU()]
layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
return nn.Sequential(*layers)
def predict(self, image):
self.eval()
image = torch.clamp(image,0,1)
image = Variable(image, volatile=True).view(1,3, 32,32)
if torch.cuda.is_available():
image = image.cuda()
output = self(image)
_, predict = torch.max(output.data, 1)
return predict[0]
def predict_batch(self, image):
self.eval()
image = torch.clamp(image,0,1)
image = Variable(image, volatile=True)
if torch.cuda.is_available():
image = image.cuda()
output = self(image)
_, predict = torch.max(output.data, 1)
return predict
class MNIST(nn.Module):
def __init__(self):
super(MNIST, self).__init__()
self.features = self._make_layers()
self.fc1 = nn.Linear(1024,200)
self.relu = nn.ReLU()
self.fc2 = nn.Linear(200,200)
self.dropout = nn.Dropout(p=0.5)
self.fc3 = nn.Linear(200,10)
def forward(self, x):
out = self.features(x)
out = out.view(out.size(0), -1)
out = self.fc1(out)
out = self.relu(out)
out = self.dropout(out)
out = self.fc2(out)
out = self.relu(out)
out = self.dropout(out)
out = self.fc3(out)
return out
def _make_layers(self):
layers=[]
in_channels= 1
layers += [nn.Conv2d(in_channels, 32, kernel_size=3),
nn.BatchNorm2d(32),
nn.ReLU()]
layers += [nn.Conv2d(32, 32, kernel_size=3),
nn.BatchNorm2d(32),
nn.ReLU()]
layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
layers += [nn.Conv2d(32, 64, kernel_size=3),
nn.BatchNorm2d(64),
nn.ReLU()]
layers += [nn.Conv2d(64, 64, kernel_size=3),
nn.BatchNorm2d(64),
nn.ReLU()]
layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
return nn.Sequential(*layers)
def predict(self, image):
self.eval()
image = torch.clamp(image,0,1)
image = Variable(image, volatile=True).view(1,1,28,28)
if torch.cuda.is_available():
image = image.cuda()
output = self(image)
_, predict = torch.max(output.data, 1)
return predict[0]
def predict_batch(self, image):
self.eval()
image = torch.clamp(image,0,1)
image = Variable(image, volatile=True)
if torch.cuda.is_available():
image = image.cuda()
output = self(image)
_, predict = torch.max(output.data, 1)
return predict
class SimpleMNIST(nn.Module):
""" Custom CNN for MNIST
stride = 1, padding = 2
Layer 1: Conv2d 5x5x16, BatchNorm(16), ReLU, Max Pooling 2x2
Layer 2: Conv2d 5x5x32, BatchNorm(32), ReLU, Max Pooling 2x2
FC 10
"""
def __init__(self):
super(SimpleMNIST, self).__init__()
self.layer1 = nn.Sequential(
nn.Conv2d(1, 16, kernel_size=5, padding=2),
nn.BatchNorm2d(16),
nn.ReLU(),
nn.MaxPool2d(2)
)
self.layer2 = nn.Sequential(
nn.Conv2d(16, 32, kernel_size=5, padding=2),
nn.BatchNorm2d(32),
nn.ReLU(),
nn.MaxPool2d(2)
)
self.fc = nn.Linear(7*7*32, 10)
def forward(self, x):
out = self.layer1(x)
out = self.layer2(out)
out = out.view(out.size(0), -1)
out = self.fc(out)
return out
def predict(self, image):
self.eval()
image = Variable(image.unsqueeze(0))
output = self(image)
_, predict = torch.max(output.data, 1)
return predict[0]
def show_image(img):
"""
Show MNSIT digits in the console.
"""
remap = " .*#"+"#"*100
img = (img.flatten()+.5)*3
if len(img) != 784: return
for i in range(28):
print("".join([remap[int(round(x))] for x in img[i*28:i*28+28]]))
def load_mnist_data():
""" Load MNIST data from torchvision.datasets
input: None
output: minibatches of train and test sets
"""
# MNIST Dataset
train_dataset = dsets.MNIST(root='./data/mnist', train=True, transform=transforms.ToTensor(), download=True)
test_dataset = dsets.MNIST(root='./data/mnist', train=False, transform=transforms.ToTensor())
# Data Loader (Input Pipeline)
train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=1000, shuffle=True)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=10, shuffle=False)
return train_loader, test_loader, train_dataset, test_dataset
def load_cifar10_data():
""" Load MNIST data from torchvision.datasets
input: None
output: minibatches of train and test sets
"""
# CIFAR10 Dataset
train_dataset = dsets.CIFAR10('./data/cifar10-py', download=True, train=True, transform= transforms.ToTensor())
test_dataset = dsets.CIFAR10('./data/cifar10-py', download=True, train=False, transform= transforms.ToTensor())
# Data Loader (Input Pipeline)
train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=1000, shuffle=False)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=10, shuffle=False)
return train_loader, test_loader, train_dataset, test_dataset
def load_imagenet_data():
""" Load MNIST data from torchvision.datasets
input: None
output: minibatches of train and test sets
"""
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
# train_dataset = dsets.ImageFolder(
# '/data/train',
# transforms.Compose([
# transforms.Resize(256),
# transforms.CenterCrop(224),
# transforms.ToTensor(),
# normalize,
# ]))
val_dataset = dsets.ImageFolder(
'/data/val',
transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
normalize,
]))
# Data Loader (Input Pipeline)
val_loader = torch.utils.data.DataLoader(val_dataset, batch_size=1000, shuffle=True)
return val_loader, val_loader, val_dataset, val_dataset
class ImagenetTestDataset(Dataset):
def __init__(self, root_file, transform=None):
self.label =[]
self.root_dir = root_file
self.transform = transform
self.img_name = sorted(os.listdir(root_file))
for img in self.img_name:
name = img.split('.')
self.label.append(int(name[0])-1)
def __getitem__(self, idx):
image = Image.open(self.root_dir + '/' + self.img_name[idx])
image = image.convert('RGB')
if self.transform:
image = self.transform(image)
#label = torch.LongTensor(self.label[idx])
label = self.label[idx]
return image, label
def __len__(self):
return len(self.label)
def imagenettest():
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
#test_dataset = ImagenetTestDataset('/data/test')
test_dataset = ImagenetTestDataset('/data/test', transforms.Compose([transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), normalize,]))
# Data Loader (Input Pipeline)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=10, shuffle=True)
return test_loader, test_dataset
def train_simple_mnist(model, train_loader):
# Loss and Optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
# Train the Model
for epoch in range(num_epochs):
for i, (images, labels) in enumerate(train_loader):
images = Variable(images)
labels = Variable(labels)
# Forward + Backward + Optimize
optimizer.zero_grad()
outputs = model(images)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
if (i+1) % 100 == 0:
print ('Epoch [%d/%d], Iter [%d] Loss: %.4f'
%(epoch+1, num_epochs, i+1, loss.data[0]))
def train_mnist(model, train_loader):
# Loss and Optimizer
model.train()
lr = 0.01
momentum = 0.9
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=lr, momentum=momentum, nesterov=True)
# Train the Model
for epoch in range(num_epochs):
for i, (images, labels) in enumerate(train_loader):
if torch.cuda.is_available():
images, labels = images.cuda(), labels.cuda()
optimizer.zero_grad()
images = Variable(images)
labels = Variable(labels)
# Forward + Backward + Optimize
outputs = model(images)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
if (i+1) % 100 == 0:
print ('Epoch [%d/%d], Iter [%d] Loss: %.4f'
%(epoch+1, num_epochs, i+1, loss.data[0]))
def test_mnist(model, test_loader):
# Test the Model
model.eval() # Change model to 'eval' mode (BN uses moving mean/var).
correct = 0
total = 0
for images, labels in test_loader:
if torch.cuda.is_available():
images, labels = images.cuda(), labels.cuda()
images = Variable(images)
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum()
print('Test Accuracy of the model on the 10000 test images: %.2f %%' % (100.0 * correct / total))
def train_cifar10(model, train_loader):
# Loss and Optimizer
model.train()
lr = 0.01
momentum = 0.9
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=lr, momentum=momentum, nesterov=True)
# Train the Model
for epoch in range(num_epochs):
if epoch%10==0 and epoch!=0:
lr = lr * 0.95
momentum = momentum * 0.5
optimizer = torch.optim.SGD(model.parameters(), lr=lr, momentum=momentum, nesterov=True)
for i, (images, labels) in enumerate(train_loader):
if torch.cuda.is_available():
images, labels = images.cuda(), labels.cuda()
optimizer.zero_grad()
images = Variable(images)
labels = Variable(labels)
# Forward + Backward + Optimize
outputs = model(images)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
if (i+1) % 100 == 0:
print ('Epoch [%d/%d], Iter [%d] Loss: %.4f'
%(epoch+1, num_epochs, i+1, loss.data[0]))
def test_cifar10(model, test_loader):
# Test the Model
model.eval() # Change model to 'eval' mode (BN uses moving mean/var).
correct = 0
total = 0
for images, labels in test_loader:
if torch.cuda.is_available():
images, labels = images.cuda(), labels.cuda()
images = Variable(images)
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum()
print('Test Accuracy of the model on the 10000 test images: %.4f %%' % (100.0 * correct / total))
class ToSpaceBGR(object):
def __init__(self, is_bgr):
self.is_bgr = is_bgr
def __call__(self, tensor):
if self.is_bgr:
new_tensor = tensor.clone()
new_tensor[0] = tensor[2]
new_tensor[2] = tensor[0]
tensor = new_tensor
return tensor
class ToRange255(object):
def __init__(self, is_255):
self.is_255 = is_255
def __call__(self, tensor):
if self.is_255:
tensor.mul_(255)
return tensor
def save_model(model, filename):
""" Save the trained model """
torch.save(model.state_dict(), filename)
def load_model(model, filename):
""" Load the training model """
model.load_state_dict(torch.load(filename))
if __name__ == '__main__':
train_loader, test_loader, train_dataset, test_dataset = load_mnist_data()
net = MNIST()
if torch.cuda.is_available():
net.cuda()
net = torch.nn.DataParallel(net, device_ids=[0])
#net = torch.nn.DataParallel(net, device_ids=range(torch.cuda.device_count()))
train_mnist(net, train_loader)
#load_model(net, 'models/mnist_gpu.pt')
#load_model(net, 'models/mnist.pt')
test_mnist(net, test_loader)
#test_cifar10(net, test_loader)
save_model(net,'./models/mnist.pt')
#net.eval()