-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtrainer.py
More file actions
executable file
·373 lines (304 loc) · 14.1 KB
/
trainer.py
File metadata and controls
executable file
·373 lines (304 loc) · 14.1 KB
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
import numpy as np
import matplotlib.pyplot as plt
import shutil
import argparse
import os
import json
import random
import warnings
from termcolor import colored
import pandas as pd
from sklearn.metrics import confusion_matrix
import cv2
import importlib
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import torch.optim as optim
import torch.utils.data as data
from ignite.contrib.handlers import ProgressBar
from ignite.engine import Engine, Events
from ignite.handlers import ModelCheckpoint, Timer
from ignite.metrics import RunningAverage
from tensorboardX import SummaryWriter
import imgaug # https://github.com/aleju/imgaug
from imgaug import augmenters as iaa
import misc as misc
import dataset as dataset
from config import Config
from define_network import define_network
####
class Trainer(Config):
####
def train_step(self, net, batch, optimizer, device):
net.train() # train mode
imgs, true = batch # batch is NHWC
imgs = imgs.permute(0, 3, 1, 2) # to NCHW
# push data to GPUs and convert to float32
imgs = imgs.to(device).float()
true = true.to(device).long() # not one-hot
# -----------------------------------------------------------
net.zero_grad() # not rnn so not accumulate
logit = net(imgs) # forward
prob = F.softmax(logit, dim=-1)
pred = torch.argmax(prob, dim=-1)
# has built-int log softmax so accept logit
loss = F.cross_entropy(logit, true, reduction='mean')
acc = torch.mean((pred == true).float()) # batch accuracy
# gradient update
loss.backward()
optimizer.step()
# -----------------------------------------------------------p
return dict(loss=loss.item(),
acc=acc.item())
####
def infer_step(self, net, batch, device):
net.eval() # infer mode
imgs, true = batch # batch is NHWC
imgs = imgs.permute(0, 3, 1, 2) # to NCHW
# push data to GPUs and convert to float32
imgs = imgs.to(device).float()
true = true.to(device).long() # not one-hot
# -----------------------------------------------------------
with torch.no_grad(): # dont compute gradient
logit = net(imgs)
prob = nn.functional.softmax(logit, dim=-1)
return dict(prob=prob.cpu().numpy(),
true=true.cpu().numpy())
####
def run_once(self, log_dir):
"""
`pretrained_path` should lead to pytorch checkpoint
"""
misc.check_manual_seed(self.seed)
train_pairs, valid_pairs = eval(f'dataset.prepare_{self.dataset}_patch_data()')
# --------------------------- Dataloader
train_augmentors = self.train_augmentors()
train_dataset = dataset.DatasetSerial(train_pairs[:],
shape_augs=iaa.Sequential(train_augmentors[0]),
input_augs=iaa.Sequential(train_augmentors[1]))
infer_augmentors = self.infer_augmentors()
infer_dataset = dataset.DatasetSerial(valid_pairs[:],
shape_augs=iaa.Sequential(infer_augmentors[0]),
input_augs=iaa.Sequential(infer_augmentors[1]))
train_loader = data.DataLoader(train_dataset,
num_workers=self.nr_procs_train,
batch_size=self.train_batch_size,
shuffle=True, drop_last=True)
valid_loader = data.DataLoader(infer_dataset,
num_workers=self.nr_procs_valid,
batch_size=self.infer_batch_size,
shuffle=True, drop_last=False)
# --------------------------- Training Sequence
if self.logging:
misc.check_log_dir(log_dir)
device = 'cuda'
# networks
net = define_network(self.network_name, self.nr_class)
net = torch.nn.DataParallel(net).to(device)
optimizer, optimizer_args = self.optimizer
optimizer = optimizer(net.parameters(), **optimizer_args)
scheduler = self.scheduler(optimizer)
trainer = Engine(lambda engine, batch: self.train_step(net, batch, optimizer, device))
inferer = Engine(lambda engine, batch: self.infer_step(net, batch, device))
train_output = ['loss', 'acc']
infer_output = ['prob', 'true']
##
if self.logging:
@trainer.on(Events.EPOCH_COMPLETED)
def save_chkpoints(engine):
torch.save(net.state_dict(), self.log_dir + '_net_' + str(engine.state.epoch) + '.pth')
timer = Timer(average=True)
timer.attach(trainer, start=Events.EPOCH_STARTED, resume=Events.ITERATION_STARTED,
pause=Events.ITERATION_COMPLETED, step=Events.ITERATION_COMPLETED)
timer.attach(inferer, start=Events.EPOCH_STARTED, resume=Events.ITERATION_STARTED,
pause=Events.ITERATION_COMPLETED, step=Events.ITERATION_COMPLETED)
# attach running average metrics computation
# decay of EMA to 0.95 to match tensorpack default
RunningAverage(alpha=0.95, output_transform=lambda x: x['loss']).attach(trainer, 'loss')
RunningAverage(alpha=0.95, output_transform=lambda x: x['acc']).attach(trainer, 'acc')
# attach progress bar
pbar = ProgressBar(persist=True)
pbar.attach(trainer, metric_names=['loss'])
pbar.attach(inferer)
# writer for tensorboard logging
if self.logging:
writer = SummaryWriter(log_dir=log_dir)
json_log_file = log_dir + '/stats.json'
with open(json_log_file, 'w') as json_file:
json.dump({}, json_file) # create empty file
@trainer.on(Events.EPOCH_STARTED)
# @trainer.on(Events.EPOCH_COMPLETED)
def log_lrs(engine):
if self.logging:
lr = float(optimizer.param_groups[0]['lr'])
writer.add_scalar("lr", lr, engine.state.epoch)
# advance scheduler clock
if scheduler is not None:
scheduler.step()
####
def update_logs(output, epoch, prefix, color):
# print values and convert
max_length = len(max(output.keys(), key=len))
for metric in output:
key = colored(prefix + '-' + metric.ljust(max_length), color)
print('------%s : ' % key, end='')
if metric != 'conf_mat':
print('%0.7f' % output[metric])
else:
conf_mat = output['conf_mat'] # use pivot to turn back
conf_mat_df = pd.DataFrame(conf_mat)
conf_mat_df.index.name = 'True'
conf_mat_df.columns.name = 'Pred'
output['conf_mat'] = conf_mat_df
print('\n', conf_mat_df)
if 'train' in prefix:
lr = float(optimizer.param_groups[0]['lr'])
key = colored(prefix + '-' + 'lr'.ljust(max_length), color)
print('------%s : %0.7f' % (key, lr))
if not self.logging:
return
# create stat dicts
stat_dict = {}
for metric in output:
if metric != 'conf_mat':
metric_value = output[metric]
else:
conf_mat_df = output['conf_mat'] # use pivot to turn back
conf_mat_df = conf_mat_df.unstack().rename('value').reset_index()
conf_mat_df = pd.Series({'conf_mat': conf_mat}).to_json(orient='records')
metric_value = conf_mat_df
stat_dict['%s-%s' % (prefix, metric)] = metric_value
# json stat log file, update and overwrite
with open(json_log_file) as json_file:
json_data = json.load(json_file)
current_epoch = str(epoch)
if current_epoch in json_data:
old_stat_dict = json_data[current_epoch]
stat_dict.update(old_stat_dict)
current_epoch_dict = {current_epoch: stat_dict}
json_data.update(current_epoch_dict)
with open(json_log_file, 'w') as json_file:
json.dump(json_data, json_file)
# log values to tensorboard
for metric in output:
if metric != 'conf_mat':
writer.add_scalar(prefix + '-' + metric, output[metric], current_epoch)
@trainer.on(Events.EPOCH_COMPLETED)
def log_train_running_results(engine):
"""
running training measurement
"""
training_ema_output = engine.state.metrics #
update_logs(training_ema_output, engine.state.epoch, prefix='train-ema', color='green')
####
def get_init_accumulator(output_names):
return {metric: [] for metric in output_names}
def process_accumulated_output(output):
#
def uneven_seq_to_np(seq, batch_size=self.infer_batch_size):
item_count = batch_size * (len(seq) - 1) + len(seq[-1])
cat_array = np.zeros((item_count,) + seq[0][0].shape, seq[0].dtype)
for idx in range(0, len(seq) - 1):
cat_array[idx * batch_size:
(idx + 1) * batch_size] = seq[idx]
cat_array[(idx + 1) * batch_size:] = seq[-1]
return cat_array
#
prob = uneven_seq_to_np(output['prob'])
true = uneven_seq_to_np(output['true'])
# threshold then get accuracy
pred = np.argmax(prob, axis=-1)
acc = np.mean(pred == true)
# confusion matrix
conf_mat = confusion_matrix(true, pred,
labels=np.arange(self.nr_classes))
#
proc_output = dict(acc=acc, conf_mat=conf_mat)
return proc_output
@trainer.on(Events.EPOCH_COMPLETED)
def infer_valid(engine):
"""
inference measurement
"""
inferer.accumulator = get_init_accumulator(infer_output)
inferer.run(valid_loader)
output_stat = process_accumulated_output(inferer.accumulator)
update_logs(output_stat, engine.state.epoch, prefix='valid', color='red')
@inferer.on(Events.ITERATION_COMPLETED)
def accumulate_outputs(engine):
batch_output = engine.state.output
for key, item in batch_output.items():
engine.accumulator[key].extend([item])
###
# Setup is done. Now let's run the training
trainer.run(train_loader, self.nr_epochs)
return
####
def run(self):
def get_last_chkpt_path(phase1_dir):
stat_file_path = phase1_dir + '/stats.json'
with open(stat_file_path) as stat_file:
info = json.load(stat_file)
chkpt_list = [int(epoch) for epoch in info.keys()]
last_chkpts_path = "%smodel_net_%d.pth" % (phase1_dir, max(chkpt_list))
return last_chkpts_path
self.run_once(self.log_dir)
return
def infer(self):
misc.check_manual_seed(self.seed)
train_pairs, valid_pairs = eval(f'dataset.prepare_{self.dataset}_patch_data()')
infer_augmentors = self.infer_augmentors()
infer_dataset = dataset.DatasetSerial(valid_pairs[:],
shape_augs=iaa.Sequential(infer_augmentors[0]),
input_augs=iaa.Sequential(infer_augmentors[1]))
device = torch.device("cuda:0", )
net = define_network(self.network_name)
ckpt = torch.load(self.saved_path)
if isinstance(ckpt, torch.nn.DataParallel):
ckpt = ckpt.module.state_dict()
from collections import OrderedDict
new_state_dict = OrderedDict()
for k, v in ckpt.items():
name = k[7:]
new_state_dict[name] = v
net.load_state_dict(new_state_dict, strict=True)
net = torch.nn.DataParallel(net).to(device)
net.eval()
true = []
pred = []
for idx in range(0, len(valid_pairs)):
image, label = infer_dataset.__getitem__(idx)
img = torch.from_numpy(np.array(image))
img = torch.unsqueeze(img, dim=0)
img = img.permute(0, 3, 1, 2)
img = img.to(device).float()
logit = net(img)
prob = nn.functional.softmax(logit, dim=-1)
prob = prob.detach().cpu().numpy()
p = np.argmax(prob, axis=-1)
pred.append(p)
true.append(label)
true = np.reshape(true, (len(true),))
pred = np.reshape(pred, (len(pred),))
conf_mat = confusion_matrix(true, pred,
labels=np.arange(self.nr_class))
print('conf_mat : ', conf_mat)
return 0
###
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--gpu', default='0', help='comma separated list of GPU(s) to use.')
parser.add_argument('--view', help='view dataset', action='store_true')
parser.add_argument('--dataset', type=str, default='colon_tma', help='colon_tma, prostate_tma')
parser.add_argument('--network_name', type=str, default='VGG', help='ResNet, MobileNetV1, EfficientNet, VGG, ResNeSt'
'MuDeep, MSDNet, Res2Net'
'ResNet_MSBP, ResNet_add, ResNet_conv, ResNet_concat'
'ResNet_concat_zm, ResNet_conv_zm')
parser.add_argument('--saved_path', type=str, default='', help='path to trained models to validate')
args = parser.parse_args()
trainer = Trainer(_args=args)
if args.gpu:
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu
trainer.run()