forked from deepak112/Keras-SRGAN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.py
239 lines (171 loc) · 7.26 KB
/
Utils.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
#!/usr/bin/env python
#title :Utils.py
#description :Have helper functions to process images and plot images
#author :Deepak Birla
#date :2018/10/30
#usage :imported in other files
#python_version :3.5.4
from keras.layers import Lambda
import tensorflow as tf
from skimage import data, io, filters
import numpy as np
from numpy import array
from numpy.random import randint
from scipy.misc import imresize
import os
import sys
import matplotlib.pyplot as plt
plt.switch_backend('agg')
# Subpixel Conv will upsample from (h, w, c) to (h/r, w/r, c/r^2)
def SubpixelConv2D(input_shape, scale=4):
def subpixel_shape(input_shape):
dims = [input_shape[0],input_shape[1] * scale,input_shape[2] * scale,int(input_shape[3] / (scale ** 2))]
output_shape = tuple(dims)
return output_shape
def subpixel(x):
return tf.depth_to_space(x, scale)
return Lambda(subpixel, output_shape=subpixel_shape)
# Takes list of images and provide HR images in form of numpy array
def hr_images(images):
images_hr = array(images)
return images_hr
# Takes list of images and provide LR images in form of numpy array
def lr_images(images_real , downscale):
images = []
for img in range(len(images_real)):
images.append(imresize(images_real[img], [images_real[img].shape[0]//downscale,images_real[img].shape[1]//downscale], interp='bicubic', mode=None))
images_lr = array(images)
return images_lr
def normalize(input_data):
return (input_data.astype(np.float32) - 127.5)/127.5
def denormalize(input_data):
input_data = (input_data + 1) * 127.5
return input_data.astype(np.uint8)
def load_path(path):
directories = []
if os.path.isdir(path):
directories.append(path)
for elem in os.listdir(path):
if os.path.isdir(os.path.join(path,elem)):
directories = directories + load_path(os.path.join(path,elem))
directories.append(os.path.join(path,elem))
return directories
def load_data_from_dirs(dirs, ext):
files = []
file_names = []
count = 0
for d in dirs:
for f in os.listdir(d):
if f.endswith(ext):
image = data.imread(os.path.join(d,f))
if len(image.shape) > 2:
files.append(image)
file_names.append(os.path.join(d,f))
count = count + 1
return files
def load_data(directory, ext):
files = load_data_from_dirs(load_path(directory), ext)
return files
def load_training_data(directory, ext, number_of_images = 1000, train_test_ratio = 0.8):
number_of_train_images = int(number_of_images * train_test_ratio)
files = load_data_from_dirs(load_path(directory), ext)
if len(files) < number_of_images:
print("Number of image files are less then you specified")
print("Please reduce number of images to %d" % len(files))
sys.exit()
test_array = array(files)
if len(test_array.shape) < 3:
print("Images are of not same shape")
print("Please provide same shape images")
sys.exit()
x_train = files[:number_of_train_images]
x_test = files[number_of_train_images:number_of_images]
x_train_hr = hr_images(x_train)
x_train_hr = normalize(x_train_hr)
x_train_lr = lr_images(x_train, 4)
x_train_lr = normalize(x_train_lr)
x_test_hr = hr_images(x_test)
x_test_hr = normalize(x_test_hr)
x_test_lr = lr_images(x_test, 4)
x_test_lr = normalize(x_test_lr)
return x_train_lr, x_train_hr, x_test_lr, x_test_hr
def load_test_data_for_model(directory, ext, number_of_images = 100):
files = load_data_from_dirs(load_path(directory), ext)
if len(files) < number_of_images:
print("Number of image files are less then you specified")
print("Please reduce number of images to %d" % len(files))
sys.exit()
x_test_hr = hr_images(files)
x_test_hr = normalize(x_test_hr)
x_test_lr = lr_images(files, 4)
x_test_lr = normalize(x_test_lr)
return x_test_lr, x_test_hr
def load_test_data(directory, ext, number_of_images = 100):
files = load_data_from_dirs(load_path(directory), ext)
if len(files) < number_of_images:
print("Number of image files are less then you specified")
print("Please reduce number of images to %d" % len(files))
sys.exit()
x_test_lr = lr_images(files, 4)
x_test_lr = normalize(x_test_lr)
return x_test_lr
# While training save generated image(in form LR, SR, HR)
# Save only one image as sample
def plot_generated_images(output_dir, epoch, generator, x_test_hr, x_test_lr , dim=(1, 3), figsize=(15, 5)):
examples = x_test_hr.shape[0]
print(examples)
value = randint(0, examples)
image_batch_hr = denormalize(x_test_hr)
image_batch_lr = x_test_lr
gen_img = generator.predict(image_batch_lr)
generated_image = denormalize(gen_img)
image_batch_lr = denormalize(image_batch_lr)
plt.figure(figsize=figsize)
plt.subplot(dim[0], dim[1], 1)
plt.imshow(image_batch_lr[value], interpolation='nearest')
plt.axis('off')
plt.subplot(dim[0], dim[1], 2)
plt.imshow(generated_image[value], interpolation='nearest')
plt.axis('off')
plt.subplot(dim[0], dim[1], 3)
plt.imshow(image_batch_hr[value], interpolation='nearest')
plt.axis('off')
plt.tight_layout()
plt.savefig(output_dir + 'generated_image_%d.png' % epoch)
#plt.show()
# Plots and save generated images(in form LR, SR, HR) from model to test the model
# Save output for all images given for testing
def plot_test_generated_images_for_model(output_dir, generator, x_test_hr, x_test_lr , dim=(1, 3), figsize=(15, 5)):
examples = x_test_hr.shape[0]
image_batch_hr = denormalize(x_test_hr)
image_batch_lr = x_test_lr
gen_img = generator.predict(image_batch_lr)
generated_image = denormalize(gen_img)
image_batch_lr = denormalize(image_batch_lr)
for index in range(examples):
plt.figure(figsize=figsize)
plt.subplot(dim[0], dim[1], 1)
plt.imshow(image_batch_lr[index], interpolation='nearest')
plt.axis('off')
plt.subplot(dim[0], dim[1], 2)
plt.imshow(generated_image[index], interpolation='nearest')
plt.axis('off')
plt.subplot(dim[0], dim[1], 3)
plt.imshow(image_batch_hr[index], interpolation='nearest')
plt.axis('off')
plt.tight_layout()
plt.savefig(output_dir + 'test_generated_image_%d.png' % index)
#plt.show()
# Takes LR images and save respective HR images
def plot_test_generated_images(output_dir, generator, x_test_lr, figsize=(5, 5)):
examples = x_test_lr.shape[0]
image_batch_lr = denormalize(x_test_lr)
gen_img = generator.predict(image_batch_lr)
generated_image = denormalize(gen_img)
for index in range(examples):
#plt.figure(figsize=figsize)
plt.imshow(generated_image[index], interpolation='nearest')
plt.axis('off')
plt.tight_layout()
plt.savefig(output_dir + 'high_res_result_image_%d.png' % index)
#plt.show()