-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
165 lines (141 loc) · 5.21 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
import argparse
import json
from datetime import datetime
from pathlib import Path
import matplotlib
import matplotlib.pyplot as plt
import torch
from data.dataset import ShapeNetDataModule, get_data_iterator, tensor_to_pil_image
from dotmap import DotMap
from model import DiffusionModule
from models.network import UNet
from pytorch_lightning import seed_everything
from scheduler import DDPMScheduler
from torchvision.transforms.functional import to_pil_image
from tqdm import tqdm
matplotlib.use("Agg")
def get_current_time():
now = datetime.now().strftime("%m-%d-%H%M%S")
return now
def main(args):
"""config"""
config = DotMap()
config.update(vars(args))
config.device = f"cuda:{args.gpu}"
now = get_current_time()
if args.use_cfg:
save_dir = Path(f"results/cfg_diffusion-{args.sample_method}-{now}")
else:
save_dir = Path(f"results/diffusion-{args.sample_method}-{now}")
save_dir.mkdir(exist_ok=True, parents=True)
print(f"save_dir: {save_dir}")
seed_everything(config.seed)
with open(save_dir / "config.json", "w") as f:
json.dump(config, f, indent=2)
"""######"""
image_resolution = 64
ds_module = ShapeNetDataModule(
"./data",
target_categories=config.target_categories,
batch_size=config.batch_size,
num_workers=config.num_workers,
max_num_images_per_cat=config.max_num_images_per_cat,
image_resolution=image_resolution
)
train_dl = ds_module.train_dataloader()
train_it = get_data_iterator(train_dl)
# Set up the scheduler
var_scheduler = DDPMScheduler(
config.num_diffusion_train_timesteps,
beta_1=config.beta_1,
beta_T=config.beta_T,
mode="linear",
)
network = UNet(
T=config.num_diffusion_train_timesteps,
image_resolution=image_resolution,
ch=128,
ch_mult=[1, 2, 2, 2],
attn=[1],
num_res_blocks=4,
dropout=0.1,
use_cfg=args.use_cfg,
cfg_dropout=args.cfg_dropout,
num_classes=getattr(ds_module, "num_classes", None),
)
ddpm = DiffusionModule(network, var_scheduler)
ddpm = ddpm.to(config.device)
optimizer = torch.optim.Adam(ddpm.network.parameters(), lr=2e-4)
scheduler = torch.optim.lr_scheduler.LambdaLR(
optimizer, lr_lambda=lambda t: min((t + 1) / config.warmup_steps, 1.0)
)
step = 0
losses = []
with tqdm(initial=step, total=config.train_num_steps) as pbar:
while step < config.train_num_steps:
if step % config.log_interval == 0:
ddpm.eval()
plt.plot(losses)
plt.savefig(f"{save_dir}/loss.png")
plt.close()
if args.use_cfg: # Conditional, CFG training
samples = ddpm.sample(
4,
class_label=torch.randint(1, 4, (4,)).to(config.device),
return_traj=False,
)
else: # Unconditional training
samples = ddpm.sample(4, return_traj=False)
pil_images = tensor_to_pil_image(samples)
for i, img in enumerate(pil_images):
img.save(save_dir / f"step={step}-{i}.png")
ddpm.save(f"{save_dir}/last.ckpt")
ddpm.train()
img, label = next(train_it)
img, label = img.to(config.device), label.to(config.device)
if args.use_cfg: # Conditional, CFG training
loss = ddpm.get_loss(img, class_label=label)
else: # Unconditional training
loss = ddpm.get_loss(img)
pbar.set_description(f"Loss: {loss.item():.4f}")
optimizer.zero_grad()
loss.backward()
optimizer.step()
scheduler.step()
losses.append(loss.item())
step += 1
pbar.update(1)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--gpu", type=int, default=0)
parser.add_argument("--batch_size", type=int, default=16)
parser.add_argument("--num_workers", type=int, default=4)
parser.add_argument("--target_categories", type=str)
parser.add_argument(
"--train_num_steps",
type=int,
default=100000,
help="the number of model training steps.",
)
parser.add_argument("--warmup_steps", type=int, default=200)
parser.add_argument("--log_interval", type=int, default=200)
parser.add_argument(
"--max_num_images_per_cat",
type=int,
default=3000,
help="max number of images per category for AFHQ dataset",
)
parser.add_argument(
"--num_diffusion_train_timesteps",
type=int,
default=1000,
help="diffusion Markov chain num steps",
)
parser.add_argument("--beta_1", type=float, default=1e-4)
parser.add_argument("--beta_T", type=float, default=0.02)
parser.add_argument("--seed", type=int, default=63)
parser.add_argument("--sample_method", type=str, default="ddpm")
parser.add_argument("--use_cfg", action="store_true")
parser.add_argument("--cfg_dropout", type=float, default=0.1)
args = parser.parse_args()
main(args)