-
Notifications
You must be signed in to change notification settings - Fork 2
/
train.py
160 lines (133 loc) · 6.58 KB
/
train.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
#!/usr/bin/env python
"""
For Evaluation
Extended from ADNet code by Hansen et al.
"""
import os
import random
import logging
import shutil
import torch.nn as nn
import torch.backends.cudnn as cudnn
import torch.optim
from torch.utils.data import DataLoader
from torch.optim.lr_scheduler import MultiStepLR
from models.fewshot import FewShotSeg
from dataloaders.datasets import TrainDataset as TrainDataset
from utils import *
from config import ex
from focal_loss.focal_loss import FocalLoss
@ex.automain
def main(_run, _config, _log):
if _run.observers:
# Set up source folder
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')
# Set up logger -> log to .txt
file_handler = logging.FileHandler(os.path.join(f'{_run.observers[0].dir}', f'logger.log'))
file_handler.setLevel('INFO')
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(name)s - %(message)s')
file_handler.setFormatter(formatter)
_log.handlers.append(file_handler)
_log.info(f'Run "{_config["exp_str"]}" with ID "{_run.observers[0].dir[-1]}"')
# Deterministic setting for reproduciablity.
if _config['seed'] is not None:
random.seed(_config['seed'])
torch.manual_seed(_config['seed'])
torch.cuda.manual_seed_all(_config['seed'])
cudnn.deterministic = True
# Enable cuDNN benchmark mode to select the fastest convolution algorithm.
cudnn.enabled = True
cudnn.benchmark = True
torch.cuda.set_device(device=_config['gpu_id'])
torch.set_num_threads(1)
_log.info(f'Create model...')
model = FewShotSeg()
model = model.cuda()
model.train()
_log.info(f'Set optimizer...')
optimizer = torch.optim.SGD(model.parameters(), **_config['optim'])
lr_milestones = [(ii + 1) * _config['max_iters_per_load'] for ii in
range(_config['n_steps'] // _config['max_iters_per_load'] - 1)]
scheduler = MultiStepLR(optimizer, milestones=lr_milestones, gamma=_config['lr_step_gamma'])
my_weight = torch.FloatTensor([0.1, 1.0]).cuda()
criterion = nn.NLLLoss(ignore_index=255, weight=my_weight)
criterion_focal = FocalLoss(gamma=0.7, weights=my_weight)
_log.info(f'Load data...')
data_config = {
'data_dir': _config['path'][_config['dataset']]['data_dir'],
'dataset': _config['dataset'],
'n_shot': _config['n_shot'],
'n_way': _config['n_way'],
'n_query': _config['n_query'],
'n_sv': _config['n_sv'],
'max_iter': _config['max_iters_per_load'],
'eval_fold': _config['eval_fold'],
'min_size': _config['min_size'],
'max_slices': _config['max_slices'],
'test_label': _config['test_label'],
'exclude_label': _config['exclude_label'],
'use_gt': _config['use_gt'],
}
train_dataset = TrainDataset(data_config)
train_loader = DataLoader(train_dataset,
batch_size=_config['batch_size'],
shuffle=True,
num_workers=_config['num_workers'],
pin_memory=True,
drop_last=True)
n_sub_epochs = _config['n_steps'] // _config['max_iters_per_load'] # number of times for reloading
log_loss = {'total_loss': 0, 'query_loss': 0, 'align_loss': 0, 'thresh_loss': 0}
i_iter = 0
_log.info(f'Start training...')
for sub_epoch in range(n_sub_epochs):
_log.info(f'This is epoch "{sub_epoch}" of "{n_sub_epochs}" epochs.')
for _, sample in enumerate(train_loader):
# Prepare episode data.
support_images = [[shot.float().cuda() for shot in way]
for way in sample['support_images']]
support_fg_mask = [[shot.float().cuda() for shot in way]
for way in sample['support_fg_labels']]
query_images = [query_image.float().cuda() for query_image in sample['query_images']]
query_labels = torch.cat([query_label.long().cuda() for query_label in sample['query_labels']], dim=0)
# Compute outputs and losses.
query_pred, align_loss, mse_loss, qry_loss = model(support_images, support_fg_mask, query_images, train=True)
query_loss = criterion(torch.log(torch.clamp(query_pred, torch.finfo(torch.float32).eps,
1 - torch.finfo(torch.float32).eps)), query_labels)
loss = query_loss + align_loss + mse_loss + qry_loss
# Compute gradient and do SGD step.
for param in model.parameters():
param.grad = None
loss.backward()
optimizer.step()
scheduler.step()
# Log loss
query_loss = query_loss.detach().data.cpu().numpy()
align_loss = align_loss.detach().data.cpu().numpy()
_run.log_scalar('total_loss', loss.item())
_run.log_scalar('query_loss', query_loss)
_run.log_scalar('align_loss', align_loss)
log_loss['total_loss'] += loss.item()
log_loss['query_loss'] += query_loss
log_loss['align_loss'] += align_loss
# Print loss and take snapshots.
if (i_iter + 1) % _config['print_interval'] == 0:
total_loss = log_loss['total_loss'] / _config['print_interval']
query_loss = log_loss['query_loss'] / _config['print_interval']
align_loss = log_loss['align_loss'] / _config['print_interval']
log_loss['total_loss'] = 0
log_loss['query_loss'] = 0
log_loss['align_loss'] = 0
_log.info(f'step {i_iter + 1}: total_loss: {total_loss}, query_loss: {query_loss},'
f' align_loss: {align_loss}')
if (i_iter + 1) % _config['save_snapshot_every'] == 0:
_log.info('###### Taking snapshot ######')
torch.save(model.state_dict(),
os.path.join(f'{_run.observers[0].dir}/snapshots', f'{i_iter + 1}.pth'))
i_iter += 1
_log.info('End of training.')
return 1