-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
executable file
·364 lines (279 loc) · 14.2 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
from __future__ import print_function
import argparse
import os
import random
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.optim as optim
import torch.utils.data
from torch.autograd import Variable
import torch.nn.functional as F
import numpy as np
import time
import math
from dataloader import listflowfile as lt
from dataloader import SecenFlowLoader as DA
from models import *
from utils import *
import shutil
from glob import glob
'''
--------------------------------------------------
This is the code for the spixel up/downsampling PSMNet (SPPSMNet)
We train the network from the scratch on sceneflow and later fine-tune on different dataset
The spixel size is fixed to 4 on sceneflow,
the image is downsampled first and fed to PSMNet and then upsampled the cost volume before the disp. regression
Author: Fengting Yang
Final modification: Nov. 2019
# note
# difference from the previous joint training
# 1. spixel is training from the scratch instead of pre-trained spixel
# 2. the disparity is upsampled instead of cost volume, be same as sp16 case
# 3. the learning schedule become lr: 1e-3 * 10==> 5e-4 * 3 ==> 1e-4 * 3
'''
parser = argparse.ArgumentParser(description='SPPSMNet')
parser.add_argument('--maxdisp', type=int ,default=192,
help='maxium disparity')
parser.add_argument('--model', default='SPPSMNet',
help='select model')
parser.add_argument('--datapath', default= '/home/fuy34/stereo_data/sceneflow/', help='datapath')
parser.add_argument('--epochs', type=int, default=13,
help='number of epochs to train (default:13)')
parser.add_argument('--batchsize', type=int, default=8, #https://github.com/JiaRenChang/PSMNet/issues/73
help='number of epochs to train(default:12 or 8)')
parser.add_argument('--test_batchsize', type=int, default=8, #https://github.com/JiaRenChang/PSMNet/issues/73
help='number of epochs to train(default:12 or 8)')
parser.add_argument('--loadmodel', default= None,
help='load model')
parser.add_argument('--preTrain_spixel', default= './preTrain_spixel_old/',
help='preTrain model')
parser.add_argument('--savemodel', default='/data/Fengting/stereo_training/SPPSMNet/useful_sceneflow/', #usefull_res_sceneflow/
help='save model')
parser.add_argument('--logname', default='SPPPSM_tst',
help='log name')
parser.add_argument('--no-cuda', action='store_true', default=False,
help='enables CUDA training')
parser.add_argument('--seed', type=int, default=1, metavar='S',
help='random seed (default: 1)')
parser.add_argument('--test_only', action='store_true', default=False,
help='test_only')
parser.add_argument('--m_w', type=float, default=30,
help='slic position weight')
parser.add_argument('--sp_w', type=float, default=0.1,
help='spixel loss weight')
parser.add_argument('--sz_list', type=float, default= [4], #, 8, 16
help='spixel loss weight')
parser.add_argument('--recFre', type=int, default=1100,
help='recording training status frequence (iter)')
parser.add_argument('--train_img_height', '-t_imgH', default=256, #384,
type=int, help='img height')
parser.add_argument('--train_img_width', '-t_imgW', default= 512, #768,
type=int, help='img width')
parser.add_argument('--val_img_height', '-v_imgH', default=544, #512
type=int, help='img height_must be 16*n') #
parser.add_argument('--val_img_width', '-v_imgW', default=960, #960
type=int, help='img width must be 16*n')
parser.add_argument('--real_img_height', '-r_imgH', default=540, #512 368
type=int, help='img height_must be 16*n') #
parser.add_argument('--real_img_width', '-r_imgW', default=960, #960 1232
type=int, help='img width must be 16*n')
parser.add_argument('--epoch_size', default=1e5, #960
type=int, help='img width must be 16*n')
args = parser.parse_args()
args.cuda = not args.no_cuda and torch.cuda.is_available()
args.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
def _init_fn(worker_id):
np.random.seed()
random.seed()
torch.manual_seed(args.seed)
if args.cuda:
torch.cuda.manual_seed(args.seed)
all_left_img, all_right_img, all_left_disp, test_left_img, test_right_img, test_left_disp = lt.dataloader(args.datapath)
train_epoch_size = min(args.epoch_size* args.batchsize, len(all_left_img)) #only for debug
val_epoch_size = min(args.epoch_size* args.batchsize, len(test_left_disp))
TrainImgLoader = torch.utils.data.DataLoader(
DA.myImageFloder(all_left_img[:train_epoch_size],all_right_img[:train_epoch_size],all_left_disp[:train_epoch_size], True),
batch_size= args.batchsize, shuffle= True, num_workers= 8, drop_last=True, worker_init_fn=_init_fn)
if args.test_only:
#Note we use 4 gpu, in the last iter some GPU may do not have data, and an error will report, but seems ok
TestImgLoader = torch.utils.data.DataLoader(
DA.myImageFloder(test_left_img,test_right_img,test_left_disp, False),
batch_size= 4, shuffle= False, num_workers= 8, drop_last=False, worker_init_fn=_init_fn)
else:
TestImgLoader = torch.utils.data.DataLoader(
DA.myImageFloder(test_left_img[:val_epoch_size],test_right_img[:val_epoch_size],test_left_disp[:val_epoch_size], False),
batch_size= args.test_batchsize, shuffle= False, num_workers= 8, drop_last=True, worker_init_fn=_init_fn)
spixel_ckpts = args.preTrain_spixel+'/4.pth.tar'
model = prePSMNet(args, spixel_ckpts, b_pretrain=False)
if args.cuda:
torch.backends.cudnn.benchmark = True
model = nn.DataParallel(model)
model.cuda()
train_params = [param for name, param in model.named_parameters() if param.requires_grad and not 'spixel' in name]
spixel_params = [param for name, param in model.named_parameters() if param.requires_grad and 'spixel' in name ] #and 'spixel4' not in name
optimizer = optim.Adam( [
{'params': train_params},
{'params': spixel_params, 'weight_decay': 4e-4} ], lr=0.001, betas=(0.9, 0.999)) #{'params': [s1, s2]},
if args.loadmodel is not None:
state_dict = torch.load(args.loadmodel)
model.load_state_dict(state_dict['state_dict'])
start_epoch = state_dict['start_epoch']
optimizer.load_state_dict(state_dict['optimz_state'])
best_err = state_dict['best_err']
total_iters = (start_epoch-1) * len(TrainImgLoader)
else:
start_epoch = 1
best_err = 1e4
total_iters = 0
if args.test_only:
start_epoch = 1
args.epochs = 1
print('Number of model parameters: {}'.format(sum([p.data.nelement() for p in model.parameters()]))) #5743010
def train(imgL,imgR, disp_L):
model.train()
imgL = Variable(torch.FloatTensor(imgL))
imgR = Variable(torch.FloatTensor(imgR))
disp_L = Variable(torch.FloatTensor(disp_L))
if args.cuda:
imgL, imgR, disp_L = imgL.cuda(), imgR.cuda(), disp_L.cuda()
optimizer.zero_grad()
outputs = model(imgL, imgR, disp_L, b_joint=True)
output1 = outputs[0][0] # torch.squeeze(outputs[0], 1)
output2 = outputs[1][0] # torch.squeeze(outputs[1], 1)
output3 = outputs[2][0] # torch.squeeze(outputs[2], 1)
disp_loss = (0.5 * output1.sum() + 0.7 * output2.sum() + output3.sum())/ args.batchsize
loss_col = 0
loss_pos = 0
sp_errs = outputs[3]
# loss_map_R = outputs[4]
for i in range(len(args.sz_list)): # todo 5
a = sp_errs[0].sum() / args.batchsize # color loss
b = args.m_w / (args.sz_list[i]) * sp_errs[1].sum() / args.batchsize #math.sqrt, in our paper we use sqrt, but actually we should not
loss_col += a
loss_pos += b
print("level {}, loss_col: {} loss_pos: {}".format(i, args.sp_w * a, args.sp_w * b))
spixle_loss = args.sp_w * (loss_col + loss_pos)
loss = disp_loss + spixle_loss
viz = {}
viz['final_output'] = outputs[2][1].cpu().numpy() # output3.detach().cpu().numpy()
viz['sp_assign_L'] = [outputs[5]]
viz["spixel_idx_list"] = outputs[7]
# for debug only
# viz['sp_assign_R'] = outputs[6]
# viz['output1'] = outputs[0][1].cpu().numpy() #output1.detach().cpu().numpy()
# viz['output2'] = outputs[1][1].cpu().numpy() #output2.detach().cpu().numpy()
# viz["spImg_l"] = outputs[8][0].numpy() #.detach().cpu().numpy()
# viz["spImg_r"] = outputs[8][1].numpy() #.detach().cpu().numpy()
# viz["disturb_img"] = outputs[-1].detach().cpu().numpy()
viz['mask'] = outputs[-1].cpu().numpy()
loss.backward()
optimizer.step()
return loss.item(), disp_loss.item(), args.sp_w * loss_col.item(), args.sp_w * loss_pos.item(), viz
def test(imgL,imgR,disp_true):
model.eval()
imgL = Variable(torch.FloatTensor(imgL))
imgR = Variable(torch.FloatTensor(imgR))
if args.cuda:
imgL, imgR , disp_true = imgL.cuda(), imgR.cuda(), disp_true.cuda()
with torch.no_grad():
outputs = model(imgL, imgR)
mask = (disp_true < 192)
output = torch.squeeze(outputs[0], 1)[:,args.val_img_height-540:,:]
viz = {}
viz['final_output'] = output.detach().cpu().numpy()
viz['mask'] = mask.type(torch.float).detach().cpu().numpy()
viz['sp_assign_L'] = outputs[1]
viz["spixel_idx_list"] = outputs[3]
if len(disp_true[mask])==0:
loss = 0
else:
loss = torch.mean(torch.abs(output[mask]-disp_true[mask])) # end-point-error
return loss.item(), viz
def adjust_learning_rate(optimizer, epoch):
if epoch <= args.epochs - 2:
lr = 1e-3
elif epoch == args.epochs - 1:
lr = 5e-4
else:
lr = 1e-4
for param_group in optimizer.param_groups:
param_group['lr'] = lr
return lr
def main():
global best_err, start_epoch, total_iters
saveName = args.logname + "_spW{}_mW{}_ep{}_b{}".format( args.sp_w, args.m_w, args.epochs, args.batchsize)
log = Logger(args.savemodel, name= saveName, s_train = 'train')
val_log = Logger(args.savemodel, name= saveName , s_train = 'val')
num_log_img = 4
print(' total training data: %d; total test data: %d'% (len(TrainImgLoader),len(TestImgLoader)) )
# with batch size 8 total training data: 4431; total test data: 546
start_full_time = time.time()
for epoch in range(start_epoch, args.epochs+1):
print('This is %d-th epoch, total iter: %d' %(epoch, len(TrainImgLoader)))
total_train_loss = 0
lr = adjust_learning_rate(optimizer,epoch)
log.scalar_summary('lr', lr, epoch)
if not args.test_only:
## training ##
start_time = time.time()
for batch_idx, (imgL_crop, imgR_crop, disp_crop_L) in enumerate(TrainImgLoader):
torch.cuda.synchronize()
data_time = time.time() - start_time
loss, disp_loss, spCol_loss, spPos_loss, viz = train(imgL_crop, imgR_crop, disp_crop_L)
torch.cuda.synchronize()
print('Epoch [%.2d: %.4d] training loss = %.3f , data time = %.2f, total time = %.2f' %\
(epoch, batch_idx, loss, data_time, time.time() - start_time))
total_train_loss += loss
if total_iters % 10 == 0:
log.scalar_summary('total_loss_batch', loss, total_iters)
log.scalar_summary('disp_loss_batch', disp_loss, total_iters)
log.scalar_summary('spixel_loss_batch', spCol_loss+spPos_loss, total_iters)
log.scalar_summary('col_loss_batch', spCol_loss, total_iters)
log.scalar_summary('pos_loss_batch', spPos_loss, total_iters)
if total_iters % args.recFre == 0:
write_log(args, total_train_loss / len(TrainImgLoader), total_iters,
viz, imgL_crop, imgR_crop, disp_crop_L,log, num_log_img)
total_iters += 1
start_time = time.time()
log.scalar_summary('avg_loss', total_train_loss / len(TrainImgLoader), epoch)
print('full training time = %.2f HR/epoch' %((time.time() - start_full_time)/3600))
torch.cuda.empty_cache()
if not args.test_only:
torch.cuda.synchronize()
savefilename = args.savemodel + '/' + saveName
b_best = False
save_ckpt({
'start_epoch': epoch + 1,
'best_err': best_err,
'state_dict': model.state_dict(),
'optimz_state': optimizer.state_dict()
}, savefilename, epoch, b_best)
list_ckpt = glob(os.path.join(savefilename, 'epoch_*.tar'))
list_ckpt.sort(key=lambda f: int(''.join(filter(str.isdigit, f))))
if len(list_ckpt) > 5:
os.remove(list_ckpt[0])
# validate every 2 epochs to save time
if epoch % 2 == 0 and epoch < args.epochs:
continue
# ------------- Val ------------------------------------------------------------
total_test_loss = 0
test_iter = len(TestImgLoader)
for val_batch_idx, (imgL, imgR, disp_L) in enumerate(TestImgLoader):
with torch.no_grad():
test_loss, viz = test(imgL,imgR, disp_L)
print('Epoch [%.2d: %.4d] test loss = %.3f' %(epoch, val_batch_idx, test_loss))
total_test_loss += test_loss
print('total test loss = %.3f' % (total_test_loss /test_iter) )
if not args.test_only:
b_best = best_err > total_test_loss / test_iter
best_err = min(total_test_loss / test_iter, best_err)
if b_best:
shutil.copyfile(savefilename + '/epoch_%d.tar' % epoch, savefilename + '/best_model.tar')
write_log(args, total_test_loss/test_iter, epoch,
viz, imgL, imgR, disp_L, val_log, num_log_img, b_train=False)
torch.cuda.empty_cache()
#----------------------------------------------------------------------------------
if __name__ == '__main__':
main()