-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmainMiccaiRecon.py
223 lines (176 loc) · 7.3 KB
/
mainMiccaiRecon.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
'''
Image Segmentation using SegNet
'''
import argparse
import os
import shutil
import numpy as np
import cv2
from PIL import Image
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.optim as optim
from torch.autograd import Variable
import torch.utils.data
import torchvision.transforms as transforms
import torch.nn.functional as F
import utils
from model.reconNet import ReconNet
from datasets.miccaiDataLoader import miccaiDataset
parser = argparse.ArgumentParser(description='PyTorch SegNet Training')
parser.add_argument('-j', '--workers', default=4, type=int, metavar='N',
help='number of data loading workers (default: 4)')
parser.add_argument('--epochs', default=20, type=int, metavar='N',
help='number of total epochs to run')
parser.add_argument('--start-epoch', default=0, type=int, metavar='N',
help='manual epoch number (useful on restarts)')
parser.add_argument('--batchSize', default=4, type=int,
help='Mini-batch size (default: 4)')
parser.add_argument('--lr', '--learning-rate', default=0.05, type=float,
metavar='LR', help='initial learning rate')
parser.add_argument('--bnMomentum', default=0.1, type=float,
help='Batch Norm Momentum (default: 0.1)')
parser.add_argument('--imageSize', default=256, type=int,
help='height/width of the input image to the network')
parser.add_argument('--resizedImageSize', default=224, type=int,
help='height/width of the resized image to the network')
parser.add_argument('--print-freq', '-p', default=1, type=int, metavar='N',
help='print frequency (default:1)')
parser.add_argument('--resume', default='', type=str, metavar='PATH',
help='path to latest checkpoint (default: none)')
parser.add_argument('-e', '--evaluate', dest='evaluate', action='store_true',
help='evaluate model on validation set')
parser.add_argument('--pre-trained', dest='pretrained', action='store_true',
help='use pre-trained model')
parser.add_argument('--save-dir', dest='save_dir',
help='The directory used to save the trained models',
default='save_temp', type=str)
best_prec1 = np.inf
use_gpu = torch.cuda.is_available()
def main():
global args, best_prec1
args = parser.parse_args()
# Check if the save directory exists or not
if not os.path.exists(args.save_dir):
os.makedirs(args.save_dir)
cudnn.benchmark = True
data_transforms = {
'train': transforms.Compose([
transforms.Resize((args.imageSize, args.imageSize), interpolation=Image.NEAREST),
#transforms.TenCrop(args.resizedImageSize),
#transforms.Lambda(lambda crops: torch.stack([transforms.ToTensor()(crop) for crop in crops]))
#transforms.RandomResizedCrop(224, interpolation=Image.NEAREST),
#transforms.RandomHorizontalFlip(),
#transforms.RandomVerticalFlip(),
transforms.ToTensor(),
]),
'test': transforms.Compose([
transforms.Resize((args.imageSize, args.imageSize), interpolation=Image.NEAREST),
transforms.ToTensor(),
]),
}
# Data Loading
data_dir = '/media/salman/DATA/NUST/MS RIME/Thesis/MICCAI Dataset/m2cai16-tool/train_dataset'
image_datasets = {x: miccaiDataset(os.path.join(data_dir, x), data_transforms[x])
for x in ['train', 'test']}
dataloaders = {x: torch.utils.data.DataLoader(image_datasets[x],
batch_size=args.batchSize,
shuffle=True,
num_workers=args.workers)
for x in ['train', 'test']}
dataset_sizes = {x: len(image_datasets[x]) for x in ['train', 'test']}
# Initialize the model
model = ReconNet(args.bnMomentum)
# Optionally resume from a checkpoint
if args.resume:
if os.path.isfile(args.resume):
print("=> loading checkpoint '{}'".format(args.resume))
checkpoint = torch.load(args.resume)
args.start_epoch = checkpoint['epoch']
model.load_state_dict(checkpoint['state_dict'])
print("=> loaded checkpoint '{}' (epoch {})".format(args.evaluate, checkpoint['epoch']))
else:
print("=> no checkpoint found at '{}'".format(args.resume))
# Define loss function (criterion) and optimizer
criterion = nn.L1Loss()
if use_gpu:
model.cuda()
criterion.cuda()
optimizer = optim.Adam(model.parameters(), lr = args.lr)
if args.evaluate:
validate(dataloaders['test'], model, criterion, 0)
return
for epoch in range(args.start_epoch, args.epochs):
#adjust_learning_rate(optimizer, epoch)
# Train for one epoch
print('>>>>>>>>>>>>>>>>>>>>>>>Training<<<<<<<<<<<<<<<<<<<<<<<')
train(dataloaders['train'], model, criterion, optimizer, epoch)
# Evaulate on validation set
print('>>>>>>>>>>>>>>>>>>>>>>>Testing<<<<<<<<<<<<<<<<<<<<<<<')
validate(dataloaders['test'], model, criterion, epoch)
save_checkpoint({
'epoch': epoch + 1,
'state_dict': model.state_dict(),
'optimizer': optimizer.state_dict(),
}, filename=os.path.join(args.save_dir, 'checkpoint_{}.tar'.format(epoch)))
def train(train_loader, model, criterion, optimizer, epoch):
'''
Run one training epoch
'''
# Switch to train mode
model.train()
for i, img in enumerate(train_loader):
# For TenCrop Data Augmentation
#img = img.view(args.batchSize*10,3,args.resizedImageSize,args.resizedImageSize)
label = Variable(img)
img = Variable(img)
if use_gpu:
img = img.cuda()
label = label.cuda()
# Compute output
gen = model(img)
loss = criterion(gen, label)
# Compute gradient and do SGD step
optimizer.zero_grad()
loss.backward()
optimizer.step()
print('[%d/%d][%d/%d] Total-Loss: %.4f'
% (epoch, args.epochs-1, i, len(train_loader)-1, loss.mean().data[0]))
utils.displayReconSamples(img, gen, use_gpu)
def validate(val_loader, model, criterion, epoch):
'''
Run evaluation
'''
# Switch to evaluate mode
model.eval()
for i, img in enumerate(val_loader):
# Process the network inputs and outputs
label = Variable(img)
img = Variable(img)
if use_gpu:
img = img.cuda()
label = label.cuda()
# Compute output
gen = model(img)
loss = criterion(gen, label)
print('[%d/%d][%d/%d] Total-Loss: %.4f'
% (epoch, args.epochs-1, i, len(val_loader)-1, loss.mean().data[0]))
utils.displayReconSamples(img, gen, use_gpu)
return loss
def save_checkpoint(state, filename='checkpoint.pth.tar'):
'''
Save the training model
'''
torch.save(state, filename)
def adjust_learning_rate(optimizer, epoch):
'''
Sets the learning rate to the initial LR decayed by a factor of 10
every 30 epochs
'''
lr = args.lr * (0.5 ** (epoch // 30))
for param_group in optimizer.param_groups:
param_group['lr'] = lr
if __name__ == '__main__':
main()