forked from shreyaspadhy/UNet-Zoo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
282 lines (225 loc) · 10.8 KB
/
main.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
# %% -*- coding: utf-8 -*-
'''
Author: Shreyas Padhy
Driver file for Standard UNet Implementation
'''
from __future__ import print_function
import argparse
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
import torch.optim as optim
from torch.optim import lr_scheduler
from torch.autograd import Variable
from torch.utils.data import DataLoader
import scipy.io as sio
import torchvision.transforms as tr
from data import BraTSDatasetUnet, BraTSDatasetLSTM, UnetPred
from losses import DICELossMultiClass, DICELoss
from models import UNet
from tqdm import tqdm
import numpy as np
import os
from plot_ims import save_prediction
# %% import transforms
# %% Training settings
parser = argparse.ArgumentParser(
description='UNet + BDCLSTM for BraTS Dataset')
parser.add_argument('--batch-size', type=int, default=1, metavar='N',
help='input batch size for training (default: 64)')
parser.add_argument('--test-batch-size', type=int, default=1, metavar='N',
help='input batch size for testing (default: 1000)')
parser.add_argument('--train', action='store_true', default=False,
help='Argument to train model (default: False)')
parser.add_argument('--epochs', type=int, default=40, metavar='N',
help='number of epochs to train (default: 10)')
parser.add_argument('--lr', type=float, default=0.001, metavar='LR',
help='learning rate (default: 0.01)')
parser.add_argument('--cuda', action='store_true', default=True,
help='enables CUDA training (default: False)')
parser.add_argument('--log-interval', type=int, default=100, metavar='N',
help='batches to wait before logging training status')
parser.add_argument('--size', type=int, default=512, metavar='N',
help='imsize')
parser.add_argument('--load', type=str,
default=None,
metavar='str',
help='weight file to load (default: None)')
# unet-final-3-20-0.001
parser.add_argument('--data-folder', type=str,
default=None,
metavar='str',
help='folder that contains data (default: test dataset)')
parser.add_argument('--save', type=str, default='OutMasks', metavar='str',
help='Identifier to save npy arrays with')
parser.add_argument('--modality', type=str, default='t2', metavar='str',
help='Modality to use for training (default: flair)')
parser.add_argument('--optimizer', type=str, default='SGD', metavar='str',
help='Optimizer (default: SGD)')
parser.add_argument('--pred', action='store_true', default=False,
help='Argument to make prediction (default: False)')
parser.add_argument('--pred-input', type=str, default=None, metavar='str',
help='folder that contains data to make predctions')
parser.add_argument('--pred-output', type=str, default=None, metavar='str',
help='folder that contains data to make predctions')
parser.add_argument('--batch-out-folder', type=str, default=None, metavar='str',
help='folder that contains data to make predctions')
parser.add_argument('--channels', type=int, default=1, metavar='N',
help='number of channels, 1 for grayscale, 3 for rgb (default: 1)')
parser.add_argument('--save-model', type=str, default='', metavar='str',
help='save model file name (default: \'\')')
args = parser.parse_args()
args.cuda = args.cuda and torch.cuda.is_available()
DATA_FOLDER = args.data_folder
PRED_INPUT = args.pred_input
PRED_OUTPUT = args.pred_output
BATCH_OUT_FOLDER = args.batch_out_folder
CHANNELS = args.channels
SAVE_MODEL_NAME = args.save_model
# %% Loading in the Dataset
if args.train:
dset_train = BraTSDatasetUnet(DATA_FOLDER, train=True,
keywords=[args.modality],
im_size=[args.size, args.size], transform=tr.ToTensor())
train_loader = DataLoader(dset_train,
batch_size=args.batch_size,
shuffle=True, num_workers=1)
dset_test = BraTSDatasetUnet(DATA_FOLDER, train=False,
keywords=[args.modality],
im_size=[args.size, args.size], transform=tr.ToTensor())
test_loader = DataLoader(dset_test,
batch_size=args.test_batch_size,
shuffle=False, num_workers=1)
print("Data folder: ", DATA_FOLDER)
print("Load : ", args.load)
print("Training Data : ", len(train_loader.dataset))
print("Testing Data : ", len(test_loader.dataset))
print("Optimizer : ", args.optimizer)
else:
dset_pred = UnetPred(PRED_INPUT, keywords=[args.modality],
im_size=[args.size, args.size], transform=tr.ToTensor())
pred_loader = DataLoader(dset_pred,
batch_size=args.test_batch_size,
shuffle=False, num_workers=1)
print("Prediction Data : ", len(pred_loader.dataset))
# %% Loading in the model
model = UNet()
if args.cuda:
model.cuda()
if args.optimizer == 'SGD':
optimizer = optim.SGD(model.parameters(), lr=args.lr,
momentum=0.99)
if args.optimizer == 'ADAM':
optimizer = optim.Adam(model.parameters(), lr=args.lr,
betas=(args.beta1, args.beta2))
exp_lr_scheduler = lr_scheduler.StepLR(optimizer, step_size=10, gamma=0.1)
# Defining Loss Function
criterion = DICELossMultiClass()
def train(epoch, scheduler, loss_lsit):
scheduler.step()
model.train()
for batch_idx, (image, mask) in enumerate(train_loader):
if args.cuda:
image, mask = image.cuda(), mask.cuda()
image, mask = Variable(image), Variable(mask)
optimizer.zero_grad()
output = model(image)
loss = criterion(output, mask)
loss_list.append(loss.data[0])
loss.backward()
optimizer.step()
if batch_idx % args.log_interval == 0:
print('Train Epoch: {} [{}/{} ({:.0f}%)]\tAverage DICE Loss: {:.6f}'.format(
epoch, batch_idx * len(image), len(train_loader.dataset),
100. * batch_idx / len(train_loader), loss.data[0]))
def test(train_accuracy=False, save_output=False):
test_loss = 0
if train_accuracy:
loader = train_loader
else:
loader = test_loader
for batch_idx, (image, mask) in tqdm(enumerate(loader)):
if args.cuda:
image, mask = image.cuda(), mask.cuda()
image, mask = Variable(image, volatile=True), Variable(
mask, volatile=True)
output = model(image)
# test_loss += criterion(output, mask).data[0]
maxes, out = torch.max(output, 1, keepdim=True)
if save_output and (not train_accuracy):
np.save('npy-files/out-files/{}-batch-{}-outs.npy'.format(args.save,
batch_idx),
out.data.byte().cpu().numpy())
np.save('npy-files/out-files/{}-batch-{}-masks.npy'.format(args.save,
batch_idx),
mask.data.byte().cpu().numpy())
np.save('npy-files/out-files/{}-batch-{}-images.npy'.format(args.save,
batch_idx),
image.data.float().cpu().numpy())
if save_output and train_accuracy:
np.save('npy-files/out-files/{}-train-batch-{}-outs.npy'.format(args.save,
batch_idx),
out.data.byte().cpu().numpy())
np.save('npy-files/out-files/{}-train-batch-{}-masks.npy'.format(args.save,
batch_idx),
mask.data.byte().cpu().numpy())
np.save('npy-files/out-files/{}-train-batch-{}-images.npy'.format(args.save,
batch_idx),
image.data.float().cpu().numpy())
test_loss += criterion(output, mask).data[0]
# Average Dice Coefficient
test_loss /= len(loader)
if train_accuracy:
print('\nTraining Set: Average DICE Coefficient: {:.4f})\n'.format(
test_loss))
else:
print('\nTest Set: Average DICE Coefficient: {:.4f})\n'.format(
test_loss))
def predict():
loader = pred_loader
for batch_idx, image in tqdm(enumerate(loader)):
if args.cuda:
image = image.cuda()
image= Variable(image, volatile=True)
output = model(image)
output.data.round_()
np.save(os.path.join(BATCH_OUT_FOLDER, '{}-batch-{}-outs.npy'.format(args.save,batch_idx)),
output.data.byte().cpu().numpy())
np.save(os.path.join(BATCH_OUT_FOLDER, '{}-batch-{}-images.npy'.format(args.save,batch_idx)),
image.data.float().cpu().numpy())
file_names = dset_pred.get_file()
save_dir = PRED_OUTPUT
base_name = 'OutMasks'
outmask_folder = BATCH_OUT_FOLDER
# plot
save_prediction(file_names, save_dir, base_name, outmask_folder)
if args.train:
loss_list = []
for i in tqdm(range(args.epochs)):
train(i, exp_lr_scheduler, loss_list)
test()
plt.plot(loss_list)
plt.title("UNet bs={}, ep={}, lr={}".format(args.batch_size,
args.epochs, args.lr))
plt.xlabel("Number of iterations")
plt.ylabel("Average DICE loss per batch")
plt.savefig("plots/{}-UNet_Loss_bs={}_ep={}_lr={}.png".format(args.save,
args.batch_size,
args.epochs,
args.lr))
np.save('npy-files/loss-files/{}-UNet_Loss_bs={}_ep={}_lr={}.npy'.format(args.save,
args.batch_size,
args.epochs,
args.lr),
np.asarray(loss_list))
torch.save(model.state_dict(), '{}unetsmall-final-{}-{}-{}'.format(SAVE_MODEL_NAME, args.batch_size,
args.epochs,
args.lr))
# elif args.pred:
# predict()
elif args.load is not None:
model.load_state_dict(torch.load(args.load))
#test()
predict()