forked from talbotmd/doodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_cnn.py
201 lines (173 loc) · 8.6 KB
/
test_cnn.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
import numpy as np
import h5py
import matplotlib.pyplot as plt
import tensorflow as tf
import os
def encoder_layer(num_filters):
initializer = tf.random_normal_initializer(0., 0.02)
model = tf.keras.Sequential()
model.add(tf.keras.layers.Conv2D(num_filters,4,strides=2,padding='same',kernel_initializer=initializer,use_bias=False))
model.add(tf.keras.layers.BatchNormalization())
model.add(tf.keras.layers.LeakyReLU())
return model
def decoder_layer(num_filters):
initializer = tf.random_normal_initializer(0., 0.02)
model = tf.keras.Sequential()
model.add(tf.keras.layers.Conv2DTranspose(num_filters,4,strides=2,padding='same',kernel_initializer=initializer,use_bias=False))
model.add(tf.keras.layers.BatchNormalization())
model.add(tf.keras.layers.LeakyReLU())
return model
def Generator():
inputs = tf.keras.layers.Input(shape=[256,256,3])
outputs = inputs
for i in range(8):
layer = encoder_layer(256)
outputs = layer(outputs)
for i in range(7):
layer = decoder_layer(256)
outputs = layer(outputs)
layer = decoder_layer(3)
# layer = tf.keras.layers.Conv2DTranspose(3,4,strides=2,padding='same',kernel_initializer=tf.random_normal_initializer(0., 0.02),
# activation='tanh')
outputs = layer(outputs)
return tf.keras.Model(inputs=inputs,outputs=outputs)
def Discriminator1():
inputs = tf.keras.layers.Input(shape=[256,256,3])
outputs = inputs
for i in range(5):
layer = encoder_layer(256)
outputs = layer(outputs)
layer = tf.keras.layers.Conv2D(1,4,strides=2,padding='same',kernel_initializer=tf.random_normal_initializer(0., 0.02),
activation='sigmoid')
return tf.keras.Model(inputs=inputs, outputs=outputs)
def Discriminator2():
inputs = tf.keras.layers.Input(shape=[256,256,6])
outputs = inputs
for i in range(5):
layer = encoder_layer(256)
outputs = layer(outputs)
layer = tf.keras.layers.Conv2D(1,4,strides=2,padding='same',kernel_initializer=tf.random_normal_initializer(0., 0.02),
activation='sigmoid')
return tf.keras.Model(inputs=inputs, outputs=outputs)
# def Generator():
# inputs = tf.keras.layers.Input(shape=[256,256,3])
# outputs = inputs
# for i in range(5):
# layer = encoder_layer()
# outputs = layer(outputs)
# return tf.keras.Model(inputs=inputs,outputs=outputs)
# def generator_cost(output,target):
# l1_loss = tf.reduce_mean(tf.abs(target - output))
# return l1_loss
def generator_cost(disc1_output,gen_output,target):
loss_fn = tf.keras.losses.BinaryCrossentropy()
return loss_fn(tf.ones_like(disc1_output), disc1_output) + tf.dtypes.cast(0.01*tf.reduce_mean(tf.abs(target - gen_output)), tf.float32)# + loss_fn(tf.ones_like(disc2_output),disc2_output)
def discriminator1_cost(gen_output,human_output):
loss_fn = tf.keras.losses.BinaryCrossentropy()
gen_loss = loss_fn(tf.zeros_like(gen_output), gen_output)
human_loss = loss_fn(tf.ones_like(human_output),human_output)
return gen_loss + human_loss, gen_loss, human_loss
def discriminator2_cost(gen_output,human_output):
loss_fn = tf.keras.losses.BinaryCrossentropy()
return loss_fn(tf.zeros_like(gen_output), gen_output) + loss_fn(tf.ones_like(human_output),human_output)
def load_dataset():
train_dataset = h5py.File('output.hdf5', "r")
train_set_x_orig = np.array(train_dataset["image_dataset"][0:5000,:],dtype='float32') # your train set features
train_set_y_orig = np.array(train_dataset["sketch_dataset"][0:5000,:],dtype='float32') # your train set labels
return train_set_x_orig/255, train_set_y_orig/255
def main():
noise = tf.random.normal([1,256,256,3])
batch_size = 20
train_x, train_y = load_dataset()
generator_model = Generator()
disc1_model = Discriminator1()
disc2_model = Discriminator2()
generator_optimizer = tf.keras.optimizers.Adam(0.001, beta_1=0.5)
disc1_optimizer = tf.keras.optimizers.Adam(0.001, beta_1=0.5)
checkpoint_dir = "./checkpoints"
checkpoint_prefix = os.path.join(checkpoint_dir, "gen_disc_chpt")
checkpoint = tf.train.Checkpoint(generator_optimizer=generator_optimizer, generator=generator_model, discriminator_optimizer=disc1_optimizer,discriminator=disc1_model)
manager = tf.train.CheckpointManager(checkpoint, checkpoint_dir, max_to_keep=3)
checkpoint.restore(manager.latest_checkpoint)
if manager.latest_checkpoint:
print("Restored from {}".format(manager.latest_checkpoint))
output = generator_model(train_x[94:95], training=False)
print("shape:",output.shape)
plt.imshow(output[0, :, :, :])
plt.show()
# return
gen_losses = []
disc_losses = []
disc_human_losses = []
disc_gen_losses = []
for epoch in range(3000):
print("epoch: ", epoch)
average_disc_cost = 0
average_human_disc1_cost = 0
average_gen_disc1_cost = 0
average_gen_cost = 0
counter = 0
for image in range(0,train_x.shape[0]-1,batch_size):
print("image: ", image)
with tf.GradientTape() as gen_tape, tf.GradientTape() as disc1_tape:
generated_images = generator_model(train_x[image:min(image+batch_size,train_x.shape[0]-1)],training=True)
disc1_gen_output = disc1_model(generated_images, training=True)
disc1_human_output = disc1_model(train_y[image:min(image+batch_size,train_x.shape[0]-1)], training=True)
# disc2_gen_output = disc2_model(generated_images)
# disc2_human_output = disc2_model(train_y[image:min(image+5,train_x.shape[0]-1)])
disc1_cost, disc1_gen_cost, disc1_human_cost = discriminator1_cost(disc1_gen_output, disc1_human_output)
# disc2_cost = discriminator2_cost(disc2_gen_output, disc2_human_output)
gen_cost = generator_cost(disc1_gen_output,train_x[image:min(image+batch_size,train_x.shape[0]-1)],train_y[image:min(image+batch_size,train_x.shape[0]-1)])
average_human_disc1_cost += disc1_human_cost
average_gen_disc1_cost += disc1_gen_cost
average_disc_cost += disc1_cost
average_gen_cost += gen_cost
counter += 1
gradients_of_generator = gen_tape.gradient(gen_cost, generator_model.trainable_variables)
gradients_of_disc1 = disc1_tape.gradient(disc1_cost, disc1_model.trainable_variables)
generator_optimizer.apply_gradients(zip(gradients_of_generator, generator_model.trainable_variables))
disc1_optimizer.apply_gradients(zip(gradients_of_disc1,disc1_model.trainable_variables))
print("disc cost: ", average_disc_cost/counter)
print("human disc cost: ", average_human_disc1_cost/counter)
print("gen disc cost: ", average_gen_disc1_cost/counter)
print("gen cost: ", average_gen_cost/counter)
gen_losses.append(np.mean(average_gen_cost)/counter)
disc_losses.append(np.mean(average_disc_cost)/counter)
disc_human_losses.append(np.mean(average_human_disc1_cost)/counter)
disc_gen_losses.append(np.mean(average_gen_disc1_cost)/counter)
# with tf.GradientTape() as gen_tape:
# generated_images = generator_model(train_x,training=True)
# cost = generator_cost(generated_images,train_y)
# gradients_of_generator = gen_tape.gradient(cost, generator_model.trainable_variables)
# generator_optimizer.apply_gradients(zip(gradients_of_generator, generator_model.trainable_variables))
# if epoch % 1 == 0:
output = generator_model(train_x[train_x.shape[0]-1:train_x.shape[0]], training=False)
plt.imshow(output[0, :, :, :])
plt.savefig("test-" + str(epoch) + ".png")
plt.clf()
output = generator_model(train_x[0:1], training=False)
plt.imshow(output[0, :, :, :])
plt.savefig("train-" + str(epoch) + ".png")
plt.clf()
plt.plot(gen_losses)
plt.savefig("gen_losses.png")
plt.clf()
plt.plot(disc_losses)
plt.savefig("disc_losses.png")
plt.clf()
plt.plot(disc_gen_losses)
plt.savefig("disc_gen_losses.png")
plt.clf()
plt.plot(disc_human_losses)
plt.savefig("disc_human_losses.png")
plt.clf()
# plt.imshow(output[0, :, :, :])
# plt.show()
if epoch % 2 == 0:
checkpoint.save(file_prefix=checkpoint_prefix)
output = generator_model(train_x[0:1], training=False)
print("shape:",output.shape)
plt.imshow(output[0, :, :, :])
plt.show()
if __name__ == "__main__":
main()