-
Notifications
You must be signed in to change notification settings - Fork 1
/
DPGAN_train.py
312 lines (237 loc) · 10.4 KB
/
DPGAN_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
import numpy as np
import matplotlib.pyplot as plt
import time
import glob
import os
import gc
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 torchcsprng as prng
from opacus import PrivacyEngine
from opacus.utils.module_modification import convert_batchnorm_modules
from opacus.utils.uniform_sampler import UniformWithReplacementSampler
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])
# workaround for a unhelpful cudann error
torch.set_default_tensor_type("torch.cuda.FloatTensor")
# 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,
num_workers=c.workers,
batch_sampler=UniformWithReplacementSampler(
num_samples=len(dataset),
sample_rate=c.batch_size / len(dataset))
)
# 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)
# Batchnorm which holds information specific to batch
# is not used in differential privacy to avoid information leak
netD = convert_batchnorm_modules(netD)
netG = convert_batchnorm_modules(netG)
# 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))
# Attaching privacy engine to critic optimizer
privacy_engine = PrivacyEngine(netD, sample_rate=c.batch_size / c.num_images,
alphas=c.alphas, noise_multiplier=c.noise_m,
max_grad_norm=c.max_norm_dp, secure_rng=c.secure_rng,
target_delta=c.delta)
privacy_engine.attach(optimizerD)
torch.set_default_tensor_type("torch.FloatTensor")
epsilon, best_alpha = optimizerD.privacy_engine.get_privacy_spent(c.delta)
print("(epsilon = %.2f, delta = %.2f) for alpha = %.2f"
% (epsilon, c.delta, best_alpha))
torch.set_default_tensor_type("torch.cuda.FloatTensor")
# 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)
# Lists to keep track of progress
G_losses = []
D_losses = []
Wasserstein_D = []
start_epoch = 0
epsilons = []
iters = 0
duration = 0
# Training Loop
print("Starting Training Loop...")
# For each epoch
for epoch in range(start_epoch, c.num_epochs):
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, 0):
# 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 clipping weights
# ######################### #
# updating the critic n_disc number of times
# before 1 update of generator - with 3D this is usually 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)
# Train with real batch
errD_real = netD(real_cpu)
# Calculate loss on all-real batch
errD_real = -errD_real.view(-1).mean()
errD_real.backward()
optimizerD.step()
# 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()
errD_fake.backward()
optimizerD.step()
errD = errD_fake.item() + errD_real.item()
# Wasserstein GAN Lipschitz continuity - Arjovsky et al. 2017
if c.add_clip:
for parameter in netD.parameters():
parameter.data.clamp_(-c.clip_param_W,
c.clip_param_W)
errD_disc_iter.append(errD)
gc.collect()
errD_disc_avg = np.mean(np.array(errD_disc_iter))
############################
# (2) Update G network
###########################
netG.zero_grad()
noise = torch.randn(b_size, c.nz, 1, 1, 1, device=device)
fake = netG(noise)
output_fake = netD(fake)
errG = -output_fake.view(-1).mean()
# Calculate gradients for G
errG.backward()
optimizerG.step()
# update the iteration errors
errD_iter.append(errD_disc_avg)
errG_iter.append(errG.item())
iters += 1
gc.collect()
# print after every 100 batches
if i % 100 == 0:
print("[%d/%d] batches done!\n" % (i + 1, 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 + 1, "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))
print('[%d/%d]\tLoss_D: %.4f\tLoss_G: %.4f'
% (epoch, c.num_epochs, avg_errD, avg_errG))
# Save Losses and outputs for plotting later
G_losses.append(avg_errG.item())
D_losses.append(avg_errD.item())
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))
# save epsilon values for each epoch
torch.set_default_tensor_type("torch.FloatTensor")
epsilon, best_alpha = optimizerD.privacy_engine.get_privacy_spent(c.delta)
print(
"(epsilon = %.2f, delta = %.2f) for alpha = %.2f"
% (epsilon, c.delta, best_alpha)
)
epsilons.append(epsilon)
torch.set_default_tensor_type("torch.cuda.FloatTensor")
np.save(c.save_results + "epsilons.npy", np.asarray(epsilons))
# 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 + "fixed_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 +
"fixed_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+1) % 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(),
}, 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(epsilons, "Epsilon over epochs",
c.save_results, "Epsilons")
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")
gc.collect()