-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_classification.py
381 lines (303 loc) · 12.2 KB
/
image_classification.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
import os
import random
import collections
import shutil
import time
import glob
import csv
import numpy as np
import torch
import torch.backends.cudnn as cudnn
import torch.nn as nn
import torch.optim as optim
import torch.utils.data as data
import torchvision.datasets as datasets
import torchvision.models as models
import torchvision.transforms as transforms
from PIL import Image
ROOT_DIR = os.getcwd()
DATA_HOME_DIR = ROOT_DIR + '/data'
data_path = DATA_HOME_DIR + '/'
split_train_path = data_path + '/train/'
full_train_path = data_path + '/train_full/'
valid_path = data_path + '/valid/'
test_path = ''#'C:/Users/gsand/Downloads/train_SOaYf6m/train/1/'#DATA_HOME_DIR + '/test/test/'
saved_model_path = ROOT_DIR + '/models/'
submission_path = ROOT_DIR + '/submissions/'
# data
batch_size = 8
import pandas as pd
# model
nb_runs = 1
nb_aug = 3
epochs = 80
lr = 1e-4
clip = 0.001
archs = ["resnext101_32x8d"]
model_names = sorted(name for name in models.__dict__ if name.islower() and not name.startswith("__"))
print(model_names)
best_prec1 = 0
def test(test_loader, model):
file1=open("result_pytorch.csv","w")
csv_map = collections.defaultdict(float)
# switch to evaluate mode
model.eval()
for aug in range(nb_aug):
print(" * Predicting on test augmentation {}".format(aug + 1))
for i, (images, filepath) in enumerate(test_loader):
im_name=filepath
# pop extension, treat as id to map
filepath = os.path.splitext(os.path.basename(filepath[0]))[0]
filepath = int(filepath)
image_var = torch.autograd.Variable(images)
y_pred = model(image_var)
# get the index of the max log-probability
smax = nn.Softmax()
smax_out = smax(y_pred)[0]
l1 = smax_out.data[0]
l2 = smax_out.data[1]
prob = l2#nem
if l1 > l2:
prob = 1 - cat_prob
if l1 > l2:
file1.write(str(im_name) + ',' + str(0))
elif l1 < l2:
file1.write(str(im_name) + ',' + str(1))
file1.write('\n')
prob = np.around(prob.cpu(), decimals=4)
prob = np.clip(prob, clip, 1 - clip)
csv_map[filepath] += (prob / nb_aug)
sub_fn = '{0}epoch_{1}clip_{2}runs'.format(epochs, clip, nb_runs)
for arch in archs:
sub_fn += "_{}".format(arch)
print("Writing Predictions to CSV...")
with open(sub_fn + '.csv', 'w') as csvfile:
fieldnames = ['id', 'label']
csv_w = csv.writer(csvfile)
csv_w.writerow(('id', 'label'))
for row in sorted(csv_map.items()):
csv_w.writerow(row)
print("Done.")
file1.close()
def validate(val_loader, model, criterion, epoch):
batch_time = AverageMeter()
losses = AverageMeter()
acc = AverageMeter()
# switch to evaluate mode
model.eval()
end = time.time()
for i, (images, labels) in enumerate(val_loader):
labels = labels.cuda()
image_var = torch.autograd.Variable(images)
label_var = torch.autograd.Variable(labels)
# compute y_pred
y_pred = model(image_var)
loss = criterion(y_pred, label_var)
# measure accuracy and record loss
prec1, temp_var = accuracy(y_pred.data, labels, topk=(1, 1))
losses.update(loss.data, images.size(0))
acc.update(prec1, images.size(0))
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
print(' * EPOCH {epoch} | Accuracy: {acc.avg:.3f} | Loss: {losses.avg:.3f}'.format(epoch=epoch,
acc=acc,
losses=losses))
return acc.avg
class TestImageFolder(data.Dataset):
def __init__(self, root, transform=None):
images = []
file = open(r"C:\Users\gsand\Downloads\test_em.csv", "r")
lines = file.readlines()
for img in lines:
filename = img.strip('\n')
images.append('{}'.format(filename))
# for filename in sorted(glob.glob(test_path + "*.jpg")):
# images.append('{}'.format(filename))
self.root ="C:/Users/gsand/Downloads/em/images/"
self.imgs = images
self.transform = transform
def __getitem__(self, index):
filename = self.imgs[index]
img = Image.open(os.path.join(self.root, filename))
if self.transform is not None:
img = self.transform(img)
return img, filename
def __len__(self):
return len(self.imgs)
def train(train_loader, model, criterion, optimizer, epoch):
batch_time = AverageMeter()
data_time = AverageMeter()
losses = AverageMeter()
acc = AverageMeter()
end = time.time()
# switch to train mode
model.train()
for i, (images, target) in enumerate(train_loader):
# measure data loading time
data_time.update(time.time() - end)
target = target.cuda()
image_var = torch.autograd.Variable(images)
label_var = torch.autograd.Variable(target)
# compute y_pred
y_pred = model(image_var)
loss = criterion(y_pred, label_var)
# measure accuracy and record loss
prec1, prec1 = accuracy(y_pred.data, target, topk=(1, 1))
losses.update(loss.data, images.size(0))
acc.update(prec1, images.size(0))
# compute gradient and do SGD step
optimizer.zero_grad()
loss.backward()
optimizer.step()
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
def save_checkpoint(state, is_best, filename='checkpoint.pth.tar'):
torch.save(state, filename)
if is_best:
shutil.copyfile(filename, 'model_best.pth.tar')
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def adjust_learning_rate(optimizer, epoch):
"""Sets the learning rate to the initial LR decayed by 10 every 30 epochs"""
global lr
lr = lr * (0.1**(epoch // 30))
for param_group in optimizer.state_dict()['param_groups']:
param_group['lr'] = lr
def accuracy(y_pred, y_actual, topk=(1, )):
"""Computes the precision@k for the specified values of k"""
maxk = max(topk)
batch_size = y_actual.size(0)
_, pred = y_pred.topk(maxk, 1, True, True)
pred = pred.t()
correct = pred.eq(y_actual.view(1, -1).expand_as(pred))
res = []
for k in topk:
correct_k = correct[:k].view(-1).float().sum(0)
res.append(correct_k.mul_(100.0 / batch_size))
return res
def shear(img):
width, height = img.size
m = random.uniform(-0.05, 0.05)
xshift = abs(m) * width
new_width = width + int(round(xshift))
img = img.transform((new_width, height), Image.AFFINE,
(1, m, -xshift if m > 0 else 0, 0, 1, 0),
Image.BICUBIC)
return img
def main(mode="train", resume='checkpoint.pth.tar'):#:
global best_prec1
for arch in archs:
# create model
print("=> Starting {0} on '{1}' model".format(mode, arch))
model = models.__dict__[arch](pretrained=True)
# Don't update non-classifier learned features in the pretrained networks
for param in model.parameters():
param.requires_grad = False
# Replace the last fully-connected layer
# Parameters of newly constructed modules have requires_grad=True by default
# Final dense layer needs to replaced with the previous out chans, and number of classes
# in this case -- resnet 101 - it's 2048 with two classes (cats and dogs)
model.fc = nn.Dropout(p=0.2)
model.fc = nn.Linear(2048, 2)#nn.Linear(2048, 2)
if arch.startswith('alexnet') or arch.startswith('vgg'):
model.features = torch.nn.DataParallel(model.features)
model.cuda()
else:
model = torch.nn.DataParallel(model).cuda()
# optionally resume from a checkpoint
if resume:
if os.path.isfile(resume):
print("=> Loading checkpoint '{}'".format(resume))
checkpoint = torch.load(resume)
start_epoch = checkpoint['epoch']
best_prec1 = checkpoint['best_prec1']
model.load_state_dict(checkpoint['state_dict'])
print("=> Loaded checkpoint (epoch {})".format(checkpoint['epoch']))
else:
print("=> No checkpoint found at '{}'".format(args.resume))
cudnn.benchmark = True
# Data loading code
traindir = "C:/Users/gsand/Downloads/train_SOaYf6m/train"#split_train_path
valdir = "C:/Users/gsand/Downloads/train_SOaYf6m/test"#valid_path
testdir ='C:/Users/gsand/Downloads/train_SOaYf6m/train/' #test_path
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
#train_loader = CustomDataset(csv_file='C:/Users/gsand/Downloads/train_SOaYf6m/' + 'train.csv', root_dir='C:/Users/gsand/Downloads/train_SOaYf6m/' + 'images')
#train_loader = DataLoader(train_dataset, batch_size=4, shuffle=True)
train_loader = data.DataLoader(
datasets.ImageFolder(traindir,
transforms.Compose([
transforms.Lambda(shear),
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize,
])),
batch_size=batch_size,
shuffle=True,
num_workers=0,
pin_memory=True)
val_loader = data.DataLoader(
datasets.ImageFolder(valdir,
transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
normalize,
])),
batch_size=batch_size,
shuffle=True,
num_workers=0,
pin_memory=True)
test_loader = data.DataLoader(
TestImageFolder(testdir,
transforms.Compose([
# transforms.Lambda(shear),
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize,
])),
batch_size=1,
shuffle=False,
num_workers=0,
pin_memory=False)
if mode == "test":
test(test_loader, model)
return
# define loss function (criterion) and pptimizer
criterion = nn.CrossEntropyLoss().cuda()
if mode == "validate":
validate(val_loader, model, criterion, 0)
return
optimizer = optim.Adam(model.module.fc.parameters(), lr, weight_decay=1e-4)
for epoch in range(epochs):
adjust_learning_rate(optimizer, epoch)
# train for one epoch
train(train_loader, model, criterion, optimizer, epoch)
# evaluate on validation set
prec1 = validate(val_loader, model, criterion, epoch)
# remember best Accuracy and save checkpoint
is_best = prec1 > best_prec1
best_prec1 = max(prec1, best_prec1)
save_checkpoint({
'epoch': epoch + 1,
'arch': arch,
'state_dict': model.state_dict(),
'best_prec1': best_prec1,
}, is_best)
main(mode="test")