-
Notifications
You must be signed in to change notification settings - Fork 2
/
step3_1_continous_training.py
208 lines (172 loc) · 8.46 KB
/
step3_1_continous_training.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
import os
import numpy as np
import torch
from torch.utils.data import DataLoader
import torch.optim as optim
import torch.nn as nn
from CT_dataset import *
from UNet3D import *
from losses_and_metrics import *
import utils
import pandas as pd
if __name__ == '__main__':
torch.cuda.set_device(utils.get_avail_gpu()) # assign which gpu will be used (only linux works)
use_visdom = True
train_list = './train_list.csv'
val_list = './val_list.csv'
previous_check_point_path = './models'
previous_check_point_name = 'latest_checkpoint.tar'
model_path = './models'
model_name = 'unet3d_cont_test'
checkpoint_name = 'latest_checkpoint_cont.tar'
num_classes = 3
num_channels = 1
num_epochs = 100
num_workers = 6
train_batch_size = 6
val_batch_size = 1
num_batches_to_print = 200
if use_visdom:
# set plotter
global plotter
plotter = utils.VisdomLinePlotter(env_name=model_name)
# mkdir 'models'
if not os.path.exists(model_path):
os.mkdir(model_path)
# set dataset
training_dataset = CT_Dataset(train_list)
val_dataset = CT_Dataset(val_list)
train_loader = DataLoader(dataset=training_dataset,
batch_size=train_batch_size,
shuffle=True,
num_workers=num_workers)
val_loader = DataLoader(dataset=val_dataset,
batch_size=val_batch_size,
shuffle=False,
num_workers=num_workers)
# set model
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = UNet3D(in_channels=num_channels, out_channels=num_classes)
model = model.to(device, dtype=torch.float)
opt = optim.Adam(model.parameters(), lr=0.0001, amsgrad=True)
# re-load
checkpoint = torch.load(os.path.join(previous_check_point_path, previous_check_point_name), map_location='cpu')
model.load_state_dict(checkpoint['model_state_dict'])
opt.load_state_dict(checkpoint['optimizer_state_dict'])
epoch_init = checkpoint['epoch']
losses = checkpoint['losses']
mdsc = checkpoint['mdsc']
val_losses = checkpoint['val_losses']
val_mdsc = checkpoint['val_mdsc']
del checkpoint
if use_visdom:
# plot previous data
for i_epoch in range(len(losses)):
plotter.plot('loss', 'train', 'Loss', i_epoch+1, losses[i_epoch])
plotter.plot('DSC', 'train', 'DSC', i_epoch+1, mdsc[i_epoch])
plotter.plot('loss', 'val', 'Loss', i_epoch+1, val_losses[i_epoch])
plotter.plot('DSC', 'val', 'DSC', i_epoch+1, val_mdsc[i_epoch])
best_val_dsc = max(val_mdsc)
#cudnn
torch.backends.cudnn.benchmark = True
torch.backends.cudnn.enabled = True
print('Continuous Training...')
class_weights = torch.Tensor([0.05, 1.0, 2.0]).to(device, dtype=torch.float)
for epoch in range(epoch_init, num_epochs):
# training
model.train()
running_loss = 0.0
running_dsc = 0.0
loss_epoch = 0.0
dsc_epoch = 0.0
for i_batch, batched_sample in enumerate(train_loader):
# send mini-batch to device
inputs, labels = batched_sample['image'].to(device, dtype=torch.float), batched_sample['label'].to(device, dtype=torch.long)
one_hot_labels = nn.functional.one_hot(labels[:, 0, :, :, :], num_classes=num_classes)
one_hot_labels = one_hot_labels.permute(0, 4, 1, 2, 3)
# zero the parameter gradients
opt.zero_grad()
# forward + backward + optimize
outputs = model(inputs)
loss = Generalized_Dice_Loss(outputs, one_hot_labels, class_weights)
dsc = weighting_DSC(outputs, one_hot_labels, class_weights)
loss.backward()
opt.step()
# print statistics
running_loss += loss.item()
running_dsc += dsc.item()
loss_epoch += loss.item()
dsc_epoch += dsc.item()
if i_batch % num_batches_to_print == num_batches_to_print-1: # print every N mini-batches
print('[Epoch: {0}/{1}, Batch: {2}/{3}] loss: {4}, dsc: {5}'.format(epoch+1, num_epochs, i_batch+1, len(train_loader), running_loss/num_batches_to_print, running_dsc/num_batches_to_print))
if use_visdom:
plotter.plot('loss', 'train', 'Loss', epoch+(i_batch+1)/len(train_loader), running_loss/num_batches_to_print)
plotter.plot('DSC', 'train', 'DSC', epoch+(i_batch+1)/len(train_loader), running_dsc/num_batches_to_print)
running_loss = 0.0
running_dsc = 0.0
# record losses and dsc
losses.append(loss_epoch/len(train_loader))
mdsc.append(dsc_epoch/len(train_loader))
#reset
loss_epoch = 0.0
dsc_epoch = 0.0
# validation
model.eval()
with torch.no_grad():
running_val_loss = 0.0
running_val_dsc = 0.0
val_loss_epoch = 0.0
val_dsc_epoch = 0.0
for i_batch, batched_val_sample in enumerate(val_loader):
# send mini-batch to device
inputs, labels = batched_val_sample['image'].to(device, dtype=torch.float), batched_val_sample['label'].to(device, dtype=torch.long)
one_hot_labels = nn.functional.one_hot(labels[:, 0, :, :, :], num_classes=num_classes)
one_hot_labels = one_hot_labels.permute(0, 4, 1, 2, 3)
outputs = model(inputs)
loss = Generalized_Dice_Loss(outputs, one_hot_labels, class_weights)
dsc = weighting_DSC(outputs, one_hot_labels, class_weights)
running_val_loss += loss.item()
running_val_dsc += dsc.item()
val_loss_epoch += loss.item()
val_dsc_epoch += dsc.item()
if i_batch % num_batches_to_print == num_batches_to_print-1: # print every N mini-batches
print('[Epoch: {0}/{1}, Val batch: {2}/{3}] val_loss: {4}, val_dsc: {5}'.format(epoch+1, num_epochs, i_batch+1, len(val_loader), running_val_loss/num_batches_to_print, running_val_dsc/num_batches_to_print))
running_val_loss = 0.0
running_val_dsc = 0.0
# record losses and dsc
val_losses.append(val_loss_epoch/len(val_loader))
val_mdsc.append(val_dsc_epoch/len(val_loader))
# reset
val_loss_epoch = 0.0
val_dsc_epoch = 0.0
# output current status
print('*****\nEpoch: {0}/{1}, loss: {2}, dsc: {3}\n val_loss: {4}, val_dsc: {5}\n*****'.format(epoch+1, num_epochs, losses[-1], mdsc[-1], val_losses[-1], val_mdsc[-1]))
if use_visdom:
plotter.plot('loss', 'train', 'Loss', epoch+1, losses[-1])
plotter.plot('DSC', 'train', 'DSC', epoch+1, mdsc[-1])
plotter.plot('loss', 'val', 'Loss', epoch+1, val_losses[-1])
plotter.plot('DSC', 'val', 'DSC', epoch+1, val_mdsc[-1])
# save the checkpoint
torch.save({'epoch': epoch,
'model_state_dict': model.state_dict(),
'optimizer_state_dict': opt.state_dict(),
'losses': losses,
'mdsc': mdsc,
'val_losses': val_losses,
'val_mdsc': val_mdsc},
os.path.join(model_path, checkpoint_name))
# save the best model
if best_val_dsc < val_mdsc[-1]:
best_val_dsc = val_mdsc[-1]
torch.save({'epoch': epoch,
'model_state_dict': model.state_dict(),
'optimizer_state_dict': opt.state_dict(),
'losses': losses,
'mdsc': mdsc,
'val_losses': val_losses,
'val_mdsc': val_mdsc},
os.path.join(model_path, '{}_best.tar'.format(model_name)))
# save all losses and mdsc data
pd_dict = {'loss': losses, 'DSC': mdsc, 'val_loss': val_losses, 'val_DSC': val_mdsc}
stat = pd.DataFrame(pd_dict)
stat.to_csv('losses_dsc_vs_epoch.csv')