Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PSNR when input is uint8 #294

Merged
merged 1 commit into from
May 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions mmedit/core/evaluation/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,12 @@ def psnr(img1, img2, crop_border=0, input_order='HWC', convert_to=None):
img1 = reorder_image(img1, input_order=input_order)
img2 = reorder_image(img2, input_order=input_order)

img1, img2 = img1.astype(np.float32), img2.astype(np.float32)
if isinstance(convert_to, str) and convert_to.lower() == 'y':
img1, img2 = img1.astype(np.float32), img2.astype(np.float32)
img1 = mmcv.bgr2ycbcr(img1 / 255., y_only=True) * 255.
img2 = mmcv.bgr2ycbcr(img2 / 255., y_only=True) * 255.
elif convert_to is not None:
raise ValueError(f'Wrong color model. Supported values are '
raise ValueError('Wrong color model. Supported values are '
'"Y" and None.')

if crop_border != 0:
Expand Down Expand Up @@ -292,7 +292,7 @@ def ssim(img1, img2, crop_border=0, input_order='HWC', convert_to=None):
img1 = np.expand_dims(img1, axis=2)
img2 = np.expand_dims(img2, axis=2)
elif convert_to is not None:
raise ValueError(f'Wrong color model. Supported values are '
raise ValueError('Wrong color model. Supported values are '
'"Y" and None')

if crop_border != 0:
Expand Down
6 changes: 6 additions & 0 deletions tests/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ def test_calculate_psnr():
psnr_result = psnr(img_hw_1, img_hw_1, crop_border=0)
assert psnr_result == float('inf')

# test uint8
img_hw_1 = np.zeros((32, 32), dtype=np.uint8)
img_hw_2 = np.ones((32, 32), dtype=np.uint8) * 255
psnr_result = psnr(img_hw_1, img_hw_2, crop_border=0)
assert psnr_result == 0


def test_calculate_ssim():
img_hw_1 = np.ones((32, 32))
Expand Down