-
Notifications
You must be signed in to change notification settings - Fork 10
/
bsd100_callback.py
223 lines (176 loc) · 9.32 KB
/
bsd100_callback.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
import os
from glob import glob
import cv2
import numpy as np
from shutil import rmtree
from keras.callbacks import Callback
from time import time
from utils import preprocess_LR, deprocess_HR
class BSD100_Evaluator(Callback):
def __init__(self, directory, filepath=None, downscale_factor=4, color_mode='rgb',
verbose=1, data_format='channels_last', margin=5):
"""
Keras Callback to use for vaidation on BSD100 dataset.
:param directory: path to the BSD100 dataset directory
:param filepath: directory where LR|SR|HR images will be saved. If None, it does not save images.
:param downscale_factor: downscale factor to apply to LR images
:param color_mode: RGB or BGR mode
:param verbose: verbose level. 0 or 1
:param data_format: order of dimensions for TensorFlow
:param margin: remove margin pixels strips from each border
"""
self.directory = directory
self.filepath = filepath
self.downscale_factor = downscale_factor
self.color_mode = color_mode
self.verbose = verbose
self.data_format = data_format
self.margin = margin
self.slice = np.s_[margin:-margin, margin:-margin,:]
# list files
self.list_files_HR = glob(os.path.join(directory, "*_HR.png"))
print("Found {} images ...".format(len(self.list_files_HR)))
# test if filepath exists, if true remove it
# then create an empty dir
if self.filepath is not None:
if os.path.isdir(self.filepath):
rmtree(self.filepath)
os.mkdir(self.filepath)
self.logs = dict()
self.logs['mse'] = []
self.logs['psnr'] = []
super(BSD100_Evaluator, self).__init__()
def on_epoch_end(self, epoch, logs=None):
logs = logs or {}
list_mse = []
list_psnr = []
current_filepath = os.path.join(self.filepath, 'epoch_{}'.format(epoch+1))
if os.path.isdir(current_filepath):
rmtree(current_filepath)
os.mkdir(current_filepath)
start = time()
for i, file_HR in enumerate(self.list_files_HR):
img_hr = cv2.imread(file_HR, cv2.IMREAD_COLOR)
img_name = file_HR.split('/')[-1].split('_')[0]
if self.color_mode == 'bgr':
img_hr = cv2.cvtColor(img_hr, cv2.COLOR_BGR2RGB)
new_shape = img_hr.shape
img_lr = cv2.resize(img_hr, (new_shape[1] // self.downscale_factor,
new_shape[0] // self.downscale_factor), interpolation=cv2.INTER_LINEAR)
img_cubic = cv2.resize(img_lr, (new_shape[1], new_shape[0]), interpolation=cv2.INTER_CUBIC)
img_lr = preprocess_LR(img_lr)
if self.data_format == 'channels_first':
# (h,w,c) to (c,h,w)
img_lr = np.transpose(img_lr, (2, 0, 1))
img_sr = self.model.predict_on_batch(np.expand_dims(img_lr, axis=0))[0]
if self.data_format == 'channels_first':
# (c,h,w) to (h,w,c)
img_sr = np.transpose(img_sr, (1, 2, 0))
# compute MSE (with images in range [0, 255])
img_sr = deprocess_HR(img_sr)
mse = np.mean(np.square(img_hr[self.slice] - img_sr[self.slice]))
list_mse.append(mse)
# compute PSNR
if mse == 0.:
psnr = 100
else:
psnr = 20 * np.log10(255. / np.sqrt(mse))
list_psnr.append(psnr)
if self.filepath is not None:
global_image = np.zeros((new_shape[0], 3*new_shape[1], 3), dtype=np.uint8)
global_image[:, 0:new_shape[1], :] = img_cubic.astype(np.uint8)
global_image[:, new_shape[1]:2*new_shape[1], :] = img_sr.astype(np.uint8)
global_image[:, 2*new_shape[1]:3*new_shape[1], :] = img_hr.astype(np.uint8)
# add black padding
global_image_ext = np.zeros((global_image.shape[0]+50, global_image.shape[1], 3), dtype=np.uint8)
global_image_ext[0:global_image.shape[0], :, :] = global_image
global_image_ext = cv2.putText(img=np.copy(global_image_ext),
text="MSE = {:.3f} | PSNR = {:.3f}".format(mse, psnr),
org=(0, new_shape[0]+50),
fontFace=1,
fontScale=2,
color=(255, 255, 255),
thickness=2)
cv2.imwrite(os.path.join(current_filepath, '{}.png'.format(img_name)), global_image_ext.astype(np.uint8),
params=[cv2.IMWRITE_PNG_COMPRESSION, 3])
self.logs['mse'].append(np.mean(list_mse))
self.logs['psnr'].append(np.mean(list_psnr))
stop = time()
if self.verbose > 0:
print('\nBSD100 Callback - Epoch %05d: MSE = %s || PSNR = %s in %05d s' % (epoch + 1, np.mean(list_mse),
np.mean(list_psnr),
stop-start))
def BSD100_evaluate(model, step, directory, filepath=None, verbose=1, color_mode='rgb',
downscale_factor=4, margin=5, data_format='channels_last'):
"""
:param model: model to evaluate on BSD100
:param step: step id. Used if filepath is not None.
:param directory: path to the BSD100 dataset directory
:param filepath: directory where LR|SR|HR images will be saved. If None, it does not save images.
:param downscale_factor: downscale factor to apply to LR images
:param color_mode: RGB or BGR mode
:param verbose: verbose level. 0 or 1
:param data_format: order of dimensions for TensorFlow
:param margin: remove margin pixels strips from each border
"""
list_files_HR = glob(os.path.join(directory, "*_HR.png"))
print("Found {} images ...".format(len(list_files_HR)))
list_mse = []
list_psnr = []
slice = np.s_[margin:-margin, margin:-margin, :]
current_filepath = os.path.join(filepath, 'epoch_{}'.format(step))
if filepath is not None:
if os.path.isdir(current_filepath):
rmtree(current_filepath)
os.mkdir(current_filepath)
start = time()
for i, file_HR in enumerate(list_files_HR):
img_hr = cv2.imread(file_HR, cv2.IMREAD_COLOR)
img_name = file_HR.split('/')[-1].split('_')[0]
if color_mode == 'bgr':
img_hr = cv2.cvtColor(img_hr, cv2.COLOR_BGR2RGB)
new_shape = img_hr.shape
img_lr = cv2.resize(img_hr, (new_shape[1] // downscale_factor,
new_shape[0] // downscale_factor), interpolation=cv2.INTER_LINEAR)
img_cubic = cv2.resize(img_lr, (new_shape[1], new_shape[0]), interpolation=cv2.INTER_CUBIC)
img_lr = preprocess_LR(img_lr)
if data_format == 'channels_first':
# (h,w,c) to (c,h,w)
img_lr = np.transpose(img_lr, (2, 0, 1))
img_sr = model.predict_on_batch(np.expand_dims(img_lr, axis=0))[0]
if data_format == 'channels_first':
# (c,h,w) to (h,w,c)
img_sr = np.transpose(img_sr, (1, 2, 0))
# compute MSE (with images in range [0, 255])
img_sr = deprocess_HR(img_sr)
mse = np.mean(np.square(img_hr[slice] - img_sr[slice]))
list_mse.append(mse)
# compute PSNR
if mse == 0.:
psnr = 100
else:
psnr = 20 * np.log10(255. / np.sqrt(mse))
list_psnr.append(psnr)
if filepath is not None:
global_image = np.zeros((new_shape[0], 3 * new_shape[1], 3), dtype=np.uint8)
global_image[:, 0:new_shape[1], :] = img_cubic.astype(np.uint8)
global_image[:, new_shape[1]:2 * new_shape[1], :] = img_sr.astype(np.uint8)
global_image[:, 2 * new_shape[1]:3 * new_shape[1], :] = img_hr.astype(np.uint8)
# add black padding
global_image_ext = np.zeros((global_image.shape[0] + 50, global_image.shape[1], 3), dtype=np.uint8)
global_image_ext[0:global_image.shape[0], :, :] = global_image
global_image_ext = cv2.putText(img=np.copy(global_image_ext),
text="MSE = {:.3f} | PSNR = {:.3f}".format(mse, psnr),
org=(0, new_shape[0] + 50),
fontFace=1,
fontScale=2,
color=(255, 255, 255),
thickness=2)
cv2.imwrite(os.path.join(current_filepath, '{}.png'.format(img_name)), global_image_ext.astype(np.uint8),
params=[cv2.IMWRITE_PNG_COMPRESSION, 3])
stop = time()
if verbose > 0:
print('\nBSD100 Callback - Epoch %05d: MSE = %s || PSNR = %s in %05d s' % (step, np.mean(list_mse),
np.mean(list_psnr),
stop - start))
return np.mean(list_mse), np.mean(list_psnr)