-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
460 lines (421 loc) · 18.6 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
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
import os
import sys
import yaml
import argparse
import logging
import math
import importlib
import datetime
import random
import munch
import time
import torch
import torch.optim as optim
import warnings
import shutil
import subprocess
import matplotlib.pyplot as plt
import tempfile
import glob
from sklearn.model_selection import KFold
from torchinfo import summary
torch.autograd.set_detect_anomaly(True)
from monai.utils import first, set_determinism
from monai.transforms import (
AsDiscrete,
AsDiscreted,
EnsureChannelFirstd,
Compose,
CropForegroundd,
LoadImaged,
Orientationd,
RandCropByPosNegLabeld,
SaveImaged,
ScaleIntensityRanged,
Spacingd,
Invertd,
)
from monai.handlers.utils import from_engine
from monai.metrics import DiceMetric
from monai.losses import DiceLoss
from monai.inferers import sliding_window_inference
from monai.data import CacheDataset, DataLoader, Dataset, decollate_batch
from monai.apps import download_and_extract
from models.layers import ConcreteDropout
from models.layers import LP_BNN_Conv3d, LP_BNN_ConvTranspose3d
from models.layers import Rank1_BNN_Conv3d, Rank1_BNN_ConvTranspose3d
# from monai.config import print_config
# print_config()
def get_file_sets(args):
### Get Data
# Setup data directory
directory = args.data_dir
root_dir = tempfile.mkdtemp() if directory is None else directory
# Download and extract dataset
resource = "https://msd-for-monai.s3-us-west-2.amazonaws.com/"+args.dataset+".tar"
md5 = "410d4a301da4e5b2f6f86ec3ddba524e"
compressed_file = os.path.join(root_dir, args.dataset+".tar")
data_dir = os.path.join(root_dir, args.dataset)
if not os.path.exists(data_dir):
download_and_extract(resource, compressed_file, root_dir, md5)
# Set data files
train_images = sorted(glob.glob(os.path.join(data_dir, "imagesTr", "*.nii.gz")))
train_labels = sorted(glob.glob(os.path.join(data_dir, "labelsTr", "*.nii.gz")))
data_dicts = [{"image": image_name, "label": label_name} for image_name, label_name in zip(train_images, train_labels)]
# Separate out test files using fold
test_size = int(.2*len(data_dicts))
val_size = int(.1*len(data_dicts))
test_files = data_dicts[args.fold*test_size:((args.fold+1)*test_size)]
train_val_files = [data_dict for data_dict in data_dicts if data_dict not in test_files]
# Create validation split
val_fold = min(args.fold,3)
val_files = train_val_files[val_fold*val_size:((val_fold+1)*val_size)]
train_files = [data_dict for data_dict in train_val_files if data_dict not in val_files]
return train_files, val_files, test_files
def train(args, log_dir):
start_time = time.time()
# Start logging
for handler in logging.root.handlers[:]:
logging.root.removeHandler(handler)
logging.basicConfig(level=logging.INFO, handlers=[logging.FileHandler(os.path.join(log_dir, 'train.log')),
logging.StreamHandler(sys.stdout)])
logging.info(str(args))
# Create model, optimizer, and loss
device = 'cuda:0' # torch.device(args.device) TODO
model_module = importlib.import_module('.%s' % args.model_name, 'models')
model = model_module.Model(args).to(device)
loss_function = DiceLoss(to_onehot_y=True, softmax=True)
if "CE_loss" in args:
loss_function = torch.nn.CrossEntropyLoss()
learning_rate = args.learning_rate if 'learning_rate' in args else 1e-4
if 'weight_decay' in args:
weight_decay=args.weight_decay
else:
weight_decay=0
optimizer = torch.optim.Adam(model.parameters(), learning_rate, weight_decay=weight_decay)
dice_metric = DiceMetric(include_background=False, reduction="mean")
if args.load_model:
logging.info(" Loading %s model's weights.")
model_dict = model.state_dict()
intial_model_dict = torch.load(args.load_model, map_location=device)
pretrained_dict = {k: v for k, v in intial_model_dict.items() if k in model_dict}
not_pretrained_dict = {k: v for k, v in model_dict.items() if k not in pretrained_dict}
model_dict.update(pretrained_dict)
model.load_state_dict(model_dict)
logging.info(f'Initializing weights for: {pretrained_dict.keys()}. ')
logging.info(f'No pretrained initialization for: {not_pretrained_dict.keys()}. ')
# summary(model, input_size=(4, 1, 96, 96, 96))
# Get data
train_files, val_files, test_files = get_file_sets(args)
# Get transforms
train_transforms, val_transforms, val_org_transforms, post_transforms = get_transforms(args)
# # Check transforms in dataloader
# check_ds = Dataset(data=val_files, transform=val_transforms)
# check_loader = DataLoader(check_ds, batch_size=1)
# check_data = first(check_loader)
# image, label = (check_data["image"][0][0], check_data["label"][0][0])
# print(f"image shape: {image.shape}, label shape: {label.shape}")
# # Plot check
# plt.figure("check", (12, 6))
# plt.subplot(1, 2, 1)
# plt.title("image")
# plt.imshow(image[:, :, 80], cmap="gray")
# plt.subplot(1, 2, 2)
# plt.title("label")
# plt.imshow(label[:, :, 80])
# plt.show()
# Create data loaders
train_ds = CacheDataset(data=train_files, transform=train_transforms, cache_rate=1.0, num_workers=args.num_workers)
# train_ds = Dataset(data=train_files, transform=train_transforms)
# use batch_size=2 to load images and use RandCropByPosNegLabeld
# to generate 2 x 4 images for network training
train_loader = DataLoader(train_ds, batch_size=args.batch_size, shuffle=True, num_workers=args.num_workers)
val_ds = CacheDataset(data=val_files, transform=val_transforms, cache_rate=1.0, num_workers=args.num_workers)
# val_ds = Dataset(data=val_files, transform=val_transforms)
val_loader = DataLoader(val_ds, batch_size=1, num_workers=args.num_workers)
logging.info('Length of train dataset:%d', len(train_ds))
logging.info('Length of val dataset:%d', len(val_ds))
# Set deterministic training for reproducibility
set_determinism(seed=args.seed)
# Train
max_epochs = args.max_epochs
val_interval = args.val_interval
best_metric = -1
best_metric_epoch = -1
epoch_loss_values = []
metric_values = []
post_pred = Compose([AsDiscrete(argmax=True, to_onehot=2)])
post_label = Compose([AsDiscrete(to_onehot=2)])
early_stop_count = 0
for epoch in range(max_epochs):
print("-" * 10)
print(f"epoch {epoch + 1}/{max_epochs}")
model.train()
epoch_loss = 0
step = 0
for batch_data in train_loader:
step += 1
inputs, labels = (
batch_data["image"].to(device),
batch_data["label"].to(device),
)
optimizer.zero_grad()
outputs = model(inputs)
reg = torch.zeros(1).to(device)
for module in filter(lambda x: isinstance(x, ConcreteDropout), model.modules()):
reg += module.regularization
for module in filter(lambda x: isinstance(x, LP_BNN_ConvTranspose3d), model.modules()):
reg += module.latent_loss
for module in filter(lambda x: isinstance(x, LP_BNN_Conv3d), model.modules()):
reg += module.latent_loss
for module in filter(lambda x: isinstance(x, Rank1_BNN_ConvTranspose3d), model.modules()):
reg += module.kld_loss
for module in filter(lambda x: isinstance(x, Rank1_BNN_Conv3d), model.modules()):
reg += module.kld_loss
if "CE_loss" in args:
loss = loss_function(outputs, labels.squeeze().long())
else:
loss = loss_function(outputs, labels) + reg
loss.backward()
optimizer.step()
epoch_loss += loss.item()
logging.info(f"{step}/{len(train_ds) // train_loader.batch_size}, " f"train_loss: {loss.item():.4f}")
epoch_loss /= step
epoch_loss_values.append(epoch_loss)
logging.info(f"epoch {epoch + 1} average loss: {epoch_loss:.4f}")
if (epoch + 1) % val_interval == 0:
model.eval()
with torch.no_grad():
for val_data in val_loader:
val_inputs, val_labels = (
val_data["image"].to(device),
val_data["label"].to(device),
)
roi_size = (160, 160, 160)
sw_batch_size = 4
val_outputs = sliding_window_inference(val_inputs, roi_size, sw_batch_size, model)
val_outputs = [post_pred(i) for i in decollate_batch(val_outputs)]
val_labels = [post_label(i) for i in decollate_batch(val_labels)]
# compute metric for current iteration
dice_metric(y_pred=val_outputs, y=val_labels)
# aggregate the final mean dice result
metric = dice_metric.aggregate().item()
# reset the status for next validation round
dice_metric.reset()
metric_values.append(metric)
if metric > best_metric:
best_metric = metric
best_metric_epoch = epoch + 1
torch.save(model.state_dict(), os.path.join(log_dir, "best_model.pth"))
logging.info("saved new best metric model")
early_stop_count = 0
else:
if epoch > 50: # Small lag for lp-bnn
early_stop_count += 1
logging.info(
f"current epoch: {epoch + 1} current mean dice: {metric:.4f}"
f"\nbest mean dice: {best_metric:.4f} "
f"at epoch: {best_metric_epoch}"
f"\nearly stop count: {early_stop_count}"
)
if early_stop_count > args.early_stop_patience:
print("Early stopping epoch:", epoch)
break
logging.info(f"train completed, best_metric: {best_metric:.4f} " f"at epoch: {best_metric_epoch}")
logging.info(f"Time: {time.time()-start_time:.4f} ")
# Plot the loss and metric
plot_loss_and_metric(epoch_loss_values, metric_values, log_dir, val_interval)
# Plot val examples
plot_examples(model, log_dir, val_loader, device)
# Eval on original image spacings
val_org_ds = Dataset(data=val_files, transform=val_org_transforms)
val_org_loader = DataLoader(val_org_ds, batch_size=1, num_workers=args.num_workers)
model.load_state_dict(torch.load(os.path.join(log_dir, "best_model.pth")))
model.eval()
with torch.no_grad():
for val_data in val_org_loader:
torch.cuda.empty_cache()
val_inputs = val_data["image"].to(device)
roi_size = (160, 160, 160)
sw_batch_size = 4
val_data["pred"] = sliding_window_inference(val_inputs, roi_size, sw_batch_size, model)
val_data = [post_transforms(i) for i in decollate_batch(val_data)]
val_outputs, val_labels = from_engine(["pred", "label"])(val_data)
# compute metric for current iteration
dice_metric(y_pred=val_outputs, y=val_labels)
# aggregate the final mean dice result
metric_org = dice_metric.aggregate().item()
# reset the status for next validation round
dice_metric.reset()
logging.info(f"Metric on original image spacing: {metric_org:.4f} ")
def plot_loss_and_metric(epoch_loss_values, metric_values, log_dir, val_interval):
plt.figure("train", (12, 6))
plt.subplot(1, 2, 1)
plt.title("Epoch Average Loss")
x = [i + 1 for i in range(len(epoch_loss_values))]
y = epoch_loss_values
plt.xlabel("epoch")
plt.plot(x, y)
plt.subplot(1, 2, 2)
plt.title("Val Mean Dice")
x = [val_interval * (i + 1) for i in range(len(metric_values))]
y = metric_values
plt.xlabel("epoch")
plt.plot(x, y)
plt.savefig(log_dir + '/train_plot.png')
plt.clf()
def plot_examples(model, log_dir, val_loader, device, num_examples=3, name='val'):
# Check best model output with the input image and label
model.load_state_dict(torch.load(os.path.join(log_dir, "best_model.pth")))
model.eval()
with torch.no_grad():
for i, val_data in enumerate(val_loader):
roi_size = (160, 160, 160)
sw_batch_size = 4
val_outputs = sliding_window_inference(val_data["image"].to(device), roi_size, sw_batch_size, model)
slice_num = 80 # val_data["image"].shape[-1]//2
plt.figure("check", (18, 6))
plt.subplot(1, 3, 1)
plt.title(f"image {i}")
plt.imshow(val_data["image"][0, 0, :, :, slice_num], cmap="gray")
plt.subplot(1, 3, 2)
plt.title(f"label {i}")
plt.imshow(val_data["label"][0, 0, :, :, slice_num])
plt.subplot(1, 3, 3)
plt.title(f"output {i}")
plt.imshow(torch.argmax(val_outputs, dim=1).detach().cpu()[0, :, :, slice_num])
plt.savefig(log_dir+'/'+name+'_output'+str(i)+'.png')
plt.clf()
if i == (num_examples-1):
break
def get_transforms(args):
# Setup transforms for training and validation
# Here we use several transforms to augment the dataset:
# 1. `LoadImaged` loads the spleen CT images and labels from NIfTI format files.
# 2. `EnsureChannelFirstd` ensures the original data to construct "channel first" shape.
# 3. `Orientationd` unifies the data orientation based on the affine matrix.
# 4. `Spacingd` adjusts the spacing by `pixdim=(1.5, 1.5, 2.)` based on the affine matrix.
# 5. `ScaleIntensityRanged` extracts intensity range [-57, 164] and scales to [0, 1].
# 6. `CropForegroundd` removes all zero borders to focus on the valid body area of the images and labels.
# 7. `RandCropByPosNegLabeld` randomly crop patch samples from big image based on pos / neg ratio.
# The image centers of negative samples must be in valid body area.
# 8. `RandAffined` efficiently performs `rotate`, `scale`, `shear`, `translate`, etc. together based on PyTorch affine transform.
if args.dataset == 'Task09_Spleen':
a_min = -57
a_max = 164
pixdim = (1.5, 1.5, 2.0)
elif args.dataset == 'Task07_Pancreas':
a_min = -87
a_max = 199
pixdim = (1.5, 1.5, 1.0)
train_transforms = Compose(
[
LoadImaged(keys=["image", "label"]),
EnsureChannelFirstd(keys=["image", "label"]),
ScaleIntensityRanged(
keys=["image"],
a_min=a_min,
a_max=a_max,
b_min=0.0,
b_max=1.0,
clip=True,
),
CropForegroundd(keys=["image", "label"], source_key="image"),
Orientationd(keys=["image", "label"], axcodes="RAS"),
Spacingd(keys=["image", "label"], pixdim=pixdim, mode=("bilinear", "nearest")),
RandCropByPosNegLabeld(
keys=["image", "label"],
label_key="label",
spatial_size=(96, 96, 96),
pos=1,
neg=1,
num_samples=4,
image_key="image",
image_threshold=0,
),
# user can also add other random transforms
# RandAffined(
# keys=['image', 'label'],
# mode=('bilinear', 'nearest'),
# prob=1.0, spatial_size=(96, 96, 96),
# rotate_range=(0, 0, np.pi/15),
# scale_range=(0.1, 0.1, 0.1)),
]
)
val_transforms = Compose(
[
LoadImaged(keys=["image", "label"]),
EnsureChannelFirstd(keys=["image", "label"]),
ScaleIntensityRanged(
keys=["image"],
a_min=-57,
a_max=164,
b_min=0.0,
b_max=1.0,
clip=True,
),
CropForegroundd(keys=["image", "label"], source_key="image"),
Orientationd(keys=["image", "label"], axcodes="RAS"),
Spacingd(keys=["image", "label"], pixdim=pixdim, mode=("bilinear", "nearest")),
]
)
val_org_transforms = Compose(
[
LoadImaged(keys=["image", "label"]),
EnsureChannelFirstd(keys=["image", "label"]),
Orientationd(keys=["image"], axcodes="RAS"),
Spacingd(keys=["image"], pixdim=pixdim, mode="bilinear"),
ScaleIntensityRanged(
keys=["image"],
a_min=-57,
a_max=164,
b_min=0.0,
b_max=1.0,
clip=True,
),
# CropForegroundd(keys=["image"], source_key="image"),
]
)
post_transforms = Compose(
[
Invertd(
keys="pred",
transform=val_org_transforms,
orig_keys="image",
meta_keys="pred_meta_dict",
orig_meta_keys="image_meta_dict",
meta_key_postfix="meta_dict",
nearest_interp=False,
to_tensor=True,
device="cpu",
),
AsDiscreted(keys="pred", argmax=True, to_onehot=2),
AsDiscreted(keys="label", to_onehot=2),
]
)
return train_transforms, val_transforms, val_org_transforms, post_transforms
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Train config file')
parser.add_argument('-c', '--config', help='path to config file', required=True)
arg = parser.parse_args()
config_path = arg.config
args = munch.munchify(yaml.safe_load(open(config_path)))
print_time = datetime.datetime.now().isoformat()[:19]
exp_name = args.model_name
# exp_name += '_'+print_time.replace(':',"-")
if 'base_model_name' in args:
exp_name += '_'+args.base_model_name
exp_name += '_seed'+str(args.seed)
log_dir = os.path.join(args.work_dir, args.dataset, 'fold'+str(args.fold), exp_name)
if not os.path.exists(log_dir):
os.makedirs(log_dir)
print(log_dir)
train(args, log_dir)
args['best_model_path'] = os.path.join(log_dir, "best_model.pth")
# Update yaml in log dir
with open(os.path.join(log_dir, os.path.basename(config_path)), 'w') as f:
yaml.dump(args, f)
print(os.path.join(log_dir, os.path.basename(config_path)))
# Test
subprocess.call(['python', 'test.py', '-c', os.path.join(log_dir, os.path.basename(config_path)), '-d', args.device])
subprocess.call(['python', 'test.py', '-c', os.path.join(log_dir, os.path.basename(config_path)), '-n', str(30), '-d', args.device])