-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGAN256.py
385 lines (279 loc) · 11.5 KB
/
GAN256.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# https://github.com/t0nberryking/DCGAN256
import os
import numpy as np
import random
from PIL import Image
import matplotlib.pyplot as plt
from math import floor
from cv2 import cv2 # Need to install an older version on windows : 'pip install opencv-python=3.3.0.9'
from loadFolderToTensorFlow import removeAllFilesInFolder
# Static params to change once
mainDir = os.getcwd()
input = mainDir + '\\input\\'
output = mainDir + '\\output\\'
resized = mainDir + '\\resized\\'
models = mainDir + '\\models\\'
removeAllFilesInFolder(output)
removeAllFilesInFolder(resized)
# removeAllFilesInFolder(models) # only if you want to clean models
stepToLoad = 7 # 0 for a fresh start
# Parameters to change
percentageOfImgToLoad = 100 # 100
isShuffle = False # False
stepsToWriteImg = 1000 # 1000
new_image_width, new_image_height = 256, 256
def zero():
return np.random.uniform(0.0, 0.01, size = [1])
def one():
return np.random.uniform(0.99, 1.0, size = [1])
def noise(n):
return np.random.uniform(-1.0, 1.0, size = [n, 4096])
print("Importing Images...")
Images = []
files = os.listdir(input)
if (isShuffle):
random.shuffle(files)
imgToLoad = int(len(files) * percentageOfImgToLoad / 100)
for n in range(1, imgToLoad):
if (n % 200 == 0):
print(str(n) + " / " + str(imgToLoad) + " loaded ...")
temp1 = Image.open(input + files[n])
temp1 = temp1.resize(size=(new_image_width, new_image_height), resample=0) # resample=0
if (n <= 5):
temp1.save(resized + "resized " + str(n) + ".png") # Save first pictures
temp = np.array(temp1.convert('RGB'), dtype='float32')
Images.append(temp / 255)
Images.append(np.flip(Images[-1], 1))
from tensorflow.keras.layers import Conv2D, LeakyReLU, BatchNormalization, Dense, AveragePooling2D, GaussianNoise
from tensorflow.keras.layers import Reshape, UpSampling2D, Activation, Dropout, Flatten, Conv2DTranspose
from tensorflow.keras.models import model_from_json, Sequential
from tensorflow.keras.optimizers import Adam
class GAN(object):
def __init__(self):
#Models
self.D = None
self.G = None
self.OD = None
self.DM = None
self.AM = None
#Config
self.LR = 0.0001
self.steps = 1
def discriminator(self):
if self.D:
return self.D
self.D = Sequential()
#add Gaussian noise to prevent Discriminator overfitting
self.D.add(GaussianNoise(0.2, input_shape = [256, 256, 3]))
#256x256x3 Image
self.D.add(Conv2D(filters = 8, kernel_size = 3, padding = 'same'))
self.D.add(LeakyReLU(0.2))
self.D.add(Dropout(0.25))
self.D.add(AveragePooling2D())
#128x128x8
self.D.add(Conv2D(filters = 16, kernel_size = 3, padding = 'same'))
self.D.add(BatchNormalization(momentum = 0.7))
self.D.add(LeakyReLU(0.2))
self.D.add(Dropout(0.25))
self.D.add(AveragePooling2D())
#64x64x16
self.D.add(Conv2D(filters = 32, kernel_size = 3, padding = 'same'))
self.D.add(BatchNormalization(momentum = 0.7))
self.D.add(LeakyReLU(0.2))
self.D.add(Dropout(0.25))
self.D.add(AveragePooling2D())
#32x32x32
self.D.add(Conv2D(filters = 64, kernel_size = 3, padding = 'same'))
self.D.add(BatchNormalization(momentum = 0.7))
self.D.add(LeakyReLU(0.2))
self.D.add(Dropout(0.25))
self.D.add(AveragePooling2D())
#16x16x64
self.D.add(Conv2D(filters = 128, kernel_size = 3, padding = 'same'))
self.D.add(BatchNormalization(momentum = 0.7))
self.D.add(LeakyReLU(0.2))
self.D.add(Dropout(0.25))
self.D.add(AveragePooling2D())
#8x8x128
self.D.add(Conv2D(filters = 256, kernel_size = 3, padding = 'same'))
self.D.add(BatchNormalization(momentum = 0.7))
self.D.add(LeakyReLU(0.2))
self.D.add(Dropout(0.25))
self.D.add(AveragePooling2D())
#4x4x256
self.D.add(Flatten())
#256
self.D.add(Dense(128))
self.D.add(LeakyReLU(0.2))
self.D.add(Dense(1, activation = 'sigmoid'))
return self.D
def generator(self):
if self.G:
return self.G
self.G = Sequential()
self.G.add(Reshape(target_shape = [1, 1, 4096], input_shape = [4096]))
#1x1x4096
self.G.add(Conv2DTranspose(filters = 256, kernel_size = 4))
self.G.add(Activation('relu'))
#4x4x256 - kernel sized increased by 1
self.G.add(Conv2D(filters = 256, kernel_size = 4, padding = 'same'))
self.G.add(BatchNormalization(momentum = 0.7))
self.G.add(Activation('relu'))
self.G.add(UpSampling2D())
#8x8x256 - kernel sized increased by 1
self.G.add(Conv2D(filters = 128, kernel_size = 4, padding = 'same'))
self.G.add(BatchNormalization(momentum = 0.7))
self.G.add(Activation('relu'))
self.G.add(UpSampling2D())
#16x16x128
self.G.add(Conv2D(filters = 64, kernel_size = 3, padding = 'same'))
self.G.add(BatchNormalization(momentum = 0.7))
self.G.add(Activation('relu'))
self.G.add(UpSampling2D())
#32x32x64
self.G.add(Conv2D(filters = 32, kernel_size = 3, padding = 'same'))
self.G.add(BatchNormalization(momentum = 0.7))
self.G.add(Activation('relu'))
self.G.add(UpSampling2D())
#64x64x32
self.G.add(Conv2D(filters = 16, kernel_size = 3, padding = 'same'))
self.G.add(BatchNormalization(momentum = 0.7))
self.G.add(Activation('relu'))
self.G.add(UpSampling2D())
#128x128x16
self.G.add(Conv2D(filters = 8, kernel_size = 3, padding = 'same'))
self.G.add(Activation('relu'))
self.G.add(UpSampling2D())
#256x256x8
self.G.add(Conv2D(filters = 3, kernel_size = 3, padding = 'same'))
self.G.add(Activation('sigmoid'))
return self.G
def DisModel(self):
if self.DM == None:
self.DM = Sequential()
self.DM.add(self.discriminator())
self.DM.compile(optimizer = Adam(lr = self.LR * (0.85 ** floor(self.steps / 10000))), loss = 'binary_crossentropy')
return self.DM
def AdModel(self):
if self.AM == None:
self.AM = Sequential()
self.AM.add(self.generator())
self.AM.add(self.discriminator())
self.AM.compile(optimizer = Adam(lr = self.LR * (0.85 ** floor(self.steps / 10000))), loss = 'binary_crossentropy')
return self.AM
def sod(self):
self.OD = self.D.get_weights()
def lod(self):
self.D.set_weights(self.OD)
class Model_GAN(object):
def __init__(self):
self.GAN = GAN()
self.DisModel = self.GAN.DisModel()
self.AdModel = self.GAN.AdModel()
self.generator = self.GAN.generator()
def train(self, batch = 16):
(a, b) = self.train_dis(batch)
c = self.train_gen(batch)
step = model.GAN.steps
print(f"{step} D Real: {str(a)}, D Fake: {str(b)}, G All: {str(c)}")
if self.GAN.steps % 500 == 0:
self.save(floor(self.GAN.steps / 1000))
# self.evaluate()
if self.GAN.steps % 5000 == 0:
self.GAN.AM = None
self.GAN.DM = None
self.AdModel = self.GAN.AdModel()
self.DisModel = self.GAN.DisModel()
self.GAN.steps = self.GAN.steps + 1
def train_dis(self, batch):
#Get Real Images
im_no = random.randint(0, len(Images) - batch - 1)
train_data = Images[im_no : im_no + int(batch / 2)]
label_data = []
for i in range(int(batch / 2)):
#label_data.append(one())
label_data.append(zero())
d_loss_real = self.DisModel.train_on_batch(np.array(train_data), np.array(label_data))
#Get Fake Images
train_data = self.generator.predict(noise(int(batch / 2)))
label_data = []
for i in range(int(batch / 2)):
#label_data.append(zero())
label_data.append(one())
d_loss_fake = self.DisModel.train_on_batch(train_data, np.array(label_data))
return (d_loss_real, d_loss_fake)
def train_gen(self, batch):
self.GAN.sod()
label_data = []
for i in range(int(batch)):
#label_data.append(one())
label_data.append(zero())
g_loss = self.AdModel.train_on_batch(noise(batch), np.array(label_data))
self.GAN.lod()
return g_loss
def evaluate(self):
im_no = random.randint(0, len(Images) - 1)
im1 = Images[im_no]
im2 = self.generator.predict(noise(2))
plt.figure(1)
plt.imshow(im1)
plt.figure(2)
plt.imshow(im2[0])
plt.figure(3)
plt.imshow(im2[1])
plt.show()
def save(self, num):
gen_json = self.GAN.G.to_json()
dis_json = self.GAN.D.to_json()
with open(models + "gen.json", "w+") as json_file:
json_file.write(gen_json)
with open(models + "dis.json", "w+") as json_file:
json_file.write(dis_json)
self.GAN.G.save_weights(models + "gen"+str(num)+".h5")
self.GAN.D.save_weights(models + "dis"+str(num)+".h5")
print(f"Model number {str(num)} Saved!")
def load(self, num):
steps1 = self.GAN.steps
self.GAN = None
self.GAN = GAN()
#Generator
gen_file = open(models + "gen.json", 'r')
gen_json = gen_file.read()
gen_file.close()
self.GAN.G = model_from_json(gen_json)
self.GAN.G.load_weights(models + "gen" + str(num) + ".h5")
#Discriminator
dis_file = open(models + "dis.json", 'r')
dis_json = dis_file.read()
dis_file.close()
self.GAN.D = model_from_json(dis_json)
self.GAN.D.load_weights(models + "dis"+str(num)+".h5")
#Reinitialize
self.generator = self.GAN.generator()
self.DisModel = self.GAN.DisModel()
self.AdModel = self.GAN.AdModel()
self.GAN.steps = steps1
def eval2(self, num = 0):
im2 = self.generator.predict(noise(48))
r1 = np.concatenate(im2[:8], axis = 1)
r2 = np.concatenate(im2[8:16], axis = 1)
r3 = np.concatenate(im2[16:24], axis = 1)
r4 = np.concatenate(im2[24:32], axis = 1)
r5 = np.concatenate(im2[32:40], axis = 1)
r6 = np.concatenate(im2[40:48], axis = 1)
c1 = np.concatenate([r1, r2, r3, r4, r5, r6], axis = 0)
x = Image.fromarray(np.uint8(c1*255))
x.save(output + "i" + str(num) + ".png")
#if training new model:
model = Model_GAN()
if (stepToLoad != 0):
model.load(stepToLoad)
model.GAN.D.summary()
model.GAN.G.summary()
print("We're off! See you in a while!")
#model.GAN.steps = 165001
while(model.GAN.steps < 500000):
model.train()
if model.GAN.steps % stepsToWriteImg == 0:
print("\nRound " + str(model.GAN.steps) + ":")
model.eval2(int(model.GAN.steps / stepsToWriteImg))