-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
107 lines (84 loc) · 4.35 KB
/
test.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
# Copyright 2022 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 os
import cv2
import torch
from natsort import natsorted
import config
import imgproc
from image_quality_assessment import PSNR, SSIM
from model import CARN
def main() -> None:
# Initialize the super-resolution model
model = CARN(config.upscale_factor).to(device=config.device, memory_format=torch.channels_last)
print("Build CARN model successfully.")
# Load the super-resolution model weights
checkpoint = torch.load(config.model_path, map_location=lambda storage, loc: storage)
model.load_state_dict(checkpoint["state_dict"])
print(f"Load CARN model weights `{os.path.abspath(config.model_path)}` successfully.")
# Create a folder of super-resolution experiment results
if not os.path.exists(config.sr_dir):
os.makedirs(config.sr_dir)
# Start the verification mode of the model.
model.eval()
# Initialize the sharpness evaluation function
psnr = PSNR(config.upscale_factor, config.only_test_y_channel)
ssim = SSIM(config.upscale_factor, config.only_test_y_channel)
# Set the sharpness evaluation function calculation device to the specified model
psnr = psnr.to(device=config.device, memory_format=torch.channels_last, non_blocking=True)
ssim = ssim.to(device=config.device, memory_format=torch.channels_last, non_blocking=True)
# Initialize IQA metrics
psnr_metrics = 0.0
ssim_metrics = 0.0
# Get a list of test image file names.
file_names = natsorted(os.listdir(config.lr_dir))
# Get the number of test image files.
total_files = len(file_names)
for index in range(total_files):
lr_image_path = os.path.join(config.lr_dir, file_names[index])
sr_image_path = os.path.join(config.sr_dir, file_names[index])
hr_image_path = os.path.join(config.hr_dir, file_names[index])
print(f"Processing `{os.path.abspath(lr_image_path)}`...")
# Read LR image and HR image
lr_image = cv2.imread(lr_image_path, cv2.IMREAD_UNCHANGED)
hr_image = cv2.imread(hr_image_path, cv2.IMREAD_UNCHANGED)
# Convert BGR channel image format data to RGB channel image format data
lr_image = cv2.cvtColor(lr_image, cv2.COLOR_BGR2RGB)
hr_image = cv2.cvtColor(hr_image, cv2.COLOR_BGR2RGB)
# Convert RGB channel image format data to Tensor channel image format data
lr_tensor = imgproc.image2tensor(lr_image, False, False).unsqueeze_(0)
hr_tensor = imgproc.image2tensor(hr_image, False, False).unsqueeze_(0)
# Transfer Tensor channel image format data to CUDA device
lr_tensor = lr_tensor.to(device=config.device, memory_format=torch.channels_last, non_blocking=True)
hr_tensor = hr_tensor.to(device=config.device, memory_format=torch.channels_last, non_blocking=True)
# Only reconstruct the Y channel image data.
with torch.no_grad():
sr_tensor = model(lr_tensor)
# Save image
sr_image = imgproc.tensor2image(sr_tensor, False, False)
sr_image = cv2.cvtColor(sr_image, cv2.COLOR_RGB2BGR)
cv2.imwrite(sr_image_path, sr_image)
# Cal IQA metrics
psnr_metrics += psnr(sr_tensor, hr_tensor).item()
ssim_metrics += ssim(sr_tensor, hr_tensor).item()
# Calculate the average value of the sharpness evaluation index,
# and all index range values are cut according to the following values
# PSNR range value is 0~100
# SSIM range value is 0~1
avg_ssim = 1 if ssim_metrics / total_files > 1 else ssim_metrics / total_files
avg_psnr = 100 if psnr_metrics / total_files > 100 else psnr_metrics / total_files
print(f"PSNR: {avg_psnr:4.2f} dB\n"
f"SSIM: {avg_ssim:4.4f} u")
if __name__ == "__main__":
main()