-
Notifications
You must be signed in to change notification settings - Fork 112
/
main.py
407 lines (323 loc) · 15.5 KB
/
main.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
import loadData as ld
import os
import torch
import pickle
import Model as net
from torch.autograd import Variable
import VisualizeGraph as viz
from Criteria import CrossEntropyLoss2d
import torch.backends.cudnn as cudnn
import Transforms as myTransforms
import DataSet as myDataLoader
import time
from argparse import ArgumentParser
from IOUEval import iouEval
import torch.optim.lr_scheduler
__author__ = "Sachin Mehta"
def val(args, val_loader, model, criterion):
'''
:param args: general arguments
:param val_loader: loaded for validation dataset
:param model: model
:param criterion: loss function
:return: average epoch loss, overall pixel-wise accuracy, per class accuracy, per class iu, and mIOU
'''
#switch to evaluation mode
model.eval()
iouEvalVal = iouEval(args.classes)
epoch_loss = []
total_batches = len(val_loader)
for i, (input, target) in enumerate(val_loader):
start_time = time.time()
if args.onGPU == True:
input = input.cuda()
target = target.cuda()
input_var = torch.autograd.Variable(input, volatile=True)
target_var = torch.autograd.Variable(target, volatile=True)
# run the mdoel
output = model(input_var)
# compute the loss
loss = criterion(output, target_var)
epoch_loss.append(loss.data[0])
time_taken = time.time() - start_time
# compute the confusion matrix
iouEvalVal.addBatch(output.max(1)[1].data, target_var.data)
print('[%d/%d] loss: %.3f time: %.2f' % (i, total_batches, loss.data[0], time_taken))
average_epoch_loss_val = sum(epoch_loss) / len(epoch_loss)
overall_acc, per_class_acc, per_class_iu, mIOU = iouEvalVal.getMetric()
return average_epoch_loss_val, overall_acc, per_class_acc, per_class_iu, mIOU
def train(args, train_loader, model, criterion, optimizer, epoch):
'''
:param args: general arguments
:param train_loader: loaded for training dataset
:param model: model
:param criterion: loss function
:param optimizer: optimization algo, such as ADAM or SGD
:param epoch: epoch number
:return: average epoch loss, overall pixel-wise accuracy, per class accuracy, per class iu, and mIOU
'''
# switch to train mode
model.train()
iouEvalTrain = iouEval(args.classes)
epoch_loss = []
total_batches = len(train_loader)
for i, (input, target) in enumerate(train_loader):
start_time = time.time()
if args.onGPU == True:
input = input.cuda()
target = target.cuda()
input_var = torch.autograd.Variable(input)
target_var = torch.autograd.Variable(target)
#run the mdoel
output = model(input_var)
#set the grad to zero
optimizer.zero_grad()
loss = criterion(output, target_var)
optimizer.zero_grad()
loss.backward()
optimizer.step()
epoch_loss.append(loss.data[0])
time_taken = time.time() - start_time
#compute the confusion matrix
iouEvalTrain.addBatch(output.max(1)[1].data, target_var.data)
print('[%d/%d] loss: %.3f time:%.2f' % (i, total_batches, loss.data[0], time_taken))
average_epoch_loss_train = sum(epoch_loss) / len(epoch_loss)
overall_acc, per_class_acc, per_class_iu, mIOU = iouEvalTrain.getMetric()
return average_epoch_loss_train, overall_acc, per_class_acc, per_class_iu, mIOU
def save_checkpoint(state, filenameCheckpoint='checkpoint.pth.tar'):
'''
helper function to save the checkpoint
:param state: model state
:param filenameCheckpoint: where to save the checkpoint
:return: nothing
'''
torch.save(state, filenameCheckpoint)
def netParams(model):
'''
helper function to see total network parameters
:param model: model
:return: total network parameters
'''
total_paramters = 0
for parameter in model.parameters():
i = len(parameter.size())
p = 1
for j in range(i):
p *= parameter.size(j)
total_paramters += p
return total_paramters
def trainValidateSegmentation(args):
'''
Main function for trainign and validation
:param args: global arguments
:return: None
'''
# check if processed data file exists or not
if not os.path.isfile(args.cached_data_file):
dataLoad = ld.LoadData(args.data_dir, args.classes, args.cached_data_file)
data = dataLoad.processData()
if data is None:
print('Error while pickling data. Please check.')
exit(-1)
else:
data = pickle.load(open(args.cached_data_file, "rb"))
q = args.q
p = args.p
# load the model
if not args.decoder:
model = net.ESPNet_Encoder(args.classes, p=p, q=q)
args.savedir = args.savedir + '_enc_' + str(p) + '_' + str(q) + '/'
else:
model = net.ESPNet(args.classes, p=p, q=q, encoderFile=args.pretrained)
args.savedir = args.savedir + '_dec_' + str(p) + '_' + str(q) + '/'
if args.onGPU:
model = model.cuda()
# create the directory if not exist
if not os.path.exists(args.savedir):
os.mkdir(args.savedir)
if args.visualizeNet:
x = Variable(torch.randn(1, 3, args.inWidth, args.inHeight))
if args.onGPU:
x = x.cuda()
y = model.forward(x)
g = viz.make_dot(y)
g.render(args.savedir + 'model.png', view=False)
total_paramters = netParams(model)
print('Total network parameters: ' + str(total_paramters))
# define optimization criteria
weight = torch.from_numpy(data['classWeights']) # convert the numpy array to torch
if args.onGPU:
weight = weight.cuda()
criteria = CrossEntropyLoss2d(weight) #weight
if args.onGPU:
criteria = criteria.cuda()
print('Data statistics')
print(data['mean'], data['std'])
print(data['classWeights'])
#compose the data with transforms
trainDataset_main = myTransforms.Compose([
myTransforms.Normalize(mean=data['mean'], std=data['std']),
myTransforms.Scale(1024, 512),
myTransforms.RandomCropResize(32),
myTransforms.RandomFlip(),
#myTransforms.RandomCrop(64).
myTransforms.ToTensor(args.scaleIn),
#
])
trainDataset_scale1 = myTransforms.Compose([
myTransforms.Normalize(mean=data['mean'], std=data['std']),
myTransforms.Scale(1536, 768), # 1536, 768
myTransforms.RandomCropResize(100),
myTransforms.RandomFlip(),
#myTransforms.RandomCrop(64),
myTransforms.ToTensor(args.scaleIn),
#
])
trainDataset_scale2 = myTransforms.Compose([
myTransforms.Normalize(mean=data['mean'], std=data['std']),
myTransforms.Scale(1280, 720), # 1536, 768
myTransforms.RandomCropResize(100),
myTransforms.RandomFlip(),
#myTransforms.RandomCrop(64),
myTransforms.ToTensor(args.scaleIn),
#
])
trainDataset_scale3 = myTransforms.Compose([
myTransforms.Normalize(mean=data['mean'], std=data['std']),
myTransforms.Scale(768, 384),
myTransforms.RandomCropResize(32),
myTransforms.RandomFlip(),
#myTransforms.RandomCrop(64),
myTransforms.ToTensor(args.scaleIn),
#
])
trainDataset_scale4 = myTransforms.Compose([
myTransforms.Normalize(mean=data['mean'], std=data['std']),
myTransforms.Scale(512, 256),
#myTransforms.RandomCropResize(20),
myTransforms.RandomFlip(),
#myTransforms.RandomCrop(64).
myTransforms.ToTensor(args.scaleIn),
#
])
valDataset = myTransforms.Compose([
myTransforms.Normalize(mean=data['mean'], std=data['std']),
myTransforms.Scale(1024, 512),
myTransforms.ToTensor(args.scaleIn),
#
])
# since we training from scratch, we create data loaders at different scales
# so that we can generate more augmented data and prevent the network from overfitting
trainLoader = torch.utils.data.DataLoader(
myDataLoader.MyDataset(data['trainIm'], data['trainAnnot'], transform=trainDataset_main),
batch_size=args.batch_size + 2, shuffle=True, num_workers=args.num_workers, pin_memory=True)
trainLoader_scale1 = torch.utils.data.DataLoader(
myDataLoader.MyDataset(data['trainIm'], data['trainAnnot'], transform=trainDataset_scale1),
batch_size=args.batch_size, shuffle=True, num_workers=args.num_workers, pin_memory=True)
trainLoader_scale2 = torch.utils.data.DataLoader(
myDataLoader.MyDataset(data['trainIm'], data['trainAnnot'], transform=trainDataset_scale2),
batch_size=args.batch_size, shuffle=True, num_workers=args.num_workers, pin_memory=True)
trainLoader_scale3 = torch.utils.data.DataLoader(
myDataLoader.MyDataset(data['trainIm'], data['trainAnnot'], transform=trainDataset_scale3),
batch_size=args.batch_size + 4, shuffle=True, num_workers=args.num_workers, pin_memory=True)
trainLoader_scale4 = torch.utils.data.DataLoader(
myDataLoader.MyDataset(data['trainIm'], data['trainAnnot'], transform=trainDataset_scale4),
batch_size=args.batch_size + 4, shuffle=True, num_workers=args.num_workers, pin_memory=True)
valLoader = torch.utils.data.DataLoader(
myDataLoader.MyDataset(data['valIm'], data['valAnnot'], transform=valDataset),
batch_size=args.batch_size + 4, shuffle=False, num_workers=args.num_workers, pin_memory=True)
if args.onGPU:
cudnn.benchmark = True
start_epoch = 0
if args.resume:
if os.path.isfile(args.resumeLoc):
print("=> loading checkpoint '{}'".format(args.resume))
checkpoint = torch.load(args.resumeLoc)
start_epoch = checkpoint['epoch']
#args.lr = checkpoint['lr']
model.load_state_dict(checkpoint['state_dict'])
print("=> loaded checkpoint '{}' (epoch {})"
.format(args.resume, checkpoint['epoch']))
else:
print("=> no checkpoint found at '{}'".format(args.resume))
logFileLoc = args.savedir + args.logFile
if os.path.isfile(logFileLoc):
logger = open(logFileLoc, 'a')
else:
logger = open(logFileLoc, 'w')
logger.write("Parameters: %s" % (str(total_paramters)))
logger.write("\n%s\t%s\t%s\t%s\t%s\t" % ('Epoch', 'Loss(Tr)', 'Loss(val)', 'mIOU (tr)', 'mIOU (val'))
logger.flush()
optimizer = torch.optim.Adam(model.parameters(), args.lr, (0.9, 0.999), eps=1e-08, weight_decay=5e-4)
# we step the loss by 2 after step size is reached
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=args.step_loss, gamma=0.5)
for epoch in range(start_epoch, args.max_epochs):
scheduler.step(epoch)
lr = 0
for param_group in optimizer.param_groups:
lr = param_group['lr']
print("Learning rate: " + str(lr))
# train for one epoch
# We consider 1 epoch with all the training data (at different scales)
train(args, trainLoader_scale1, model, criteria, optimizer, epoch)
train(args, trainLoader_scale2, model, criteria, optimizer, epoch)
train(args, trainLoader_scale4, model, criteria, optimizer, epoch)
train(args, trainLoader_scale3, model, criteria, optimizer, epoch)
lossTr, overall_acc_tr, per_class_acc_tr, per_class_iu_tr, mIOU_tr = train(args, trainLoader, model, criteria, optimizer, epoch)
# evaluate on validation set
lossVal, overall_acc_val, per_class_acc_val, per_class_iu_val, mIOU_val = val(args, valLoader, model, criteria)
save_checkpoint({
'epoch': epoch + 1,
'arch': str(model),
'state_dict': model.state_dict(),
'optimizer': optimizer.state_dict(),
'lossTr': lossTr,
'lossVal': lossVal,
'iouTr': mIOU_tr,
'iouVal': mIOU_val,
'lr': lr
}, args.savedir + 'checkpoint.pth.tar')
#save the model also
model_file_name = args.savedir + '/model_' + str(epoch + 1) + '.pth'
torch.save(model.state_dict(), model_file_name)
with open(args.savedir + 'acc_' + str(epoch) + '.txt', 'w') as log:
log.write("\nEpoch: %d\t Overall Acc (Tr): %.4f\t Overall Acc (Val): %.4f\t mIOU (Tr): %.4f\t mIOU (Val): %.4f" % (epoch, overall_acc_tr, overall_acc_val, mIOU_tr, mIOU_val))
log.write('\n')
log.write('Per Class Training Acc: ' + str(per_class_acc_tr))
log.write('\n')
log.write('Per Class Validation Acc: ' + str(per_class_acc_val))
log.write('\n')
log.write('Per Class Training mIOU: ' + str(per_class_iu_tr))
log.write('\n')
log.write('Per Class Validation mIOU: ' + str(per_class_iu_val))
logger.write("\n%d\t\t%.4f\t\t%.4f\t\t%.4f\t\t%.4f\t\t%.7f" % (epoch, lossTr, lossVal, mIOU_tr, mIOU_val, lr))
logger.flush()
print("Epoch : " + str(epoch) + ' Details')
print("\nEpoch No.: %d\tTrain Loss = %.4f\tVal Loss = %.4f\t mIOU(tr) = %.4f\t mIOU(val) = %.4f" % (epoch, lossTr, lossVal, mIOU_tr, mIOU_val))
logger.close()
if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument('--model', default="ESPNet", help='Model name')
parser.add_argument('--data_dir', default="./city", help='Data directory')
parser.add_argument('--inWidth', type=int, default=1024, help='Width of RGB image')
parser.add_argument('--inHeight', type=int, default=512, help='Height of RGB image')
parser.add_argument('--scaleIn', type=int, default=8, help='For ESPNet-C, scaleIn=8. For ESPNet, scaleIn=1')
parser.add_argument('--max_epochs', type=int, default=300, help='Max. number of epochs')
parser.add_argument('--num_workers', type=int, default=4, help='No. of parallel threads')
parser.add_argument('--batch_size', type=int, default=12, help='Batch size. 12 for ESPNet-C and 6 for ESPNet. '
'Change as per the GPU memory')
parser.add_argument('--step_loss', type=int, default=100, help='Decrease learning rate after how many epochs.')
parser.add_argument('--lr', type=float, default=5e-4, help='Initial learning rate')
parser.add_argument('--savedir', default='./results_enc_', help='directory to save the results')
parser.add_argument('--visualizeNet', type=bool, default=True, help='If you want to visualize the model structure')
parser.add_argument('--resume', type=bool, default=False, help='Use this flag to load last checkpoint for training') #
parser.add_argument('--classes', type=int, default=20, help='No of classes in the dataset. 20 for cityscapes')
parser.add_argument('--cached_data_file', default='city.p', help='Cached file name')
parser.add_argument('--logFile', default='trainValLog.txt', help='File that stores the training and validation logs')
parser.add_argument('--onGPU', default=True, help='Run on CPU or GPU. If TRUE, then GPU.')
parser.add_argument('--decoder', type=bool, default=False,help='True if ESPNet. False for ESPNet-C') # False for encoder
parser.add_argument('--pretrained', default='../pretrained/encoder/espnet_p_2_q_8.pth', help='Pretrained ESPNet-C weights. '
'Only used when training ESPNet')
parser.add_argument('--p', default=2, type=int, help='depth multiplier')
parser.add_argument('--q', default=8, type=int, help='depth multiplier')
trainValidateSegmentation(parser.parse_args())