-
Notifications
You must be signed in to change notification settings - Fork 9
/
train_SAQ.py
385 lines (309 loc) · 12.5 KB
/
train_SAQ.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
from dataloaders.data import ROOT, DATA_CONTAINER, multibatch_collate_fn
from dataloaders.transform import TrainTransform, TestTransform
from utils.logger import Logger, AverageMeter
from utils.loss import *
from utils.utility import write_mask, save_checkpoint, adjust_learning_rate
from models_STM.models_SAQ import STM
from jaccard import eval_jaccard
import torch
import torch.nn as nn
import torch.optim as optim
import torch.utils.data as data
import numpy as np
import os
import os.path as osp
import shutil
import time
import pickle
import argparse
import random
from progress.bar import Bar
from collections import OrderedDict
from options import OPTION as opt
MAX_FLT = 1e6
np.set_printoptions(threshold=1e6)
def parse_args():
parser = argparse.ArgumentParser('Training Mask Segmentation')
parser.add_argument('--gpu', default='', type=str, help='set gpu id to train the network, split with comma')
return parser.parse_args()
def main():
start_epoch = 0
random.seed(0)
args = parse_args()
# Use GPU
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu if args.gpu != '' else str(opt.gpu_id)
if not os.path.isdir(opt.checkpoint):
os.makedirs(opt.checkpoint)
# Data
print('==> Preparing dataset')
input_dim = opt.input_size
train_transformer = TrainTransform(size=input_dim)
test_transformer = TestTransform(size=input_dim)
try:
if isinstance(opt.trainset, list):
datalist = []
for dataset, freq, max_skip in zip(opt.trainset, opt.datafreq, opt.max_skip):
ds = DATA_CONTAINER[dataset](
train=True,
sampled_frames=opt.sampled_frames,
transform=train_transformer,
max_skip=max_skip,
samples_per_video=opt.samples_per_video
)
datalist += [ds] * freq
trainset = data.ConcatDataset(datalist)
else:
max_skip = opt.max_skip[0] if isinstance(opt.max_skip, list) else opt.max_skip
trainset = DATA_CONTAINER[opt.trainset](
train=True,
sampled_frames=opt.sampled_frames,
transform=train_transformer,
max_skip=max_skip,
samples_per_video=opt.samples_per_video
)
except KeyError as ke:
print('[ERROR] invalide dataset name is encountered. The current acceptable datasets are:')
print(list(DATA_CONTAINER.keys()))
exit()
testset = DATA_CONTAINER[opt.valset](
train=False,
transform=test_transformer,
samples_per_video=1
)
trainloader = data.DataLoader(trainset, batch_size=opt.train_batch, shuffle=True, num_workers=opt.workers,
collate_fn=multibatch_collate_fn, drop_last=True)
testloader = data.DataLoader(testset, batch_size=1, shuffle=False, num_workers=opt.workers,
collate_fn=multibatch_collate_fn)
# Model
print("==> creating model")
net = STM(opt.keydim, opt.valdim, 'train',
mode=opt.mode, iou_threshold=opt.iou_threshold)
print(' Total params: %.2fM' % (sum(p.numel() for p in net.parameters())/1000000.0))
net.eval()
net = nn.DataParallel(net).cuda()
# set training parameters
for p in net.parameters():
p.requires_grad = True
criterion = None
celoss = binary_entropy_loss
if opt.loss == 'ce':
criterion = celoss
elif opt.loss == 'iou':
criterion = mask_iou_loss
elif opt.loss == 'both':
criterion = lambda pred, target, obj: celoss(pred, target, obj) + lovasz_softmax(pred, target)
else:
raise TypeError('unknown training loss %s' % opt.loss)
optimizer = None
if opt.solver == 'sgd':
optimizer = optim.SGD(net.parameters(), lr=opt.learning_rate,
momentum=opt.momentum[0], weight_decay=opt.weight_decay)
elif opt.solver == 'adam':
optimizer = optim.Adam(net.parameters(), lr=opt.learning_rate,
betas=opt.momentum, weight_decay=opt.weight_decay)
else:
raise TypeError('unkown solver type %s' % opt.solver)
# Resume
title = 'STM'
minloss = float('inf')
max_jaccard = 0.0
opt.checkpoint = osp.join(osp.join(opt.checkpoint, opt.valset))
if not osp.exists(opt.checkpoint):
os.mkdir(opt.checkpoint)
if opt.resume:
# Load checkpoint.
print('==> Resuming from checkpoint {}'.format(opt.resume))
assert os.path.isfile(opt.resume), 'Error: no checkpoint directory found!'
# opt.checkpoint = os.path.dirname(opt.resume)
checkpoint = torch.load(opt.resume)
minloss = checkpoint['minloss']
start_epoch = checkpoint['epoch']
max_jaccard = checkpoint['max_jaccard']
net.load_state_dict(checkpoint['state_dict'])
optimizer.load_state_dict(checkpoint['optimizer'])
skips = checkpoint['max_skip']
try:
if isinstance(skips, list):
for idx, skip in enumerate(skips):
trainloader.dataset.datasets[idx].set_max_skip(skip)
else:
trainloader.dataset.set_max_skip(skips)
except:
print('[Warning] Initializing max skip fail')
logger = Logger(os.path.join(opt.checkpoint, opt.mode+'_log.txt'), resume=True)
else:
if opt.initial:
print('==> Initialize model with weight file {}'.format(opt.initial))
weight = torch.load(opt.initial)
if isinstance(weight, OrderedDict):
net.module.load_param(weight)
else:
net.module.load_param(weight['state_dict'])
logger = Logger(os.path.join(opt.checkpoint, opt.mode+'_log.txt'), resume=False)
start_epoch = 0
logger.set_items(['Epoch', 'LR', 'Train Loss'])
# Train and val
for epoch in range(start_epoch):
adjust_learning_rate(optimizer, epoch, opt)
for epoch in range(start_epoch, opt.epochs):
print('\nEpoch: [%d | %d] LR: %f' % (epoch + 1, opt.epochs, opt.learning_rate))
adjust_learning_rate(optimizer, epoch, opt)
net.module.phase = 'train'
train_loss = train(trainloader,
model=net,
criterion=criterion,
optimizer=optimizer,
epoch=epoch,
use_cuda=True,
iter_size=opt.iter_size,
mode=opt.mode,
threshold=opt.iou_threshold)
if (epoch + 1) % opt.epoch_per_test == 0:
net.module.phase = 'test'
test_loss = test(testloader,
model=net.module,
criterion=criterion,
epoch=epoch,
use_cuda=True)
# append logger file
logger.log(epoch+1, opt.learning_rate, train_loss)
# adjust max skip
if (epoch + 1) % opt.epochs_per_increment == 0:
if isinstance(trainloader.dataset, data.ConcatDataset):
for dataset in trainloader.dataset.datasets:
dataset.increase_max_skip()
else:
trainloader.dataset.increase_max_skip()
# save model
# max_jaccard = 0.0
J = eval_jaccard()
is_best = J >= max_jaccard
max_jaccard = max(J, max_jaccard)
# is_best = train_loss <= minloss
minloss = min(minloss, train_loss)
skips = [ds.max_skip for ds in trainloader.dataset.datasets] \
if isinstance(trainloader.dataset, data.ConcatDataset) \
else trainloader.dataset.max_skip
if (epoch + 1) % opt.epoch_per_save == 0:
save_checkpoint({
'epoch': epoch + 1,
'state_dict': net.state_dict(),
'loss': train_loss,
'minloss': minloss,
'max_jaccard':max_jaccard,
'optimizer': optimizer.state_dict(),
'max_skip': skips,
}, epoch + 1, is_best, checkpoint=opt.checkpoint, filename=opt.mode)
logger.close()
print('minimum loss:')
print(minloss)
print('max jaccard:')
print(max_jaccard)
def train(trainloader, model, criterion, optimizer, epoch, use_cuda, iter_size, mode, threshold):
# switch to train mode
loss_func = nn.MSELoss()
data_time = AverageMeter()
loss = AverageMeter()
end = time.time()
bar = Bar('Processing', max=len(trainloader))
optimizer.zero_grad()
for batch_idx, data in enumerate(trainloader):
frames, masks, objs, infos = data
data_time.update(time.time() - end)
if use_cuda:
frames = frames.cuda()
masks = masks.cuda()
objs = objs.cuda()
objs[objs==0] = 1
N, T, C, H, W = frames.size()
max_obj = masks.shape[2]-1
total_loss = 0.0
out, quality, ious = model(frame=frames, mask=masks, num_objects=objs, criterion=mask_iou_loss)
for idx in range(N):
for t in range(1, T):
gt = masks[idx, t:t+1]
pred = out[idx, t-1: t]
No = objs[idx].item()
total_loss = total_loss + criterion(pred, gt, No)
total_loss = total_loss / (N * (T-1))
# print('1 {}'.format(total_loss))
total_loss += loss_func(quality*100,torch.Tensor(ious).cuda()*100)/1000
# print('2 {}'.format(total_loss))
# record loss
if total_loss.item() > 0.0:
loss.update(total_loss.item(), 1)
# compute gradient and do SGD step (divided by accumulated steps)
total_loss /= iter_size
total_loss.backward()
if (batch_idx+1) % iter_size == 0:
optimizer.step()
model.zero_grad()
# measure elapsed time
end = time.time()
# plot progress
bar.suffix = '({batch}/{size}) Data: {data:.3f}s |Loss: {loss:.5f}'.format(
batch=batch_idx + 1,
size=len(trainloader),
data=data_time.val,
loss=loss.avg
)
bar.next()
bar.finish()
return loss.avg
def test(testloader, model, criterion, epoch, use_cuda):
data_time = AverageMeter()
bar = Bar('Processing', max=len(testloader))
with torch.no_grad():
for batch_idx, data in enumerate(testloader):
frames, masks, objs, infos = data
if use_cuda:
frames = frames.cuda()
masks = masks.cuda()
frames = frames[0]
masks = masks[0]
num_objects = objs[0]
info = infos[0]
max_obj = masks.shape[1]-1
# compute output
t1 = time.time()
T, _, H, W = frames.shape
pred = [masks[0:1]]
keys = []
vals = []
for t in range(1, T):
if t-1 == 0:
tmp_mask = masks[0:1]
elif 'frame' in info and t-1 in info['frame']:
# start frame
mask_id = info['frame'].index(t-1)
tmp_mask = masks[mask_id:mask_id+1]
num_objects = max(num_objects, tmp_mask.max())
else:
tmp_mask = out
# memorize
key, val, _ = model(frame=frames[t-1:t, :, :, :], mask=tmp_mask, num_objects=num_objects)
# segment
tmp_key = torch.cat(keys+[key], dim=1)
tmp_val = torch.cat(vals+[val], dim=1)
logits, ps, quality = model(frame=frames[t:t+1, :, :, :], keys=tmp_key, values=tmp_val, num_objects=num_objects, max_obj=max_obj)
out = torch.softmax(logits, dim=1)
pred.append(out)
if (t-1) % opt.save_freq == 0 and t<=100:
keys.append(key)
vals.append(val)
pred = torch.cat(pred, dim=0)
pred = pred.detach().cpu().numpy()
write_mask(pred, info, opt, directory=opt.output_dir)
toc = time.time() - t1
data_time.update(toc, 1)
# plot progress
bar.suffix = '({batch}/{size}) Time: {data:.3f}s'.format(
batch=batch_idx + 1,
size=len(testloader),
data=data_time.sum
)
bar.next()
bar.finish()
return
if __name__ == '__main__':
main()