'''metrics''' # Copyright 2021 Huawei Technologies Co., Ltd # # 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 math import numpy as np def quantize(img, rgb_range): '''metrics''' pixel_range = 255 / rgb_range img = np.multiply(img, pixel_range) img = np.clip(img, 0, 255) img = np.round(img) / pixel_range return img def calc_psnr(sr, hr, scale, rgb_range, y_only=False, dataset=None): '''metrics''' hr = np.float32(hr) sr = np.float32(sr) diff = (sr - hr) / rgb_range gray_coeffs = np.array([65.738, 129.057, 25.064] ).reshape((1, 3, 1, 1)) / 256 diff = np.multiply(diff, gray_coeffs).sum(1) if np.size(hr) == 1: return 0 if scale != 1: shave = scale else: shave = scale + 6 if scale == 1: valid = diff else: valid = diff[..., shave:-shave, shave:-shave] mse = np.mean(pow(valid, 2)) return -10 * math.log10(mse) def rgb2ycbcr(img, y_only=True): '''metrics''' img.astype(np.float32) if y_only: rlt = np.dot(img, [65.481, 128.553, 24.966]) / 255.0 + 16.0 return rlt