forked from Project-MONAI/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_autoencoder.py
285 lines (247 loc) · 11.9 KB
/
train_autoencoder.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
# Copyright (c) MONAI Consortium
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import json
import logging
import os
import sys
from pathlib import Path
import torch
from generative.losses import PatchAdversarialLoss, PerceptualLoss
from generative.networks.nets import PatchDiscriminator
from monai.config import print_config
from monai.utils import set_determinism
from torch.nn import L1Loss, MSELoss
from torch.nn.parallel import DistributedDataParallel as DDP
from torch.utils.tensorboard import SummaryWriter
from utils import KL_loss, define_instance, prepare_dataloader, setup_ddp
from visualize_image import visualize_one_slice_in_3d_image
def main():
parser = argparse.ArgumentParser(description="PyTorch VAE-GAN training")
parser.add_argument(
"-e",
"--environment-file",
default="./config/environment.json",
help="environment json file that stores environment path",
)
parser.add_argument(
"-c",
"--config-file",
default="./config/config_train_32g.json",
help="config json file that stores hyper-parameters",
)
parser.add_argument("-g", "--gpus", default=1, type=int, help="number of gpus per node")
args = parser.parse_args()
# Step 0: configuration
ddp_bool = args.gpus > 1 # whether to use distributed data parallel
if ddp_bool:
rank = int(os.environ["LOCAL_RANK"])
world_size = int(os.environ["WORLD_SIZE"])
dist, device = setup_ddp(rank, world_size)
else:
rank = 0
world_size = 1
device = 0
torch.cuda.set_device(device)
print(f"Using {device}")
print_config()
torch.backends.cudnn.benchmark = True
torch.set_num_threads(4)
torch.autograd.set_detect_anomaly(True)
env_dict = json.load(open(args.environment_file, "r"))
config_dict = json.load(open(args.config_file, "r"))
for k, v in env_dict.items():
setattr(args, k, v)
for k, v in config_dict.items():
setattr(args, k, v)
set_determinism(42)
# Step 1: set data loader
size_divisible = 2 ** (len(args.autoencoder_def["num_channels"]) - 1)
train_loader, val_loader = prepare_dataloader(
args,
args.autoencoder_train["batch_size"],
args.autoencoder_train["patch_size"],
randcrop=True,
rank=rank,
world_size=world_size,
cache=1.0,
download=False,
size_divisible=size_divisible,
amp=False,
)
# Step 2: Define Autoencoder KL network and discriminator
autoencoder = define_instance(args, "autoencoder_def").to(device)
discriminator_norm = "INSTANCE"
discriminator = PatchDiscriminator(
spatial_dims=args.spatial_dims,
num_layers_d=3,
num_channels=32,
in_channels=1,
out_channels=1,
norm=discriminator_norm,
).to(device)
if ddp_bool:
# When using DDP, BatchNorm needs to be converted to SyncBatchNorm.
discriminator = torch.nn.SyncBatchNorm.convert_sync_batchnorm(discriminator)
trained_g_path = os.path.join(args.model_dir, "autoencoder.pt")
trained_d_path = os.path.join(args.model_dir, "discriminator.pt")
trained_g_path_last = os.path.join(args.model_dir, "autoencoder_last.pt")
trained_d_path_last = os.path.join(args.model_dir, "discriminator_last.pt")
if rank == 0:
Path(args.model_dir).mkdir(parents=True, exist_ok=True)
if args.resume_ckpt:
map_location = {"cuda:%d" % 0: "cuda:%d" % rank}
try:
autoencoder.load_state_dict(torch.load(trained_g_path, map_location=map_location))
print(f"Rank {rank}: Load trained autoencoder from {trained_g_path}")
except:
print(f"Rank {rank}: Train autoencoder from scratch.")
try:
discriminator.load_state_dict(torch.load(trained_d_path, map_location=map_location))
print(f"Rank {rank}: Load trained discriminator from {trained_d_path}")
except:
print(f"Rank {rank}: Train discriminator from scratch.")
if ddp_bool:
autoencoder = DDP(autoencoder, device_ids=[device], output_device=rank, find_unused_parameters=True)
discriminator = DDP(discriminator, device_ids=[device], output_device=rank, find_unused_parameters=True)
# Step 3: training config
if "recon_loss" in args.autoencoder_train and args.autoencoder_train["recon_loss"] == "l2":
intensity_loss = MSELoss()
if rank == 0:
print("Use l2 loss")
else:
intensity_loss = L1Loss()
if rank == 0:
print("Use l1 loss")
adv_loss = PatchAdversarialLoss(criterion="least_squares")
loss_perceptual = PerceptualLoss(spatial_dims=3, network_type="squeeze", is_fake_3d=True, fake_3d_ratio=0.2)
loss_perceptual.to(device)
adv_weight = 0.01
perceptual_weight = args.autoencoder_train["perceptual_weight"]
# kl_weight: important hyper-parameter.
# If too large, decoder cannot recon good results from latent space.
# If too small, latent space will not be regularized enough for the diffusion model
kl_weight = args.autoencoder_train["kl_weight"]
optimizer_g = torch.optim.Adam(params=autoencoder.parameters(), lr=args.autoencoder_train["lr"] * world_size)
optimizer_d = torch.optim.Adam(params=discriminator.parameters(), lr=args.autoencoder_train["lr"] * world_size)
# initialize tensorboard writer
if rank == 0:
Path(args.tfevent_path).mkdir(parents=True, exist_ok=True)
tensorboard_path = os.path.join(args.tfevent_path, "autoencoder")
Path(tensorboard_path).mkdir(parents=True, exist_ok=True)
tensorboard_writer = SummaryWriter(tensorboard_path)
# Step 4: training
autoencoder_warm_up_n_epochs = 5
n_epochs = args.autoencoder_train["n_epochs"]
val_interval = args.autoencoder_train["val_interval"]
intermediary_images = []
n_example_images = 4
best_val_recon_epoch_loss = 100.0
total_step = 0
for epoch in range(n_epochs):
# train
autoencoder.train()
discriminator.train()
if ddp_bool:
# if ddp, distribute data across n gpus
train_loader.sampler.set_epoch(epoch)
val_loader.sampler.set_epoch(epoch)
for step, batch in enumerate(train_loader):
images = batch["image"].to(device)
# train Generator part
optimizer_g.zero_grad(set_to_none=True)
reconstruction, z_mu, z_sigma = autoencoder(images)
recons_loss = intensity_loss(reconstruction, images)
kl_loss = KL_loss(z_mu, z_sigma)
p_loss = loss_perceptual(reconstruction.float(), images.float())
loss_g = recons_loss + kl_weight * kl_loss + perceptual_weight * p_loss
if epoch > autoencoder_warm_up_n_epochs:
logits_fake = discriminator(reconstruction.contiguous().float())[-1]
generator_loss = adv_loss(logits_fake, target_is_real=True, for_discriminator=False)
loss_g = loss_g + adv_weight * generator_loss
loss_g.backward()
optimizer_g.step()
if epoch > autoencoder_warm_up_n_epochs:
# train Discriminator part
optimizer_d.zero_grad(set_to_none=True)
logits_fake = discriminator(reconstruction.contiguous().detach())[-1]
loss_d_fake = adv_loss(logits_fake, target_is_real=False, for_discriminator=True)
logits_real = discriminator(images.contiguous().detach())[-1]
loss_d_real = adv_loss(logits_real, target_is_real=True, for_discriminator=True)
discriminator_loss = (loss_d_fake + loss_d_real) * 0.5
loss_d = adv_weight * discriminator_loss
loss_d.backward()
optimizer_d.step()
# write train loss for each batch into tensorboard
if rank == 0:
total_step += 1
tensorboard_writer.add_scalar("train_recon_loss_iter", recons_loss, total_step)
tensorboard_writer.add_scalar("train_kl_loss_iter", kl_loss, total_step)
tensorboard_writer.add_scalar("train_perceptual_loss_iter", p_loss, total_step)
if epoch > autoencoder_warm_up_n_epochs:
tensorboard_writer.add_scalar("train_adv_loss_iter", generator_loss, total_step)
tensorboard_writer.add_scalar("train_fake_loss_iter", loss_d_fake, total_step)
tensorboard_writer.add_scalar("train_real_loss_iter", loss_d_real, total_step)
# validation
if epoch % val_interval == 0:
autoencoder.eval()
val_recon_epoch_loss = 0
for step, batch in enumerate(val_loader):
images = batch["image"].to(device) # choose only one of Brats channels
with torch.no_grad():
reconstruction, z_mu, z_sigma = autoencoder(images)
recons_loss = intensity_loss(
reconstruction.float(), images.float()
) + perceptual_weight * loss_perceptual(reconstruction.float(), images.float())
val_recon_epoch_loss += recons_loss.item()
val_recon_epoch_loss = val_recon_epoch_loss / (step + 1)
if rank == 0:
# save last model
print(f"Epoch {epoch} val_recon_loss: {val_recon_epoch_loss}")
if ddp_bool:
torch.save(autoencoder.module.state_dict(), trained_g_path_last)
torch.save(discriminator.module.state_dict(), trained_d_path_last)
else:
torch.save(autoencoder.state_dict(), trained_g_path_last)
torch.save(discriminator.state_dict(), trained_d_path_last)
# save best model
if val_recon_epoch_loss < best_val_recon_epoch_loss and rank == 0:
best_val_recon_epoch_loss = val_recon_epoch_loss
if ddp_bool:
torch.save(autoencoder.module.state_dict(), trained_g_path)
torch.save(discriminator.module.state_dict(), trained_d_path)
else:
torch.save(autoencoder.state_dict(), trained_g_path)
torch.save(discriminator.state_dict(), trained_d_path)
print("Got best val recon loss.")
print("Save trained autoencoder to", trained_g_path)
print("Save trained discriminator to", trained_d_path)
# write val loss for each epoch into tensorboard
tensorboard_writer.add_scalar("val_recon_loss", val_recon_epoch_loss, epoch)
for axis in range(3):
tensorboard_writer.add_image(
"val_img_" + str(axis),
visualize_one_slice_in_3d_image(images[0, 0, ...], axis).transpose([2, 1, 0]),
epoch,
)
tensorboard_writer.add_image(
"val_recon_" + str(axis),
visualize_one_slice_in_3d_image(reconstruction[0, 0, ...], axis).transpose([2, 1, 0]),
epoch,
)
if __name__ == "__main__":
logging.basicConfig(
stream=sys.stdout,
level=logging.INFO,
format="[%(asctime)s.%(msecs)03d][%(levelname)5s](%(name)s) - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
main()