-
Notifications
You must be signed in to change notification settings - Fork 5
/
train_conf.py
executable file
·392 lines (334 loc) · 13.1 KB
/
train_conf.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
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
from torch.optim.lr_scheduler import LambdaLR
import torch.backends.cudnn as cudnn
#from utils.dataloader_cmplt import CompletionDataset, MultiViewDataset
#from models import SegNet, ConfNet, ResNet, Bottleneck, DeconvBottleneck
from utils.dataloader_cmplt import MultiViewDataset
from models import ResNet, Bottleneck, DeconvBottleneck
from utils.utils import d3_41_colors_rgb
import argparse
import json
from tensorboardX import SummaryWriter
import os
import shutil
import time
import cv2
import random
import csv
from tqdm import tqdm
def weights_init(m):
classname = m.__class__.__name__
if classname.find('Conv') != -1:
m.weight.data.normal_(0.0, 0.02)
elif classname.find('BatchNorm') != -1:
m.weight.data.normal_(0.0, 0.02)
m.bias.data.fill_(0)
def validate(device, cd, fd, test_loader, writer, batch_size,
save_dir, epoch, CLoss, ce, mask, oh, ratio):
c_losses = []
fd.eval()
with torch.no_grad():
for vid, sample in enumerate(test_loader):
# read sample
sem_in = sample['sem_in'].float().to(device)
sem_gt = sample['sem_gt'].float().to(device)
pred = cd(sem_in).detach()
pred = torch.nn.functional.softmax(pred, dim=1)
if oh:
max_idx = torch.argmax(pred, 1, keepdim=True)
pred = torch.FloatTensor(pred.shape).to(device)
pred.zero_()
pred.scatter_(1, max_idx, 1)
if mask:
valid = (sem_gt != sem_in.shape[1]-1) & (torch.argmax(sem_in,
dim=1) == sem_in.shape[1]-1)
valid = valid.float()
sem_gt = (sem_gt == torch.argmax(pred,
dim=1))
output = fd(torch.cat((sem_in, pred),
dim=1))
if output.shape[1] == 1:
output = output.squeeze(1)
# calculate loss
if not ce:
c_loss = CLoss(output, sem_gt.float())
else:
c_loss = CLoss(output, sem_gt.long())
if mask:
c_loss_unseen = c_loss * valid * ratio
c_loss_unseen = torch.sum(c_loss_unseen) / torch.sum(valid)
c_loss_seen = c_loss * (1. - valid) * (1. - ratio)
c_loss_seen = torch.sum(c_loss_seen) / torch.sum(1.-valid)
c_loss = c_loss_unseen + c_loss_seen
c_losses.append(float(c_loss))
# calculate accuracy
writer.add_scalar("Test_Loss", sum(c_losses)/len(c_losses), epoch)
fd.train()
# get experiment id
parser = argparse.ArgumentParser(description="confidence")
parser.add_argument('conf_id', help='which experiment to pick')
args = parser.parse_args()
conf_id = args.conf_id
# start to log this process
log_dir = os.path.join('./run', conf_id)
if os.path.exists(log_dir):
assert False, "Dir exists!"
shutil.rmtree(log_dir)
os.makedirs(log_dir)
writer = SummaryWriter(log_dir=log_dir)
# save path for this process
save_dir = os.path.join('./result', conf_id)
if os.path.exists(save_dir):
assert False, "Dir exists!"
shutil.rmtree(save_dir)
os.makedirs(save_dir)
# read hyperparameters
config_path = 'configs/config.json'
with open(config_path, 'r') as f:
data = json.load(f)
setting = data[conf_id]
seed = setting['seed']
benchmark = setting['benchmark']
deterministic = setting['deterministic']
enabled = setting['enabled']
root_dirs = setting['root_dirs']
limit = setting['limit']
test_dirs = setting['test_dirs']
batch_size = setting['batch_size']
shuffle = setting['shuffle']
num_workers = setting['num_workers']
pin_memory = setting['pin_memory']
weight = setting['weight']
num_channels = setting['num_channels']
lr = setting['lr']
weight_decay = setting['weight_decay']
lr_decay_rate = setting['lr_decay_rate']
lr_epoch_per_decay = setting['lr_epoch_per_decay']
epochs = setting['epochs']
save_interval = setting['save_interval']
vis_train = setting['vis_train']
vis_test = setting['vis_test']
vis_freq = setting['vis_freq']
layer_infos = setting['layer_infos']
cd_path = setting['cd_path']
ce = setting['ce']
mask = setting['mask']
oh = setting['oh']
ratio = setting['ratio']
'''
# test pipeline
num_workers = 2
save_interval = 10
'''
# set random seed
random.seed(seed)
torch.manual_seed(seed)
# cudnn setting
cudnn.benchmark = benchmark
cudnn.deterministic = deterministic
cudnn.enabled = enabled
# get cuda device
device = torch.device("cuda" if torch.cuda.is_available() else 'cpu')
# prepare training data
train_data = MultiViewDataset(root_dirs, num_channels, limit=limit)
train_loader = DataLoader(train_data, \
batch_size=batch_size,\
shuffle=shuffle,\
num_workers=num_workers,\
pin_memory=pin_memory)
# prepare testing data
test_data = MultiViewDataset(test_dirs, num_channels)
test_loader = DataLoader(test_data,\
batch_size=batch_size,\
shuffle=shuffle,\
num_workers=num_workers,\
pin_memory=pin_memory)
# prepare models
#cd = SegNet(n_classes=num_channels, in_channels=num_channels,\
# add_last=add_last, dilation=dilation, configures=configures).to(device)
cd = ResNet(Bottleneck, DeconvBottleneck, layer_infos, num_channels).to(device)
if cd_path != "":
cd.load_state_dict(torch.load(cd_path))
cd = nn.DataParallel(cd)
cd.eval()
layer_infos = [
[64, 7, 2, 3],
[3, 2, 1],
[12, 64, 3, 2, 1, 1],
[16, 128, 3, 1, 1, 1],
[24, 256, 3, 1, 1, 1],
[12, 512, 3, 1, 1, 1],
[12, 512, 3, 1, 1, 0, 1],
[24, 256, 3, 1, 1, 0, 1],
[16, 128, 3, 1, 1, 0, 1],
[12, 64, 3, 2, 1, 1, 1],
[4, 64, 3, 2, 1, 1, 1]
]
fd = ResNet(Bottleneck, DeconvBottleneck, layer_infos, 41, inp=1+int(ce)).to(device)
#fd = ConfNet(41, ce).to(device)
fd = nn.DataParallel(fd)
fd.train()
# loss definition
if ce:
if mask:
CLoss = nn.CrossEntropyLoss(reduction='none')
else:
CLoss = nn.CrossEntropyLoss()
elif mask:
CLoss = nn.MSELoss(reduction='none')
else:
CLoss = nn.MSELoss()
# optimizer
optimizer = torch.optim.Adam(fd.module.parameters(),\
lr=lr, weight_decay=weight_decay)
# lr decay
#lr_decay_lambda = lambda epoch: lr_decay_rate ** (epoch // lr_epoch_per_decay)
#scheduler = LambdaLR(optimizer, lr_lambda=lr_decay_lambda)
def adjust_learning_rate(optimizer, base_lr, max_iters,
cur_iters, power=0.9):
lr = base_lr*((1-float(cur_iters)/max_iters)**(power))
optimizer.param_groups[0]['lr'] = lr
return lr
birth = time.time()
length = int(len(train_data) / batch_size)
if len(train_data) % batch_size != 0:
length += 1
anchors = []
for anchor in range(vis_freq):
anchors.append(int(length * (anchor+1) /vis_freq))
for epoch in range(epochs):
pbar = tqdm(total = length)
for batch_idx, sample in enumerate(train_loader):
# start time
start = time.time()
pbar.update(1)
# read sample
sem_in = sample['sem_in'].float().to(device)
sem_gt = sample['sem_gt'].float().to(device)
# loss and optimize
optimizer.zero_grad()
pred = cd(sem_in).detach()
pred = torch.nn.functional.softmax(pred, dim=1)
if oh:
max_idx = torch.argmax(pred, 1, keepdim=True)
pred = torch.FloatTensor(pred.shape).to(device)
pred.zero_()
pred.scatter_(1, max_idx, 1)
if mask:
valid = (sem_gt != sem_in.shape[1]-1) & (torch.argmax(sem_in,
dim=1) == sem_in.shape[1]-1)
valid = valid.float()
sem_gt = (sem_gt == torch.argmax(pred,
dim=1))
output = fd(torch.cat((sem_in, pred), dim=1))
if output.shape[1] == 1:
output = output.squeeze(1)
if not ce:
loss = CLoss(output, sem_gt.float())
else:
loss = CLoss(output, sem_gt.long())
if mask:
loss_unseen = loss * valid * ratio
loss_unseen = torch.sum(loss_unseen) / torch.sum(valid)
loss_seen = loss * (1. - valid) * (1. - ratio)
loss_seen = torch.sum(loss_seen) / torch.sum(1.-valid)
loss = loss_unseen + loss_seen
loss.backward()
optimizer.step()
# update learning rate
adjust_learning_rate(optimizer, lr, length * epochs, epoch * length
+ batch_idx)
# end time
end = time.time()
# visualization
writer.add_scalar('Loss', float(loss), epoch * length + batch_idx)
#print("Time:%s, Epoch#:%s/%s, Iteration#:%s/%s, Loss:%s" % (end-start,
# epoch, epochs, batch_idx, length, float(loss)))
'''
cv2.imwrite(os.path.join(save_dir, "sem_in.png"),\
d3_41_colors_rgb[torch.argmax(sem_in[0], dim=0).cpu().numpy()])
cv2.imwrite(os.path.join(save_dir, "output.png"),\
d3_41_colors_rgb[torch.argmax(output[0],\
dim=0).detach().cpu().numpy()])
cv2.imwrite(os.path.join(save_dir, "sem_gt.png"),\
d3_41_colors_rgb[sem_gt[0].int().cpu().numpy()])
'''
# visualize certain pairs
if (batch_idx + 1) not in anchors:
continue
nam = epoch * vis_freq + anchors.index(batch_idx + 1)
fd.eval()
# get a batch of training sample to visualize
sem_in = None
sem_gt = None
for idx in vis_train:
sample = train_data[idx]
if sem_in is None:
sem_in = sample['sem_in'].unsqueeze(0).float().to(device)
sem_gt = sample['sem_gt'].unsqueeze(0).float().to(device)
else:
sem_in = torch.cat((sem_in,
sample['sem_in'].unsqueeze(0).float().to(device)), dim=0)
sem_gt = torch.cat((sem_gt,
sample['sem_gt'].unsqueeze(0).float().to(device)), dim=0)
with torch.no_grad():
output = cd(sem_in)
output = torch.nn.functional.softmax(output, dim=1)
if not ce:
foutput = fd(torch.cat((sem_in, output), dim=1)).squeeze(1)
else:
foutput = fd(torch.cat((sem_in, output), dim=1))
foutput = torch.nn.functional.softmax(foutput, dim=1)[:, 1, ...]
for s in range(len(vis_train)):
# save sample imgs
cv2.imwrite(os.path.join(save_dir, "%s_%s_sem_in.png" % (nam,s)),\
d3_41_colors_rgb[torch.argmax(sem_in[s], dim=0).cpu().numpy()])
cv2.imwrite(os.path.join(save_dir, "%s_%s_output.png" % (nam,s)),\
d3_41_colors_rgb[torch.argmax(output[s], dim=0).detach().cpu().numpy()])
cv2.imwrite(os.path.join(save_dir, "%s_%s_sem_gt.png" % (nam,s)),\
d3_41_colors_rgb[sem_gt[s].int().cpu().numpy()])
cv2.imwrite(os.path.join(save_dir, "%s_%s_conf.png" % (nam, s)),\
foutput[s].cpu().numpy() * 255.)
sem_in = None
sem_gt = None
for idx in vis_test:
sample = test_data[idx]
if sem_in is None:
sem_in = sample['sem_in'].unsqueeze(0).float().to(device)
sem_gt = sample['sem_gt'].unsqueeze(0).float().to(device)
else:
sem_in = torch.cat((sem_in,
sample['sem_in'].unsqueeze(0).float().to(device)), dim=0)
sem_gt = torch.cat((sem_gt,
sample['sem_gt'].unsqueeze(0).float().to(device)), dim=0)
with torch.no_grad():
output = cd(sem_in)
output = torch.nn.functional.softmax(output, dim=1)
if not ce:
foutput = fd(torch.cat((sem_in, output), dim=1)).squeeze(1)
else:
foutput = fd(torch.cat((sem_in, output), dim=1))
foutput = torch.nn.functional.softmax(foutput, dim=1)[:, 1, ...]
for s in range(len(vis_test)):
# save sample imgs
cv2.imwrite(os.path.join(save_dir, "%s_%s_sem_in_test.png" % (nam,s)),\
d3_41_colors_rgb[torch.argmax(sem_in[s], dim=0).cpu().numpy()])
cv2.imwrite(os.path.join(save_dir, "%s_%s_output_test.png" % (nam,s)),\
d3_41_colors_rgb[torch.argmax(output[s], dim=0).detach().cpu().numpy()])
cv2.imwrite(os.path.join(save_dir, "%s_%s_sem_gt_test.png" % (nam,s)),\
d3_41_colors_rgb[sem_gt[s].int().cpu().numpy()])
cv2.imwrite(os.path.join(save_dir, "%s_%s_conf_test.png" % (nam, s)),\
foutput[s].cpu().numpy() * 255.)
fd.train()
pbar.close()
# save model
if (epoch+1) % save_interval == 0:
nam = int((epoch+1) / save_interval)
torch.save(fd.module.state_dict(), os.path.join(save_dir, "%s_fd.pth" % nam))
## update learning rate
#scheduler.step(epoch + 1)
# evaluate this epoch's model on test set
validate(device, cd, fd, test_loader, writer, batch_size, save_dir, epoch,
CLoss, mask=mask, ce=ce, oh=oh, ratio=ratio)