-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageSuperResolution(SoC).py
679 lines (475 loc) · 18.7 KB
/
ImageSuperResolution(SoC).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
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
#!/usr/bin/env python
# coding: utf-8
import matplotlib.pyplot as plt
import keras
import random
import pandas as pd
import numpy as np
from functools import partial
from math import *
import cv2 as cv
from PIL import Image, ImageOps
from IPython.display import display
import tensorflow as tf
from tensorflow.keras import datasets, layers, models
import tensorflow_datasets as tfds
from scipy import stats
import sklearn
import sklearn.feature_extraction
def show_bgrimg(image):#for diplaying bgr images
"""
Displays a BGR image given the array (numpy or otherwise) corresponding
to the image, using the OpenCV library.
Args:
An array of an image.
Returns:
A BGR image: The rendition of the image file in the Jupyter Notebook.
How to be used:
For example,
>> arr = cv.imread("butterfly.png")
>> show_bgrimg(arr)
will display the image in "butterfly.png" in BGR format.
"""
plt.figure(figsize=(12, 10), dpi=80)
plt.imshow(cv.cvtColor(image, cv.COLOR_BGR2RGB))
plt.show()
def show_rgbimg(image):#for displaying rgb images
"""
Displays a RGB image given the array corresponding
to the image, using the OpenCV library.
Args:
An array of an image.
Returns:
A RGB image: The rendition of the image file in the Jupyter Notebook.
How to be used:
For example,
>> arr = cv.imread("butterfly.png")
>> show_rgbimg(arr)
will display the image in "butterfly.png" in RGB format.
"""
plt.figure(figsize=(12, 10), dpi=80)
plt.imshow(image)
plt.show()
def show_bnw(image):#for displaying grayscale images
"""
Displays a grayscale image given the array corresponding
to the image, using the OpenCV library.
Args:
An array of an image.
Returns:
A grayscale image: The rendition of the image file in the Jupyter Notebook.
How to be used:
For example,
>> arr = cv.imread("butterfly.png")
>> show_bnw(arr)
will display the image in "butterfly.png" in grayscale format.
"""
plt.figure(figsize=(12, 10), dpi=80)
plt.imshow(image,cmap="gray")
plt.show()
def PSNR_tf(y_grdtrth,y_pred):
"""
Returns the Peak Signal to Noise Ratio (PSNR)
given 2 lists on the basis of the mean squared
error between them.(using tensorflow)
Args:
2 python lists (y_grdtrth, standing for the "ground Truth"
and y_pred, standing for the "prediction").
Returns:
A float type number
"""
m = tf.keras.metrics.MeanSquaredError()
m.update_state(y_grdtrth,y_pred)
MSE=m.result().numpy()
result=10*log10((255**2)/MSE)
return result
def PSNR(original, compressed):
"""
Returns the Peak Signal to Noise Ratio (PSNR)
given 2 lists on the basis of the mean squared
error between them.(using numpy)
Args:
2 python lists (y_grdtrth, standing for the "ground Truth"
and y_pred, standing for the "prediction").
Returns:
A float type number
"""
mse = np.mean((original - compressed) ** 2)
if(mse == 0): # MSE is zero means no noise is present in the signal .
# Therefore PSNR have no importance.
return 100
max_pixel = 255.0
psnr = 20 * log10(max_pixel / sqrt(mse))
return psnr
#sample image
sample=cv.imread('sage.png')
#function to finc DCT(discrete cosine transform) of an image
def convert_img_to_dct(image):
"""
Refer documentation for cv.dct and image.astype("float").
"""
imagefloat=image.astype("float")
dct_img=cv.dct(imagefloat)
return dct_img
#function to convert Img from DCT
def convert_dct_to_img(dct_img):
"""
Refer documentation for cv.idct.
"""
image=cv.idct(dct_img)
return image
#function to convert to ycbcr
def conv_ycbcr(image):
"""
Returns a numpy array corresponding to the YCbCr representation of an image,
given an array of an image.
Args:
An array of an image.
Returns:
A luminance channel in the form of numpy array of the image in it's YCbCr representation.
How to be used:
>> arr = cv.imread("butterfly.png")
>> print(conv_ycbcr(arr)) #a,b..z are for representation purposes only.
[[[a],[b],...,[z]], [[a],[b],...,[z]], ..., [[a],[b],...,[z]]]
>> print(conv_ycbcr(arr).shape) #Assume "butterfly.png" is a 256x256 image.
(256,256,1)
"""
height=image.shape[0]
width=image.shape[1]
ycbcr_img=cv.cvtColor(image, cv.COLOR_BGR2YCR_CB)
return np.resize((ycbcr_img[:,:,0]).flatten(),(height,width,1))
def patch_extraction(image,subw,subh):
"""
Extracts non-overlapping patches of width subw and height subh (discards remaining part)
Args:
ndarray, patch width, patch height
Returns:
A grayscale image: The rendition of the image file in the Jupyter Notebook.
How to be used:
For example,
>> arr = cv.imread("butterfly.png")
>> show_bnw(arr)
will display the image in "butterfly.png" in grayscale format.
"""
result=[]
n_x=image.shape[1]//subw
n_y=image.shape[0]//subh
for i in range(n_x):
for j in range(n_y):
result.append(image[j*subh:(j+1)*subh:1,i*subw:(i+1)*subw:1])
return result
#function for bilateral filter and bicubic interpolation
def preprocessing(image,scale=2):
"""
Returns a numpy array corresponding to an image scaled up by 'scale'
of the image given to it as an input.
Args:
An array of an image.
Returns:
A numpy array of the scaled up image.
How to be used:
>> arr = cv.imread("butterfly.png")
>> preprocessing(arr) #a,b..z are for representation purposes only.
[[[a],[b],...,[z]], [[a],[b],...,[z]], ..., [[a],[b],...,[z]]]
>> print(preprocessing(arr).shape) #Assume "butterfly.png" is a 256x256 image, and
>> # scale is 2.
(512,512,1)
"""
image=cv.cvtColor(image, cv.COLOR_BGR2YCR_CB)
image=image[:,:,0]
width=image.shape[1]
height=image.shape[0]
resized_img=cv.resize(image,(width*scale,height*scale), interpolation=cv.INTER_CUBIC)
resized_img=np.resize(resized_img.flatten(),(height*scale,width*scale,1))
resized_img = resized_img.astype('float32')
return resized_img
def bicubic_resize(layer_output,image):
"""
Returns a numpy array corresponding to an image of dimensions 'layer_output'
from the image given to it as an input.
Args:
A pair of integers, An array of an image.
Returns:
A numpy array of the image of the pair of integers.
How to be used:
>> arr = cv.imread("butterfly.png")
>> bicubic_resize((100,100),arr) #a,b..z are for representation purposes only.
[[[a],[b],...,[z]], [[a],[b],...,[z]], ..., [[a],[b],...,[z]]]
>> print(bicubic_resize((100,100),arr).shape) #Assume "butterfly.png" is a 256x256 image
(100,100,1)
"""
resized_img=cv.resize(image, layer_output, interpolation=cv.INTER_CUBIC)
resized_img=np.resize(resized_img.flatten(),tuple(list(layer_output)+[1]))
return resized_img
#loading datasets
tfds.list_builders()
ds,ds_info = tfds.load('div2k', split='train', shuffle_files=True,with_info=True)
print(type(ds))
print(ds_info)
#to show the functions of "ds"
print(dir(ds))
#testing if the dataset has loaded
for example in ds.take(1): # example is `{'image': tf.Tensor, 'label': tf.Tensor}`
print(list(example.keys()))
hr_img = example["hr"]
lr_img = example["lr"]
print(hr_img.shape, lr_img.shape)
show_rgbimg(np.array(hr_img))
show_rgbimg(np.array(lr_img))
# How to extract data from a tensorflow dataset: https://www.tensorflow.org/datasets/overview
#extracting images from dataset
train_x=[]
train_y=[]
no_of_images=100
i=0
for sample in ds:
train_x.append(np.array(sample["lr"]))
train_y.append(np.array(sample["hr"]))
i+=1
if(i==no_of_images):
break
#shape of the images stored
for i in range(5):
print(train_x[i].shape,train_y[i].shape)
#initializing parameters
batch_size=30
height=50
width=50
#pre-processing
processed_images_x=list(map(preprocessing,train_x))
grayscale_images_y=list(map(conv_ycbcr,train_y))
show_bnw(processed_images_x[1])
show_bnw(grayscale_images_y[1])
#cropping
cropped_images_x=np.array(patch_extraction(processed_images_x[0],height,width))
cropped_images_y=np.array(patch_extraction(grayscale_images_y[0],height,width))
for element in processed_images_x[1:]:
cropped_images_x=np.append(cropped_images_x,patch_extraction(element,height,width),axis=0)
for element in grayscale_images_y[1:]:
cropped_images_y=np.append(cropped_images_y,patch_extraction(element,height,width),axis=0)
shape_x=cropped_images_x.shape
shape_y=cropped_images_y.shape
normalized_cropped_images_x=np.resize(stats.zscore(cropped_images_x.flatten()),shape_x)
normalized_cropped_images_y=cropped_images_y/255
#network architecture
network=models.Sequential()
network.add(layers.Conv2D(64, (9,9),activation='relu',input_shape=(height,width,1),name='first'))
network.add(layers.Conv2D(32,(1,1),activation='relu',name='second'))
network.add(layers.Conv2D(1,(5,5),activation='relu',name='last'))
#summary of achitecture
network.summary()
#output dimensions of the last layer
last_output=network.get_layer('last').output_shape[1:3]
resize=partial(bicubic_resize,last_output)
resized_normalized_cropped_images_y=np.array(list(map(resize,normalized_cropped_images_y)))
#network training
network.compile(optimizer='adam',
loss=tf.keras.losses.MeanSquaredError(),
metrics=[tf.keras.metrics.MeanSquaredError()])
history = network.fit(normalized_cropped_images_x[:100000], resized_normalized_cropped_images_y[:100000],batch_size=30,epochs=20)
#saving network
network.save('without_dct_E20_B30_I100000')
#loading the saved network
network=models.load_model('without_dct_E20_B30_I100000')
#checkin if network is loaded
network.summary()
#getting the predictions
predictions=network.predict(normalized_cropped_images_x[100000:])
#testing
z=105010
print("Processed input")
show_bnw(np.resize(cropped_images_x[z].flatten(),(height,width)))
print("Predicted")
show_bnw(np.resize(predictions[z-100000].flatten(),last_output))
print("Ground Truth")
show_bnw(np.resize((resized_normalized_cropped_images_y[z]*255).flatten(),last_output))
#evaluation
results = network.evaluate(normalized_cropped_images_x[100000:],resized_normalized_cropped_images_y[100000:], batch_size=30)
print(results)
#final class for super resolution
class SuperResolution:
"""
Class for predicting the output image of an input array (image) based on a
trained CNN ('without_dct_E20_B30_I100000') above.
Args:
image: An array of an image.
type : How the output is to be rendered. Default setting is 'bgr'.
scale : The upscale factor of resolution. Default setting is 2,
but no point in changing it as this is a fixed hyperparameter of our CNN.
"""
def __init__(self, image, type='bgr',scale=2):
self.image=image
self.network=models.load_model('without_dct_E20_B30_I100000')
self.shape=image.shape
self.input_dim=(50,50)
self.type=type
self.scale=2
self.n_x=(self.image.shape[1]*self.scale)//(50)
self.n_y=(self.image.shape[0]*self.scale)//(50)
self.output=None
self.output_cb=None
self.output_cr=None
self.output_y=None
self.predictions=None
#displaying functions
def show_bgrimg(self,image):#for diplaying bgr images
plt.figure(figsize=(12, 10), dpi=80)
plt.imshow(cv.cvtColor(image, cv.COLOR_BGR2RGB))
plt.show()
def show_rgbimg(self,image):#for displaying rgb images
plt.figure(figsize=(12, 10), dpi=80)
plt.imshow(image)
plt.show()
def show_bnw(self,image):#for displaying grayscale images
plt.figure(figsize=(12, 10), dpi=80)
plt.imshow(image,cmap="gray")
plt.show()
#summary of model to be used for super resolution
def model_summary(self):
self.network.summary()
#dividing the given image into sub-images
def __patch_extraction(self,image,stride=0):
"""
Returns a numpy array of patches of the image.
Args:
self, image
Returns:
A numpy array of patches of the image.
How to be used:
For internal usage by the class's methods. Not part of the API interface.
"""
result=[]
print("image",image.shape)
resized_img=cv.resize(image, ((image.shape[1]//50)*50,(image.shape[0]//50)*50), interpolation=cv.INTER_CUBIC)
resized_img=np.resize(resized_img,((image.shape[0]//50)*50,(image.shape[1]//50)*50,1))
print("cv",resized_img.shape)
print(self.n_x)
if stride==0:
for i in range(image.shape[0]//50):
for j in range(image.shape[1]//50):
result.append(resized_img[i*50:(i+1)*50,j*50:(j+1)*50])
else:
for i in range((image.shape[0]//50)*2-1):
for j in range((image.shape[1]//50)*2-1):
result.append(resized_img[i*25:i*25+50,j*25:j*25+50])
return np.array(result)
def __preprocessing(self):
ycrcb_img=None
if self.type=='bgr':
ycrcb_img=cv.cvtColor(self.image, cv.COLOR_BGR2YCR_CB)
elif self.type=='rgb':
yrbcb_img=cv.cvtColor(self.image, cv.COLOR_RGB2YCR_CB)
else:
assert(False)
width=self.shape[1]
height=self.shape[0]
luminance=ycrcb_img[:,:,0]
self.output_cr=cv.resize(ycrcb_img[:,:,2],(width*self.scale,height*self.scale),interpolation=cv.INTER_CUBIC)
self.output_cb=cv.resize(ycrcb_img[:,:,1],(width*self.scale,height*self.scale),interpolation=cv.INTER_CUBIC)
resized_img=cv.resize(luminance,(width*self.scale,height*self.scale), interpolation=cv.INTER_CUBIC)
resized_img=np.resize(resized_img.flatten(),(height*self.scale,width*self.scale,1))
resized_img = resized_img.astype('float32')
print("resized",resized_img.shape)
return resized_img
def __reconstruction(self,patches):
img_width=self.n_x*38
img_height=self.n_y*38
result=np.zeros((img_height,img_width))
try:
for i in range(self.n_y*2-1):
for j in range(self.n_x*2-1):
req_patch=patches[i*(self.n_x*2-1)+j]
for y in range(38):
for x in range(38):
result[i*19+y,j*19+x]+=req_patch[y,x,0]
except IndexError:
print("Index Error")
return result/4
def __thresholding(self,image,max_intensity):
"""
Returns an ndarray with all the elements greater thn max_intesity clipped to max_intensity
Args:
image: ndarray max_intensity: required max value where elements are to be clipped
Returns:
An array of the predicted super-resolved image
"""
i,j=0,0
for rows in image:
i=0
for e in rows:
if e>max_intensity:
image[j,i]=max_intensity
i+=1
j+=1
return image
#predicting the higher resolution image
def prediction(self):
"""
Returns the prediction of our input image's super-resolved version
using the the CNN trained earlier.
Args:
no parameters (member function)
Returns:
An array of the predicted super-resolved image
How to be used:
>> arr_x = np.array(cv.imread("building.png"))#Assume 1020x888 pixels
>> test = SuperResolution(arr_x)
>> test.prediction()
resized (2040, 1776, 1)
image (2040, 1776, 1)
cv (2000, 1750, 1)
35
patches (1400, 50, 50, 1)
executed bgr
(2040, 1776, 3)
[[[a],[b],...,[z]], [[a],[b],...,[z]], ..., [[a],[b],...,[z]]]
"""
patches=np.array(sklearn.feature_extraction.image.extract_patches_2d(self.__preprocessing(),(50,50)))
normalized=stats.zscore(np.array(patches).flatten())
normalized=np.resize(normalized,(patches.shape[0],patches.shape[1],patches.shape[2],1))
predictions=self.network.predict(normalized)
self.predictions=predictions*255
resized_predictions=np.array(list(map(partial(bicubic_resize,(50,50)),predictions)))
resized_predictions=np.resize(resized_predictions,tuple(list(resized_predictions.shape)[:3]))
result=sklearn.feature_extraction.image.reconstruct_from_patches_2d(resized_predictions,(self.n_y*50,self.n_x*50))
result=self.__thresholding(result*255,255)
result=cv.resize(result,(self.shape[1]*self.scale,self.shape[0]*self.scale),interpolation=cv.INTER_CUBIC)
self.output_y=result.astype(np.uint8)
self.output=np.dstack((self.output_y,self.output_cr,self.output_cb))
if self.type=="bgr":
self.output=cv.cvtColor(self.output,cv.COLOR_YCR_CB2BGR)
elif self.type=='rgb':
self.output=cv.cvtColor(self.output,cv.COLOR_YCR_CB2RGB)
return self.output
def compare_psnr(self,expected):
return tf.image.psnr(expected,self.output,255)
#testing the class
i=70
test=SuperResolution(train_x[i][:100,:100])
test.prediction()
print("Bicubic")
show_bnw(processed_images_x[i][:200,:200])
print("Output")
show_bgrimg(test.output)
print("Grayscale Y")
show_bnw(grayscale_images_y[i][:200,:200])
print("Train_Y")
show_rgbimg(train_y[i][:200,:200])
print("Y")
show_rgbimg(train_y[i][:200,:200])
print("X Bicubic Whole")
show_rgbimg(cv.resize(train_x[i][:100,:100],(200,200),interpolation=cv.INTER_CUBIC))
bicubic=cv.resize(train_x[i],(train_y[i].shape[1],train_y[i].shape[0]),interpolation=cv.INTER_CUBIC)[:200,:200]
print(PSNR(train_y[i][:200,:200],test.output))
print(PSNR(train_y[i][:200,:200],bicubic))
#evaluating
pred_avg=0
bicubic_avg=0
for i in range(20):
test=SuperResolution(train_x[i][:100,:100])
test.prediction()
pred_avg+=PSNR(test.output,train_y[i][:200,:200])
bicubic_avg+=PSNR(cv.resize(train_x[i],(train_y[i].shape[1],train_y[i].shape[0]),interpolation=cv.INTER_CUBIC)[:200,:200],train_y[i][:200,:200])
print(i)
print(pred_avg/20)
print(bicubic_avg/20)