-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathuse_SRCNN.py
276 lines (226 loc) · 9.45 KB
/
use_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
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
import tensorflow as tf
import numpy as np
import os
import time
import cv2 as cv
import h5py
import scipy.misc
import scipy.ndimage
from prepare_test import im2double, modcrop, revert, modcrop_color
from net import SRCNN, load_data, load_ckpt
import math
# Initialization
size_input = 33
size_label = 21
# images = tf.placeholder(tf.float32, [1536, 1536], name='images')
# labels = tf.placeholder(tf.float32, [None, size_label, size_label, 1], name='labels')
learning_rate = 1e-4
num_epoch = 15000
batch_size = 128
num_training = 21712
num_testing = 1113
ckpt_dir = './checkpoint/'
multiplier = 2
# Read and prepare the test image for SRCNN.
def prepare_data(path):
# Settings.
data = []
label = []
padding = abs(size_input - size_label) / 2
stride = 21
# Read in image and convert to ycrcb color space.
img_input = cv.imread(path)
im = cv.cvtColor(img_input, cv.COLOR_BGR2YCR_CB)
img = im2double(im) # Only use the luminance value.
# Create groundtruth and baseline image.
im_label = modcrop_color(img, scale=multiplier)
color_base = modcrop_color(im, scale=multiplier)
size = im_label.shape
h = size[0]
w = size[1]
im_blur = scipy.misc.imresize(im_label, 1 / multiplier, interp='bicubic')
im_input = scipy.misc.imresize(im_blur, multiplier * 1.0, interp='bicubic')
# print('im_temp shape:', im_temp.shape)
# print('im_input shape:', im_input.shape)
# Generate subimages.
# for x in range(0, h - size_input, stride):
# for y in range(0, w - size_input, stride):
# subim_input = im_input[x : x + size_input, y : y + size_input]
# subim_label = im_label[int(x + padding) : int(x + padding + size_label), int(y + padding) : int(y + padding + size_label)]
# subim_input = subim_input.reshape([size_input, size_input, 1])
# subim_label = subim_label.reshape([size_label, size_label, 1])
# data.append(subim_input)
# label.append(subim_label)
data = np.array(im_input[:,:,0]).reshape([1, h, w, 1])
color = np.array(color_base[:,:,1:3])
label = np.array(modcrop_color(img_input))
# Write to HDF5 file.
# savepath = os.path.join(os.getcwd(), 'checkpoint/test_image.h5')
# with h5py.File(savepath, 'w') as hf:
# hf.create_dataset('data', data=data)
# hf.create_dataset('label', data=label)
return data, label, color
# Prepare original data without blurring.
def prepare_raw(path):
# Settings.
data = []
color = []
# Read in image and convert to ycrcb color space.
img = cv.imread(path)
im = cv.cvtColor(img, cv.COLOR_BGR2YCR_CB)
img = im2double(im) # Only use the luminance value.
size = img.shape
img_temp = scipy.misc.imresize(img, [size[0] * multiplier, size[1] * multiplier], interp='bicubic')
color_temp = scipy.misc.imresize(im, [size[0] * multiplier, size[1] * multiplier], interp='bicubic')
im_label = img_temp[:, :, 0]
im_color = color_temp[:, :, 1:3]
data = np.array(im_label).reshape([1, img.shape[0] * multiplier, img.shape[1] * multiplier, 1])
color = np.array(im_color)
return data, color
# Use the trained model to generate super-resolutioned image.
def generate_SR(path, save_dir):
# Initialization.
images = tf.placeholder(tf.float32, [None, None, None, 1], name='images')
model = SRCNN(images)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print('Generating super-resolutioned image...')
# Load the saved checkpoint.
saver = tf.train.Saver()
if load_ckpt(sess, ckpt_dir, saver):
print('Successfully loaded checkpoint.')
else:
print('Failed to load checkpoint.')
if os.path.isfile(path):
test_data, test_label, color = prepare_data(path)
print('test_data has shape:', test_data.shape, 'label has shape:', test_label.shape)
print('color has shape:', color.shape)
# Generate super-resolutioned image.
conv_out = model.eval({images: test_data}) # Result in patch of size 21x21.
conv_out = conv_out.squeeze()
result_bw = revert(conv_out)
result = np.zeros([result_bw.shape[0], result_bw.shape[1], 3], dtype=np.uint8)
result[:, :, 0] = result_bw
result[:, :, 1:3] = color
result = cv.cvtColor(result, cv.COLOR_YCrCb2RGB)
bicubic = scipy.misc.imresize(test_label, 1 / multiplier, interp='bicubic')
bicubic = scipy.misc.imresize(bicubic, multiplier * 1.0, interp='bicubic')
bicubic = cv.cvtColor(bicubic, cv.COLOR_BGR2RGB)
# Save the image.
save_path = os.path.join(save_dir, os.path.basename(path))
scipy.misc.imsave(save_path, result)
bicubic_path = os.path.join(save_dir, 'bicubic_' + os.path.basename(path))
scipy.misc.imsave(bicubic_path, bicubic)
print('Finished testing', path)
elif os.path.isdir(path):
for root, dirs, files in os.walk(path):
for im_name in files:
img_path = os.path.join(path, im_name)
print('Testing on image', img_path)
test_data, test_label, color = prepare_data(img_path)
print('test_data has shape:', test_data.shape, 'label has shape:', test_label.shape)
print('color has shape:', color.shape)
# Generate super-resolutioned image.
conv_out = model.eval({images: test_data}) # Result in patch of size 21x21.
conv_out = conv_out.squeeze()
result_bw = revert(conv_out)
result = np.zeros([result_bw.shape[0], result_bw.shape[1], 3], dtype=np.uint8)
result[:, :, 0] = result_bw
result[:, :, 1:3] = color
result = cv.cvtColor(result, cv.COLOR_YCrCb2RGB)
bicubic = scipy.misc.imresize(test_label, 1 / multiplier, interp='bicubic')
bicubic = scipy.misc.imresize(bicubic, multiplier * 1.0, interp='bicubic')
bicubic = cv.cvtColor(bicubic, cv.COLOR_BGR2RGB)
# img = cv.imread(img_path)
# h, w = img.shape[0], img.shape[1]
# num_hor = math.ceil((h - size_input) / 21)
# num_ver = math.ceil((w - size_input) / 21)
# test_data, test_label = prepare_data(img_path)
# # Generate super-resolutioned image.
# conv_out = model.eval({images: test_data}) # Result in patch of size 21x21.
# height, width = conv_out.shape[1], conv_out.shape[2]
# # print('conv_out has shape:', conv_out.shape)
# result = np.zeros([height * num_hor, width * num_ver, 1])
# original = np.zeros([height * num_hor, width * num_ver, 1])
# # print('result has shape:', result.shape)
# # print('num_hor =', num_hor, 'num_ver =', num_ver)
# i, j = 0, 0
# for idx, image in enumerate(conv_out):
# j = idx // num_ver
# i = idx - j * num_ver
# # print('idx =', idx, 'i =', i, 'j =', j)
# result[j * height : j * height + height, i * width : i * width + width, :] = image
# result = result.squeeze()
# result = revert(result)
# i, j = 0, 0
# for idx, image in enumerate(test_label):
# j = idx // num_ver
# i = idx - j * num_ver
# original[j * height : j * height + height, i * width : i * width + width, :] = image
# original = original.squeeze()
# size_original = original.shape
# bicubic = scipy.misc.imresize(original, 1/3, interp='bicubic')
# bicubic = scipy.misc.imresize(bicubic, size_original, interp='bicubic')
# Save the image.
save_path = os.path.join(save_dir, os.path.basename(img_path))
scipy.misc.imsave(save_path, result)
bicubic_path = os.path.join(save_dir, 'bicubic_' + os.path.basename(img_path))
scipy.misc.imsave(bicubic_path, bicubic)
print('Finished testing', os.path.basename(img_path))
print('Finished testing all images.')
else:
print(' [*] Invalid input path.')
# Directly feed the original image to SRCNN.
def enhance(path, save_dir):
# Initialization.
images = tf.placeholder(tf.float32, [None, None, None, 1], name='images')
model = SRCNN(images)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
# Load the saved checkpoint.
saver = tf.train.Saver()
checkpoint_dir = ckpt_dir
if load_ckpt(sess, checkpoint_dir, saver):
print('Successfully loaded checkpoint.')
else:
print('Failed to load checkpoint.')
if os.path.isfile(path):
print('Upscaling image', path, '...')
test_data, test_color = prepare_raw(path)
# Generate super-resolutioned image.
conv_out = model.eval({images: test_data})
conv_out = conv_out.squeeze()
result_bw = revert(conv_out)
result = np.zeros([result_bw.shape[0], result_bw.shape[1], 3], dtype=np.uint8)
result[:, :, 0] = result_bw
result[:, :, 1:3] = test_color
result = cv.cvtColor(result, cv.COLOR_YCrCb2RGB)
save_path = os.path.join(save_dir, os.path.basename(path))
scipy.misc.imsave(save_path, result)
# Upscale_single_image(model, path, save_dir)
print('Finished upscaling image', path)
elif os.path.isdir(path):
for root, dirs, files in os.walk(path):
for im_name in files:
img_path = os.path.join(path, im_name)
print('Upscaling image', img_path, '...')
test_data, test_color = prepare_raw(img_path)
# Generate super-resolutioned image.
conv_out = model.eval({images: test_data})
conv_out = conv_out.squeeze()
result_bw = revert(conv_out)
result = np.zeros([result_bw.shape[0], result_bw.shape[1], 3], dtype=np.uint8)
result[:, :, 0] = result_bw
result[:, :, 1:3] = test_color
result = cv.cvtColor(result, cv.COLOR_YCrCb2RGB)
save_path = os.path.join(save_dir, os.path.basename(img_path))
scipy.misc.imsave(save_path, result)
print('Finished upscaling image', img_path)
print('Finished upscaling all images.')
else:
print(' [*] Invalid input path.')
# Calculate num_ver and num_hor.
img_path = './Test/compressed_video_test/'
save_path = './result/compressed_video_result/'
# generate_SR(img_path, save_path)
enhance(img_path, save_path)