/
tester.py
245 lines (213 loc) · 11.2 KB
/
tester.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
# Copyright 2020 Dakewe Biotech Corporation. All Rights Reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
import logging
import math
import os
import cv2
import numpy as np
import torch
import torch.utils.data
import torchvision.transforms as transforms
import torchvision.utils as vutils
from PIL import Image
from tqdm import tqdm
from ssrgan.dataset import CustomTestDataset
from ssrgan.utils import configure
from ssrgan.utils import image_quality_evaluation
from ssrgan.utils import inference
from ssrgan.utils import process_image
logger = logging.getLogger(__name__)
logging.basicConfig(format="[ %(levelname)s ] %(message)s", level=logging.INFO)
class Test(object):
def __init__(self, args):
self.args = args
self.model, self.device = configure(args)
logger.info("Load testing dataset")
self.dataloader = torch.utils.data.DataLoader(CustomTestDataset(args.dataroot, img_size=216),
batch_size=1,
pin_memory=True,
num_workers=int(args.workers))
logger.info(f"Dataset information\n"
f"\tDataset dir is `{os.getcwd()}/{args.dataroot}`\n"
f"\tBatch size is 1\n"
f"\tWorkers is {int(args.workers)}\n"
f"\tLoad dataset to CUDA")
def run(self):
# Evaluate algorithm performance.
total_mse_value = 0.0
total_rmse_value = 0.0
total_psnr_value = 0.0
total_ssim_value = 0.0
total_mssim_value = 0.0
total_niqe_value = 0.0
total_sam_value = 0.0
total_vif_value = 0.0
total_lpips_value = 0.0
# Start evaluate model performance.
progress_bar = tqdm(enumerate(self.dataloader), total=len(self.dataloader))
# Concat image.
images = []
for i, (input, bicubic, target) in progress_bar:
# Set model gradients to zero
lr = input.to(self.device)
hr = target.to(self.device)
sr = inference(self.model, lr)
vutils.save_image(sr, f"./{self.args.outf}/sr_{i}.bmp") # Save super resolution image.
vutils.save_image(hr, f"./{self.args.outf}/hr_{i}.bmp") # Save high resolution image.
# Evaluate performance
if self.args.quickly:
mse_value = ((sr - hr) ** 2).data.mean()
psnr_value = 10 * math.log10(1. / mse_value)
total_psnr_value += psnr_value
progress_bar.set_description(f"[{i + 1}/{len(self.dataloader)}] PSNR: {psnr_value:.2f}dB")
else:
value = image_quality_evaluation(f"./{self.args.outf}/sr_{i}.bmp", f"./{self.args.outf}/hr_{i}.bmp",
self.device)
total_mse_value += value[0]
total_rmse_value += value[1]
total_psnr_value += value[2]
total_ssim_value += value[3][0]
total_mssim_value += value[4].real
total_niqe_value += value[5]
total_sam_value += value[6]
total_vif_value += value[7]
total_lpips_value += value[8].item()
progress_bar.set_description(f"[{i + 1}/{len(self.dataloader)}] "
f"PSNR: {value[2]:.2f}dB "
f"SSIM: {value[3][0]:.4f}")
images.extend([hr.data.cpu().squeeze(0), bicubic.squeeze(0), sr.data.cpu().squeeze(0)])
images = torch.stack(images)
images = torch.chunk(images, len(self.dataloader) // 3)
bar = tqdm(images, desc="[saving testing results]")
index = 1
for image in bar:
image = vutils.make_grid(image, nrow=3, padding=5)
vutils.save_image(image, f"{self.args.outf}/{index}.bmp", padding=5)
index += 1
print(f"Performance avg results:\n")
print(f"indicator Score\n")
print(f"--------- -----\n")
if self.args.quickly:
print(f"PSNR {total_psnr_value / len(self.dataloader):.2f}\n")
else:
print(f"MSE {total_mse_value / len(self.dataloader):.2f}\n"
f"RMSE {total_rmse_value / len(self.dataloader):.2f}\n"
f"PSNR {total_psnr_value / len(self.dataloader):.2f}\n"
f"SSIM {total_ssim_value / len(self.dataloader):.4f}\n"
f"MS-SSIM {total_mssim_value / len(self.dataloader):.4f}\n"
f"NIQE {total_niqe_value / len(self.dataloader):.2f}\n"
f"SAM {total_sam_value / len(self.dataloader):.4f}\n"
f"VIF {total_vif_value / len(self.dataloader):.4f}\n"
f"LPIPS {total_lpips_value / len(self.dataloader):.4f}\n")
class Estimate(object):
def __init__(self, args):
self.args = args
self.model, self.device = configure(args)
def run(self):
# Read img to tensor and transfer to the specified device for processing.
img = Image.open(self.args.lr)
lr = process_image(img, self.device)
sr, use_time = inference(self.model, lr, statistical_time=True)
vutils.save_image(sr, f"./{self.args.outf}/{self.args.lr.split('/')[-1]}") # Save super resolution image.
value = image_quality_evaluation(f"./{self.args.outf}/{self.args.lr}", self.args.hr, self.device)
print(f"Performance avg results:\n")
print(f"indicator Score\n")
print(f"--------- -----\n")
if self.args.detail:
print(f"MSE {value[0]:.2f}\n"
f"RMSE {value[1]:.2f}\n"
f"PSNR {value[2]:.2f}\n"
f"SSIM {value[3][0]:.4f}\n"
f"MS-SSIM {value[4].real:.4f}\n"
f"NIQE {value[5]:.2f}\n"
f"SAM {value[6]:.4f}\n"
f"VIF {value[7]:.4f}\n"
f"LPIPS {value[8].item():.4f}\n"
f"Use time: {use_time * 1000:.2f}ms | {use_time:.4f}s")
else:
print(f"PSNR {value[0]:.2f}\n"
f"SSIM {value[1][0]:.2f}\n"
f"Use time: {use_time * 1000:.2f}ms | {use_time:.4f}s")
class Video(object):
def __init__(self, args):
self.args = args
self.model, self.device = configure(args)
# Image preprocessing operation
self.tensor2pil = transforms.ToPILImage()
self.video_capture = cv2.VideoCapture(args.file)
# Prepare to write the processed image into the video.
self.fps = self.video_capture.get(cv2.CAP_PROP_FPS)
self.total_frames = int(self.video_capture.get(cv2.CAP_PROP_FRAME_COUNT))
# Set video size
self.size = (int(self.video_capture.get(cv2.CAP_PROP_FRAME_WIDTH)),
int(self.video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT)))
self.sr_size = (self.size[0] * args.upscale_factor, self.size[1] * args.upscale_factor)
self.pare_size = (self.sr_size[0] * 2 + 10, self.sr_size[1] + 10 + self.sr_size[0] // 5 - 9)
# Video write loader.
self.sr_writer = cv2.VideoWriter(f"./video/sr_{args.scale_factor}x_{os.path.basename(args.file)}",
cv2.VideoWriter_fourcc(*"MPEG"), self.fps, self.sr_size)
self.compare_writer = cv2.VideoWriter(f"./video/compare_{args.scale_factor}x_{os.path.basename(args.file)}",
cv2.VideoWriter_fourcc(*"MPEG"), self.fps, self.pare_size)
def run(self):
# Set eval model.
self.model.eval()
# read frame
success, raw_frame = self.video_capture.read()
progress_bar = tqdm(range(self.total_frames), desc="[processing video and saving/view result videos]")
for _ in progress_bar:
if success:
# Read img to tensor and transfer to the specified device for processing.
img = Image.open(self.args.lr)
lr = process_image(img, self.device)
sr = inference(self.model, lr)
sr = sr.cpu()
sr = sr.data[0].numpy()
sr *= 255.0
sr = (np.uint8(sr)).transpose((1, 2, 0))
# save sr video
self.sr_writer.write(sr)
# make compared video and crop shot of left top\right top\center\left bottom\right bottom
sr = self.tensor2pil(sr)
# Five areas are selected as the bottom contrast map.
crop_sr_imgs = transforms.FiveCrop(size=sr.width // 5 - 9)(sr)
crop_sr_imgs = [np.asarray(transforms.Pad(padding=(10, 5, 0, 0))(img)) for img in crop_sr_imgs]
sr = transforms.Pad(padding=(5, 0, 0, 5))(sr)
# Five areas in the contrast map are selected as the bottom contrast map
compare_img = transforms.Resize((self.sr_size[1], self.sr_size[0]),
interpolation=Image.BICUBIC)(self.tensor2pil(raw_frame))
crop_compare_imgs = transforms.FiveCrop(size=compare_img.width // 5 - 9)(compare_img)
crop_compare_imgs = [np.asarray(transforms.Pad(padding=(0, 5, 10, 0))(img)) for img in
crop_compare_imgs]
compare_img = transforms.Pad(padding=(0, 0, 5, 5))(compare_img)
# concatenate all the pictures to one single picture
# 1. Mosaic the left and right images of the video.
top_img = np.concatenate((np.asarray(compare_img), np.asarray(sr)), axis=1)
# 2. Mosaic the bottom left and bottom right images of the video.
bottom_img = np.concatenate(crop_compare_imgs + crop_sr_imgs, axis=1)
bottom_img_height = int(top_img.shape[1] / bottom_img.shape[1] * bottom_img.shape[0])
bottom_img_width = top_img.shape[1]
# 3. Adjust to the right size.
bottom_img = np.asarray(
transforms.Resize((bottom_img_height, bottom_img_width))(self.tensor2pil(bottom_img)))
# 4. Combine the bottom zone with the upper zone.
final_image = np.concatenate((top_img, bottom_img))
# save compare video
self.compare_writer.write(final_image)
if self.args.view:
# display video
cv2.imshow("LR video convert HR video ", final_image)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
# next frame
success, raw_frame = self.video_capture.read()