-
Notifications
You must be signed in to change notification settings - Fork 20
/
train_part_sem.py
414 lines (343 loc) · 18.5 KB
/
train_part_sem.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
407
408
409
410
411
412
# -*- coding: utf-8 -*-
# @Author : Xiangyi Zhang
# @File : train.py
# @Email : [email protected]
import os
import shutil
import torch
import torch.nn as nn
import torch.optim
from torch.utils.data import DataLoader
from torch.optim.lr_scheduler import MultiStepLR
import torch.backends.cudnn as cudnn
from torchvision.transforms import Compose
from models.FewShotSegPartResnetSem import FewShotSegPart as FewShotSegPartResnetSem
from dataloaders.customized import voc_fewshot
from dataloaders.transforms import RandomMirror, Resize, ToTensorNormalize
from util.utils import set_seed, CLASS_LABELS, get_params
from config import ex
from util.metric import Metric
import tqdm
import torch.nn.functional as F
from tensorboardX import SummaryWriter
import matplotlib.pyplot as plt
from util.utils import check_dir
import numpy as np
import time
import os.path as osp
import pprint
from util.lr_scheduler import PolynomialLR
@ex.automain
def main(_run, _config, _log):
logdir = f'{_run.observers[0].dir}/'
print(logdir)
category = ['aeroplane','bicycle','bird','boat','bottle','bus','car','cat','chair','cow','diningtable','dog','horse','motorbike','person','pottedplant','sheep','sofa','train','tvmonitor']
if _run.observers:
os.makedirs(f'{_run.observers[0].dir}/snapshots', exist_ok=True)
for source_file, _ in _run.experiment_info['sources']:
os.makedirs(os.path.dirname(f'{_run.observers[0].dir}/source/{source_file}'),
exist_ok=True)
_run.observers[0].save_file(source_file, f'source/{source_file}')
shutil.rmtree(f'{_run.observers[0].basedir}/_sources')
cfg = _config
data_name = _config['dataset']
max_label=20 if data_name == 'VOC' else 80
set_seed(_config['seed'])
cudnn.enabled = True
cudnn.benchmark = True
torch.cuda.set_device(device=_config['gpu_id'])
torch.set_num_threads(1)
_log.info(f'###### Setting: {_run.observers[0].dir} ######')
print(_config['ckpt_dir'])
tbwriter = SummaryWriter(osp.join(_config['ckpt_dir']))
training_tags = {'loss': "ATraining/total_loss", "query_loss": "ATraining/query_loss",
'aligned_loss': "ATraining/aligned_loss", 'base_loss': "ATraining/base_loss",}
infer_tags = {
'mean_iou': "MeanIoU/mean_iou",
"mean_iou_binary": "MeanIoU/mean_iou_binary",
}
_log.info('###### Create model ######')
model = FewShotSegPartResnetSem(pretrained_path=_config['path']['init_path'], cfg=_config)
_log.info('Model: FewShotSegPartResnetSem')
model = nn.DataParallel(model.cuda(), device_ids=[_config['gpu_id'],])
model.train()
_log.info('###### Load data ######')
data_name = _config['dataset']
make_data = voc_fewshot
labels = CLASS_LABELS[data_name][_config['label_sets']]
transforms = Compose([Resize(size=_config['input_size']),RandomMirror()])
dataset = make_data(
base_dir=_config['path'][data_name]['data_dir'],
split=_config['path'][data_name]['data_split'],
transforms=transforms,
to_tensor=ToTensorNormalize(),
labels=labels,
max_iters=_config['n_steps'] * _config['batch_size'],
n_ways=_config['task']['n_ways'],
n_shots=_config['task']['n_shots'],
n_queries=_config['task']['n_queries'],
n_unlabel=_config['task']['n_unlabels'],
cfg=_config
)
trainloader = DataLoader(
dataset,
batch_size=_config['batch_size'],
shuffle=True,
num_workers=_config['num_workers'],
pin_memory=True,
drop_last=True
)
_log.info('###### Set optimizer ######')
# optimizer = torch.optim.SGD(model.parameters(), **_config['optim'])
# Optimizer
if _config['fix']:
print('Optimizer: fix')
optimizer = torch.optim.SGD(
params=[
{
"params": model.module.encoder.layer3.parameters(),
"lr": _config['optim']['lr'],
"weight_decay": _config['optim']['weight_decay']
},
{
"params": model.module.encoder.layer4.parameters(),
"lr": _config['optim']['lr'],
"weight_decay": _config['optim']['weight_decay']
},
{
"params": get_params(model, key='20x'),
"lr": 20 * _config['optim']['lr'],
"weight_decay": 0
},
{
"params": get_params(model, key='10x'),
"lr": 10 * _config['optim']['lr'],
"weight_decay": _config['optim']['weight_decay']
}], lr=_config['optim']['lr'], weight_decay=_config['optim']['weight_decay'],
momentum=_config['optim']['momentum'])
else:
print('Optimizer: Not fix')
optimizer = torch.optim.SGD(
params=[
{
"params": model.module.encoder.parameters(),
"lr": _config['optim']['lr'],
"weight_decay": _config['optim']['weight_decay']
},
{
"params": get_params(model, key='20x'),
"lr": 20 * _config['optim']['lr'],
"weight_decay": 0
},
{
"params": get_params(model, key='10x'),
"lr": 10 * _config['optim']['lr'],
"weight_decay": _config['optim']['weight_decay']
}], lr=_config['optim']['lr'], weight_decay=_config['optim']['weight_decay'], momentum=_config['optim']['momentum'])
print('scheduler: MultiStepLR')
scheduler = MultiStepLR(optimizer, milestones=_config['lr_milestones'], gamma=0.1)
criterion = nn.CrossEntropyLoss(ignore_index=_config['ignore_label'])
log_loss = {'loss': 0, 'align_loss': 0, 'base_loss': 0}
_log.info('###### Training ######')
highest_iou = 0
metrics = {}
device = torch.device('cuda')
for i_iter, sample_batched in enumerate(trainloader):
if _config['fix']:
model.module.encoder.conv1.eval()
model.module.encoder.bn1.eval()
model.module.encoder.layer1.eval()
model.module.encoder.layer2.eval()
if _config['eval']:
if i_iter == 0:
break
# Prepare input
support_images = [[shot.cuda() for shot in way] for way in sample_batched['support_images']]
support_fg_mask = [[shot[f'fg_mask'].float().cuda() for shot in way] for way in sample_batched['support_mask']]
support_bg_mask = [[shot[f'bg_mask'].float().cuda() for shot in way] for way in sample_batched['support_mask']]
query_images = [query_image.cuda() for query_image in sample_batched['query_images']]
query_labels = torch.cat([query_label.long().cuda() for query_label in sample_batched['query_labels']], dim=0)#1*417*417
# Forward and Backward
optimizer.zero_grad()
query_pred, output_semantic, align_loss = model(support_images, support_fg_mask, support_bg_mask, query_images)
support_label_base = torch.cat([torch.cat([shot.long().cuda() for shot in way]) for way in sample_batched['support_labels_base']]) # 2*1*417*417
query_labels_base = torch.cat([query_label.long().cuda() for query_label in sample_batched['query_labels_base']], dim=0) # 1*417*417
label_base = torch.cat((support_label_base, query_labels_base)) # 3*53*53
query_loss = criterion(query_pred, query_labels) #1*3*417*417, 1*417*417
base_loss = criterion(output_semantic, label_base) # 3*16*53*53, 3*53*53
loss = query_loss + align_loss * _config['align_loss_scaler'] + base_loss * _config['base_loss_scaler']
loss.backward()
optimizer.step()
scheduler.step(epoch=i_iter)
# Log loss
query_loss = query_loss.detach().data.cpu().numpy()
align_loss = align_loss.detach().data.cpu().numpy()
base_loss = base_loss.detach().data.cpu().numpy()
log_loss['loss'] += query_loss
log_loss['align_loss'] += align_loss
log_loss['base_loss'] += base_loss
# print loss and take snapshots
if (i_iter + 1) % _config['print_interval'] == 0:
loss = log_loss['loss'] / (i_iter + 1)
align_loss = log_loss['align_loss'] / (i_iter + 1)
base_loss = log_loss['base_loss'] / (i_iter + 1)
print(f'step {i_iter+1}: loss: {loss}, align_loss: {align_loss}, base_loss: {base_loss}')
_log.info(f'step {i_iter+1}: loss: {loss}, align_loss: {align_loss}, base_loss: {base_loss}')
metrics['loss'] = loss
metrics['query_loss'] = query_loss
metrics['aligned_loss'] = align_loss
metrics['base_loss'] = base_loss
# for k, v in metrics.items():
# tbwriter.add_scalar(training_tags[k], v, i_iter)
if (i_iter + 1) % _config['evaluate_interval'] == 0:
_log.info('###### Evaluation begins ######')
_log.info(f'###### Setting: {_run.observers[0].dir} ######')
print(_config['ckpt_dir'])
model.eval()
labels = CLASS_LABELS[data_name]['all'] - CLASS_LABELS[data_name][_config['label_sets']]
transforms = [Resize(size=_config['input_size'])]
transforms = Compose(transforms)
metric = Metric(max_label=max_label, n_runs=_config['n_runs'])
with torch.no_grad():
for run in range(1):
_log.info(f'### Run {run + 1} ###')
set_seed(_config['seed'] + run)
_log.info(f'### Load data ###')
dataset = make_data(
base_dir=_config['path'][data_name]['data_dir'],
split=_config['path'][data_name]['data_split'],
transforms=transforms,
to_tensor=ToTensorNormalize(),
labels=labels,
max_iters=_config['infer_max_iters'],
n_ways=_config['task']['n_ways'],
n_shots=_config['task']['n_shots'],
n_queries=_config['task']['n_queries'],
n_unlabel=_config['task']['n_unlabels'],
cfg=_config
)
testloader = DataLoader(dataset, batch_size=_config['batch_size'], shuffle=False,
num_workers=_config['num_workers'], pin_memory=True, drop_last=False)
_log.info(f"Total # of Data: {len(dataset)}")
for sample_batched in tqdm.tqdm(testloader):
label_ids = list(sample_batched['class_ids'])
support_images = [[shot.cuda() for shot in way] for way in sample_batched['support_images']]
suffix = 'mask'
support_fg_mask = [[shot[f'fg_{suffix}'].float().cuda() for shot in way] for way in sample_batched['support_mask']]
support_bg_mask = [[shot[f'bg_{suffix}'].float().cuda() for shot in way]for way in sample_batched['support_mask']]
query_images = [query_image.cuda() for query_image in sample_batched['query_images']]
query_labels = torch.cat([query_label.cuda() for query_label in sample_batched['query_labels']], dim=0)
query_pred, _, _ = model(support_images, support_fg_mask, support_bg_mask, query_images)
curr_iou = metric.record(query_pred.argmax(dim=1)[0], query_labels[0], labels=label_ids, n_run=run)
classIoU, meanIoU = metric.get_mIoU(labels=sorted(labels), n_run=run)
classIoU_binary, meanIoU_binary = metric.get_mIoU_binary(n_run=run)
_run.log_scalar('classIoU', classIoU.tolist())
_run.log_scalar('meanIoU', meanIoU.tolist())
_run.log_scalar('classIoU_binary', classIoU_binary.tolist())
_run.log_scalar('meanIoU_binary', meanIoU_binary.tolist())
_log.info(f'classIoU: {classIoU}')
_log.info(f'meanIoU: {meanIoU}')
_log.info(f'classIoU_binary: {classIoU_binary}')
_log.info(f'meanIoU_binary: {meanIoU_binary}')
print(f'meanIoU: {meanIoU}, meanIoU_binary: {meanIoU_binary}')
metrics = {}
metrics['mean_iou'] = meanIoU
metrics['mean_iou_binary'] = meanIoU_binary
for k, v in metrics.items():
tbwriter.add_scalar(infer_tags[k], v, i_iter)
if meanIoU > highest_iou:
print(f'The highest iou is in iter: {i_iter} : {meanIoU}, save: {_config["ckpt_dir"]}/best.pth')
highest_iou = meanIoU
torch.save(model.state_dict(),os.path.join(f'{_config["ckpt_dir"]}/best.pth'))
else:
print(f'The highest iou is in iter: {i_iter} : {meanIoU}')
torch.save(model.state_dict(), os.path.join(f'{_config["ckpt_dir"]}/{i_iter + 1}.pth'))
model.train()
_log.info(f'###### Setting: {_run.observers[0].dir} ######')
print(_config['ckpt_dir'])
_log.info(' --------- Testing begins ---------')
labels = CLASS_LABELS[data_name]['all'] - CLASS_LABELS[data_name][_config['label_sets']]
transforms = [Resize(size=_config['input_size'])]
transforms = Compose(transforms)
ckpt = os.path.join(f'{_config["ckpt_dir"]}/best.pth')
model.load_state_dict(torch.load(ckpt, map_location='cpu'))
model.eval()
metric = Metric(max_label=max_label, n_runs=5)
with torch.no_grad():
for run in range(5):
n_iter = 0
_log.info(f'### Run {run + 1} ###')
set_seed(_config['seed'] + run)
_log.info(f'### Load data ###')
dataset = make_data(
base_dir=_config['path'][data_name]['data_dir'],
split=_config['path'][data_name]['data_split'],
transforms=transforms,
to_tensor=ToTensorNormalize(),
labels=labels,
max_iters=_config['infer_max_iters'],
n_ways=_config['task']['n_ways'],
n_shots=_config['task']['n_shots'],
n_queries=_config['task']['n_queries'],
n_unlabel=_config['task']['n_unlabels'],
cfg=_config
)
testloader = DataLoader(dataset, batch_size=_config['batch_size'], shuffle=False,
num_workers=_config['num_workers'], pin_memory=True, drop_last=False)
_log.info(f"Total # of Data: {len(dataset)}")
for sample_batched in tqdm.tqdm(testloader):
label_ids = list(sample_batched['class_ids'])
support_images = [[shot.cuda() for shot in way] for way in sample_batched['support_images']]
suffix = 'mask'
support_fg_mask = [[shot[f'fg_{suffix}'].float().cuda() for shot in way] for way in sample_batched['support_mask']]
support_bg_mask = [[shot[f'bg_{suffix}'].float().cuda() for shot in way] for way in sample_batched['support_mask']]
query_images = [query_image.cuda() for query_image in sample_batched['query_images']]
query_labels = torch.cat([query_label.cuda() for query_label in sample_batched['query_labels']], dim=0)
query_pred, _, _ = model(support_images, support_fg_mask, support_bg_mask, query_images)
curr_iou = metric.record(query_pred.argmax(dim=1)[0], query_labels[0], labels=label_ids, n_run=run)
n_iter += 1
classIoU, meanIoU = metric.get_mIoU(labels=sorted(labels), n_run=run)
classIoU_binary, meanIoU_binary = metric.get_mIoU_binary(n_run=run)
_run.log_scalar('classIoU', classIoU.tolist())
_run.log_scalar('meanIoU', meanIoU.tolist())
_run.log_scalar('classIoU_binary', classIoU_binary.tolist())
_run.log_scalar('meanIoU_binary', meanIoU_binary.tolist())
_log.info(f'classIoU: {classIoU}')
_log.info(f'meanIoU: {meanIoU}')
_log.info(f'classIoU_binary: {classIoU_binary}')
_log.info(f'meanIoU_binary: {meanIoU_binary}')
classIoU, classIoU_std, meanIoU, meanIoU_std = metric.get_mIoU(labels=sorted(labels))
classIoU_binary, classIoU_std_binary, meanIoU_binary, meanIoU_std_binary = metric.get_mIoU_binary()
_run.log_scalar('meanIoU', meanIoU.tolist())
_run.log_scalar('meanIoU_binary', meanIoU_binary.tolist())
_run.log_scalar('final_classIoU', classIoU.tolist())
_run.log_scalar('final_classIoU_std', classIoU_std.tolist())
_run.log_scalar('final_meanIoU', meanIoU.tolist())
_run.log_scalar('final_meanIoU_std', meanIoU_std.tolist())
_run.log_scalar('final_classIoU_binary', classIoU_binary.tolist())
_run.log_scalar('final_classIoU_std_binary', classIoU_std_binary.tolist())
_run.log_scalar('final_meanIoU_binary', meanIoU_binary.tolist())
_run.log_scalar('final_meanIoU_std_binary', meanIoU_std_binary.tolist())
_log.info('----- Final Result -----')
_log.info(f'classIoU mean: {classIoU}')
_log.info(f'classIoU std: {classIoU_std}')
_log.info(f'meanIoU mean: {meanIoU}')
_log.info(f'meanIoU std: {meanIoU_std}')
_log.info(f'classIoU_binary mean: {classIoU_binary}')
_log.info(f'classIoU_binary std: {classIoU_std_binary}')
_log.info(f'meanIoU_binary mean: {meanIoU_binary}')
_log.info(f'meanIoU_binary std: {meanIoU_std_binary}')
_log.info("## ------------------------------------------ ##")
_log.info(f'###### Setting: {_run.observers[0].dir} ######')
_log.info("Running {num_run} runs, meanIoU:{miou:.4f}, meanIoU_binary:{mbiou:.4f} "
"meanIoU_std:{miou_std:.4f}, meanIoU_binary_std:{mbiou_std:.4f}".format(
num_run=5, miou=meanIoU, mbiou=meanIoU_binary, miou_std=meanIoU_std,
mbiou_std=meanIoU_std_binary))
_log.info(f"Current setting is {_run.observers[0].dir}")
print("Running {num_run} runs, meanIoU:{miou:.4f}, meanIoU_binary:{mbiou:.4f} "
"meanIoU_std:{miou_std:.4f}, meanIoU_binary_std:{mbiou_std:.4f}".format(
num_run=5, miou=meanIoU, mbiou=meanIoU_binary, miou_std=meanIoU_std,
mbiou_std=meanIoU_std_binary))
print(f"Current setting is {_run.observers[0].dir}")
print(_config['ckpt_dir'])
print(logdir)