-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgan.py
executable file
·172 lines (130 loc) · 6.08 KB
/
gan.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
from __future__ import print_function
import numpy as np
import tensorflow as tf
import os
import cv2
import illustrator.utils
batch_size = 1
image_height = 200
image_width = 200
def conv(x, filter_size=8, stride=2, num_filters=64, is_output=False, name="conv"):
filter_height, filter_width = filter_size, filter_size
in_channels = x.get_shape().as_list()[-1]
out_channels = num_filters
with tf.variable_scope(name):
W = tf.get_variable("W",
shape=[filter_height, filter_width, in_channels, out_channels],
initializer=tf.contrib.layers.variance_scaling_initializer())
b = tf.get_variable("b",
shape=[out_channels],
initializer=tf.contrib.layers.variance_scaling_initializer())
conv = tf.nn.conv2d(x, W, [1, stride, stride, 1], padding="SAME")
out = tf.nn.bias_add(conv, b)
if is_output:
return out
return tf.nn.relu(out)
# return tf.contrib.layers.batch_norm(tf.nn.relu(out))
def convt(x, out_shape, filter_size=8, stride=2, is_output=False, name="convt"):
filter_height, filter_width = filter_size, filter_size
in_channels = x.get_shape().as_list()[-1]
with tf.variable_scope(name):
W = tf.get_variable("W",
shape=[filter_height, filter_width, out_shape[-1], in_channels],
initializer=tf.contrib.layers.variance_scaling_initializer())
b = tf.get_variable("b",
shape=[out_shape[-1]],
initializer=tf.contrib.layers.variance_scaling_initializer())
conv = tf.nn.conv2d_transpose(x, W, out_shape, [1, stride, stride, 1], padding="SAME")
out = tf.nn.bias_add(conv, b)
if is_output:
return out
return tf.nn.relu(out)
# return tf.contrib.layers.batch_norm(tf.nn.relu(out))
def fc(x, out_size=50, is_output=False, name="fc"):
in_size = x.get_shape().as_list()[-1]
with tf.variable_scope(name):
W = tf.get_variable("W", shape=[in_size, out_size],
initializer=tf.contrib.layers.variance_scaling_initializer())
b = tf.get_variable("b", shape=[out_size],
initializer=tf.contrib.layers.variance_scaling_initializer())
out = tf.matmul(x, W) + b
if is_output:
return out
return tf.nn.relu(out)
def gen_model(seed_imgs):
with tf.variable_scope("generator"):
# fc1 = fc(seed_imgs, out_size=7*7*128, name="g_fc1")
# fc1_reshaped = tf.reshape(fc1, [batch_size, 7, 7, 128])
# convt1 = convt(seed_imgs, [batch_size, 7, 7, 128], filter_size=5, stride=2, name="g_convt1")
conv1 = conv(seed_imgs, filter_size=5, stride=2, num_filters=64, name="g_convt1")
# print("convt1", conv1.get_shape())
convt2 = convt(conv1, [batch_size, image_height, image_width, 1], filter_size=5, stride=2, is_output=True,
name="g_convt2")
# print("convt2", convt2.get_shape())
return tf.sigmoid(convt2)
def disc_model(imgs, reuse):
with tf.variable_scope("discriminator", reuse=reuse):
# imgs_reshaped = tf.reshape(imgs, [batch_size, 28, 28, 1])
conv1 = conv(imgs, filter_size=5, stride=2, num_filters=32, name="d_conv1")
# print("conv1", conv1.get_shape())
conv2 = conv(conv1, filter_size=5, stride=2, num_filters=64, name="d_conv2")
# print("conv2", conv2.get_shape())
_, height, width, channels = conv2.get_shape()
conv2_reshaped = tf.reshape(conv2, [batch_size, int(height) * int(width) * 64])
# print("conv2_reshaped", conv2_reshaped.get_shape())
fc1 = fc(conv2_reshaped, out_size=1024, name="d_fc1")
# print("fc1", fc1.get_shape())
fc_out = fc(fc1, out_size=1, is_output=True, name="d_fc_out")
# print("fc_out", fc_out.get_shape())
return tf.sigmoid(fc_out) # probability
with tf.name_scope('gan'):
gen_input = tf.placeholder(tf.float32, [image_height, image_width], name="gen_input")
disc_input = tf.placeholder(tf.float32, [image_height, image_width], name="disc_input") # true images
gen_input_reshaped = tf.reshape(gen_input, [batch_size, image_height, image_width, 1])
disc_input_reshaped = tf.reshape(disc_input, [batch_size, image_height, image_width, 1])
gen_output = gen_model(gen_input_reshaped)
true_probs = disc_model(disc_input_reshaped, reuse=False)
gen_probs = disc_model(gen_output, reuse=True)
disc_loss = tf.reduce_mean(tf.log(true_probs + 0.4) + tf.log(0.6 - gen_probs))
gen_loss = tf.reduce_mean(tf.log(gen_probs))
disc_acc = (tf.reduce_mean(true_probs) + tf.reduce_mean(1.0 - gen_probs)) / 2.0 # TODO: images or probs?
t_vars = tf.trainable_variables()
disc_vars = [var for var in t_vars if "d_" in var.name]
disc_optim = tf.train.AdamOptimizer(0.00001, beta1=0.01).minimize(-disc_loss, var_list=disc_vars)
gen_vars = [var for var in t_vars if "g_" in var.name]
gen_optim = tf.train.AdamOptimizer(0.000001, beta1=0.01).minimize(-gen_loss, var_list=gen_vars)
# TODO: Train discriminator on true images 1 time for every 3 times on generated images
def load_data(path='/mnt/pccfs/not_backed_up/data/imagenet'):
for filename in os.listdir(path):
original_image = cv2.imread(os.path.join(path, filename))
if original_image is None:
continue
yield illustrator.utils.convert_to_sketch(cv2.resize(original_image, (image_width, image_height)))
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for epoch in range(100000):
i = 0
seed_image, true_image = None, None
for image in load_data():
if seed_image is None:
seed_image = image
continue
if true_image is None:
true_image = image
# train the GAN on a real image
sess.run(disc_optim, feed_dict={gen_input: seed_image, disc_input: true_image})
# train the GAN on 3 generated images
for j in range(3):
sess.run(gen_optim, feed_dict={gen_input: seed_image})
if i % 10 == 0:
cv2.imshow("seed image", np.reshape(seed_image, (image_height, image_width)))
cv2.imshow("true image", np.reshape(true_image, (image_height, image_width)))
disc_acc_val, disc_loss_val, gen_loss_val, gen_output_val, true_probs_val, gen_probs_val = sess.run(
[disc_acc, disc_loss, gen_loss, gen_output, true_probs, gen_probs],
feed_dict={gen_input: seed_image, disc_input: true_image})
print(i, disc_loss_val, gen_loss_val, disc_acc_val, true_probs_val, gen_probs_val, sep='\t')
cv2.imshow("generated", gen_output_val[0])
cv2.waitKey(10)
seed_image = None
true_image = None
i += 1