forked from elvisyjlin/AttGAN-PyTorch
-
Notifications
You must be signed in to change notification settings - Fork 2
/
train.py
193 lines (168 loc) · 9.68 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
# Copyright (C) 2018 Elvis Yu-Jing Lin <[email protected]>
#
# This work is licensed under the MIT License. To view a copy of this license,
# visit https://opensource.org/licenses/MIT.
"""Main entry point for training AttGAN network."""
import argparse
import datetime
import json
import os
from os.path import join
import torch.utils.data as data
import torch
import torchvision.utils as vutils
from attgan import AttGAN
from data import check_attribute_conflict
from helpers import Progressbar, add_scalar_dict
from tensorboardX import SummaryWriter
attrs_default = [
'Bald', 'Bangs', 'Black_Hair', 'Blond_Hair', 'Brown_Hair', 'Bushy_Eyebrows',
'Eyeglasses', 'Male', 'Mouth_Slightly_Open', 'Mustache', 'No_Beard', 'Pale_Skin', 'Young'
]
def parse(args=None):
parser = argparse.ArgumentParser()
parser.add_argument('--attrs', dest='attrs', default=attrs_default, nargs='+', help='attributes to learn')
parser.add_argument('--data', dest='data', type=str, choices=['CelebA', 'CelebA-HQ'], default='CelebA')
parser.add_argument('--data_path', dest='data_path', type=str, default='data/img_align_celeba')
parser.add_argument('--attr_path', dest='attr_path', type=str, default='data/list_attr_celeba.txt')
parser.add_argument('--image_list_path', dest='image_list_path', type=str, default='data/image_list.txt')
parser.add_argument('--img_size', dest='img_size', type=int, default=128)
parser.add_argument('--shortcut_layers', dest='shortcut_layers', type=int, default=1)
parser.add_argument('--inject_layers', dest='inject_layers', type=int, default=0)
parser.add_argument('--enc_dim', dest='enc_dim', type=int, default=64)
parser.add_argument('--dec_dim', dest='dec_dim', type=int, default=64)
parser.add_argument('--dis_dim', dest='dis_dim', type=int, default=64)
parser.add_argument('--dis_fc_dim', dest='dis_fc_dim', type=int, default=1024)
parser.add_argument('--enc_layers', dest='enc_layers', type=int, default=5)
parser.add_argument('--dec_layers', dest='dec_layers', type=int, default=5)
parser.add_argument('--dis_layers', dest='dis_layers', type=int, default=5)
parser.add_argument('--enc_norm', dest='enc_norm', type=str, default='batchnorm')
parser.add_argument('--dec_norm', dest='dec_norm', type=str, default='batchnorm')
parser.add_argument('--dis_norm', dest='dis_norm', type=str, default='instancenorm')
parser.add_argument('--dis_fc_norm', dest='dis_fc_norm', type=str, default='none')
parser.add_argument('--enc_acti', dest='enc_acti', type=str, default='lrelu')
parser.add_argument('--dec_acti', dest='dec_acti', type=str, default='relu')
parser.add_argument('--dis_acti', dest='dis_acti', type=str, default='lrelu')
parser.add_argument('--dis_fc_acti', dest='dis_fc_acti', type=str, default='relu')
parser.add_argument('--lambda_1', dest='lambda_1', type=float, default=100.0)
parser.add_argument('--lambda_2', dest='lambda_2', type=float, default=10.0)
parser.add_argument('--lambda_3', dest='lambda_3', type=float, default=1.0)
parser.add_argument('--lambda_gp', dest='lambda_gp', type=float, default=10.0)
parser.add_argument('--mode', dest='mode', default='wgan', choices=['wgan', 'lsgan', 'dcgan'])
parser.add_argument('--epochs', dest='epochs', type=int, default=200, help='# of epochs')
parser.add_argument('--batch_size', dest='batch_size', type=int, default=32)
parser.add_argument('--num_workers', dest='num_workers', type=int, default=0)
parser.add_argument('--lr', dest='lr', type=float, default=0.0002, help='learning rate')
parser.add_argument('--beta1', dest='beta1', type=float, default=0.5)
parser.add_argument('--beta2', dest='beta2', type=float, default=0.999)
parser.add_argument('--n_d', dest='n_d', type=int, default=5, help='# of d updates per g update')
parser.add_argument('--b_distribution', dest='b_distribution', default='none', choices=['none', 'uniform', 'truncated_normal'])
parser.add_argument('--thres_int', dest='thres_int', type=float, default=0.5)
parser.add_argument('--test_int', dest='test_int', type=float, default=1.0)
parser.add_argument('--n_samples', dest='n_samples', type=int, default=16, help='# of sample images')
parser.add_argument('--save_interval', dest='save_interval', type=int, default=1000)
parser.add_argument('--sample_interval', dest='sample_interval', type=int, default=1000)
parser.add_argument('--gpu', dest='gpu', action='store_true')
parser.add_argument('--multi_gpu', dest='multi_gpu', action='store_true')
parser.add_argument('--experiment_name', dest='experiment_name', default=datetime.datetime.now().strftime("%I:%M%p on %B %d, %Y"))
return parser.parse_args(args)
args = parse()
print(args)
args.lr_base = args.lr
args.n_attrs = len(args.attrs)
args.betas = (args.beta1, args.beta2)
os.makedirs(join('output', args.experiment_name), exist_ok=True)
os.makedirs(join('output', args.experiment_name, 'checkpoint'), exist_ok=True)
os.makedirs(join('output', args.experiment_name, 'sample_training'), exist_ok=True)
with open(join('output', args.experiment_name, 'setting.txt'), 'w') as f:
f.write(json.dumps(vars(args), indent=4, separators=(',', ':')))
if args.data == 'CelebA':
from data import CelebA
train_dataset = CelebA(args.data_path, args.attr_path, args.img_size, 'train', args.attrs)
valid_dataset = CelebA(args.data_path, args.attr_path, args.img_size, 'valid', args.attrs)
if args.data == 'CelebA-HQ':
from data import CelebA_HQ
train_dataset = CelebA_HQ(args.data_path, args.attr_path, args.image_list_path, args.img_size, 'train', args.attrs)
valid_dataset = CelebA_HQ(args.data_path, args.attr_path, args.image_list_path, args.img_size, 'valid', args.attrs)
train_dataloader = data.DataLoader(
train_dataset, batch_size=args.batch_size, num_workers=args.num_workers,
shuffle=True, drop_last=True
)
valid_dataloader = data.DataLoader(
valid_dataset, batch_size=args.n_samples, num_workers=args.num_workers,
shuffle=False, drop_last=False
)
print('Training images:', len(train_dataset), '/', 'Validating images:', len(valid_dataset))
attgan = AttGAN(args)
progressbar = Progressbar()
writer = SummaryWriter(join('output', args.experiment_name, 'summary'))
fixed_img_a, fixed_att_a = next(iter(valid_dataloader))
fixed_img_a = fixed_img_a.cuda() if args.gpu else fixed_img_a
fixed_att_a = fixed_att_a.cuda() if args.gpu else fixed_att_a
fixed_att_a = fixed_att_a.type(torch.float)
sample_att_b_list = [fixed_att_a]
for i in range(args.n_attrs):
tmp = fixed_att_a.clone()
tmp[:, i] = 1 - tmp[:, i]
tmp = check_attribute_conflict(tmp, args.attrs[i], args.attrs)
sample_att_b_list.append(tmp)
it = 0
it_per_epoch = len(train_dataset) // args.batch_size
for epoch in range(args.epochs):
# train with base lr in the first 100 epochs
# and half the lr in the last 100 epochs
lr = args.lr_base / (10 ** (epoch // 100))
attgan.set_lr(lr)
writer.add_scalar('LR/learning_rate', lr, it+1)
for img_a, att_a in progressbar(train_dataloader):
attgan.train()
img_a = img_a.cuda() if args.gpu else img_a
att_a = att_a.cuda() if args.gpu else att_a
idx = torch.randperm(len(att_a))
att_b = att_a[idx].contiguous()
att_a = att_a.type(torch.float)
att_b = att_b.type(torch.float)
att_a_ = (att_a * 2 - 1) * args.thres_int
if args.b_distribution == 'none':
att_b_ = (att_b * 2 - 1) * args.thres_int
if args.b_distribution == 'uniform':
att_b_ = (att_b * 2 - 1) * \
torch.rand_like(att_b) * \
(2 * args.thres_int)
if args.b_distribution == 'truncated_normal':
att_b_ = (att_b * 2 - 1) * \
(torch.fmod(torch.randn_like(att_b), 2) + 2) / 4.0 * \
(2 * args.thres_int)
if (it+1) % (args.n_d+1) != 0:
errD = attgan.trainD(img_a, att_a, att_a_, att_b, att_b_)
add_scalar_dict(writer, errD, it+1, 'D')
else:
errG = attgan.trainG(img_a, att_a, att_a_, att_b, att_b_)
add_scalar_dict(writer, errG, it+1, 'G')
progressbar.say(epoch=epoch, iter=it+1, d_loss=errD['d_loss'], g_loss=errG['g_loss'])
if (it+1) % args.save_interval == 0:
# To save storage space, I only checkpoint the weights of G.
# If you'd like to keep weights of G, D, optim_G, optim_D,
# please use save() instead of saveG().
attgan.saveG(os.path.join(
'output', args.experiment_name, 'checkpoint', 'weights.{:d}.pth'.format(epoch)
))
# attgan.save(os.path.join(
# 'output', args.experiment_name, 'checkpoint', 'weights.{:d}.pth'.format(epoch)
# ))
if (it+1) % args.sample_interval == 0:
attgan.eval()
with torch.no_grad():
samples = [fixed_img_a]
for i, att_b in enumerate(sample_att_b_list):
att_b_ = (att_b * 2 - 1) * args.thres_int
if i > 0:
att_b_[..., i - 1] = att_b_[..., i - 1] * args.test_int / args.thres_int
samples.append(attgan.G(fixed_img_a, att_b_))
samples = torch.cat(samples, dim=3)
writer.add_image('sample', vutils.make_grid(samples, nrow=1, normalize=True, range=(-1., 1.)), it+1)
vutils.save_image(samples, os.path.join(
'output', args.experiment_name, 'sample_training',
'Epoch_({:d})_({:d}of{:d}).jpg'.format(epoch, it%it_per_epoch+1, it_per_epoch)
), nrow=1, normalize=True, range=(-1., 1.))
it += 1