-
Notifications
You must be signed in to change notification settings - Fork 2
/
validateMR.py
210 lines (166 loc) · 7.25 KB
/
validateMR.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
from keras.models import load_model
from keras.layers import Lambda
from keras.preprocessing import image
import keras
import tensorflow as tf
import numpy as np
#import matplotlib.pyplot as plt
from PIL import Image
from keras import backend as K
import os
import math
oti = 'adam'
lr = 0.0001
def get_cpsnr(RGB1,RGB2,b):
RGB1 = RGB1.astype('double');
RGB2 = RGB2.astype('double');
diff = RGB1[b:-1-b,b:-1-b,:]-RGB2[b:-1-b,b:-1-b,:];
num = np.size(diff[:,:,1]);
MSE_R = np.sum( np.power(diff[:,:,2],2) );
MSE_G = np.sum( np.power(diff[:,:,1],2) );
MSE_B = np.sum( np.power(diff[:,:,0],2) );
CMSE = (MSE_R + MSE_G + MSE_B)/(3*num);
CPSNR = 10*math.log(255*255/CMSE,10);
return CPSNR;
def create_model():
inputs = keras.Input(shape=(None,None,1))
ini = keras.layers.Conv2D(filters = 128, #feature map number
kernel_size = 3,
strides = 1, # 2
padding = 'same',
input_shape = (None,None,1))(inputs)
x = keras.layers.PReLU(alpha_initializer='zeros', alpha_regularizer=None, alpha_constraint=None, shared_axes=[1,2])(ini)
ini = keras.layers.Conv2D(filters = 64, #feature map number
kernel_size = 3,
strides = 1, # 2
activation = 'relu',
padding = 'same',
input_shape = (None,None,128))(x)
x = keras.layers.PReLU(alpha_initializer='zeros', alpha_regularizer=None, alpha_constraint=None, shared_axes=[1,2])(ini)
##Subpixel Construction
sub_layer_2 = Lambda(lambda x:tf.nn.space_to_depth(x,2))
init = sub_layer_2(inputs=x)
##Learning Residual (DCNN)
####Conv 3x3x64x64 + PReLu
x = keras.layers.Conv2D(filters = 64, #feature map number
kernel_size = 3,
strides = 1, # 2
padding = 'same',
input_shape = (None,None,256))(init)
x = keras.layers.PReLU(alpha_initializer='zeros', alpha_regularizer=None, alpha_constraint=None, shared_axes=[1,2])(x)
x = keras.layers.Conv2D(filters = 64, #feature map number
kernel_size = 3,
strides = 1, # 2
padding = 'same',
input_shape = (None,None,64))(x)
x = keras.layers.PReLU(alpha_initializer='zeros', alpha_regularizer=None, alpha_constraint=None, shared_axes=[1,2])(x)
####Residual Block
for i in range(6):
Conv1 = keras.layers.Conv2D(filters = 128, #feature map number
kernel_size = 3,
strides = 1, # 2
padding = 'same',
input_shape = (None,None,64))(x)
PReLu = keras.layers.PReLU(alpha_initializer='zeros', alpha_regularizer=None, alpha_constraint=None, shared_axes=[1,2])(Conv1)
Conv2 = keras.layers.Conv2D(filters = 64, #feature map number
kernel_size = 3,
strides = 1, # 2
padding = 'same',
input_shape = (None,None,128))(PReLu)
x = keras.layers.Add()([Conv2,x])
x = keras.layers.PReLU(alpha_initializer='zeros', alpha_regularizer=None, alpha_constraint=None, shared_axes=[1,2])(x)
####Conv 3x3x64x64 + PReLu
x = keras.layers.Conv2D(filters = 64, #feature map number
kernel_size = 3,
strides = 1, # 2
padding = 'same',
input_shape = (None,None,64))(x)
##########1->64
x = keras.layers.PReLU(alpha_initializer='zeros', alpha_regularizer=None, alpha_constraint=None, shared_axes=[1,2])(x)
x = keras.layers.Conv2D(filters = 64, #feature map number
kernel_size = 3,
strides = 1, # 2
padding = 'same',
input_shape = (None,None,64))(x)
##########1->64
x = keras.layers.PReLU(alpha_initializer='zeros', alpha_regularizer=None, alpha_constraint=None, shared_axes=[1,2])(x)
####Conv 3x3x64x48
x = keras.layers.Conv2D(filters = 48, #feature map number
kernel_size = 3,
strides = 1,
padding = 'same',
input_shape = (None,None,64))(x)
###########Learning Residual (DCNN)############
##Recovery From Subpixel
sub_layer = Lambda(lambda x:tf.nn.depth_to_space(x,4))
Residual_Output = sub_layer(inputs=x)
#Residual_Output_ = keras.layers.Conv2D(filters = 3, #feature map number
# kernel_size = 3,
# strides = 1, # 2
# padding = 'same',
# activation ='relu',
# input_shape = (None,None,3))(Residual_Output)
##Initial Prediction
R = Lambda(lambda x: x[:,:,:,0])(init)
G = Lambda(lambda x: x[:,:,:,1:3])(init)
G = Lambda(lambda x: K.mean(x, axis=3))(G)
B = Lambda(lambda x: x[:,:,:,3])(init)
print(init.shape)
print(R.shape)
print(G.shape)
print(B.shape)
R = Lambda(lambda x: tf.expand_dims(x, -1))(R)
G = Lambda(lambda x: tf.expand_dims(x, -1))(G)
B = Lambda(lambda x: tf.expand_dims(x, -1))(B)
#rgb = tf.keras.backend.stack((R, G,B),axis = 3)
print(R.shape)
rg = keras.layers.Concatenate(axis = 3)([R , G])
rgb = keras.layers.Concatenate(axis = 3)([rg,B])
print(rgb.shape)
Coarse_Output = keras.layers.UpSampling2D(size=(4, 4),interpolation="bilinear")(rgb)
## +
outputs = keras.layers.Add()([Residual_Output,Coarse_Output])
model = keras.Model(inputs=inputs, outputs=outputs, name="mnist_model")
return model
model = create_model()
model.load_weights('./model.hdf5')
#model = keras.Model(inputs=(64,64,1), outputs=(128,128,3), name="mnist_model")
#model.load_weights('trashup.h5')
#model.compile(optimizer=keras.optimizers.Nadam(lr), loss = 'mean_squared_error', metrics = ['mse'])
# test_image = image.load_img('./p/im_002_0.png', target_size = (64, 64))
# test_image = image.img_to_array(test_image)
# print(test_image)
# test_image = test_image[:,:,0]
# test_image = test_image[np.newaxis,:,:,np.newaxis]
# result = model.predict(test_image)
# print(result.shape)
# out = image.array_to_img(result[0])
# plt.imshow(out)
# plt.show()
if not os.path.exists('MRout'):
os.makedirs('MRout')
sum = 0
entries = os.listdir('./Set5p/')
for entry in entries:
path = './Set5p/'+entry
test_image = image.load_img(path)
test_image = image.img_to_array(test_image)
test_image = test_image[:,:,0]
test_image = test_image[np.newaxis,:,:,np.newaxis]
out = model.predict(test_image)
path = './Set5/'+entry
ori = image.load_img(path)
ori = image.img_to_array(ori)
out = out[0];
out[out>255] = 255;
out[out<0] = 0;
#out = out*255;
print(entry,get_cpsnr(out,ori,10) );
sum+=get_cpsnr(out,ori,10);
out = image.array_to_img(out)
path = './Set5out/'+entry
out.save(path)
# plt.imshow(out)
# plt.show()
print('avg')
print(sum/5)