forked from fenghansen/ESRGAN-Keras
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
436 lines (384 loc) · 17.4 KB
/
util.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
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
import os
import gc
import numpy as np
from PIL import Image
from random import choice
from skimage.measure import compare_psnr, compare_ssim
from keras.models import load_model
from keras.utils import Sequence
try:
import matplotlib.pyplot as plt
except:
pass
class DataLoader(Sequence):
def __init__(self, datapath, batch_size, height_hr, width_hr, scale, crops_per_image):
"""
:param string datapath: filepath to training images
:param int height_hr: Height of high-resolution images
:param int width_hr: Width of high-resolution images
:param int height_hr: Height of low-resolution images
:param int width_hr: Width of low-resolution images
:param int scale: Upscaling factor
"""
# Store the datapath
self.datapath = datapath
self.batch_size = batch_size
self.height_hr = height_hr
self.height_lr = int(height_hr / scale)
self.width_hr = width_hr
self.width_lr = int(width_hr / scale)
self.scale = scale
self.crops_per_image = crops_per_image
self.total_imgs = None
# Options for resizing
self.options = [Image.NEAREST, Image.BILINEAR, Image.BICUBIC, Image.LANCZOS]
# Check data source
self.img_paths = []
for dirpath, _, filenames in os.walk(self.datapath):
for filename in [f for f in filenames if any(filetype in f.lower() for filetype in ['jpeg', 'png', 'jpg'])]:
self.img_paths.append(os.path.join(dirpath, filename))
self.total_imgs = len(self.img_paths)
print(f">> Found {self.total_imgs} images in dataset")
def random_crop(self, img, random_crop_size):
# Note: image_data_format is 'channel_last'
assert img.shape[2] == 3
height, width = img.shape[0], img.shape[1]
dy, dx = random_crop_size
x = np.random.randint(0, width - dx + 1)
y = np.random.randint(0, height - dy + 1)
return img[y:(y + dy), x:(x + dx), :]
@staticmethod
def scale_lr_imgs(imgs):
"""Scale low-res images prior to passing to SRGAN"""
return imgs / 255.
@staticmethod
def unscale_lr_imgs(imgs):
"""Un-Scale low-res images"""
return imgs * 255
@staticmethod
def scale_hr_imgs(imgs):
"""Scale high-res images prior to passing to SRGAN"""
return imgs / 127.5 - 1
@staticmethod
def unscale_hr_imgs(imgs):
"""Un-Scale high-res images"""
return (imgs + 1.) * 127.5
@staticmethod
def load_img(path, training=True):
img = Image.open(path)
if img.mode != 'RGB':
img = img.convert('RGB')
if training:
flag = np.random.randint(0, 8)
if flag > 3:
img = img.transpose(Image.FLIP_LEFT_RIGHT)
# img = img.rotate(flag * 90)
return np.array(img)
def __len__(self):
return int(self.total_imgs / float(self.batch_size))
def __getitem__(self, idx):
return self.load_batch(idx=idx)
def load_batch(self, idx=0, img_paths=None, training=True, bicubic=False):
"""Loads a batch of images from datapath folder"""
# Starting index to look in
cur_idx = 0
if not img_paths:
cur_idx = idx * self.batch_size
# Scale and pre-process images
imgs_hr, imgs_lr = [], []
while True:
# Check if done with batch
if img_paths is None:
if cur_idx >= self.total_imgs:
cur_idx = 0
if len(imgs_hr) >= self.batch_size:
break
if img_paths is not None and len(imgs_hr) == len(img_paths):
break
try:
# Load image
img_hr = None
if img_paths:
img_hr = self.load_img(img_paths[cur_idx], training)
else:
img_hr = self.load_img(self.img_paths[cur_idx], training)
# Create HR images to go through
img_crops = []
if training:
for i in range(self.crops_per_image):
# print(idx, cur_idx, "Loading crop: ", i)
img_crops.append(self.random_crop(img_hr, (self.height_hr, self.width_hr)))
else:
img_crops = [img_hr]
# Downscale the HR images and save
for img_hr in img_crops:
# TODO: Refactor this so it does not occur multiple times
if img_paths is None:
if cur_idx >= self.total_imgs:
cur_idx = 0
if len(imgs_hr) >= self.batch_size:
break
if img_paths is not None and len(imgs_hr) == len(img_paths):
break
# For LR, do bicubic downsampling
method = Image.BICUBIC if bicubic else choice(self.options)
lr_shape = (int(img_hr.shape[1] / self.scale), int(img_hr.shape[0] / self.scale))
img_lr = Image.fromarray(img_hr.astype(np.uint8))
img_lr = np.array(img_lr.resize(lr_shape, method))
# Scale color values
img_hr = self.scale_hr_imgs(img_hr)
img_lr = self.scale_lr_imgs(img_lr)
# Store images
imgs_hr.append(img_hr)
imgs_lr.append(img_lr)
except Exception as e:
# print(e)
pass
finally:
cur_idx += 1
# Convert to numpy arrays when we are training
# Note: all are cropped to same size, which is not the case when not training
if training:
imgs_hr = np.array(imgs_hr)
imgs_lr = np.array(imgs_lr)
# Return image batch
return imgs_lr, imgs_hr
def plot_test_images(model, loader, datapath_test, test_output, epoch, name='SRGAN', refer_model=None):
"""
:param SRGAN model: The trained SRGAN model
:param DataLoader loader: Instance of DataLoader for loading images
:param str datapath_test: path to folder with testing images
:param string test_output: Directory path for outputting testing images
:param int epoch: Identifier for how long the model has been trained
"""
try:
# SRResNet = load_model('./data/weights/DIV2K_generator.h5')
# Get the location of test images
test_images = [os.path.join(datapath_test, f) for f in os.listdir(datapath_test) if
any(filetype in f.lower() for filetype in ['jpeg', 'png', 'jpg'])]
# Load the images to perform test on images
imgs_lr, imgs_hr = loader.load_batch(img_paths=test_images, training=False, bicubic=True)
# Create super resolution and bicubic interpolation images
imgs_res = []
imgs_sr = []
imgs_bc = []
for i in range(len(test_images)):
# Bicubic interpolation
pil_img = loader.unscale_lr_imgs(imgs_lr[i]).astype('uint8')
pil_img = Image.fromarray(pil_img)
hr_shape = (imgs_hr[i].shape[1], imgs_hr[i].shape[0])
imgs_bc.append(
loader.scale_lr_imgs(
np.array(pil_img.resize(hr_shape, resample=Image.BICUBIC))
)
)
# refer_model prediction
if refer_model is not None:
imgs_res.append(
np.squeeze(
refer_model.predict(
np.expand_dims(imgs_lr[i], 0),
batch_size=1
),
axis=0
)
)
# SRGAN prediction
imgs_sr.append(
np.squeeze(
model.generator.predict(
np.expand_dims(imgs_lr[i], 0),
batch_size=1
),
axis=0
)
)
# Unscale colors values
imgs_lr = [loader.unscale_lr_imgs(img).astype(np.uint8) for img in imgs_lr]
if refer_model is not None:
imgs_res = [loader.unscale_hr_imgs(img).astype(np.uint8) for img in imgs_res]
imgs_hr = [loader.unscale_hr_imgs(img).astype(np.uint8) for img in imgs_hr]
imgs_sr = [loader.unscale_hr_imgs(img).astype(np.uint8) for img in imgs_sr]
if refer_model is None:
# Loop through images
for img_hr, img_lr, img_bc, img_sr, img_path in zip(imgs_hr, imgs_lr, imgs_bc, imgs_sr, test_images):
# Get the filename
filename = os.path.basename(img_path).split(".")[0]
psnr = []
ssim = []
psnr.append(-1)
ssim.append(-1)
psnr.append(compare_psnr(img_hr, img_bc))
psnr.append(compare_psnr(img_hr, img_sr))
ssim.append(compare_ssim(img_hr, img_bc, multichannel=True))
ssim.append(compare_ssim(img_hr, img_sr, multichannel=True))
psnr.append(-1)
ssim.append(-1)
# Images and titles
images = {
'Low Resolution': img_lr,
'Bicubic Interpolation': img_bc,
# 'SRResNet': img_res,
name: img_sr,
'Original': img_hr
}
plt.imsave(os.path.join(test_output, "{}_out.png".format(filename)), img_sr)
# Plot the images. Note: rescaling and using squeeze since we are getting batches of size 1
fig, axes = plt.subplots(1, 4, figsize=(40, 10))
for i, (title, img) in enumerate(images.items()):
axes[i].imshow(img)
axes[i].set_title("{} - {} - psnr:{:.4f} - ssim{:.4f}".format(title, img.shape, psnr[i], ssim[i]))
if psnr[i] is not -1:
print("{} - psnr:{:.4f} - ssim{:.4f}".format(title, psnr[i], ssim[i]))
axes[i].axis('off')
plt.suptitle('{} - Epoch: {}'.format(filename, epoch))
# Save directory
savefile = os.path.join(test_output, "{}-Epoch{}.png".format(filename, epoch))
fig.savefig(savefile)
plt.close()
gc.collect()
else:
# Loop through images
for img_hr, img_bc, img_res, img_sr, img_path in zip(imgs_hr, imgs_bc, imgs_res, imgs_sr, test_images):
# Get the filename
filename = os.path.basename(img_path).split(".")[0]
psnr = []
ssim = []
psnr.append(compare_psnr(img_hr, img_bc))
psnr.append(compare_psnr(img_hr, img_res))
psnr.append(compare_psnr(img_hr, img_sr))
ssim.append(compare_ssim(img_hr, img_bc, multichannel=True))
ssim.append(compare_ssim(img_hr, img_res, multichannel=True))
ssim.append(compare_ssim(img_hr, img_sr, multichannel=True))
psnr.append(-1)
ssim.append(-1)
# Images and titles
images = {
'Bicubic Interpolation': img_bc,
'SR-RRDB': img_res,
'SRGAN': img_sr,
'Original': img_hr
}
plt.imsave(os.path.join(test_output, "{}_out.png".format(filename)), img_sr)
# Plot the images. Note: rescaling and using squeeze since we are getting batches of size 1
fig, axes = plt.subplots(1, 4, figsize=(40, 10))
for i, (title, img) in enumerate(images.items()):
axes[i].imshow(img)
axes[i].set_title("{} - {} - psnr:{:.4f} - ssim{:.4f}".format(title, img.shape, psnr[i], ssim[i]))
axes[i].axis('off')
plt.suptitle('{} - Epoch: {}'.format(filename, epoch))
# Save directory
savefile = os.path.join(test_output, "{}-Epoch{}.png".format(filename, epoch))
fig.savefig(savefile)
plt.close()
gc.collect()
except Exception as e:
print(">> Could not perform printing. Maybe matplotlib is not installed.")
def plot_bigger_images(model, loader, datapath_test, test_output, epoch, name='SRGAN', refer_model=None):
"""
:param SRGAN model: The trained SRGAN model
:param DataLoader loader: Instance of DataLoader for loading images
:param str datapath_test: path to folder with testing images
:param string test_output: Directory path for outputting testing images
:param int epoch: Identifier for how long the model has been trained
"""
try:
# SRResNet = load_model('./data/weights/DIV2K_generator.h5')
# Get the location of test images
test_images = [os.path.join(datapath_test, f) for f in os.listdir(datapath_test) if
any(filetype in f.lower() for filetype in ['jpeg', 'png', 'jpg'])]
# Load the images to perform test on images
_, imgs_hr = loader.load_batch(img_paths=test_images, training=False, bicubic=True)
# Create super resolution and bicubic interpolation images
imgs_res = []
imgs_sr = []
imgs_bc = []
for i in range(len(test_images)):
# Bicubic interpolation
pil_img = loader.unscale_hr_imgs(imgs_hr[i]).astype('uint8')
pil_img = Image.fromarray(pil_img)
hr_shape = (4*imgs_hr[i].shape[1], 4*imgs_hr[i].shape[0])
tmp_hr = loader.scale_lr_imgs(np.array(pil_img))
imgs_bc.append(
loader.scale_lr_imgs(
np.array(pil_img.resize(hr_shape, resample=Image.BICUBIC))
)
)
# refer_model prediction
if refer_model is not None:
imgs_res.append(
np.squeeze(
refer_model.predict(
np.expand_dims(tmp_hr, 0),
batch_size=1
),
axis=0
)
)
# SRGAN prediction
imgs_sr.append(
np.squeeze(
model.generator.predict(
np.expand_dims(tmp_hr, 0),
batch_size=1
),
axis=0
)
)
# Unscale colors values
imgs_hr = [loader.unscale_hr_imgs(img).astype(np.uint8) for img in imgs_hr]
if refer_model is not None:
imgs_res = [loader.unscale_hr_imgs(img).astype(np.uint8) for img in imgs_res]
imgs_sr = [loader.unscale_hr_imgs(img).astype(np.uint8) for img in imgs_sr]
if refer_model is None:
# Loop through images
for img_hr, img_bc, img_sr, img_path in zip(imgs_hr, imgs_bc, imgs_sr, test_images):
# Get the filename
filename = os.path.basename(img_path).split(".")[0]
# Images and titles
images = {
'Original': img_hr,
'Bicubic Interpolation': img_bc,
# 'SRResNet': img_res,
name: img_sr,
}
plt.imsave(os.path.join(test_output, "{}_out.png".format(filename)), img_sr)
# Plot the images. Note: rescaling and using squeeze since we are getting batches of size 1
fig, axes = plt.subplots(1, 3, figsize=(30, 10))
for i, (title, img) in enumerate(images.items()):
axes[i].imshow(img)
axes[i].set_title("{} - {}".format(title, img.shape))
axes[i].axis('off')
plt.suptitle('{}'.format(filename))
# Save directory
savefile = os.path.join(test_output, "{}.png".format(filename))
fig.savefig(savefile)
plt.close()
gc.collect()
else:
# Loop through images
for img_hr, img_bc, img_res, img_sr, img_path in zip(imgs_hr, imgs_bc, imgs_res, imgs_sr, test_images):
# Get the filename
filename = os.path.basename(img_path).split(".")[0]
# Images and titles
images = {
'Original': img_hr,
'Bicubic Interpolation': img_bc,
'SR-RRDB': img_res,
'SRGAN': img_sr,
}
plt.imsave(os.path.join(test_output, "{}_out.png".format(filename)), img_sr)
# Plot the images. Note: rescaling and using squeeze since we are getting batches of size 1
fig, axes = plt.subplots(1, 4, figsize=(40, 10))
for i, (title, img) in enumerate(images.items()):
axes[i].imshow(img)
axes[i].set_title("{} - {}".format(title, img.shape))
axes[i].axis('off')
plt.suptitle('{}'.format(filename))
# Save directory
savefile = os.path.join(test_output, "{}.png".format(filename))
fig.savefig(savefile)
plt.close()
gc.collect()
except Exception as e:
print(">> Could not perform printing. Maybe matplotlib is not installed.")