forked from LynnHo/CycleGAN-Tensorflow-2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
train.py
241 lines (182 loc) · 10.1 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
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
import functools
import imlib as im
import numpy as np
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 =
# ==============================================================================
py.arg('--dataset', default='horse2zebra')
py.arg('--output_index', default='')
py.arg('--load_size', type=int, default=286) # load image to this size
py.arg('--crop_size', type=int, default=256) # then crop to this size
py.arg('--batch_size', type=int, default=1)
py.arg('--epochs', type=int, default=200)
py.arg('--epoch_decay', type=int, default=100) # epoch to start decaying learning rate
py.arg('--lr', type=float, default=0.0002)
py.arg('--beta_1', type=float, default=0.5)
py.arg('--adversarial_loss_mode', default='lsgan', 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('--cycle_loss_weight', type=float, default=10.0)
py.arg('--identity_loss_weight', type=float, default=0.0)
py.arg('--pool_size', type=int, default=50) # pool size to store fake samples
#model parameters
py.arg('--dim', type=int, default=64)
py.arg('--n_downsamplings', type=int, default = 2)
py.arg('--n_blocks', type=int, default = 9)
py.arg('--norm', type=str, default = 'instance_norm')
py.arg('--augmentation', type=str, default= 'Normal')
args = py.args()
# output_dir
output_dir = py.join('output', args.dataset + ('' if args.output_index == '' else '_'+ str(args.output_index)))
py.mkdir(output_dir)
# save settings
py.args_to_yaml(py.join(output_dir, 'settings.yml'), args)
# ==============================================================================
# = data =
# ==============================================================================
A_img_paths = py.glob(py.join('datasets', args.dataset, 'trainA'), '*.jpg')
B_img_paths = py.glob(py.join('datasets', args.dataset, 'trainB'), '*.jpg')
A_B_dataset, len_dataset = data.make_zip_dataset(A_img_paths, B_img_paths, args.batch_size, args.load_size, args.crop_size, training=True, repeat=False, augmentation_preset = args.augmentation)
A2B_pool = data.ItemPool(args.pool_size)
B2A_pool = data.ItemPool(args.pool_size)
A_img_paths_test = py.glob(py.join('datasets', args.dataset, 'testA'), '*.jpg')
B_img_paths_test = py.glob(py.join('datasets', args.dataset, 'testB'), '*.jpg')
A_B_dataset_test, _ = data.make_zip_dataset(A_img_paths_test, B_img_paths_test, args.batch_size, args.load_size, args.crop_size, training=False, repeat=True, augmentation_preset = args.augmentation)
# ==============================================================================
# = models =
# ==============================================================================
G_downsamplings = args.n_downsamplings
D_downsamplings = min(args.n_downsamplings + 1, 4)
G_A2B = module.ResnetGenerator(input_shape=(args.crop_size, args.crop_size, 3), dim = args.dim, n_downsamplings = G_downsamplings, n_blocks = args.n_blocks, norm = args.norm)
G_B2A = module.ResnetGenerator(input_shape=(args.crop_size, args.crop_size, 3), dim = args.dim, n_downsamplings = G_downsamplings, n_blocks = args.n_blocks, norm = args.norm)
D_A = module.ConvDiscriminator(input_shape=(args.crop_size, args.crop_size, 3), dim = args.dim, n_downsamplings = D_downsamplings, norm = args.norm)
D_B = module.ConvDiscriminator(input_shape=(args.crop_size, args.crop_size, 3), dim = args.dim, n_downsamplings = D_downsamplings, norm = args.norm)
d_loss_fn, g_loss_fn = gan.get_adversarial_losses_fn(args.adversarial_loss_mode)
cycle_loss_fn = tf.losses.MeanAbsoluteError()
identity_loss_fn = tf.losses.MeanAbsoluteError()
G_lr_scheduler = module.LinearDecay(args.lr, args.epochs * len_dataset, args.epoch_decay * len_dataset)
D_lr_scheduler = module.LinearDecay(args.lr, args.epochs * len_dataset, args.epoch_decay * len_dataset)
G_optimizer = keras.optimizers.Adam(learning_rate=G_lr_scheduler, beta_1=args.beta_1)
D_optimizer = keras.optimizers.Adam(learning_rate=D_lr_scheduler, beta_1=args.beta_1)
# ==============================================================================
# = train step =
# ==============================================================================
@tf.function
def train_G(A, B):
with tf.GradientTape() as t:
A2B = G_A2B(A, training=True)
B2A = G_B2A(B, training=True)
A2B2A = G_B2A(A2B, training=True)
B2A2B = G_A2B(B2A, training=True)
A2B_d_logits = D_B(A2B, training=True)
B2A_d_logits = D_A(B2A, training=True)
A2B_g_loss = g_loss_fn(A2B_d_logits)
B2A_g_loss = g_loss_fn(B2A_d_logits)
A2B2A_cycle_loss = cycle_loss_fn(A, A2B2A)
B2A2B_cycle_loss = cycle_loss_fn(B, B2A2B)
if args.identity_loss_weight > 0.0001:
A2B_id_loss = identity_loss_fn(A, A2B)
B2A_id_loss = identity_loss_fn(B, B2A)
G_loss = (A2B_g_loss + B2A_g_loss) + (A2B2A_cycle_loss + B2A2B_cycle_loss) * args.cycle_loss_weight
if args.identity_loss_weight > 0.0001:
G_loss = G_loss + (A2B_id_loss + B2A_id_loss) * args.identity_loss_weight
G_grad = t.gradient(G_loss, G_A2B.trainable_variables + G_B2A.trainable_variables)
G_optimizer.apply_gradients(zip(G_grad, G_A2B.trainable_variables + G_B2A.trainable_variables))
loss_dict = {'A2B_g_loss': A2B_g_loss,
'B2A_g_loss': B2A_g_loss,
'A2B2A_cycle_loss': A2B2A_cycle_loss,
'B2A2B_cycle_loss': B2A2B_cycle_loss}
if args.identity_loss_weight > 0.0001:
loss_dict.update({'A2B_id_loss': A2B_id_loss,
'B2A_id_loss': B2A_id_loss})
return A2B, B2A, {'A2B_g_loss': A2B_g_loss,
'B2A_g_loss': B2A_g_loss,
'A2B2A_cycle_loss': A2B2A_cycle_loss,
'B2A2B_cycle_loss': B2A2B_cycle_loss}
@tf.function
def train_D(A, B, A2B, B2A):
with tf.GradientTape() as t:
A_d_logits = D_A(A, training=True)
B2A_d_logits = D_A(B2A, training=True)
B_d_logits = D_B(B, training=True)
A2B_d_logits = D_B(A2B, training=True)
A_d_loss, B2A_d_loss = d_loss_fn(A_d_logits, B2A_d_logits)
B_d_loss, A2B_d_loss = d_loss_fn(B_d_logits, A2B_d_logits)
D_A_gp = gan.gradient_penalty(functools.partial(D_A, training=True), A, B2A, mode=args.gradient_penalty_mode)
D_B_gp = gan.gradient_penalty(functools.partial(D_B, training=True), B, A2B, mode=args.gradient_penalty_mode)
D_loss = (A_d_loss + B2A_d_loss) + (B_d_loss + A2B_d_loss) + (D_A_gp + D_B_gp) * args.gradient_penalty_weight
D_grad = t.gradient(D_loss, D_A.trainable_variables + D_B.trainable_variables)
D_optimizer.apply_gradients(zip(D_grad, D_A.trainable_variables + D_B.trainable_variables))
return {'A_d_loss': A_d_loss + B2A_d_loss,
'B_d_loss': B_d_loss + A2B_d_loss,
'D_A_gp': D_A_gp,
'D_B_gp': D_B_gp}
def train_step(A, B):
A2B, B2A, G_loss_dict = train_G(A, B)
# cannot autograph `A2B_pool`
A2B = A2B_pool(A2B) # or A2B = A2B_pool(A2B.numpy()), but it is much slower
B2A = B2A_pool(B2A) # because of the communication between CPU and GPU
D_loss_dict = train_D(A, B, A2B, B2A)
return G_loss_dict, D_loss_dict
@tf.function
def sample(A, B):
A2B = G_A2B(A, training=False)
B2A = G_B2A(B, training=False)
A2B2A = G_B2A(A2B, training=False)
B2A2B = G_A2B(B2A, training=False)
return A2B, B2A, A2B2A, B2A2B
# ==============================================================================
# = run =
# ==============================================================================
# epoch counter
ep_cnt = tf.Variable(initial_value=0, trainable=False, dtype=tf.int64)
# checkpoint
checkpoint = tl.Checkpoint(dict(G_A2B=G_A2B,
G_B2A=G_B2A,
D_A=D_A,
D_B=D_B,
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
test_iter = iter(A_B_dataset_test)
sample_dir = py.join(output_dir, 'samples_training')
py.mkdir(sample_dir)
# main loop
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 A, B in tqdm.tqdm(A_B_dataset, desc='Inner Epoch Loop', total=len_dataset):
G_loss_dict, D_loss_dict = train_step(A, B)
# # summary
tl.summary(G_loss_dict, step=G_optimizer.iterations, name='G_losses')
tl.summary(D_loss_dict, step=G_optimizer.iterations, name='D_losses')
tl.summary({'learning rate': G_lr_scheduler.current_learning_rate}, step=G_optimizer.iterations, name='learning rate')
# sample
if G_optimizer.iterations.numpy() % 100 == 0:
A, B = next(test_iter)
A2B, B2A, A2B2A, B2A2B = sample(A, B)
img = im.immerge(np.concatenate([A, A2B, A2B2A, B, B2A, B2A2B], axis=0), n_rows=2)
im.imwrite(img, py.join(sample_dir, 'iter-%09d.jpg' % G_optimizer.iterations.numpy()))
# save checkpoint
checkpoint.save(ep)