-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathmodelz.py
234 lines (190 loc) · 7.08 KB
/
modelz.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
import os
import time
import math
from glob import glob
import tensorflow as tf
import numpy as np
import h5py
import cv2
from ops import *
class ZGAN(object):
def __init__(self, sess, is_training = False, z_vector_dim=128, z_dim=128, df_dim=2048, gf_dim=2048, dataset_name='default', checkpoint_dir=None, sample_dir=None, data_dir='./data'):
"""
Args:
too lazy to explain
"""
self.sess = sess
self.z_dim = z_dim
self.z_vector_dim = z_vector_dim
self.df_dim = df_dim
self.gf_dim = gf_dim
self.dataset_namez = dataset_name+'_z'
self.checkpoint_dir = checkpoint_dir
self.data_dir = data_dir
if os.path.exists(self.data_dir+'/'+self.dataset_namez+'.hdf5'):
self.data_dict = h5py.File(self.data_dir+'/'+self.dataset_namez+'.hdf5', 'r')
self.data_z = self.data_dict['zs'][:]
if (self.z_vector_dim!=self.data_z.shape[1]):
print("error: self.z_vector_dim!=self.data_z.shape")
exit(0)
else:
if is_training:
print("error: cannot load "+self.data_dir+'/'+self.dataset_namez+'.hdf5')
exit(0)
else:
print("warning: cannot load "+self.data_dir+'/'+self.dataset_namez+'.hdf5')
self.build_model()
def build_model(self):
self.z_vector = tf.placeholder(shape=[None,self.z_vector_dim], dtype=tf.float32)
self.z = tf.placeholder(shape=[None,self.z_dim], dtype=tf.float32)
self.G = self.generator(self.z, reuse=False)
self.D = self.discriminator(self.z_vector, reuse=False)
self.D_ = self.discriminator(self.G, reuse=True)
self.sG = self.generator(self.z, reuse=True)
self.d_loss = tf.reduce_mean(self.D) - tf.reduce_mean(self.D_)
self.g_loss = tf.reduce_mean(self.D_)
epsilon = tf.random_uniform([], 0.0, 1.0)
x_hat = epsilon * self.z_vector + (1 - epsilon) * self.G
d_hat = self.discriminator(x_hat, reuse=True)
ddx = tf.gradients(d_hat, x_hat)[0]
print(ddx.get_shape().as_list())
ddx = tf.sqrt(tf.reduce_sum(tf.square(ddx), axis=1))
ddx = tf.reduce_mean(tf.square(ddx - 1.0) * 10.0)
self.d_loss = self.d_loss + ddx
self.vars = tf.trainable_variables()
self.g_vars = [var for var in self.vars if 'g_' in var.name]
self.d_vars = [var for var in self.vars if 'd_' in var.name]
self.saver = tf.train.Saver(max_to_keep=20)
def generator(self, z, reuse=False):
with tf.variable_scope("generator") as scope:
if reuse: scope.reuse_variables()
h1 = lrelu(linear(z, self.gf_dim, 'g_1_lin'))
h2 = lrelu(linear(h1, self.gf_dim, 'g_2_lin'))
h3 = linear(h2, self.z_vector_dim, 'g_3_lin')
return tf.nn.sigmoid(h3)
def discriminator(self, z_vector, reuse=False):
with tf.variable_scope("discriminator") as scope:
if reuse: scope.reuse_variables()
h1 = lrelu(linear(z_vector, self.df_dim, 'd_1_lin'))
h2 = lrelu(linear(h1, self.df_dim, 'd_2_lin'))
h3 = linear(h2, 1, 'd_3_lin')
return h3
def train(self, config):
d_optim = tf.train.AdamOptimizer(config.learning_rate, beta1=config.beta1).minimize(self.d_loss, var_list=self.d_vars)
g_optim = tf.train.AdamOptimizer(config.learning_rate, beta1=config.beta1).minimize(self.g_loss, var_list=self.g_vars)
self.sess.run(tf.global_variables_initializer())
counter = 0
start_time = time.time()
could_load, checkpoint_counter = self.load(self.checkpoint_dir)
if could_load:
counter = checkpoint_counter+1
print(" [*] Load SUCCESS")
else:
print(" [!] Load failed...")
batch_index_num = len(self.data_z)
batch_index_list = np.arange(batch_index_num)
batch_size = 50
batch_num = int(batch_index_num/batch_size)
for epoch in range(counter, config.epoch+1):
np.random.shuffle(batch_index_list)
errD_total = 0
errG_total = 0
for minib in range(batch_num):
batch_z = np.random.normal(0, 0.2, [batch_size, self.z_dim]).astype(np.float32)
batch_vector_z = self.data_z[minib*batch_size:(minib+1)*batch_size]
# Update D network
_, errD = self.sess.run([d_optim, self.d_loss],
feed_dict={
self.z_vector: batch_vector_z,
self.z: batch_z,
})
# Update G network
_, errG = self.sess.run([g_optim, self.g_loss],
feed_dict={
self.z: batch_z,
})
errD_total += errD
errG_total += errG
print("Epoch: [%4d/%4d] time: %4.4f, d_loss: %.8f, g_loss: %.8f" % (epoch, config.epoch, time.time() - start_time, errD_total/batch_num, errG_total/batch_num))
if epoch%1000 == 0:
self.save(config.checkpoint_dir, epoch)
#training z
z_height = 64
z_counter = np.zeros([z_height,self.z_vector_dim],np.int32)
z_img = np.zeros([z_height,self.z_vector_dim],np.uint8)
z_vector = self.data_z
for i in range(batch_index_num):
for j in range(self.z_dim):
slot = int(z_vector[i,j]*(z_height-0.0001))
if slot>z_height or slot<0: print("error slot")
z_counter[slot,j] += 1
maxz = 50#np.max(z_counter)
for i in range(z_height):
for j in range(self.z_dim):
x = int(z_counter[i,j]*256/maxz)
if (x>255): x=255
z_img[i,j] = x
cv2.imwrite("z_train.png", z_img)
#generated z
z_height = 64
z_counter = np.zeros([z_height,self.z_vector_dim],np.int32)
z_img = np.zeros([z_height,self.z_vector_dim],np.uint8)
batch_z = np.random.normal(0, 0.2, [batch_index_num, self.z_dim]).astype(np.float32)
z_vector = self.sess.run(self.sG,
feed_dict={
self.z: batch_z,
}
)
for i in range(batch_index_num):
for j in range(self.z_dim):
slot = int(z_vector[i,j]*(z_height-0.0001))
if slot>z_height or slot<0: print("error slot")
z_counter[slot,j] += 1
for i in range(z_height):
for j in range(self.z_dim):
x = int(z_counter[i,j]*256/maxz)
if (x>255): x=255
z_img[i,j] = x
cv2.imwrite("z_gen.png", z_img)
print("[Visualized Z]")
def get_z(self, config, num):
could_load, checkpoint_counter = self.load(self.checkpoint_dir)
if could_load:
print(" [*] Load SUCCESS")
else:
print(" [!] Load failed...")
return
#generated z
batch_z = np.random.normal(0, 0.2, [num, self.z_dim]).astype(np.float32)
z_vector = self.sess.run(self.sG,
feed_dict={
self.z: batch_z,
}
)
return z_vector
@property
def model_dir(self):
return "{}_{}_{}".format(
self.dataset_namez, self.z_dim, self.z_vector_dim)
def save(self, checkpoint_dir, step):
model_name = "ZGAN.model"
checkpoint_dir = os.path.join(checkpoint_dir, self.model_dir)
if not os.path.exists(checkpoint_dir):
os.makedirs(checkpoint_dir)
self.saver.save(self.sess,
os.path.join(checkpoint_dir, model_name),
global_step=step)
def load(self, checkpoint_dir):
import re
print(" [*] Reading checkpoints...")
checkpoint_dir = os.path.join(checkpoint_dir, self.model_dir)
ckpt = tf.train.get_checkpoint_state(checkpoint_dir)
if ckpt and ckpt.model_checkpoint_path:
ckpt_name = os.path.basename(ckpt.model_checkpoint_path)
self.saver.restore(self.sess, os.path.join(checkpoint_dir, ckpt_name))
counter = int(next(re.finditer("(\d+)(?!.*\d)",ckpt_name)).group(0))
print(" [*] Success to read {}".format(ckpt_name))
return True, counter
else:
print(" [*] Failed to find a checkpoint")
return False, 0