-
Notifications
You must be signed in to change notification settings - Fork 6
/
train_domino.py
406 lines (349 loc) · 13.8 KB
/
train_domino.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
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# coding=utf-8
#load packages:
#standard packages -
import os
import shutil
import tempfile
import matplotlib.pyplot as plt
import numpy as np
from tqdm import tqdm
import torch
import torch.nn as nn
import time
import argparse
import math
import pandas as pd
import torch.nn.functional as F
#load monai functions -
from monai.losses import DiceCELoss
from monai.inferers import sliding_window_inference
from monai.transforms import (
AsDiscrete,
AddChanneld,
Compose,
CropForegroundd,
LoadImaged,
Orientationd,
RandFlipd,
RandCropByPosNegLabeld,
RandShiftIntensityd,
ScaleIntensityRanged,
Spacingd,
RandRotate90d,
ToTensord,
SpatialPadd,
RandGaussianNoised
)
from monai.config import print_config
from monai.metrics import DiceMetric
from monai.networks.nets import UNETR
from monai.optimizers import WarmupCosineSchedule
#from monai.networks.nets import UNet
from monai.data import (
DataLoader,
load_decathlon_datalist,
decollate_batch,
Dataset,
pad_list_data_collate,
)
#-----------------------------------
#set up starting conditions:
start_time = time.time()
print_config()
# our CLI parser
parser = argparse.ArgumentParser()
parser.add_argument("--data_dir", type=str, default="/mnt/training_pairs_v5/", help="directory the dataset is in")
parser.add_argument("--batch_size_train", type=int, default=1, help="batch size training data")
parser.add_argument("--batch_size_validation", type=int, default=1, help="batch size validation data")
parser.add_argument("--num_gpu", type=int, default=3, help="number of gpus")
parser.add_argument("--N_classes", type=int, default=12, help="number of tissues classes")
parser.add_argument("--spatial_size", type=int, default=256, help="one patch dimension")
parser.add_argument("--model_save_name", type=str, default="unetr_v5_cos", help="model save name")
parser.add_argument("--a_max_value", type=int, default=255, help="maximum image intensity")
parser.add_argument("--a_min_value", type=int, default=0, help="minimum image intensity")
parser.add_argument("--max_iteration", type=int, default=25000, help="number of iterations")
parser.add_argument("--num_samples", type=int, default=1, help="number of iterations")
parser.add_argument("--csv_matrixpenalty", type=str, default="/mnt/UNETR_matrixpenalty_v5_2d.csv", help="matrix penalty csv")
parser.add_argument("--loss_type", type=str, default="DOMINO", help="which loss to use")
args = parser.parse_args()
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
split_JSON = "dataset_1.json"
datasets = args.data_dir + split_JSON
#-----------------------------------
#special loss
matrix_vals = pd.read_csv(args.csv_matrixpenalty, header = None)#, header = 0, index_col = 0)
matrix_vals = matrix_vals.to_numpy()
ENCODINGS = 3.0 * matrix_vals
#print(ENCODINGS)
#plt.figure()
#plt.imshow(ENCODINGS)
#plt.show()
matrix_penalty = torch.from_numpy(ENCODINGS).to(device)
matrix_penalty = matrix_penalty.float()
Npixels = args.spatial_size**3
length_targets = args.batch_size_train*args.num_samples
total_batch = Npixels*length_targets
N_classes = args.N_classes
class criterion(nn.Module):
def __init__(self,weight=None, size_average=True):
super(criterion,self).__init__()
def forward(self, outputs, targets, matrix_penalty=matrix_penalty, N_classes=N_classes, Npixels=Npixels, length_targets=length_targets, total_batch=total_batch):
#currently I set them like this to do each data point rather than whole batch at once
penalty_term = torch.zeros(0).cuda()
entropy_term = torch.zeros(0).cuda()
matrix_penalty = matrix_penalty.cuda()
target_vector = torch.reshape(targets, (length_targets, Npixels)) # B * P
target_vector = F.one_hot(target_vector.to(torch.int64), N_classes) #int8 instead of int64, #B * P * N
output_vector = torch.reshape(outputs, (length_targets, N_classes, Npixels)) #B * N * P
output_vector = torch.swapaxes(output_vector, 0, 1) # N * B * P
for i in range(length_targets): #B
for j in range(len(target_vector)): #P
target_vectorr = torch.flatten(target_vector[i:i+1, j:j+1, :]).float() #1 * 1 * N = N
target_vectorr = torch.reshape(target_vectorr, (1,N_classes)) # 1 x N
output_vectorr = torch.flatten(output_vector[:, i:i+1, j:j+1]).float() # N * 1 * 1
output_vectorr = torch.reshape(output_vectorr, (N_classes,1)) # N x 1
output_vectorr = torch.exp(output_vectorr)/torch.sum(torch.exp(output_vector))
penalty = torch.mm(target_vectorr,matrix_penalty) # (1 x N) * (N x N) = 1 x N
penalty_term = torch.cat((penalty_term,torch.mm(penalty.float(),output_vectorr)),-1)
loss_diceCE = DiceCELoss(to_onehot_y=True, softmax=True)
entropy_term = loss_diceCE(outputs,targets)
beta = 1.
total_loss = entropy_term + beta*(torch.sum(penalty_term)/total_batch)
return total_loss
#------------------------------------
#data transformations:
train_transforms = Compose(
[
LoadImaged(keys=["image", "label"]),
AddChanneld(keys=["image", "label"]),
Spacingd(
keys=["image", "label"],
pixdim=(1.0, 1.0, 1.0),
mode=("bilinear", "nearest"),
),
Orientationd(keys=["image", "label"], axcodes="RAS"),
ScaleIntensityRanged(
keys=["image"],
a_min=args.a_min_value,
a_max=args.a_max_value, #my original data is in UINT8
b_min=0.0,
b_max=1.0,
clip=True,
),
#CropForegroundd(keys=["image", "label"], source_key="image"), #can crop data since taking patches that are less than full
RandCropByPosNegLabeld(
keys=["image", "label"],
label_key="label",
spatial_size=(args.spatial_size, args.spatial_size, args.spatial_size),
pos=1,
neg=1,
num_samples=args.num_samples, #this number poses a limitation on training since inputs are batch size x num_samples x spatial_size
image_key="image",
image_threshold=0,
),
RandFlipd(
keys=["image", "label"],
spatial_axis=[0],
prob=0.10,
),
RandFlipd(
keys=["image", "label"],
spatial_axis=[1],
prob=0.10,
),
RandFlipd(
keys=["image", "label"],
spatial_axis=[2],
prob=0.10,
),
RandRotate90d(
keys=["image", "label"],
prob=0.10,
max_k=3,
),
RandShiftIntensityd(
keys=["image"],
offsets=0.10,
prob=0.10,
),
RandGaussianNoised(keys = "image", prob = .50, mean = 0, std = 0.1),
ToTensord(keys=["image", "label"]),
]
)
val_transforms = Compose(
[
LoadImaged(keys=["image", "label"]),
AddChanneld(keys=["image", "label"]),
Spacingd(
keys=["image", "label"],
pixdim=(1.0, 1.0, 1.0),
mode=("bilinear", "nearest"),
),
Orientationd(keys=["image", "label"], axcodes="RAS"),
ScaleIntensityRanged(
keys=["image"], a_min=args.a_min_value, a_max=args.a_max_value, b_min=0.0, b_max=1.0, clip=True
),
#CropForegroundd(keys=["image", "label"], source_key="image"),
ToTensord(keys=["image", "label"]),
]
)
#-----------------------------------
#set up data loaders
train_files = load_decathlon_datalist(datasets, True, "training")
val_files = load_decathlon_datalist(datasets, True, "validation")
train_ds = Dataset(
data=train_files,
transform=train_transforms,
)
train_loader = DataLoader(
train_ds, batch_size=args.batch_size_train, shuffle=True, num_workers=4, pin_memory=True, collate_fn=pad_list_data_collate,
)
val_ds = Dataset(
data=val_files, transform=val_transforms,
)
val_loader = DataLoader(
val_ds, batch_size=args.batch_size_validation, shuffle=False, num_workers=4, pin_memory=True, collate_fn=pad_list_data_collate,
)
#-----------------------------------
#set up gpu device and unetr model
#os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
#device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = nn.DataParallel(
UNETR(
in_channels=1,
out_channels=args.N_classes, #12 for all tissues
img_size=(args.spatial_size, args.spatial_size, args.spatial_size),
feature_size=16,
hidden_size=768,
mlp_dim=3072,
num_heads=12,
pos_embed="perceptron",
norm_name="instance",
res_block=True,
dropout_rate=0.0,
), device_ids=[i for i in range(args.num_gpu)]).cuda()
#model = model.to(device)
if args.loss_type=="DOMINO":
loss_function = criterion()
else:
loss_function = DiceCELoss(to_onehot_y=True, softmax=True)
torch.backends.cudnn.benchmark = True
optimizer = torch.optim.AdamW(model.parameters(), lr=1e-4, weight_decay=1e-5)
#-----------------------------------
def validation(epoch_iterator_val):
model.eval()
dice_vals = list()
with torch.no_grad():
for step, batch in enumerate(epoch_iterator_val):
val_inputs, val_labels = (batch["image"].cuda(), batch["label"].cuda())
val_outputs = sliding_window_inference(val_inputs, (args.spatial_size, args.spatial_size, args.spatial_size), 4, model)
val_labels_list = decollate_batch(val_labels)
val_labels_convert = [
post_label(val_label_tensor) for val_label_tensor in val_labels_list
]
val_outputs_list = decollate_batch(val_outputs)
val_output_convert = [
post_pred(val_pred_tensor) for val_pred_tensor in val_outputs_list
]
dice_metric(y_pred=val_output_convert, y=val_labels_convert)
dice = dice_metric.aggregate().item()
dice_vals.append(dice)
epoch_iterator_val.set_description(
"Validate (%d / %d Steps) (dice=%2.5f)" % (global_step, 10.0, dice)
)
dice_metric.reset()
mean_dice_val = np.mean(dice_vals)
return mean_dice_val
#-----------------------------------
def train(global_step, train_loader, dice_val_best, global_step_best):
model.train()
epoch_loss = 0
step = 0
epoch_iterator = tqdm(
train_loader, desc="Training (X / X Steps) (loss=X.X)", dynamic_ncols=True
)
for step, batch in enumerate(epoch_iterator):
step += 1
x, y = (batch["image"].cuda(), batch["label"].cuda())
logit_map = model(x)
loss = loss_function(logit_map, y)
loss.backward()
epoch_loss += loss.item()
optimizer.step()
optimizer.zero_grad()
epoch_iterator.set_description(
"Training (%d / %d Steps) (loss=%2.5f)" % (global_step, max_iterations, loss)
)
if (
global_step % eval_num == 0 and global_step != 0
) or global_step == max_iterations:
epoch_iterator_val = tqdm(
val_loader, desc="Validate (X / X Steps) (dice=X.X)", dynamic_ncols=True
)
dice_val = validation(epoch_iterator_val)
epoch_loss /= step
epoch_loss_values.append(epoch_loss)
metric_values.append(dice_val)
if dice_val > dice_val_best:
dice_val_best = dice_val
global_step_best = global_step
torch.save(
model.state_dict(), os.path.join(args.data_dir, args.model_save_name + ".pth")
)
print(
"Model Was Saved ! Current Best Avg. Dice: {} Current Avg. Dice: {}".format(
dice_val_best, dice_val
)
)
else:
print(
"Model Was Not Saved ! Current Best Avg. Dice: {} Current Avg. Dice: {}".format(
dice_val_best, dice_val
)
)
global_step += 1
return global_step, dice_val_best, global_step_best
#-----------------------------------
max_iterations = args.max_iteration #25000
eval_num = math.ceil(args.max_iteration * 0.02)#500
#WarmupCosineSchedule(optimizer, 10, 500, cycles = 0.5)
post_label = AsDiscrete(to_onehot=True, num_classes=args.N_classes)
post_pred = AsDiscrete(argmax=True, to_onehot=True, num_classes=args.N_classes)
dice_metric = DiceMetric(include_background=True, reduction="mean", get_not_nans=False)
global_step = 0
dice_val_best = 0.0
global_step_best = 0
epoch_loss_values = []
metric_values = []
while global_step < max_iterations:
global_step, dice_val_best, global_step_best = train(
global_step, train_loader, dice_val_best, global_step_best
)
model.load_state_dict(torch.load(os.path.join(args.data_dir, args.model_save_name + ".pth")))
#-----------------------------------
#training loss and validation evaluation
plt.figure("train", (12, 6))
plt.subplot(1, 2, 1)
plt.title("Iteration Average Loss")
x = [eval_num * (i + 1) for i in range(len(epoch_loss_values))]
y = epoch_loss_values
plt.xlabel("Iteration")
plt.plot(x, y)
dict = {'Iteration': x, 'Loss': y}
df = pd.DataFrame(dict)
df.to_csv(os.path.join(args.data_dir,args.model_save_name + '_Loss.csv'))
plt.subplot(1, 2, 2)
plt.title("Val Mean Dice")
x = [eval_num * (i + 1) for i in range(len(metric_values))]
y = metric_values
plt.xlabel("Iteration")
plt.plot(x, y)
#plt.show()
plt.savefig(os.path.join(args.data_dir, args.model_save_name + "_training_metrics.pdf"))
dict = {'Iteration': x, 'Dice': y}
df = pd.DataFrame(dict)
df.to_csv(os.path.join(args.data_dir,args.model_save_name + '_ValidationDice.csv'))
#------------------------------------
#time since start
print("--- %s seconds ---" % (time.time() - start_time))