forked from shreyaspadhy/UNet-Zoo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_small.py
215 lines (172 loc) · 8.57 KB
/
main_small.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
# %% -*- coding: utf-8 -*-
'''
Author: Shreyas Padhy
Driver file for Unet and BDC-LSTM 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.autograd import Variable
from torch.utils.data import DataLoader
import torchvision.transforms as tr
from data import BraTSDatasetUnet, BraTSDatasetLSTM
from losses import DICELoss
from models import UNetSmall
from tqdm import tqdm
import scipy.io as sio
import numpy as np
# %% import transforms
# %% Training settings
parser = argparse.ArgumentParser(description='UNet+BDCLSTM for BraTS Dataset')
parser.add_argument('--batch-size', type=int, default=4, metavar='N',
help='input batch size for training (default: 64)')
parser.add_argument('--test-batch-size', type=int, default=1000, 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=1, 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=False,
help='enables CUDA training (default: False)')
parser.add_argument('--log-interval', type=int, default=1, metavar='N',
help='batches to wait before logging training status')
parser.add_argument('--size', type=int, default=128, metavar='N',
help='imsize')
parser.add_argument('--load', type=str, default=None, metavar='str',
help='weight file to load (default: None)')
parser.add_argument('--data-folder', type=str, default='./Data/', 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='flair', metavar='str',
help='Modality to use for training (default: flair)')
parser.add_argument('--optimizer', type=str, default='ADAM', metavar='str',
help='Optimizer (default: SGD)')
parser.add_argument('--clip', action='store_true', default=False,
help='enables gradnorm clip of 1.0 (default: False)')
args = parser.parse_args()
args.cuda = args.cuda and torch.cuda.is_available()
DATA_FOLDER = args.data_folder
# %% Loading in the Dataset
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("Training Data : ", len(train_loader.dataset))
print("Testing Data : ", len(test_loader.dataset))
# %% Loading in the model
model = UNetSmall()
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=(0.9, 0.999))
# Defining Loss Function
criterion = DICELoss()
# Define Training Loop
def train(epoch, loss_list):
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 args.clip:
nn.utils.clip_grad_norm(model.parameters(), max_norm=1)
if batch_idx % args.log_interval == 0:
print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.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
correct = 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]
output.data.round_()
if save_output and (not train_accuracy):
np.save('./npy-files/out-files/{}-unetsmall-batch-{}-outs.npy'.format(args.save,
batch_idx),
output.data.byte().cpu().numpy())
np.save('./npy-files/out-files/{}-unetsmall--batch-{}-masks.npy'.format(args.save,
batch_idx),
mask.data.byte().cpu().numpy())
np.save('./npy-files/out-files/{}-unetsmall--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/{}-unetsmall-train-batch-{}-outs.npy'.format(args.save,
batch_idx),
output.data.byte().cpu().numpy())
np.save('./npy-files/out-files/{}-unetsmall-train-batch-{}-masks.npy'.format(args.save,
batch_idx),
mask.data.byte().cpu().numpy())
np.save('./npy-files/out-files/{}-unetsmall-train-batch-{}-images.npy'.format(args.save,
batch_idx),
image.data.float().cpu().numpy())
# 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))
if args.train:
loss_list = []
for i in tqdm(range(args.epochs)):
train(i, loss_list)
test(train_accuracy=False, save_output=False)
test(train_accuracy=True, save_output=False)
plt.plot(loss_list)
plt.title("UNetSmall 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/{}-UNetSmall_Loss_bs={}_ep={}_lr={}.png".format(args.save,
args.batch_size,
args.epochs,
args.lr))
np.save('./npy-files/loss-files/{}-UNetSmall_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(args.batch_size,
args.epochs,
args.lr))
else:
model.load_state_dict(torch.load(args.load))
test(save_output=True)
test(train_accuracy=True)