-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain_art_wgan.py
233 lines (206 loc) · 9.63 KB
/
main_art_wgan.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
# -*- coding: utf-8 -*-
"""
Created on Mon Nov 26 10:24:13 2018
@author: tgill
"""
import numpy as np
import matplotlib.pyplot as plt
from keras.datasets import cifar10
from keras.optimizers import Adam
from keras.utils.np_utils import to_categorical
from keras.layers import Input
from keras.models import Model
from keras import backend as K
import cv2
from tqdm import tqdm
import queue
import threading
from networks import generator, discriminator, RandomWeightedAverage, gradient_penalty_loss, wasserstein_loss, null_loss, res_generator, res_discriminator
from functools import partial
from utils import producer, getPaths, scale, mean
data_train = r"C:\Users\tgill\OneDrive\Documents\GD_AI\ArtGAN\wikipaintings_full\wikipaintings_train"
data_test = r"C:\Users\tgill\OneDrive\Documents\GD_AI\ArtGAN\wikipaintings_full\wikipaintings_train"
train_paths, y_train, classes = getPaths(data_train)
test_paths, y_test, classes = getPaths(data_test)
ls = [-np.sum(y_train==i) for i in range(25)]
arg = np.argsort(ls)
classement = np.argsort(arg)
nb_select = 9
select = arg[:nb_select]
idx_select = np.isin(y_train, select)
train_paths = train_paths[idx_select]
y_train = y_train[idx_select]
y_train = classement[y_train]
print(train_paths.shape)
n_epochs = 100
epoch_size = 1400
batch_size = 32
target_size = (64, 64, 3)
noise_dim=100
n_critic = 5
n_labels=10
n_channels=3
n_labels=nb_select
prep_func = mean
gen = res_generator(target_size[0], target_size[1], 128, noise_dim, n_labels, target_size[2], tanh=True)
disc = res_discriminator(target_size[0], target_size[1], 128, n_labels, target_size[2], wgan=True)
lr = 0.0002
opt = Adam(lr, 0.0, 0.9)
label_loss = 'categorical_crossentropy' #null_loss()# 'categorical_crossentropy'
#-------------------------------
# Construct Computational Graph
# for the Critic
#-------------------------------
# Freeze generator's layers while training critic
gen.trainable = False
# Image input (real sample)
real_img = Input(shape=target_size)
# Noise input
z_disc = Input(shape=(noise_dim,))
label_inp = Input(shape=(n_labels,))
# Generate image based of noise (fake sample)
fake_img = gen([z_disc, label_inp])
# Discriminator determines validity of the real and fake images
fake, fake_label = disc(fake_img)
valid, valid_label = disc(real_img)
# Construct weighted average between real and fake images
interpolated_img = RandomWeightedAverage(batch_size)([real_img, fake_img])
# Determine validity of weighted sample
validity_interpolated, _ = disc(interpolated_img)
# Use Python partial to provide loss function with additional
# 'averaged_samples' argument
partial_gp_loss = partial(gradient_penalty_loss,
averaged_samples=interpolated_img)
partial_gp_loss.__name__ = 'gradient_penalty' # Keras requires function names
disc_model = Model(inputs=[real_img, z_disc, label_inp],
outputs=[valid, valid_label, fake, fake_label, validity_interpolated])
disc_model.compile(loss=[wasserstein_loss, label_loss,
wasserstein_loss, label_loss,
partial_gp_loss],
optimizer=opt,
loss_weights=[1, 1, 1, 1, 10])
#-------------------------------
# Construct Computational Graph
# for Generator
#-------------------------------
# For the generator we freeze the critic's layers
disc.trainable = False
gen.trainable = True
# Sampled noise for input to generator
z_gen = Input(shape=(100,))
# Generate images based of noise
img = gen([z_gen, label_inp])
# Discriminator determines validity
valid, valid_label = disc(img)
# Defines generator model
gen_model = Model([z_gen, label_inp], [valid, valid_label])
gen_model.compile(loss=[wasserstein_loss, label_loss], optimizer=opt)
#Fixed targets
y_true = -np.ones([batch_size, 1])
y_fake = np.ones([batch_size, 1])
y_dummy = np.zeros([batch_size, 1])
q = queue.Queue(maxsize=100)
stop_event = threading.Event()
writer = threading.Thread(target=producer, args=(q, stop_event, train_paths, y_train, batch_size, target_size[:2], prep_func))
writer2 = threading.Thread(target=producer, args=(q, stop_event, train_paths, y_train, batch_size, target_size[:2], prep_func))
writer3 = threading.Thread(target=producer, args=(q, stop_event, train_paths, y_train, batch_size, target_size[:2], prep_func))
writer4 = threading.Thread(target=producer, args=(q, stop_event, train_paths, y_train, batch_size, target_size[:2], prep_func))
writer5 = threading.Thread(target=producer, args=(q, stop_event, train_paths, y_train, batch_size, target_size[:2], prep_func))
writer6 = threading.Thread(target=producer, args=(q, stop_event, train_paths, y_train, batch_size, target_size[:2], prep_func))
writer7 = threading.Thread(target=producer, args=(q, stop_event, train_paths, y_train, batch_size, target_size[:2], prep_func))
writer8 = threading.Thread(target=producer, args=(q, stop_event, train_paths, y_train, batch_size, target_size[:2], prep_func))
#writer9 = threading.Thread(target=producer, args=(q, stop_event, train_paths, y_train, batch_size, target_size[:2], prep_func))
#writer10 = threading.Thread(target=producer, args=(q, stop_event, train_paths, y_train, batch_size, target_size[:2], prep_func))
#writer11 = threading.Thread(target=producer, args=(q, stop_event, train_paths, y_train, batch_size, target_size[:2], prep_func))
#writer12 = threading.Thread(target=producer, args=(q, stop_event, train_paths, y_train, batch_size, target_size[:2], prep_func))
#writer13 = threading.Thread(target=producer, args=(q, stop_event, train_paths, y_train, batch_size, target_size[:2], prep_func))
#writer14 = threading.Thread(target=producer, args=(q, stop_event, train_paths, y_train, batch_size, target_size[:2], prep_func))
#writer15 = threading.Thread(target=producer, args=(q, stop_event, train_paths, y_train, batch_size, target_size[:2], prep_func))
#writer16 = threading.Thread(target=producer, args=(q, stop_event, train_paths, y_train, batch_size, target_size[:2], prep_func))
writer.start()
writer2.start()
writer3.start()
writer4.start()
writer5.start()
writer6.start()
writer7.start()
writer8.start()
#writer9.start()
#writer10.start()
#writer11.start()
#writer12.start()
#writer13.start()
#writer14.start()
#writer15.start()
#writer16.start()
#examples=nb_select
examples=nb_select
noise_disp = np.random.randn(examples, noise_dim)
for i in range(n_epochs):
print("Epoch ", i)
loss_disc=[]
loss_gen=[]
print(q.qsize())
decay = max(0.0, 1-i/n_epochs)
K.set_value(opt.lr, lr*decay)
for j in tqdm(range(epoch_size)):
for k in range(n_critic):
X_true, label_true = q.get()
#print(q.qsize())
label_true = to_categorical(label_true, n_labels)
#noise = np.random.uniform(-1.0, 1.0, size=[batch_size, noise_dim])
noise = np.random.randn(batch_size, noise_dim)
sampled_labels = np.random.randint(0, n_labels, (batch_size, 1))
sampled_labels = to_categorical(sampled_labels, num_classes=n_labels)
d_loss = disc_model.train_on_batch([X_true, noise, sampled_labels],
[y_true, label_true, y_fake, sampled_labels, y_dummy])
loss_disc.append(d_loss)
#X_noise = np.random.uniform(-1.0, 1.0, size=[batch_size, noise_dim])
X_noise = np.random.randn(batch_size, noise_dim)
sampled_labels = np.random.randint(0, n_labels, (batch_size, 1))
sampled_labels = to_categorical(sampled_labels, num_classes=n_labels)
gen_loss = gen_model.train_on_batch([X_noise, sampled_labels], [y_true, sampled_labels])
loss_gen.append(gen_loss)
#print(gen_loss)
print("Disc", np.mean(loss_disc, axis=0))
print(np.mean(loss_disc, axis=0)[3]+np.mean(loss_disc, axis=0)[1])
print("Gen", np.mean(loss_gen, axis=0))
#plt.imshow(X_fake[1][:,:,0])
#plt.title(sampled_labels[1])
if i%1==0:
plt.figure(figsize=(10, 10))
# noise_disp = np.random.randn(examples, noise_dim)
#noise = np.random.uniform(-1.0, 1.0, size=[examples, noise_dim])
#noise = np.random.randn(examples, noise_dim)
labels = np.unique(y_train)
# l = labels.reshape(-1, 1)
# l = np.tile(l, 10)
# l = l.reshape(100)
# labels = np.tile(labels, 10)
labels = to_categorical(labels, num_classes = n_labels)
# l = to_categorical(l)
# labels = labels+l
images = gen.predict([noise_disp, labels], batch_size=batch_size)
images = images.reshape(-1, target_size[0],target_size[1], 3)
dim = int(np.sqrt(nb_select))
for i in range(images.shape[0]):
ax=plt.subplot(dim, dim, i+1)
#ax.title.set_text(classes[i][1], fontsize=5)
ax.set_title(classes[arg[i]][1], fontsize=5)
plt.imshow((images[i]+1)/2, interpolation='nearest', cmap='gray_r')
plt.axis('off')
plt.tight_layout()
plt.show()
plt.pause(0.05)
# sampled_labels = np.random.randint(0, n_labels, (examples, 1))
# images = gen.predict([noise_disp, sampled_labels], batch_size=batch_size)
# dim = int(np.sqrt(examples))
# for i in range(images.shape[0]):
# ax=plt.subplot(dim, dim, i+1)
# #ax.title.set_text(classes[i][1], fontsize=5)
# plt.imshow((images[i]+1.)/2., interpolation='nearest', cmap='gray_r')
# plt.axis('off')
# plt.tight_layout()
# plt.show()
# plt.pause(0.05)
stop_event.set()