-
Notifications
You must be signed in to change notification settings - Fork 2
/
train_model.py
370 lines (306 loc) · 13.5 KB
/
train_model.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
import datetime
import logging
import pathlib
import random
import shutil
import time
import numpy as np
import torch
import torchvision
from tensorboardX import SummaryWriter
from torch.nn import functional as F
from torch.utils.data import DataLoader
from common.args import Args
from common.subsample import MaskFunc
from data import transforms
from data.mri_data import SliceData
from models.unet.unet_model import UnetModel, CustomUnetModel
from loss import FeatureLoss
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class DataTransform:
"""
Data Transformer for training U-Net models.
"""
def __init__(self, mask_func, resolution, which_challenge, use_seed=True):
"""
Args:
mask_func (common.subsample.MaskFunc): A function that can create a mask of
appropriate shape.
resolution (int): Resolution of the image.
which_challenge (str): Either "singlecoil" or "multicoil" denoting the dataset.
use_seed (bool): If true, this class computes a pseudo random number generator seed
from the filename. This ensures that the same mask is used for all the slices of
a given volume every time.
"""
if which_challenge not in ('singlecoil', 'multicoil'):
raise ValueError(f'Challenge should either be "singlecoil" or "multicoil"')
self.mask_func = mask_func
self.resolution = resolution
self.which_challenge = which_challenge
self.use_seed = use_seed
def __call__(self, kspace, target, attrs, fname, slice):
"""
Args:
kspace (numpy.array): Input k-space of shape (num_coils, rows, cols, 2) for multi-coil
data or (rows, cols, 2) for single coil data.
target (numpy.array): Target image
attrs (dict): Acquisition related information stored in the HDF5 object.
fname (str): File name
slice (int): Serial number of the slice.
Returns:
(tuple): tuple containing:
image (torch.Tensor): Zero-filled input image.
target (torch.Tensor): Target image converted to a torch Tensor.
mean (float): Mean value used for normalization.
std (float): Standard deviation value used for normalization.
norm (float): L2 norm of the entire volume.
"""
kspace = transforms.to_tensor(kspace)
# Apply mask
seed = None if not self.use_seed else tuple(map(ord, fname))
masked_kspace, mask = transforms.apply_mask(kspace, self.mask_func, seed)
# Inverse Fourier Transform to get zero filled solution
image = transforms.ifft2(masked_kspace)
# Crop input image
image = transforms.complex_center_crop(image, (self.resolution, self.resolution))
# Absolute value
image = transforms.complex_abs(image)
# Apply Root-Sum-of-Squares if multicoil data
if self.which_challenge == 'multicoil':
image = transforms.root_sum_of_squares(image)
# Normalize input
image, mean, std = transforms.normalize_instance(image, eps=1e-11)
image = image.clamp(-6, 6)
target = transforms.to_tensor(target)
# Normalize target
target = transforms.normalize(target, mean, std, eps=1e-11)
target = target.clamp(-6, 6)
return image, target, mean, std, attrs['norm'].astype(np.float32)
def create_datasets(args):
train_mask = MaskFunc(args.center_fractions, args.accelerations)
dev_mask = MaskFunc(args.center_fractions, args.accelerations)
train_data = SliceData(
root=args.data_path / f'{args.challenge}_train',
transform=DataTransform(train_mask, args.resolution, args.challenge),
sample_rate=args.sample_rate,
challenge=args.challenge
)
dev_data = SliceData(
root=args.data_path / f'{args.challenge}_val',
transform=DataTransform(dev_mask, args.resolution, args.challenge, use_seed=True),
sample_rate=args.sample_rate,
challenge=args.challenge,
)
return dev_data, train_data
def create_data_loaders(args):
dev_data, train_data = create_datasets(args)
display_data = [dev_data[i] for i in range(0, len(dev_data), len(dev_data) // 16)]
train_loader = DataLoader(
dataset=train_data,
batch_size=args.batch_size,
shuffle=True,
num_workers=8,
pin_memory=True,
)
dev_loader = DataLoader(
dataset=dev_data,
batch_size=args.batch_size,
num_workers=8,
pin_memory=True,
)
display_loader = DataLoader(
dataset=display_data,
batch_size=16,
num_workers=8,
pin_memory=True,
)
return train_loader, dev_loader, display_loader
def tv_loss(img, tv_weight):
"""
Compute total variation loss.
Inputs:
- img: PyTorch Variable of shape (1, 3, H, W) holding an input image.
- tv_weight: Scalar giving the weight w_t to use for the TV loss.
Returns:
- loss: PyTorch Variable holding a scalar giving the total variation loss
for img weighted by tv_weight.
"""
w_variance = torch.sum(torch.pow(img[:,:,:,:-1] - img[:,:,:,1:], 2))
h_variance = torch.sum(torch.pow(img[:,:,:-1,:] - img[:,:,1:,:], 2))
loss = tv_weight * (h_variance + w_variance)
return loss
def train_epoch(args, loss_func, epoch, model, data_loader, optimizer, writer):
model.train()
avg_loss = 0.
start_epoch = start_iter = time.perf_counter()
global_step = epoch * len(data_loader)
for iter, data in enumerate(data_loader):
input, target, mean, std, norm = data
input = input.unsqueeze(1).to(args.device)
target = target.to(args.device)
output = model(input).squeeze(1)
# Residual learning
output += input.squeeze(1)
loss = loss_func(output, target)
optimizer.zero_grad()
loss.backward()
optimizer.step()
avg_loss = 0.99 * avg_loss + 0.01 * loss.item() if iter > 0 else loss.item()
writer.add_scalar('TrainLoss', loss.item(), global_step + iter)
if iter % args.report_interval == 0:
logging.info(
f'Epoch = [{epoch:3d}/{args.num_epochs:3d}] '
f'Iter = [{iter:4d}/{len(data_loader):4d}] '
f'Loss = {loss.item():.4g} Avg Loss = {avg_loss:.4g} '
f'Time = {time.perf_counter() - start_iter:.4f}s',
)
start_iter = time.perf_counter()
return avg_loss, time.perf_counter() - start_epoch
def evaluate(args, epoch, model, data_loader, writer):
model.eval()
losses = []
start = time.perf_counter()
with torch.no_grad():
for iter, data in enumerate(data_loader):
input, target, mean, std, norm = data
input = input.unsqueeze(1).to(args.device)
target = target.to(args.device)
output = model(input).squeeze(1)
# Residual prediction
output += input.squeeze(1)
mean = mean.unsqueeze(1).unsqueeze(2).to(args.device)
std = std.unsqueeze(1).unsqueeze(2).to(args.device)
target = target * std + mean
output = output * std + mean
norm = norm.unsqueeze(1).unsqueeze(2).to(args.device)
loss = F.mse_loss(output / norm, target / norm, reduction='sum')
losses.append(loss.item())
writer.add_scalar('Dev_Loss', np.mean(losses), epoch)
return np.mean(losses), time.perf_counter() - start
def visualize(args, epoch, model, data_loader, writer):
def save_image(image, tag):
image -= max(image.min(), -6)
image /= image.max()
grid = torchvision.utils.make_grid(image, nrow=4, pad_value=1)
writer.add_image(tag, grid, epoch)
model.eval()
with torch.no_grad():
for iter, data in enumerate(data_loader):
input, target, mean, std, norm = data
input = input.unsqueeze(1).to(args.device)
target = target.unsqueeze(1).to(args.device)
output = model(input)
# Residual prediction
output += input
save_image(input, 'Zero-Fill')
save_image(target, 'Target')
save_image(output, 'Reconstruction')
save_image(torch.abs(target - output), 'Error')
break
def save_model(args, exp_dir, epoch, model, optimizer, best_dev_loss, is_new_best, timestr):
torch.save(
{
'epoch': epoch,
'args': args,
'model': model.state_dict(),
'optimizer': optimizer.state_dict(),
'best_dev_loss': best_dev_loss,
'exp_dir': exp_dir
},
f=exp_dir / 'model_{}.pt'.format(timestr)
)
if is_new_best:
shutil.copyfile(exp_dir / 'model_{}.pt'.format(timestr), exp_dir / 'best_model_{}.pt'.format(timestr))
def build_model(args):
model = CustomUnetModel(
in_chans=1,
out_chans=1,
chans=args.num_chans,
num_pool_layers=args.num_pools,
drop_prob=args.drop_prob
).to(args.device)
return model
def load_model(checkpoint_file):
checkpoint = torch.load(checkpoint_file)
args = checkpoint['args']
model = build_model(args)
if args.data_parallel:
model = torch.nn.DataParallel(model)
model.load_state_dict(checkpoint['model'])
optimizer = build_optim(args, model.parameters())
optimizer.load_state_dict(checkpoint['optimizer'])
return checkpoint, model, optimizer
def build_optim(args, params):
optimizer = torch.optim.Adam(params, args.lr, weight_decay=args.weight_decay)
return optimizer
def main(args):
timestr = datetime.datetime.now().strftime("%Y-%m-%d_%H:%M:%S")
args.exp_dir.mkdir(parents=True, exist_ok=True)
writer = SummaryWriter(log_dir=args.exp_dir / 'summary')
loss_func = FeatureLoss("./checkpoints/best_model_2019-09-18_10:54:19.pt",
args,
feat_weight=0.1)
if args.resume:
checkpoint, model, optimizer = load_model(args.checkpoint)
args = checkpoint['args']
best_dev_loss = checkpoint['best_dev_loss']
start_epoch = checkpoint['epoch'] + 1
del checkpoint
else:
model = build_model(args)
if args.data_parallel:
model = torch.nn.DataParallel(model)
optimizer = build_optim(args, model.parameters())
best_dev_loss = 1e9
start_epoch = 0
logging.info(args)
logging.info(model)
train_loader, dev_loader, display_loader = create_data_loaders(args)
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, args.lr_step_size, args.lr_gamma)
for epoch in range(start_epoch, args.num_epochs):
train_loss, train_time = train_epoch(args, loss_func, epoch, model, train_loader, optimizer, writer)
scheduler.step(epoch)
dev_loss, dev_time = evaluate(args, epoch, model, dev_loader, writer)
visualize(args, epoch, model, display_loader, writer)
is_new_best = dev_loss < best_dev_loss
best_dev_loss = min(best_dev_loss, dev_loss)
save_model(args, args.exp_dir, epoch, model, optimizer, best_dev_loss, is_new_best,timestr)
logging.info(
f'Epoch = [{epoch:4d}/{args.num_epochs:4d}] TrainLoss = {train_loss:.4g} '
f'DevLoss = {dev_loss:.4g} TrainTime = {train_time:.4f}s DevTime = {dev_time:.4f}s',
)
writer.close()
def create_arg_parser():
parser = Args()
parser.add_argument('--num-pools', type=int, default=4, help='Number of U-Net pooling layers')
parser.add_argument('--drop-prob', type=float, default=0.0, help='Dropout probability')
parser.add_argument('--num-chans', type=int, default=32, help='Number of U-Net channels')
parser.add_argument('--batch-size', default=16, type=int, help='Mini batch size')
parser.add_argument('--num-epochs', type=int, default=50, help='Number of training epochs')
parser.add_argument('--lr', type=float, default=0.001, help='Learning rate')
parser.add_argument('--lr-step-size', type=int, default=40,
help='Period of learning rate decay')
parser.add_argument('--lr-gamma', type=float, default=0.1,
help='Multiplicative factor of learning rate decay')
parser.add_argument('--weight-decay', type=float, default=0.,
help='Strength of weight decay regularization')
parser.add_argument('--report-interval', type=int, default=100, help='Period of loss reporting')
parser.add_argument('--data-parallel', action='store_true',
help='If set, use multiple GPUs using data parallelism')
parser.add_argument('--device', type=str, default='cuda',
help='Which device to train on. Set to "cuda" to use the GPU')
parser.add_argument('--exp-dir', type=pathlib.Path, default='checkpoints',
help='Path where model and results should be saved')
parser.add_argument('--resume', action='store_true',
help='If set, resume the training from a previous model checkpoint. '
'"--checkpoint" should be set with this')
parser.add_argument('--checkpoint', type=str,
help='Path to an existing checkpoint. Used along with "--resume"')
return parser
if __name__ == '__main__':
args = create_arg_parser().parse_args()
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
main(args)