-
Notifications
You must be signed in to change notification settings - Fork 49
/
train.py
261 lines (218 loc) · 9.8 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
# -*- coding: utf-8 -*-
# @Time : 2020-02-26 17:44
# @Author : Zonas
# @Email : zonas.wang@gmail.com
# @File : train.py
"""
"""
import argparse
import logging
import os
import os.path as osp
import sys
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch import optim
from torch.utils.data import DataLoader, random_split
from torch.utils.tensorboard import SummaryWriter
from tqdm import tqdm
from unet import NestedUNet
from unet import UNet
from utils.dataset import BasicDataset
from config import UNetConfig
from losses import LovaszLossSoftmax
from losses import LovaszLossHinge
from losses import dice_coeff
cfg = UNetConfig()
def train_net(net, cfg):
dataset = BasicDataset(cfg.images_dir, cfg.masks_dir, cfg.scale)
val_percent = cfg.validation / 100
n_val = int(len(dataset) * val_percent)
n_train = len(dataset) - n_val
train, val = random_split(dataset, [n_train, n_val])
train_loader = DataLoader(train,
batch_size=cfg.batch_size,
shuffle=True,
num_workers=8,
pin_memory=True)
val_loader = DataLoader(val,
batch_size=cfg.batch_size,
shuffle=False,
num_workers=8,
pin_memory=True)
writer = SummaryWriter(comment=f'LR_{cfg.lr}_BS_{cfg.batch_size}_SCALE_{cfg.scale}')
global_step = 0
logging.info(f'''Starting training:
Epochs: {cfg.epochs}
Batch size: {cfg.batch_size}
Learning rate: {cfg.lr}
Optimizer: {cfg.optimizer}
Training size: {n_train}
Validation size: {n_val}
Checkpoints: {cfg.save_cp}
Device: {device.type}
Images scaling: {cfg.scale}
''')
if cfg.optimizer == 'Adam':
optimizer = optim.Adam(net.parameters(),
lr=cfg.lr)
elif cfg.optimizer == 'RMSprop':
optimizer = optim.RMSprop(net.parameters(),
lr=cfg.lr,
weight_decay=cfg.weight_decay)
else:
optimizer = optim.SGD(net.parameters(),
lr=cfg.lr,
momentum=cfg.momentum,
weight_decay=cfg.weight_decay,
nesterov=cfg.nesterov)
scheduler = optim.lr_scheduler.MultiStepLR(optimizer,
milestones=cfg.lr_decay_milestones,
gamma = cfg.lr_decay_gamma)
if cfg.n_classes > 1:
criterion = LovaszLossSoftmax()
else:
criterion = LovaszLossHinge()
for epoch in range(cfg.epochs):
net.train()
epoch_loss = 0
with tqdm(total=n_train, desc=f'Epoch {epoch + 1}/{cfg.epochs}', unit='img') as pbar:
for batch in train_loader:
batch_imgs = batch['image']
batch_masks = batch['mask']
assert batch_imgs.shape[1] == cfg.n_channels, \
f'Network has been defined with {cfg.n_channels} input channels, ' \
f'but loaded images have {batch_imgs.shape[1]} channels. Please check that ' \
'the images are loaded correctly.'
batch_imgs = batch_imgs.to(device=device, dtype=torch.float32)
mask_type = torch.float32 if cfg.n_classes == 1 else torch.long
batch_masks = batch_masks.to(device=device, dtype=mask_type)
inference_masks = net(batch_imgs)
if cfg.n_classes == 1:
inferences = inference_masks.squeeze(1)
masks = batch_masks.squeeze(1)
else:
inferences = inference_masks
masks = batch_masks
if cfg.deepsupervision:
loss = 0
for inference_mask in inferences:
loss += criterion(inference_mask, masks)
loss /= len(inferences)
else:
loss = criterion(inferences, masks)
epoch_loss += loss.item()
writer.add_scalar('Loss/train', loss.item(), global_step)
writer.add_scalar('model/lr', optimizer.param_groups[0]['lr'], global_step)
pbar.set_postfix(**{'loss (batch)': loss.item()})
optimizer.zero_grad()
loss.backward()
optimizer.step()
scheduler.step()
pbar.update(batch_imgs.shape[0])
global_step += 1
if global_step % (len(dataset) // (10 * cfg.batch_size)) == 0:
val_score = eval_net(net, val_loader, device, n_val, cfg)
if cfg.n_classes > 1:
logging.info('Validation cross entropy: {}'.format(val_score))
writer.add_scalar('CrossEntropy/test', val_score, global_step)
else:
logging.info('Validation Dice Coeff: {}'.format(val_score))
writer.add_scalar('Dice/test', val_score, global_step)
writer.add_images('images', batch_imgs, global_step)
if cfg.deepsupervision:
inference_masks = inference_masks[-1]
if cfg.n_classes == 1:
# writer.add_images('masks/true', batch_masks, global_step)
inference_mask = torch.sigmoid(inference_masks) > cfg.out_threshold
writer.add_images('masks/inference',
inference_mask,
global_step)
else:
# writer.add_images('masks/true', batch_masks, global_step)
ids = inference_masks.shape[1] # N x C x H x W
inference_masks = torch.chunk(inference_masks, ids, dim=1)
for idx in range(0, len(inference_masks)):
inference_mask = torch.sigmoid(inference_masks[idx]) > cfg.out_threshold
writer.add_images('masks/inference_'+str(idx),
inference_mask,
global_step)
if cfg.save_cp:
try:
os.mkdir(cfg.checkpoints_dir)
logging.info('Created checkpoint directory')
except OSError:
pass
ckpt_name = 'epoch_' + str(epoch + 1) + '.pth'
torch.save(net.state_dict(),
osp.join(cfg.checkpoints_dir, ckpt_name))
logging.info(f'Checkpoint {epoch + 1} saved !')
writer.close()
def eval_net(net, loader, device, n_val, cfg):
"""
Evaluation without the densecrf with the dice coefficient
"""
net.eval()
tot = 0
with tqdm(total=n_val, desc='Validation round', unit='img', leave=False) as pbar:
for batch in loader:
imgs = batch['image']
true_masks = batch['mask']
imgs = imgs.to(device=device, dtype=torch.float32)
mask_type = torch.float32 if cfg.n_classes == 1 else torch.long
true_masks = true_masks.to(device=device, dtype=mask_type)
# compute loss
if cfg.deepsupervision:
masks_preds = net(imgs)
loss = 0
for masks_pred in masks_preds:
tot_cross_entropy = 0
for true_mask, pred in zip(true_masks, masks_pred):
pred = (pred > cfg.out_threshold).float()
if cfg.n_classes > 1:
sub_cross_entropy = F.cross_entropy(pred.unsqueeze(dim=0), true_mask.unsqueeze(dim=0).squeeze(1)).item()
else:
sub_cross_entropy = dice_coeff(pred, true_mask.squeeze(dim=1)).item()
tot_cross_entropy += sub_cross_entropy
tot_cross_entropy = tot_cross_entropy / len(masks_preds)
tot += tot_cross_entropy
else:
masks_pred = net(imgs)
for true_mask, pred in zip(true_masks, masks_pred):
pred = (pred > cfg.out_threshold).float()
if cfg.n_classes > 1:
tot += F.cross_entropy(pred.unsqueeze(dim=0), true_mask.unsqueeze(dim=0).squeeze(1)).item()
else:
tot += dice_coeff(pred, true_mask.squeeze(dim=1)).item()
pbar.update(imgs.shape[0])
return tot / n_val
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s')
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# device = torch.device('cpu')
logging.info(f'Using device {device}')
net = eval(cfg.model)(cfg)
logging.info(f'Network:\n'
f'\t{cfg.model} model\n'
f'\t{cfg.n_channels} input channels\n'
f'\t{cfg.n_classes} output channels (classes)\n'
f'\t{"Bilinear" if cfg.bilinear else "Dilated conv"} upscaling')
if cfg.load:
net.load_state_dict(
torch.load(cfg.load, map_location=device)
)
logging.info(f'Model loaded from {cfg.load}')
net.to(device=device)
# faster convolutions, but more memory
# cudnn.benchmark = True
try:
train_net(net=net, cfg=cfg)
except KeyboardInterrupt:
torch.save(net.state_dict(), 'INTERRUPTED.pth')
logging.info('Saved interrupt')
try:
sys.exit(0)
except SystemExit:
os._exit(0)