forked from samuelschen/DSB2018
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
299 lines (283 loc) · 12.1 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
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
# python built-in library
import os
import argparse
import time
from multiprocessing import Manager
# 3rd party library
import numpy as np
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
from torch.utils.data.sampler import RandomSampler, WeightedRandomSampler
from tensorboardX import SummaryWriter
# own code
from model import build_model
from dataset import KaggleDataset, Compose
from helper import config, AverageMeter, iou_mean, save_ckpt, load_ckpt
from loss import contour_criterion, focal_criterion
def main(resume=True, n_epoch=None, learn_rate=None):
model_name = config['param']['model']
if learn_rate is None:
learn_rate = config['param'].getfloat('learn_rate')
width = config.getint(model_name, 'width')
weight_map = config['param'].getboolean('weight_map')
c = config['train']
log_name = c.get('log_name')
n_batch = c.getint('n_batch')
n_worker = c.getint('n_worker')
n_cv_epoch = c.getint('n_cv_epoch')
if n_epoch is None:
n_epoch = c.getint('n_epoch')
balance_group = c.getboolean('balance_group')
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = build_model(model_name)
model = model.to(device)
# define optimizer
optimizer = torch.optim.Adam(
filter(lambda p: p.requires_grad, model.parameters()),
lr=args.learn_rate,
weight_decay=1e-6
)
# dataloader workers are forked process thus we need a IPC manager to keep cache in same memory space
manager = Manager()
cache = manager.dict()
compose = Compose()
# prepare dataset
if os.path.exists('data/valid'):
# advance mode: use valid folder as CV
train_dataset = KaggleDataset('data/train', transform=compose, cache=cache)
valid_dataset = KaggleDataset('data/valid', transform=compose, cache=cache)
else:
# auto mode: split part of train dataset as CV
train_dataset = KaggleDataset('data/train', transform=compose, cache=cache, use_filter=True)
train_dataset, valid_dataset = train_dataset.split()
# decide whether to balance training set
if balance_group:
weights, ratio = train_dataset.class_weight()
# Len of weights is number of original epoch samples.
# After oversample balance, majority class will be under-sampled (least sampled)
# Multipling raito is to gain chance for each sample to be visited at least once in each epoch
sampler = WeightedRandomSampler(weights, int(len(weights) * ratio))
else:
sampler = RandomSampler(train_dataset)
# data loader
train_loader = DataLoader(
train_dataset,
sampler=sampler,
batch_size=n_batch,
num_workers=n_worker,
pin_memory=torch.cuda.is_available())
valid_loader = DataLoader(
valid_dataset,
shuffle=False,
batch_size=n_batch,
num_workers=n_worker)
# resume checkpoint
start_epoch = iou_tr = iou_cv = 0
if resume:
start_epoch = load_ckpt(model, optimizer)
if start_epoch == 0:
print('Grand new training ...')
# put model to GPU
if torch.cuda.device_count() > 1:
print("Let's use", torch.cuda.device_count(), "GPUs!")
model = nn.DataParallel(model)
# decide log directory name
log_dir = os.path.join(
'logs', log_name, '{}-{}'.format(model_name, width),
'ep_{},{}-lr_{}'.format(
start_epoch,
n_epoch + start_epoch,
learn_rate,
)
)
with SummaryWriter(log_dir) as writer:
if start_epoch == 0 and False:
# dump graph only for very first training, disable by default
dump_graph(model, writer, n_batch, width)
print('Training started...')
for epoch in range(start_epoch + 1, n_epoch + start_epoch + 1): # 1 base
iou_tr = train(train_loader, model, optimizer, epoch, writer)
if len(valid_dataset) > 0 and epoch % n_cv_epoch == 0:
with torch.no_grad():
iou_cv = valid(valid_loader, model, epoch, writer, len(train_loader))
save_ckpt(model, optimizer, epoch, iou_tr, iou_cv)
print('Training finished...')
def dump_graph(model, writer, n_batch, width):
# Prerequisite
# $ sudo apt-get install libprotobuf-dev protobuf-compiler
# $ pip3 install onnx
print('Dump model graph...')
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
dummy_input = torch.rand(n_batch, 3, width, width, device=device)
torch.onnx.export(model, dummy_input, "checkpoint/model.pb", verbose=False)
writer.add_graph_onnx("checkpoint/model.pb")
def train(loader, model, optimizer, epoch, writer):
batch_time = AverageMeter()
data_time = AverageMeter()
losses = AverageMeter()
iou = AverageMeter() # semantic IoU
iou_c = AverageMeter() # contour IoU
iou_m = AverageMeter() # marker IoU
print_freq = config['train'].getfloat('print_freq')
only_contour = config['contour'].getboolean('exclusive')
weight_map = config['param'].getboolean('weight_map')
model_name = config['param']['model']
with_contour = config.getboolean(model_name, 'branch_contour')
with_marker = config.getboolean(model_name, 'branch_marker')
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Sets the module in training mode.
model.train()
end = time.time()
n_step = len(loader)
for i, data in enumerate(loader):
# measure data loading time
data_time.update(time.time() - end)
# split sample data
inputs = data['image'].to(device)
labels = data['label'].to(device)
labels_c = data['label_c'].to(device)
labels_m = data['label_m'].to(device)
# get loss weight
weights = None
if weight_map and 'weight' in data:
weights = data['weight'].to(device)
# zero the parameter gradients
optimizer.zero_grad()
# forward step
outputs = model(inputs)
if with_contour and with_marker:
outputs, outputs_c, outputs_m = outputs
elif with_contour:
outputs, outputs_c = outputs
# compute loss
if only_contour:
loss = contour_criterion(outputs, labels_c)
else:
# weight_criterion equals to segment_criterion if weights is none
loss = focal_criterion(outputs, labels, weights)
if with_contour:
loss += focal_criterion(outputs_c, labels_c, weights)
if with_marker:
loss += focal_criterion(outputs_m, labels_m, weights)
# compute gradient and do backward step
loss.backward()
optimizer.step()
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
# measure accuracy and record loss
# NOT instance-level IoU in training phase, for better speed & instance separation handled in post-processing
losses.update(loss.item(), inputs.size(0))
if only_contour:
batch_iou = iou_mean(outputs, labels_c)
else:
batch_iou = iou_mean(outputs, labels)
iou.update(batch_iou, inputs.size(0))
if with_contour:
batch_iou_c = iou_mean(outputs_c, labels_c)
iou_c.update(batch_iou_c, inputs.size(0))
if with_marker:
batch_iou_m = iou_mean(outputs_m, labels_m)
iou_m.update(batch_iou_m, inputs.size(0))
# log to summary
#step = i + epoch * n_step
#writer.add_scalar('training/loss', loss.item(), step)
#writer.add_scalar('training/batch_elapse', batch_time.val, step)
#writer.add_scalar('training/batch_iou', iou.val, step)
#writer.add_scalar('training/batch_iou_c', iou_c.val, step)
#writer.add_scalar('training/batch_iou_m', iou_m.val, step)
if (i + 1) % print_freq == 0:
print(
'Epoch: [{0}][{1}/{2}]\t'
'Time: {batch_time.avg:.2f} (io: {data_time.avg:.2f})\t'
'Loss: {loss.val:.4f} (avg: {loss.avg:.4f})\t'
'IoU: {iou.avg:.3f} (Coutour: {iou_c.avg:.3f}, Marker: {iou_m.avg:.3f})\t'
.format(
epoch, i, n_step, batch_time=batch_time,
data_time=data_time, loss=losses, iou=iou, iou_c=iou_c, iou_m=iou_m
)
)
# end of loop, dump epoch summary
writer.add_scalar('training/epoch_loss', losses.avg, epoch)
writer.add_scalar('training/epoch_iou', iou.avg, epoch)
writer.add_scalar('training/epoch_iou_c', iou_c.avg, epoch)
writer.add_scalar('training/epoch_iou_m', iou_m.avg, epoch)
return iou.avg # return epoch average iou
def valid(loader, model, epoch, writer, n_step):
iou = AverageMeter() # semantic IoU
iou_c = AverageMeter() # contour IoU
iou_m = AverageMeter() # marker IoU
losses = AverageMeter()
only_contour = config['contour'].getboolean('exclusive')
weight_map = config['param'].getboolean('weight_map')
model_name = config['param']['model']
with_contour = config.getboolean(model_name, 'branch_contour')
with_marker = config.getboolean(model_name, 'branch_marker')
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Sets the model in evaluation mode.
model.eval()
for i, data in enumerate(loader):
# get the inputs
inputs = data['image'].to(device)
labels = data['label'].to(device)
labels_c = data['label_c'].to(device)
labels_m = data['label_m'].to(device)
# get loss weight
weights = None
if weight_map and 'weight' in data:
weights = data['weight'].to(device)
# forward step
outputs = model(inputs)
if with_contour and with_marker:
outputs, outputs_c, outputs_m = outputs
elif with_contour:
outputs, outputs_c = outputs
# compute loss
if only_contour:
loss = contour_criterion(outputs, labels_c)
else:
# weight_criterion equals to segment_criterion if weights is none
loss = focal_criterion(outputs, labels, weights)
if with_contour:
loss += focal_criterion(outputs_c, labels_c, weights)
if with_marker:
loss += focal_criterion(outputs_m, labels_m, weights)
# measure accuracy and record loss (Non-instance level IoU)
losses.update(loss.item(), inputs.size(0))
if only_contour:
batch_iou = iou_mean(outputs, labels_c)
else:
batch_iou = iou_mean(outputs, labels)
iou.update(batch_iou, inputs.size(0))
if with_contour:
batch_iou_c = iou_mean(outputs_c, labels_c)
iou_c.update(batch_iou_c, inputs.size(0))
if with_marker:
batch_iou_m = iou_mean(outputs_m, labels_m)
iou_m.update(batch_iou_m, inputs.size(0))
# end of loop, dump epoch summary
writer.add_scalar('CV/epoch_loss', losses.avg, epoch)
writer.add_scalar('CV/epoch_iou', iou.avg, epoch)
writer.add_scalar('CV/epoch_iou_c', iou_c.avg, epoch)
writer.add_scalar('CV/epoch_iou_m', iou_m.avg, epoch)
print(
'Epoch: [{0}]\t\tcross-validation\t'
'Loss: N/A (avg: {loss.avg:.4f})\t'
'IoU: {iou.avg:.3f} (Coutour: {iou_c.avg:.3f}, Marker: {iou_m.avg:.3f})\t'
.format(
epoch, loss=losses, iou=iou, iou_c=iou_c, iou_m=iou_m
)
)
return iou.avg # return epoch average iou
if __name__ == '__main__':
learn_rate = config['param'].getfloat('learn_rate')
n_epoch = config['train'].getint('n_epoch')
parser = argparse.ArgumentParser()
parser.add_argument('--resume', dest='resume', action='store_true')
parser.add_argument('--no-resume', dest='resume', action='store_false')
parser.add_argument('--epoch', type=int, help='run number of epoch')
parser.add_argument('--lr', type=float, dest='learn_rate', help='learning rate')
parser.set_defaults(resume=True, epoch=n_epoch, learn_rate=learn_rate)
args = parser.parse_args()
main(args.resume, args.epoch, args.learn_rate)