-
Notifications
You must be signed in to change notification settings - Fork 2
/
trainer.py
233 lines (209 loc) · 8.44 KB
/
trainer.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
# Copyright 2020 - 2021 MONAI Consortium
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import time
import shutil
import numpy as np
import paddle
from paddle.amp import GradScaler, auto_cast
from tensorboardX import SummaryWriter
from monai.data import decollate_batch
def dice(x, y):
intersect = np.sum(np.sum(np.sum(x * y)))
y_sum = np.sum(np.sum(np.sum(y)))
if y_sum == 0:
return 0.0
x_sum = np.sum(np.sum(np.sum(x)))
return 2 * intersect / (x_sum + y_sum)
class AverageMeter(object):
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = np.where(self.count > 0,
self.sum / self.count,
self.sum)
def train_epoch(model,
loader,
optimizer,
scaler,
epoch,
loss_func,
args):
model.train()
start_time = time.time()
run_loss = AverageMeter()
for idx, batch_data in enumerate(loader):
if isinstance(batch_data, list):
data, target = batch_data
else:
data, target = batch_data['image'], batch_data['label']
data, target = data.cuda(args.rank), target.cuda(args.rank)
with auto_cast(enable=args.amp):
logits = model(data)
loss = loss_func(logits, target)
if args.amp:
scaled = scaler.scale(loss)
scaled.backward()
scaler.step(optimizer)
scaler.update()
optimizer.clear_grad()
else:
loss.backward()
optimizer.step()
optimizer.clear_grad()
run_loss.update(loss.item(), n=args.batch_size)
if args.rank == 0:
print('Epoch {}/{} {}/{}'.format(epoch, args.max_epochs, idx, len(loader)),
'loss: {:.4f}'.format(run_loss.avg),
'time {:.2f}s'.format(time.time() - start_time))
start_time = time.time()
return run_loss.avg
def val_epoch(model,
loader,
epoch,
acc_func,
args,
model_inferer=None,
post_label=None,
post_pred=None):
model.eval()
start_time = time.time()
avg_acc_list = []
with paddle.no_grad():
for idx, batch_data in enumerate(loader):
if isinstance(batch_data, list):
data, target = batch_data
else:
data, target = batch_data['image'], batch_data['label']
data, target = data.cuda(args.rank), target.cuda(args.rank)
with auto_cast(enable=args.amp):
if model_inferer is not None:
logits = model_inferer(data)
else:
logits = model(data)
val_labels_list = decollate_batch(target)
val_labels_convert = [post_label(val_label_tensor) for val_label_tensor in val_labels_list]
val_outputs_list = decollate_batch(logits)
val_output_convert = [post_pred(val_pred_tensor) for val_pred_tensor in val_outputs_list]
acc = acc_func(y_pred=val_output_convert, y=val_labels_convert)
acc = acc.cuda(args.rank)
acc_list = acc.cpu().numpy()
avg_acc = np.mean([np.nanmean(l) for l in acc_list])
avg_acc_list.append(avg_acc)
if args.rank == 0:
print('Val {}/{} {}/{}'.format(epoch, args.max_epochs, idx, len(loader)),
'acc', avg_acc,
'time {:.2f}s'.format(time.time() - start_time))
start_time = time.time()
return np.mean(avg_acc_list)
def save_checkpoint(model,
epoch,
args,
filename='model.pdparams',
best_acc=0,
optimizer=None,
scheduler=None):
state_dict = model.state_dict()
save_dict = {
'epoch': epoch,
'best_acc': best_acc,
'state_dict': state_dict
}
if optimizer is not None:
save_dict['optimizer'] = optimizer.state_dict()
if scheduler is not None:
save_dict['scheduler'] = scheduler.state_dict()
filename = os.path.join(args.logdir, filename)
paddle.save(save_dict, filename)
print('Saving checkpoint', filename)
def run_training(model,
train_loader,
val_loader,
optimizer,
loss_func,
acc_func,
args,
model_inferer=None,
scheduler=None,
start_epoch=0,
post_label=None,
post_pred=None
):
writer = True
if args.logdir is not None and args.rank == 0:
writer = SummaryWriter(log_dir=args.logdir)
if args.rank == 0:
print('Writing Tensorboard logs to ', args.logdir)
scaler = None
if args.amp:
scaler = GradScaler()
val_acc_max = 0.
for epoch in range(start_epoch, args.max_epochs):
print(args.rank, time.ctime(), 'Epoch:', epoch)
epoch_time = time.time()
train_loss = train_epoch(model,
train_loader,
optimizer,
scaler=scaler,
epoch=epoch,
loss_func=loss_func,
args=args)
if args.rank == 0:
print('Final training {}/{}'.format(epoch, args.max_epochs - 1), 'loss: {:.4f}'.format(train_loss),
'time {:.2f}s'.format(time.time() - epoch_time))
if args.rank == 0 and writer is not None:
writer.add_scalar('train_loss', train_loss, epoch)
b_new_best = False
if (epoch + 1) % args.val_every == 0:
epoch_time = time.time()
val_avg_acc = val_epoch(model,
val_loader,
epoch=epoch,
acc_func=acc_func,
model_inferer=model_inferer,
args=args,
post_label=post_label,
post_pred=post_pred)
if args.rank == 0:
print('Final validation {}/{}'.format(epoch, args.max_epochs - 1),
'acc', val_avg_acc, 'time {:.2f}s'.format(time.time() - epoch_time))
if writer is not None:
writer.add_scalar('val_acc', val_avg_acc, epoch)
if val_avg_acc > val_acc_max:
print('new best ({:.6f} --> {:.6f}). '.format(val_acc_max, val_avg_acc))
val_acc_max = val_avg_acc
b_new_best = True
if args.rank == 0 and args.logdir is not None and args.save_checkpoint:
save_checkpoint(model, epoch, args,
best_acc=val_acc_max,
optimizer=optimizer,
scheduler=scheduler)
if args.rank == 0 and args.logdir is not None and args.save_checkpoint:
save_checkpoint(model,
epoch,
args,
best_acc=val_acc_max,
filename='model_final.pdparams')
if b_new_best:
print('Copying to model.pt new best model!!!!')
shutil.copyfile(os.path.join(args.logdir, 'model_final.pdparams'), os.path.join(args.logdir, 'model.pdparams'))
if scheduler is not None:
scheduler.step()
print('Training Finished !, Best Accuracy: ', val_acc_max)
return val_acc_max