-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsrgans_minor_project.py
More file actions
496 lines (371 loc) · 14 KB
/
srgans_minor_project.py
File metadata and controls
496 lines (371 loc) · 14 KB
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
# -*- coding: utf-8 -*-
"""SRGANs_Minor_Project.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1Uk2DbA_KTHH-oWz8Ii_dHcCdo1lXjt73
"""
from google.colab import drive
drive.mount('/content/drive')
import zipfile
zip_ref = zipfile.ZipFile('/content/drive/MyDrive/dataset/mirflickr25k.zip', 'r')
zip_ref.extractall('/content/')
zip_ref.close()
import cv2
import os
source_dir = "/content/mirflickr"
hr_dir = "/content/hr_images"
lr_dir = "/content/lr_images"
os.makedirs(hr_dir, exist_ok=True)
os.makedirs(lr_dir, exist_ok=True)
for img in os.listdir(source_dir):
img_path = os.path.join(source_dir, img)
img_array = cv2.imread(img_path)
if img_array is None:
print(f"Failed to load image {img}")
continue
hr_img_array = cv2.resize(img_array, (128, 128))
lr_img_array = cv2.resize(hr_img_array, (32, 32))
cv2.imwrite(os.path.join(hr_dir, img), hr_img_array)
cv2.imwrite(os.path.join(lr_dir, img), lr_img_array)
print("Processing complete.")
import os
import cv2
import numpy as np
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras import layers, Model
from sklearn.model_selection import train_test_split
import numpy as np
from keras import Model
from keras.layers import Conv2D, PReLU,BatchNormalization, Flatten
from keras.layers import UpSampling2D, LeakyReLU, Dense, Input, add
from tqdm import tqdm
#Define blocks to build the generator
def res_block(ip):
res_model = Conv2D(64, (3,3), padding = "same")(ip)
res_model = BatchNormalization(momentum = 0.5)(res_model)
res_model = PReLU(shared_axes = [1,2])(res_model)
res_model = Conv2D(64, (3,3), padding = "same")(res_model)
res_model = BatchNormalization(momentum = 0.5)(res_model)
return add([ip,res_model])
def upscale_block(ip):
up_model = Conv2D(256, (3,3), padding="same")(ip)
up_model = UpSampling2D( size = 2 )(up_model)
up_model = PReLU(shared_axes=[1,2])(up_model)
return up_model
#Generator model
def create_gen(gen_ip, num_res_block):
layers = Conv2D(64, (9,9), padding="same")(gen_ip)
layers = PReLU(shared_axes=[1,2])(layers)
temp = layers
for i in range(num_res_block):
layers = res_block(layers)
layers = Conv2D(64, (3,3), padding="same")(layers)
layers = BatchNormalization(momentum=0.5)(layers)
layers = add([layers,temp])
layers = upscale_block(layers)
layers = upscale_block(layers)
op = Conv2D(3, (9,9), padding="same")(layers)
return Model(inputs=gen_ip, outputs=op)
#Descriminator block that will be used to construct the discriminator
def discriminator_block(ip, filters, strides=1, bn=True):
disc_model = Conv2D(filters, (3,3), strides = strides, padding="same")(ip)
if bn:
disc_model = BatchNormalization( momentum=0.8 )(disc_model)
disc_model = LeakyReLU( alpha=0.2 )(disc_model)
return disc_model
#Descriminartor, as described in the original paper
def create_disc(disc_ip):
df = 64
d1 = discriminator_block(disc_ip, df, bn=False)
d2 = discriminator_block(d1, df, strides=2)
d3 = discriminator_block(d2, df*2)
d4 = discriminator_block(d3, df*2, strides=2)
d5 = discriminator_block(d4, df*4)
d6 = discriminator_block(d5, df*4, strides=2)
d7 = discriminator_block(d6, df*8)
d8 = discriminator_block(d7, df*8, strides=2)
d8_5 = Flatten()(d8)
d9 = Dense(df*16)(d8_5)
d10 = LeakyReLU(alpha=0.2)(d9)
validity = Dense(1, activation='sigmoid')(d10)
return Model(disc_ip, validity)
#VGG19
from keras.applications import VGG19
def build_vgg(hr_shape):
vgg = VGG19(weights="imagenet",include_top=False, input_shape=hr_shape)
return Model(inputs=vgg.inputs, outputs=vgg.layers[10].output)
#Combined model
def create_comb(gen_model, disc_model, vgg, lr_ip, hr_ip):
gen_img = gen_model(lr_ip)
gen_features = vgg(gen_img)
disc_model.trainable = False
validity = disc_model(gen_img)
return Model(inputs=[lr_ip, hr_ip], outputs=[validity, gen_features])
n = 5000
lr_list = os.listdir("/content/lr_images")[:n]
lr_images = []
for img in lr_list:
img_lr = cv2.imread(os.path.join("/content/lr_images", img))
if img_lr is not None:
img_lr = cv2.cvtColor(img_lr, cv2.COLOR_BGR2RGB)
lr_images.append(img_lr)
hr_list = os.listdir("/content/hr_images")[:n]
hr_images = []
for img in hr_list:
img_hr = cv2.imread(os.path.join("/content/hr_images", img))
if img_hr is not None:
img_hr = cv2.cvtColor(img_hr, cv2.COLOR_BGR2RGB)
hr_images.append(img_hr)
lr_images = np.array(lr_images)
hr_images = np.array(hr_images)
import random
import numpy as np
image_number = random.randint(0, len(lr_images)-1)
plt.figure(figsize=(12, 6))
plt.subplot(121)
plt.imshow(np.reshape(lr_images[image_number], (32, 32, 3)))
plt.subplot(122)
plt.imshow(np.reshape(hr_images[image_number], (128, 128, 3)))
plt.show()
lr_images = lr_images / 255.
hr_images = hr_images / 255.
#Split to train and test
lr_train, lr_test, hr_train, hr_test = train_test_split(lr_images, hr_images,
test_size=0.33, random_state=42)
hr_shape = (hr_train.shape[1], hr_train.shape[2], hr_train.shape[3])
lr_shape = (lr_train.shape[1], lr_train.shape[2], lr_train.shape[3])
lr_ip = Input(shape=lr_shape)
hr_ip = Input(shape=hr_shape)
generator = create_gen(lr_ip, num_res_block = 16)
generator.summary()
discriminator = create_disc(hr_ip)
discriminator.compile(loss="binary_crossentropy", optimizer="adam", metrics=['accuracy'])
discriminator.summary()
vgg = build_vgg((128,128,3))
print(vgg.summary())
vgg.trainable = False
gan_model = create_comb(generator, discriminator, vgg, lr_ip, hr_ip)
gan_model.compile(loss=["binary_crossentropy", "mse"], loss_weights=[1e-3, 1], optimizer="adam")
gan_model.summary()
#Create a list of images for LR and HR in batches from which a batch of images
#would be fetched during training.
batch_size = 1
train_lr_batches = []
train_hr_batches = []
for it in range(int(hr_train.shape[0] / batch_size)):
start_idx = it * batch_size
end_idx = start_idx + batch_size
train_hr_batches.append(hr_train[start_idx:end_idx])
train_lr_batches.append(lr_train[start_idx:end_idx])
epochs = 5
# Enumerate training over epochs
for e in range(epochs):
fake_label = np.zeros((batch_size, 1)) # Assign a label of 0 to all fake (generated images)
real_label = np.ones((batch_size, 1)) # Assign a label of 1 to all real images
# Create empty lists to populate gen and disc losses
g_losses = []
d_losses = []
# Enumerate training over batches
for b in tqdm(range(len(train_hr_batches))):
lr_imgs = train_lr_batches[b] # Fetch a batch of LR images for training
hr_imgs = train_hr_batches[b] # Fetch a batch of HR images for training
fake_imgs = generator.predict_on_batch(lr_imgs) # Fake images
# First, train the discriminator on fake and real HR images
discriminator.trainable = True
d_loss_gen = discriminator.train_on_batch(fake_imgs, fake_label)
d_loss_real = discriminator.train_on_batch(hr_imgs, real_label)
# Now, train the generator by fixing discriminator as non-trainable
discriminator.trainable = False
# Average the discriminator loss, just for reporting purposes
d_loss = 0.5 * np.add(d_loss_gen, d_loss_real)
# Extract VGG features, to be used towards calculating loss
image_features = vgg.predict(hr_imgs)
# Train the generator via GAN
# Remember that we have 2 losses, adversarial loss and content (VGG) loss
g_loss, _, _ = gan_model.train_on_batch([lr_imgs, hr_imgs], [real_label, image_features])
# Save losses to a list so we can average and report
d_losses.append(d_loss)
g_losses.append(g_loss)
# Convert the list of losses to an array to make it easy to average
g_losses = np.array(g_losses)
d_losses = np.array(d_losses)
# Calculate the average losses for generator and discriminator
g_loss = np.sum(g_losses, axis=0) / len(g_losses)
d_loss = np.sum(d_losses, axis=0) / len(d_losses)
# Report the progress during training
print("epoch:", e+1, "g_loss:", g_loss, "d_loss:", d_loss)
# Save the generator after every 5 epochs
if (e+1) % 5 == 0:
generator.save("gen_e_" + str(e+1) + ".h5")
#Test - perform super resolution using saved generator model
from keras.models import load_model
from numpy.random import randint
generator = load_model('gen_e_5.h5', compile=False)
[X1, X2] = [lr_test, hr_test]
# select random example
ix = randint(0, len(X1), 1)
src_image, tar_image = X1[ix], X2[ix]
# generate image from source
gen_image = generator.predict(src_image)
# plot all three images
plt.figure(figsize=(16, 8))
plt.subplot(231)
plt.title('LR Image')
plt.imshow(src_image[0,:,:,:])
# plt.subplot(232)
# plt.title('Superresolution')
# plt.imshow(gen_image[0,:,:,:])
plt.subplot(232)
plt.title('HR image')
plt.imshow(tar_image[0,:,:,:])
plt.show()
# plot all three images
plt.figure(figsize=(16, 8))
plt.subplot(231)
plt.title('LR Image')
plt.imshow(src_image[0,:,:,:])
plt.subplot(232)
plt.title('Superresolution')
plt.imshow(gen_image[0,:,:,:])
plt.subplot(233)
plt.title('HR image')
plt.imshow(tar_image[0,:,:,:])
plt.show()
sreeni_lr = cv2.imread("/content/dog_32.jpg")
sreeni_hr = cv2.imread("/content/dogg_256.jpg")
#Change images from BGR to RGB for plotting.
#Remember that we used cv2 to load images which loads as BGR.
sreeni_lr = cv2.cvtColor(sreeni_lr, cv2.COLOR_BGR2RGB)
sreeni_hr = cv2.cvtColor(sreeni_hr, cv2.COLOR_BGR2RGB)
sreeni_lr = sreeni_lr / 255.
sreeni_hr = sreeni_hr / 255.
sreeni_lr = np.expand_dims(sreeni_lr, axis=0)
sreeni_hr = np.expand_dims(sreeni_hr, axis=0)
generated_sreeni_hr = generator.predict(sreeni_lr)
# plot all three images
plt.figure(figsize=(16, 8))
plt.subplot(231)
plt.title('LR Image')
plt.imshow(sreeni_lr[0,:,:,:])
# plt.subplot(232)
# plt.title('Superresolution')
# plt.imshow(generated_sreeni_hr[0,:,:,:])
plt.subplot(232)
plt.title(' HR image')
plt.imshow(sreeni_hr[0,:,:,:])
plt.show()
sreeni_lr = cv2.imread("/content/dog_32.jpg")
sreeni_hr = cv2.imread("/content/dogg_256.jpg")
#Change images from BGR to RGB for plotting.
#Remember that we used cv2 to load images which loads as BGR.
sreeni_lr = cv2.cvtColor(sreeni_lr, cv2.COLOR_BGR2RGB)
sreeni_hr = cv2.cvtColor(sreeni_hr, cv2.COLOR_BGR2RGB)
sreeni_lr = sreeni_lr / 255.
sreeni_hr = sreeni_hr / 255.
sreeni_lr = np.expand_dims(sreeni_lr, axis=0)
sreeni_hr = np.expand_dims(sreeni_hr, axis=0)
generated_sreeni_hr = generator.predict(sreeni_lr)
# plot all three images
plt.figure(figsize=(16, 8))
plt.subplot(231)
plt.title('LR Image')
plt.imshow(sreeni_lr[0,:,:,:])
plt.subplot(232)
plt.title('Superresolution')
plt.imshow(generated_sreeni_hr[0,:,:,:])
plt.subplot(233)
plt.title(' HR image')
plt.imshow(sreeni_hr[0,:,:,:])
plt.show()
"""### **Resort Images**"""
sreeni_lr = cv2.imread("/content/resort_32.jpg")
sreeni_hr = cv2.imread("/content/resort_256.jpg")
#Change images from BGR to RGB for plotting.
#Remember that we used cv2 to load images which loads as BGR.
sreeni_lr = cv2.cvtColor(sreeni_lr, cv2.COLOR_BGR2RGB)
sreeni_hr = cv2.cvtColor(sreeni_hr, cv2.COLOR_BGR2RGB)
sreeni_lr = sreeni_lr / 255.
sreeni_hr = sreeni_hr / 255.
sreeni_lr = np.expand_dims(sreeni_lr, axis=0)
sreeni_hr = np.expand_dims(sreeni_hr, axis=0)
generated_sreeni_hr = generator.predict(sreeni_lr)
# plot all three images
plt.figure(figsize=(16, 8))
plt.subplot(231)
plt.title('LR Image')
plt.imshow(sreeni_lr[0,:,:,:])
plt.subplot(232)
plt.title('Superresolution')
plt.imshow(generated_sreeni_hr[0,:,:,:])
plt.subplot(233)
plt.title(' HR image')
plt.imshow(sreeni_hr[0,:,:,:])
plt.show()
sreeni_lr = cv2.imread("/content/resort_32.jpg")
sreeni_hr = cv2.imread("/content/resort_256.jpg")
#Change images from BGR to RGB for plotting.
#Remember that we used cv2 to load images which loads as BGR.
sreeni_lr = cv2.cvtColor(sreeni_lr, cv2.COLOR_BGR2RGB)
sreeni_hr = cv2.cvtColor(sreeni_hr, cv2.COLOR_BGR2RGB)
sreeni_lr = sreeni_lr / 255.
sreeni_hr = sreeni_hr / 255.
sreeni_lr = np.expand_dims(sreeni_lr, axis=0)
sreeni_hr = np.expand_dims(sreeni_hr, axis=0)
generated_sreeni_hr = generator.predict(sreeni_lr)
# plot all three images
plt.figure(figsize=(16, 8))
plt.subplot(231)
plt.title('LR Image')
plt.imshow(sreeni_lr[0,:,:,:])
# plt.subplot(232)
# plt.title('Superresolution')
# plt.imshow(generated_sreeni_hr[0,:,:,:])
plt.subplot(232)
plt.title(' HR image')
plt.imshow(sreeni_hr[0,:,:,:])
plt.show()
"""### **PARK IMAGES**"""
sreeni_lr = cv2.imread("/content/park__32.jpg")
sreeni_hr = cv2.imread("/content/park__256.jpg")
#Change images from BGR to RGB for plotting.
#Remember that we used cv2 to load images which loads as BGR.
sreeni_lr = cv2.cvtColor(sreeni_lr, cv2.COLOR_BGR2RGB)
sreeni_hr = cv2.cvtColor(sreeni_hr, cv2.COLOR_BGR2RGB)
sreeni_lr = sreeni_lr / 255.
sreeni_hr = sreeni_hr / 255.
sreeni_lr = np.expand_dims(sreeni_lr, axis=0)
sreeni_hr = np.expand_dims(sreeni_hr, axis=0)
generated_sreeni_hr = generator.predict(sreeni_lr)
# plot all three images
plt.figure(figsize=(16, 8))
plt.subplot(231)
plt.title('LR Image')
plt.imshow(sreeni_lr[0,:,:,:])
plt.subplot(232)
plt.title('Superresolution')
plt.imshow(generated_sreeni_hr[0,:,:,:])
plt.subplot(233)
plt.title(' HR image')
plt.imshow(sreeni_hr[0,:,:,:])
plt.show()
sreeni_lr = cv2.imread("/content/park__32.jpg")
sreeni_hr = cv2.imread("/content/park__256.jpg")
#Change images from BGR to RGB for plotting.
#Remember that we used cv2 to load images which loads as BGR.
sreeni_lr = cv2.cvtColor(sreeni_lr, cv2.COLOR_BGR2RGB)
sreeni_hr = cv2.cvtColor(sreeni_hr, cv2.COLOR_BGR2RGB)
sreeni_lr = sreeni_lr / 255.
sreeni_hr = sreeni_hr / 255.
sreeni_lr = np.expand_dims(sreeni_lr, axis=0)
sreeni_hr = np.expand_dims(sreeni_hr, axis=0)
generated_sreeni_hr = generator.predict(sreeni_lr)
# plot all three images
plt.figure(figsize=(16, 8))
plt.subplot(231)
plt.title('LR Image')
plt.imshow(sreeni_lr[0,:,:,:])
plt.subplot(232)
plt.title(' HR image')
plt.imshow(sreeni_hr[0,:,:,:])
plt.show()