-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpred_SRCNN.py
126 lines (87 loc) · 3.22 KB
/
pred_SRCNN.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
import numpy as np
import matplotlib.pyplot as plt
import cv2
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
from tensorflow.keras.models import load_model
model = load_model('./results/epoch10000_SRCNN.h5')
framObjTrain = {'img': [],
'mask': []
}
def LoadData(frameObj=None, imgPath=None, maskPath=None, shape=256):
imgNames = os.listdir(imgPath)
maskNames = []
imgAddr = imgPath + '/'
maskAddr = maskPath + '/'
for i in range(len(imgNames)):
img = plt.imread(imgAddr + imgNames[i])
mask = plt.imread(maskAddr + imgNames[i])
img = cv2.resize(img, (shape, shape))
mask = cv2.resize(mask, (shape, shape))
frameObj['img'].append(img)
frameObj['mask'].append(mask)
return frameObj
def predict(test_X, test_y, shape=256):
img = test_X[:5]
imgProc = np.array(img)
mask = test_y[:5]
predictions = model.predict(imgProc)
for i in range(len(predictions)):
predictions[i] = cv2.merge((predictions[i, :, :, 0], predictions[i, :, :, 1], predictions[i, :, :, 2]))
return predictions, imgProc, mask
def Plotter(img, predMask, groundTruth):
plt.figure(figsize=(20, 10))
plt.subplot(1, 3, 1)
plt.imshow(img)
plt.title('Low Res image')
## Adding Image sharpening step here
## it is a sharpening filter
filter = np.array([[-1, -1, -1], [-1, 9, -1], [-1, -1, -1]])
imgSharpen = cv2.filter2D(predMask, -1, filter)
plt.subplot(1, 3, 2)
plt.imshow(imgSharpen)
plt.title('Predicted High Res Image')
plt.subplot(1, 3, 3)
plt.imshow(groundTruth)
plt.title('actual High Res Image')
plt.show()
framObjTrain = LoadData( framObjTrain, imgPath = './Data/LR', maskPath = './Data/HR', shape = 256)
test_X = np.array(framObjTrain['img'])[:1]
test_y = np.array(framObjTrain['mask'])[:1]
print(test_X.shape, test_y.shape)
prediction, actuals, masks = predict(test_X, test_y, model)
Plotter(actuals[0], prediction[0], masks[0])
"""
PSNR SSIM LCS version
"""
import math
from skimage.metrics import structural_similarity as ssim
def psnr(img1, img2, max_val):
mse = np.mean( (img1 - img2) ** 2 )
if mse == 0:
return 100
PIXEL_MAX = max_val
return 20 * math.log10(PIXEL_MAX / math.sqrt(mse))
predictions = model.predict(test_X)
for i in range(len(predictions)):
# Input data
test_X = np.array(framObjTrain['img'])[:1]
sq_test_X = np.squeeze(test_X)
input_max_val = sq_test_X.max()
print('1', sq_test_X.shape)
# Target data
test_y = np.array(framObjTrain['mask'])[:1]
sq_test_y = np.squeeze(test_y)
target_max_val = sq_test_y.max()
print('3', sq_test_y.shape)
# Pred data
pred = model.predict(test_X)
pred_max_val = pred.max()
final_pred = np.squeeze(pred)
print('5', final_pred.shape)
input_psnr = psnr(sq_test_y, sq_test_X, input_max_val)
input_ssim = ssim(sq_test_y, sq_test_X, data_range=input_max_val, multichannel=True)
pred_psnr = psnr(sq_test_y, final_pred, pred_max_val)
pred_ssim = ssim(sq_test_y, final_pred, data_range=pred_max_val, multichannel=True)
result = [round(input_psnr, 4), round(pred_psnr, 4), round(input_ssim, 4), round(pred_ssim, 4)]
print('Result : \n', result)