-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCollagenSegTrain.py
More file actions
366 lines (279 loc) · 13.4 KB
/
Copy pathCollagenSegTrain.py
File metadata and controls
366 lines (279 loc) · 13.4 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
# -*- coding: utf-8 -*-
"""
Created on Fri Jul 23 09:37:56 2021
@author: spborder
DGCS Training Loop
from: https://github.com/qubvel/segmentation_models.pytorch/blob/master/examples/cars%20segmentation%20(camvid).ipynb
A lot of different segmentation networks are available from the 'segmentation_models_pytorch' module
"""
import torch
import numpy as np
import segmentation_models_pytorch as smp
from torch.utils.data import DataLoader
import matplotlib.pyplot as plt
from PIL import Image
import pandas as pd
from tqdm import tqdm
import sys
import os
from math import pi, floor
from CollagenSegUtils import visualize_continuous
class MultiModalModel(torch.nn.Module):
def __init__(self,
in_channels,
active,
n_classes,
duet_decay = False,
phase = 'train'):
super().__init__()
self.in_channels = in_channels
self.active = active
self.n_classes = n_classes
self.duet_decay = duet_decay
self.phase = phase
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
encoder = 'resnet34'
encoder_weights = 'imagenet'
if self.duet_decay:
self.decay_count = -1
self.decay_sigma = 500
self.decay_stop = 4000
if self.active=='sigmoid':
self.final_active = torch.nn.Sigmoid()
elif self.active =='softmax':
self.final_active = torch.nn.Softmax(dim=1)
elif self.active == 'linear':
self.active = None
self.final_active = torch.nn.Identity()
else:
self.active = None
self.final_active = torch.nn.ReLU()
self.model_b = smp.UnetPlusPlus(
encoder_name = encoder,
encoder_weights = encoder_weights,
in_channels = int(self.in_channels/2),
classes = self.n_classes,
activation = self.active
)
self.model_d = smp.UnetPlusPlus(
encoder_name = encoder,
encoder_weights = encoder_weights,
in_channels = int(self.in_channels/2),
classes = self.n_classes,
activation = self.active
)
self.combine_layers = torch.nn.Sequential(
torch.nn.LazyConv2d(64,kernel_size=1),
torch.nn.Dropout(p=0.1),
torch.nn.ReLU(inplace=True),
torch.nn.Conv2d(64,self.n_classes,kernel_size=1)
)
def update_decay(self):
"""
Decreasing influence of DUET side of the ensemble model on final prediction as training progresses
"""
self.decay_count += 1
if self.decay_count < self.decay_stop:
# https://en.wikipedia.org/wiki/Half-normal_distribution
# multiplied by decay sigma to get the initial value closer to 1
# since this is used both in validation and training passes
decay_iteration = floor(self.decay_count/2)
new_val = ((2**0.5)/(self.decay_sigma*(pi**0.5))) * np.exp2(-1*((decay_iteration**2)/(2*(self.decay_sigma**2))))
if self.decay_count == 0:
self.decay_scale = 1/new_val
new_val = new_val * self.decay_scale
else:
new_val = 0.0
return new_val
def forward(self,input):
b_input = input[:,0:int(self.in_channels/2),:,:]
d_input = input[:,int(self.in_channels/2):self.in_channels,:,:]
b_output = self.model_b.decoder(*self.model_b.encoder(b_input))
d_output = self.model_d.decoder(*self.model_d.encoder(d_input))
if not self.phase == 'test':
if self.duet_decay:
self.decay_val = self.update_decay()
d_output = d_output * self.decay_val
combined_output = torch.cat((b_output,d_output),dim=1)
final_prediction = self.final_active(self.combine_layers(combined_output))
return final_prediction
def Training_Loop(dataset_train, dataset_valid, train_parameters, nept_run):
model_details = train_parameters['model_details']
if not model_details['architecture'] == 'DUnet':
encoder = model_details['encoder']
encoder_weights = model_details['encoder_weights']
nept_run['encoder'] = encoder
nept_run['encoder_pre_train'] = encoder_weights
output_type = 'comparison'
active = model_details['active']
target_type = model_details['target_type']
ann_classes = model_details['ann_classes'].split(',')
output_dir = train_parameters['output_dir']
model_dir = output_dir+'/models/'
if not os.path.exists(model_dir):
os.makedirs(model_dir)
if active=='None':
active = None
in_channels = model_details['in_channels']
if target_type=='binary':
loss = smp.losses.DiceLoss(mode='binary')
#n_classes = len(ann_classes)
n_classes = 1
elif target_type=='nonbinary':
if train_parameters['loss']=='MSE':
loss = torch.nn.MSELoss(reduction='mean')
elif train_parameters['loss'] == 'L1':
loss = torch.nn.L1Loss(reduction='mean')
elif train_parameters['loss'] == 'BCE':
loss = torch.nn.BCELoss()
n_classes = 1
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(f'Is training on GPU available? : {torch.cuda.is_available()}')
print(f'Device is : {device}')
print(f'Torch Cuda version is : {torch.version.cuda}')
nept_run['Architecture'] = model_details['architecture']
nept_run['Loss'] = train_parameters['loss']
nept_run['output_type'] = output_type
nept_run['lr'] = train_parameters['lr']
if 'training_normalization' in train_parameters:
nept_run['Image Means'] = ','.join([str(i) for i in train_parameters['training_normalization']['mean'].tolist()])
nept_run['Image Stds'] = ','.join([str(i) for i in train_parameters['training_normalization']['std'].tolist()])
if model_details['architecture']=='Unet++':
model = smp.UnetPlusPlus(
encoder_name = encoder,
encoder_weights = encoder_weights,
in_channels = in_channels,
classes = n_classes,
activation = active
)
elif model_details['architecture']=='multimodal':
model = MultiModalModel(
in_channels = in_channels,
active = active,
n_classes = n_classes
)
optimizer = torch.optim.Adam([
dict(params = model.parameters(), lr = train_parameters['lr'],weight_decay = 0.0001)
])
lr_plateau = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer,patience=250,verbose=True)
# Sending model to current device ('cuda','cuda:0','cuda:1',or 'cpu')
model = model.to(device)
loss = loss.to(device)
batch_size = train_parameters['batch_size']
train_loader = DataLoader(dataset_train,batch_size=batch_size,shuffle=True)
valid_loader = DataLoader(dataset_valid,batch_size=batch_size,shuffle=True)
# Maximum number of epochs defined here as well as how many steps between model saves and example outputs
epoch_num = train_parameters['step_num']
save_step = train_parameters['save_step']
train_loss = 0
val_loss = 0
# Recording training and validation loss
train_loss_list = []
val_loss_list = []
with tqdm(total = epoch_num, position = 0, leave = True, file = sys.stdout) as pbar:
for i in range(0,epoch_num):
# Turning on dropout
model.train()
# Controlling the progress bar, printing training and validation losses
if i==1:
pbar.set_description(f'Epoch: {i}/{epoch_num}')
pbar.update(i)
elif i%5==0:
pbar.set_description(f'Epoch: {i}/{epoch_num}, Train/Val Loss: {round(train_loss,4)},{round(val_loss,4)}')
pbar.update(5)
# Clear existing gradients in optimizer
optimizer.zero_grad()
# Loading training and validation samples from dataloaders
if 'sub_categories_file' not in train_parameters:
train_imgs, train_masks, _ = next(iter(train_loader))
else:
train_imgs, train_masks, _ = next(train_loader)
# Sending to device
train_imgs = train_imgs.to(device)
train_masks = train_masks.to(device)
# Running predictions on training batch
train_preds = model(train_imgs)
# Calculating loss
train_loss = loss(train_preds,train_masks)
# Backpropagation
train_loss.backward()
train_loss = train_loss.item()
train_loss_list.append(train_loss)
if not 'current_k_fold' in train_parameters:
nept_run['training_loss'].log(train_loss)
else:
nept_run[f'training_loss_{train_parameters["current_k_fold"]}'].log(train_loss)
# Logging decay value if there is one
if 'decay_val' in vars(model):
nept_run['DUET_decay'].log(model.decay_val)
# Updating optimizer
optimizer.step()
# Validation (don't want it to influence gradients in network)
with torch.no_grad():
# This turns off any dropout in the network
model.eval()
val_imgs, val_masks, _ = next(iter(valid_loader))
val_imgs = val_imgs.to(device)
val_masks = val_masks.to(device)
# Predicting on the validation images
val_preds = model(val_imgs)
# Finding validation loss
val_loss = loss(val_preds,val_masks)
val_loss = val_loss.item()
val_loss_list.append(val_loss)
if not 'current_k_fold' in train_parameters:
nept_run['validation_loss'].log(val_loss)
else:
nept_run[f'validation_loss_{train_parameters["current_k_fold"]}'].log(val_loss)
# Stepping the learning rate plateau with the current validation loss
#scheduler.step()
lr_plateau.step(val_loss)
# Saving model if current i is a multiple of "save_step"
# Also generating example output segmentation and uploading that to Neptune
if i%save_step == 0:
torch.save(model.state_dict(),model_dir+f'Collagen_Seg_Model_Latest.pth')
if batch_size==1:
current_img = val_imgs.cpu().numpy()
current_gt = val_masks.cpu().numpy()
current_pred = val_preds.cpu().numpy()
else:
current_img = val_imgs[0].cpu().numpy()
current_gt = val_masks[0].cpu().numpy()
current_pred = val_preds[0].cpu().numpy()
"""
if target_type=='binary':
current_pred = current_pred.round()
"""
# Un-normalizing current image
norm_means = train_parameters['training_normalization']['mean'].tolist()
norm_stds = train_parameters['training_normalization']['std'].tolist()
for idx, (m,s) in enumerate(zip(norm_means,norm_stds)):
current_img[idx,:,:] += m
current_img[idx,:,:] *= s
if type(in_channels)==int:
if in_channels == 6:
current_img = np.concatenate((current_img[0:3,:,:],current_img[2:5,:,:]),axis=-2)
elif in_channels==4:
current_img = np.concatenate((np.stack((current_img[0,:,:],)*3,axis=1),current_img[0:3,:,:]),axis=-2)
elif in_channels==2:
current_img = np.concatenate((current_img[0,:,:],current_img[1,:,:]),axis=-2)
elif type(in_channels)==list:
if sum(in_channels)==6:
current_img = np.concatenate((current_img[0:3,:,:],current_img[2:5,:,:]),axis=-2)
elif sum(in_channels)==2:
current_img = np.concatenate((current_img[0,:,:][None,:,:],current_img[1,:,:][None,:,:]),axis=-2)
img_dict = {'Image':np.uint8(255*current_img), 'Pred_Mask':np.uint8(255*current_pred),'Ground_Truth':np.uint8(255*current_gt)}
fig = visualize_continuous(img_dict,output_type)
# Different process for saving comparison figures vs. only predictions
if output_type == 'comparison':
fig.savefig(output_dir+f'/Training_Epoch_{i}_Example.png')
nept_run[f'Example_Output_{i}'].upload(output_dir+f'/Training_Epoch_{i}_Example.png')
elif output_type == 'prediction':
im = Image.fromarray(fig.astype(np.uint8))
im.save(output_dir+f'/Training_Epoch_{i}_Example.tif')
nept_run[f'Example_Output_{i}'].upload(output_dir+f'/Training_Epoch_{i}_Example.tif')
if not i%save_step==0:
torch.save(model.state_dict(),model_dir+f'Collagen_Seg_Model_Latest.pth')
loss_df = pd.DataFrame(data = {'TrainingLoss':train_loss_list,'ValidationLoss':val_loss_list})
loss_df.to_csv(output_dir+'/Training_Validation_Loss.csv')
return model_dir+f'Collagen_Seg_Model_Latest.pth'