-
Notifications
You must be signed in to change notification settings - Fork 1
/
train.py
345 lines (274 loc) · 12 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
import numpy as np
import matplotlib.pyplot as plt
import time
import glob
import os
import torch
import torch.nn as nn
import torch.utils.data as data_utils
import torch.optim as optim
import torch.backends.cudnn as cudnn
from pytorch_model_summary import summary
import config as c
import model
import utils as ut
from dataset import GANDataset
# Set random seed for reproducibility
ut.set_all_seeds_as(c.seed)
# Needed for reproducibility
cudnn.deterministic = True
# increase the speed of training if you are not
# varying the size of image after each epoch
cudnn.benchmark = True
# to fix error with matplotlib
plt.switch_backend('agg')
# to ensure it doesn't run partly on another gpu
torch.cuda.set_device(c.cuda_n[0])
# get patches and labels paths
path_patches = glob.glob(c.dataroot+"train/patches/"+"*.gz")
path_labels = glob.glob(c.dataroot+"train/seg_labels/"+"*.gz")
dataset = GANDataset(path_patches, path_labels)
dataloader = data_utils.DataLoader(dataset, batch_size=c.batch_size,
shuffle=True, num_workers=c.workers)
# Device selection
device = torch.device("cuda:" + str(c.cuda_n[0]) if (torch.cuda.is_available()
and
c.ngpu > 0) else "cpu")
# ####Create generator object##### #
netG = model.Generator().to(device)
# #### Create discriminator object #### #
netD = model.Discriminator().to(device)
if not c.continue_train:
# Apply the weights_init function to randomly initialize all weights
netG.apply(model.weights_init)
# Apply the weights_init function to randomly initialize all weights
netD.apply(model.weights_init)
# Handle multi-gpu if desired
if (device.type == 'cuda') and (c.ngpu > 1):
netG = nn.DataParallel(netG, c.cuda_n)
netD = nn.DataParallel(netD, c.cuda_n)
# Print the models
print(netG)
print(summary(netG, torch.zeros(c.batch_size, c.nz, 1, 1, 1).to(device)))
print(netD)
print(summary(netD, torch.zeros(c.batch_size, c.nc, c.image_size[0],
c.image_size[1], c.image_size[2]).to(device)))
# Create batch of latent vectors that we will use to visualize
# the progression of the generator
fixed_noise = torch.randn(c.batch_size, c.nz, 1, 1, 1, device=device)
# Setup Adam optimizers for both G and D
optimizerD = optim.Adam(netD.parameters(), lr=c.lrd,
betas=(c.beta1d, c.beta2d))
optimizerG = optim.Adam(netG.parameters(), lr=c.lrg,
betas=(c.beta1g, c.beta2g))
# print and save config params in trials.csv
print("Configuration for the run: \n")
print(dict(zip(c.list_config_names, c.list_config)))
ut.save_config(c.save_config, c.list_config_names, c.list_config)
if c.continue_train:
saved_params_dict = torch.load(c.saved_model_path,
map_location=lambda storage, loc: storage)
# Load Generator
netG.load_state_dict(saved_params_dict['Generator_state_dict'])
optimizerG.load_state_dict(saved_params_dict['OptimizerG_state_dict'])
# Load Discriminator
netD.load_state_dict(saved_params_dict['Discriminator_state_dict'])
optimizerD.load_state_dict(saved_params_dict['OptimizerD_state_dict'])
# Load lists with last values to keep track of progress
G_losses = np.load(c.save_results + 'G_losses.npy')[
:c.epoch_num_to_continue+1].tolist()
D_losses = np.load(c.save_results + 'D_losses.npy')[
:c.epoch_num_to_continue+1].tolist()
Wasserstein_D = np.load(c.save_results + 'Wasserstein_D.npy')[
:c.epoch_num_to_continue+1].tolist()
start_epoch = c.epoch_num_to_continue+1
else:
# Lists to keep track of progress
G_losses = []
D_losses = []
Wasserstein_D = []
start_epoch = 1
iters = 0
duration = 0
# Training Loop
print("Starting Training Loop...")
# Scaler that tracks the scaling of gradients
# only used when mixed precision is used
scaler = torch.cuda.amp.GradScaler(enabled=c.use_mixed_precision)
# For each epoch
for epoch in range(start_epoch, c.num_epochs+1):
epoch_start_time = time.time()
# For each batch in the dataloader
errD_iter = []
errG_iter = []
Wasserstein_D_iter = []
batch_start_time = time.time()
for i, data in enumerate(dataloader, 1):
# for each iteration in the epoch
errD_disc_iter = []
Wasserstein_D_disc_iter = []
batch_duration = 0
# Format batch of real data
real_cpu = data.to(device)
b_size = real_cpu.size(0)
# ########################## #
# (1) Update D network n_disc times:
# with wasserstein_loss gradient penalty
# ######################### #
# updating the critic n_disc number of times
# before 1 update of generator - with 3D this is set to 1
for k in range(c.n_disc):
netD.zero_grad()
netG.zero_grad()
# For training with all-fake batch
# Generate batch of latent vectors
noise = torch.randn(b_size, c.nz, 1, 1, 1, device=device)
# Training within the mixed-precision autocast - enabled/disabled
with torch.cuda.amp.autocast(enabled=c.use_mixed_precision):
# Train with real batch
errD_real = netD(real_cpu)
# Calculate loss on all-real batch
errD_real = -errD_real.view(-1).mean()
# Generate fake image batch with G
fake = netG(noise)
# Train with fake batch
errD_fake = netD(fake.detach())
# Calculate D's loss on the all-fake batch
errD_fake = errD_fake.view(-1).mean()
with torch.cuda.amp.autocast(enabled=False):
# get epsilon value from uniform distribution
eps = torch.rand(1).item()
interpolate = eps * real_cpu + (1 - eps) * fake
d_interpolate = netD(interpolate)
# get gradient penalty
gradient_penalty = ut.wasserstein_gradient_penalty(interpolate,
d_interpolate,
c.lambdaa,
scaler)
# Calculate gradients for D in backward pass
scaler.scale(errD_real).backward(retain_graph=True)
scaler.scale(errD_fake).backward(retain_graph=True)
scaler.scale(gradient_penalty).backward()
errD = errD_fake.item() + errD_real.item() \
+ gradient_penalty.item()
wasserstein = errD_fake.item() + errD_real.item()
errD_disc_iter.append(errD)
Wasserstein_D_disc_iter.append(wasserstein)
scaler.step(optimizerD)
del errD_fake
del errD_real
del wasserstein
del gradient_penalty
del errD
del eps
del interpolate
del d_interpolate
del fake
del noise
errD_disc_avg = np.mean(np.array(errD_disc_iter))
Wasserstein_D_disc_avg = np.mean(np.array(Wasserstein_D_disc_iter))
############################
# (2) Update G network
###########################
netG.zero_grad()
noise = torch.randn(b_size, c.nz, 1, 1, 1, device=device)
with torch.cuda.amp.autocast(enabled=c.use_mixed_precision):
fake = netG(noise)
output_fake = netD(fake)
errG = -output_fake.view(-1).mean()
# Calculate gradients for G
scaler.scale(errG).backward()
scaler.step(optimizerG)
scaler.update()
# update the iteration errors
errD_iter.append(errD_disc_avg)
errG_iter.append(errG.item())
Wasserstein_D_iter.append(Wasserstein_D_disc_avg)
del errG
del errD_disc_avg
del Wasserstein_D_disc_avg
del real_cpu
del noise
del fake
del output_fake
iters += 1
# print after every 100 batches
if i % 100 == 0:
print("[%d/%d] batches done!\n" % (i,
len(dataset)//c.batch_size))
batch_end_time = time.time()
batch_duration = batch_duration + batch_end_time - batch_start_time
print("Training time for", i, "batches: ", batch_duration / 60,
" minutes.")
print(" End of Epoch %d \n" % epoch)
# Output training stats after each epoch
avg_errD = np.mean(np.array(errD_iter))
avg_errG = np.mean(np.array(errG_iter))
avg_Wasserstein_D = np.mean(np.array(Wasserstein_D_iter))
print('[%d/%d]\tLoss_D: %.4f\tLoss_G: %.4f\tWasserstein D: %.4f'
% (epoch, c.num_epochs, avg_errD, avg_errG, avg_Wasserstein_D))
# Save Losses and outputs for plotting later
G_losses.append(avg_errG.item())
D_losses.append(avg_errD.item())
Wasserstein_D.append(avg_Wasserstein_D)
if not os.path.exists(c.save_results):
os.makedirs(c.save_results)
np.save(c.save_results + 'G_losses.npy', np.asarray(G_losses))
np.save(c.save_results + 'D_losses.npy', np.asarray(D_losses))
np.save(c.save_results + 'Wasserstein_D.npy', np.asarray(Wasserstein_D))
# Check how the generator is doing by saving G's output on fixed_noise
with torch.no_grad():
fixed_fake = netG(fixed_noise).detach().cpu()
sample_idx = [0, 1, 2, 3]
for idx in sample_idx:
# hard thresholding for visualisation
sample = fixed_fake[idx].clone()
if c.save_nifti:
ut.convert_and_save_to_nifti(sample[0].to(
dtype=torch.float32).numpy(),
c.save_results
+ "fake_while_training_"
"epoch_%d_sample_%d_patch.nii.gz"
% (epoch, idx))
if c.nc == 2:
ut.convert_and_save_to_nifti(sample[1].to(
dtype=torch.float32).numpy(),
c.save_results
+ "fake_while_training_"
"epoch_%d_sample_%d_label.nii.gz"
% (epoch, idx))
# save model parameters'
if c.is_model_saved:
if not os.path.exists(c.save_model):
os.makedirs(c.save_model)
if (epoch) % c.save_n_epochs == 0:
torch.save({'Discriminator_state_dict': netD.state_dict(),
'Generator_state_dict': netG.state_dict(),
'OptimizerD_state_dict': optimizerD.state_dict(),
'OptimizerG_state_dict': optimizerG.state_dict(),
'Scaler_dict': scaler.state_dict()
}, c.save_model + "epoch_{}.pth".format(epoch))
# plot and save G_loss, D_loss and wasserstein distance
ut.plot_and_save(G_losses, "Generator Loss during training",
c.save_results, "Generator_loss")
ut.plot_and_save(D_losses, "Discriminator Loss during training",
c.save_results, "Discriminator_loss")
ut.plot_and_save(Wasserstein_D, "Wasserstein distance during training",
c.save_results, "Wasserstein distance")
epoch_end_time = time.time()
duration = duration + (epoch_end_time - epoch_start_time)
approx_time_to_finish = duration / (epoch + 1) * (c.num_epochs
- (epoch + 1))
print("Training time for epoch ", epoch, ": ", (epoch_end_time
- epoch_start_time) / 60,
" minutes = ", (epoch_end_time - epoch_start_time) / 3600, "hours.")
print("Approximate time remaining for run to finish: ",
approx_time_to_finish / 3600, " hours")
del errD_iter
del errG_iter
del Wasserstein_D_iter
del avg_errD
del avg_errG
del avg_Wasserstein_D
del fixed_fake
del sample