-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathtrain.py
199 lines (154 loc) · 7.66 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
import functools
import imlib as im
import pylib as py
import tensorflow as tf
import tensorflow.keras as keras
import tf2lib as tl
import tf2gan as gan
import tqdm
import data
import module
# ==============================================================================
# = param =
# ==============================================================================
# command line
py.arg('--dataset', default='fashion_mnist', choices=['cifar10', 'fashion_mnist', 'mnist', 'celeba', 'anime', 'custom'])
py.arg('--batch_size', type=int, default=64)
py.arg('--epochs', type=int, default=25)
py.arg('--lr', type=float, default=0.0002)
py.arg('--beta_1', type=float, default=0.5)
py.arg('--n_d', type=int, default=1) # # d updates per g update
py.arg('--z_dim', type=int, default=128)
py.arg('--adversarial_loss_mode', default='gan', choices=['gan', 'hinge_v1', 'hinge_v2', 'lsgan', 'wgan'])
py.arg('--gradient_penalty_mode', default='none', choices=['none', 'dragan', 'wgan-gp'])
py.arg('--gradient_penalty_weight', type=float, default=10.0)
py.arg('--experiment_name', default='none')
args = py.args()
# output_dir
if args.experiment_name == 'none':
args.experiment_name = '%s_%s' % (args.dataset, args.adversarial_loss_mode)
if args.gradient_penalty_mode != 'none':
args.experiment_name += '_%s' % args.gradient_penalty_mode
output_dir = py.join('output', args.experiment_name)
py.mkdir(output_dir)
# save settings
py.args_to_yaml(py.join(output_dir, 'settings.yml'), args)
# ==============================================================================
# = data =
# ==============================================================================
# setup dataset
if args.dataset in ['cifar10', 'fashion_mnist', 'mnist']: # 32x32
dataset, shape, len_dataset = data.make_32x32_dataset(args.dataset, args.batch_size)
n_G_upsamplings = n_D_downsamplings = 3
elif args.dataset == 'celeba': # 64x64
img_paths = py.glob('data/img_align_celeba', '*.jpg')
dataset, shape, len_dataset = data.make_celeba_dataset(img_paths, args.batch_size)
n_G_upsamplings = n_D_downsamplings = 4
elif args.dataset == 'anime': # 64x64
img_paths = py.glob('data/faces', '*.jpg')
dataset, shape, len_dataset = data.make_anime_dataset(img_paths, args.batch_size)
n_G_upsamplings = n_D_downsamplings = 4
elif args.dataset == 'custom':
# ======================================
# = custom =
# ======================================
img_paths = ... # image paths of custom dataset
dataset, shape, len_dataset = data.make_custom_dataset(img_paths, args.batch_size)
n_G_upsamplings = n_D_downsamplings = ... # 3 for 32x32 and 4 for 64x64
# ======================================
# = custom =
# ======================================
# ==============================================================================
# = model =
# ==============================================================================
# setup the normalization function for discriminator
if args.gradient_penalty_mode == 'none':
d_norm = 'batch_norm'
elif args.gradient_penalty_mode in ['dragan', 'wgan-gp']: # cannot use batch normalization with gradient penalty
# TODO(Lynn)
# Layer normalization is more stable than instance normalization here,
# but instance normalization works in other implementations.
# Please tell me if you find out the cause.
d_norm = 'layer_norm'
# networks
G = module.ConvGenerator(input_shape=(1, 1, args.z_dim), output_channels=shape[-1], n_upsamplings=n_G_upsamplings, name='G_%s' % args.dataset)
D = module.ConvDiscriminator(input_shape=shape, n_downsamplings=n_D_downsamplings, norm=d_norm, name='D_%s' % args.dataset)
G.summary()
D.summary()
# adversarial_loss_functions
d_loss_fn, g_loss_fn = gan.get_adversarial_losses_fn(args.adversarial_loss_mode)
G_optimizer = keras.optimizers.Adam(learning_rate=args.lr, beta_1=args.beta_1)
D_optimizer = keras.optimizers.Adam(learning_rate=args.lr, beta_1=args.beta_1)
# ==============================================================================
# = train step =
# ==============================================================================
@tf.function
def train_G():
with tf.GradientTape() as t:
z = tf.random.normal(shape=(args.batch_size, 1, 1, args.z_dim))
x_fake = G(z, training=True)
x_fake_d_logit = D(x_fake, training=True)
G_loss = g_loss_fn(x_fake_d_logit)
G_grad = t.gradient(G_loss, G.trainable_variables)
G_optimizer.apply_gradients(zip(G_grad, G.trainable_variables))
return {'g_loss': G_loss}
@tf.function
def train_D(x_real):
with tf.GradientTape() as t:
z = tf.random.normal(shape=(args.batch_size, 1, 1, args.z_dim))
x_fake = G(z, training=True)
x_real_d_logit = D(x_real, training=True)
x_fake_d_logit = D(x_fake, training=True)
x_real_d_loss, x_fake_d_loss = d_loss_fn(x_real_d_logit, x_fake_d_logit)
gp = gan.gradient_penalty(functools.partial(D, training=True), x_real, x_fake, mode=args.gradient_penalty_mode)
D_loss = (x_real_d_loss + x_fake_d_loss) + gp * args.gradient_penalty_weight
D_grad = t.gradient(D_loss, D.trainable_variables)
D_optimizer.apply_gradients(zip(D_grad, D.trainable_variables))
return {'d_loss': x_real_d_loss + x_fake_d_loss, 'gp': gp}
@tf.function
def sample(z):
return G(z, training=False)
# ==============================================================================
# = run =
# ==============================================================================
# epoch counter
ep_cnt = tf.Variable(initial_value=0, trainable=False, dtype=tf.int64)
# checkpoint
checkpoint = tl.Checkpoint(dict(G=G,
D=D,
G_optimizer=G_optimizer,
D_optimizer=D_optimizer,
ep_cnt=ep_cnt),
py.join(output_dir, 'checkpoints'),
max_to_keep=5)
try: # restore checkpoint including the epoch counter
checkpoint.restore().assert_existing_objects_matched()
except Exception as e:
print(e)
# summary
train_summary_writer = tf.summary.create_file_writer(py.join(output_dir, 'summaries', 'train'))
# sample
sample_dir = py.join(output_dir, 'samples_training')
py.mkdir(sample_dir)
# main loop
z = tf.random.normal((100, 1, 1, args.z_dim)) # a fixed noise for sampling
with train_summary_writer.as_default():
for ep in tqdm.trange(args.epochs, desc='Epoch Loop'):
if ep < ep_cnt:
continue
# update epoch counter
ep_cnt.assign_add(1)
# train for an epoch
for x_real in tqdm.tqdm(dataset, desc='Inner Epoch Loop', total=len_dataset):
D_loss_dict = train_D(x_real)
tl.summary(D_loss_dict, step=D_optimizer.iterations, name='D_losses')
if D_optimizer.iterations.numpy() % args.n_d == 0:
G_loss_dict = train_G()
tl.summary(G_loss_dict, step=G_optimizer.iterations, name='G_losses')
# sample
if G_optimizer.iterations.numpy() % 100 == 0:
x_fake = sample(z)
img = im.immerge(x_fake, n_rows=10).squeeze()
im.imwrite(img, py.join(sample_dir, 'iter-%09d.jpg' % G_optimizer.iterations.numpy()))
# save checkpoint
checkpoint.save(ep)