forked from kozistr/Awesome-GANs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsagan_train.py
161 lines (126 loc) · 5.71 KB
/
sagan_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
from __future__ import absolute_import
from __future__ import print_function
from __future__ import division
import tensorflow as tf
import numpy as np
import sys
import time
import sagan_model as sagan
sys.path.append('../')
import image_utils as iu
from datasets import DataIterator
from datasets import CelebADataSet as DataSet
results = {
'output': './gen_img/',
'model': './model/SAGAN-model.ckpt'
}
train_step = {
'epochs': 11,
'batch_size': 64,
'global_step': 10001,
'logging_interval': 500,
}
def main():
start_time = time.time() # Clocking start
height, width, channel = 128, 128, 3
# loading CelebA DataSet
ds = DataSet(height=height,
width=height,
channel=channel,
# ds_image_path="D:\\DataSet/CelebA/CelebA-128.h5",
ds_label_path="D:\\DataSet/CelebA/Anno/list_attr_celeba.txt",
ds_image_path="D:\\DataSet/CelebA/Img/img_align_celeba/",
ds_type="CelebA",
use_save=True,
save_file_name="D:\\DataSet/CelebA/CelebA-128.h5",
save_type="to_h5",
use_img_scale=False,
# img_scale="-1,1"
)
# saving sample images
test_images = np.reshape(iu.transform(ds.images[:16], inv_type='127'), (16, height, width, channel))
iu.save_images(test_images,
size=[4, 4],
image_path=results['output'] + 'sample.png',
inv_type='127')
ds_iter = DataIterator(x=ds.images,
y=None,
batch_size=train_step['batch_size'],
label_off=True)
# GPU configure
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
with tf.Session(config=config) as s:
# SAGAN Model
model = sagan.SAGAN(s, height=height, width=width, channel=channel,
batch_size=train_step['batch_size'],
use_gp=False, use_hinge_loss=True)
# Initializing
s.run(tf.global_variables_initializer())
print("[*] Reading checkpoints...")
saved_global_step = 0
ckpt = tf.train.get_checkpoint_state('./model/')
if ckpt and ckpt.model_checkpoint_path:
# Restores from checkpoint
model.saver.restore(s, ckpt.model_checkpoint_path)
saved_global_step = int(ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1])
print("[+] global step : %d" % saved_global_step, " successfully loaded")
else:
print('[-] No checkpoint file found')
global_step = saved_global_step
start_epoch = global_step // (ds.num_images // model.batch_size) # recover n_epoch
ds_iter.pointer = saved_global_step % (ds.num_images // model.batch_size) # recover n_iter
for epoch in range(start_epoch, train_step['epochs']):
for batch_x in ds_iter.iterate():
batch_x = iu.transform(batch_x, inv_type='127')
batch_x = np.reshape(batch_x, (model.batch_size, model.height, model.width, model.channel))
batch_z = np.random.uniform(-1., 1., [model.batch_size, model.z_dim]).astype(np.float32)
# Update D network
_, d_loss = s.run([model.d_op, model.d_loss],
feed_dict={
model.x: batch_x,
model.z: batch_z,
})
# Update G network
_, g_loss = s.run([model.g_op, model.g_loss],
feed_dict={
model.x: batch_x,
model.z: batch_z,
})
if global_step % train_step['logging_interval'] == 0:
summary = s.run(model.merged,
feed_dict={
model.x: batch_x,
model.z: batch_z,
})
# Print loss
print("[+] Epoch %04d Step %08d => " % (epoch, global_step),
" D loss : {:.8f}".format(d_loss),
" G loss : {:.8f}".format(g_loss))
# Training G model with sample image and noise
sample_z = np.random.uniform(-1., 1., [model.sample_num, model.z_dim]).astype(np.float32)
samples = s.run(model.g_test,
feed_dict={
model.z_test: sample_z,
})
# Summary saver
model.writer.add_summary(summary, global_step)
# Export image generated by model G
sample_image_height = model.sample_size
sample_image_width = model.sample_size
sample_dir = results['output'] + 'train_{:08d}.png'.format(global_step)
# Generated image save
iu.save_images(samples,
size=[sample_image_height, sample_image_width],
image_path=sample_dir,
inv_type='127')
# Model save
model.saver.save(s, results['model'], global_step)
global_step += 1
end_time = time.time() - start_time # Clocking end
# Elapsed time
print("[+] Elapsed time {:.8f}s".format(end_time))
# Close tf.Session
s.close()
if __name__ == '__main__':
main()