|
| 1 | +# Copyright The PyTorch Lightning team. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +from collections import namedtuple |
| 15 | +from functools import partial |
| 16 | + |
| 17 | +import pytest |
| 18 | +import torch |
| 19 | +from skimage.metrics import structural_similarity |
| 20 | + |
| 21 | +from tests.helpers import seed_all |
| 22 | +from tests.helpers.testers import BATCH_SIZE, NUM_BATCHES, MetricTester |
| 23 | +from torchmetrics.functional.image.uqi import universal_image_quality_index |
| 24 | +from torchmetrics.image.uqi import UniversalImageQualityIndex |
| 25 | + |
| 26 | +seed_all(42) |
| 27 | + |
| 28 | +# UQI is SSIM with both constants k1 and k2 as 0 |
| 29 | +skimage_uqi = partial(structural_similarity, k1=0, k2=0) |
| 30 | + |
| 31 | +Input = namedtuple("Input", ["preds", "target", "multichannel"]) |
| 32 | + |
| 33 | +_inputs = [] |
| 34 | +for size, channel, coef, multichannel, dtype in [ |
| 35 | + (12, 3, 0.9, True, torch.float), |
| 36 | + (13, 1, 0.8, False, torch.float32), |
| 37 | + (14, 1, 0.7, False, torch.double), |
| 38 | + (15, 3, 0.6, True, torch.float64), |
| 39 | +]: |
| 40 | + preds = torch.rand(NUM_BATCHES, BATCH_SIZE, channel, size, size, dtype=dtype) |
| 41 | + _inputs.append( |
| 42 | + Input( |
| 43 | + preds=preds, |
| 44 | + target=preds * coef, |
| 45 | + multichannel=multichannel, |
| 46 | + ) |
| 47 | + ) |
| 48 | + |
| 49 | + |
| 50 | +def _sk_uqi(preds, target, data_range, multichannel, kernel_size): |
| 51 | + c, h, w = preds.shape[-3:] |
| 52 | + sk_preds = preds.view(-1, c, h, w).permute(0, 2, 3, 1).numpy() |
| 53 | + sk_target = target.view(-1, c, h, w).permute(0, 2, 3, 1).numpy() |
| 54 | + if not multichannel: |
| 55 | + sk_preds = sk_preds[:, :, :, 0] |
| 56 | + sk_target = sk_target[:, :, :, 0] |
| 57 | + |
| 58 | + return skimage_uqi( |
| 59 | + sk_target, |
| 60 | + sk_preds, |
| 61 | + data_range=data_range, |
| 62 | + multichannel=multichannel, |
| 63 | + gaussian_weights=True, |
| 64 | + win_size=kernel_size, |
| 65 | + sigma=1.5, |
| 66 | + use_sample_covariance=False, |
| 67 | + ) |
| 68 | + |
| 69 | + |
| 70 | +@pytest.mark.parametrize( |
| 71 | + "preds, target, multichannel", |
| 72 | + [(i.preds, i.target, i.multichannel) for i in _inputs], |
| 73 | +) |
| 74 | +@pytest.mark.parametrize("kernel_size", [5, 11]) |
| 75 | +class TestUQI(MetricTester): |
| 76 | + atol = 6e-3 |
| 77 | + |
| 78 | + @pytest.mark.parametrize("ddp", [True, False]) |
| 79 | + @pytest.mark.parametrize("dist_sync_on_step", [True, False]) |
| 80 | + def test_uqi(self, preds, target, multichannel, kernel_size, ddp, dist_sync_on_step): |
| 81 | + self.run_class_metric_test( |
| 82 | + ddp, |
| 83 | + preds, |
| 84 | + target, |
| 85 | + UniversalImageQualityIndex, |
| 86 | + partial(_sk_uqi, data_range=1.0, multichannel=multichannel, kernel_size=kernel_size), |
| 87 | + metric_args={"data_range": 1.0, "kernel_size": (kernel_size, kernel_size)}, |
| 88 | + dist_sync_on_step=dist_sync_on_step, |
| 89 | + ) |
| 90 | + |
| 91 | + def test_uqi_functional(self, preds, target, multichannel, kernel_size): |
| 92 | + self.run_functional_metric_test( |
| 93 | + preds, |
| 94 | + target, |
| 95 | + universal_image_quality_index, |
| 96 | + partial(_sk_uqi, data_range=1.0, multichannel=multichannel, kernel_size=kernel_size), |
| 97 | + metric_args={"data_range": 1.0, "kernel_size": (kernel_size, kernel_size)}, |
| 98 | + ) |
| 99 | + |
| 100 | + # UQI half + cpu does not work due to missing support in torch.log |
| 101 | + @pytest.mark.xfail(reason="UQI metric does not support cpu + half precision") |
| 102 | + def test_uqi_half_cpu(self, preds, target, multichannel, kernel_size): |
| 103 | + self.run_precision_test_cpu( |
| 104 | + preds, target, UniversalImageQualityIndex, universal_image_quality_index, {"data_range": 1.0} |
| 105 | + ) |
| 106 | + |
| 107 | + @pytest.mark.skipif(not torch.cuda.is_available(), reason="test requires cuda") |
| 108 | + def test_uqi_half_gpu(self, preds, target, multichannel, kernel_size): |
| 109 | + self.run_precision_test_gpu( |
| 110 | + preds, target, UniversalImageQualityIndex, universal_image_quality_index, {"data_range": 1.0} |
| 111 | + ) |
| 112 | + |
| 113 | + |
| 114 | +@pytest.mark.parametrize( |
| 115 | + ["pred", "target", "kernel", "sigma"], |
| 116 | + [ |
| 117 | + ([1, 16, 16], [1, 16, 16], [11, 11], [1.5, 1.5]), # len(shape) |
| 118 | + ([1, 1, 16, 16], [1, 1, 16, 16], [11, 11], [1.5]), # len(kernel), len(sigma) |
| 119 | + ([1, 1, 16, 16], [1, 1, 16, 16], [11], [1.5, 1.5]), # len(kernel), len(sigma) |
| 120 | + ([1, 1, 16, 16], [1, 1, 16, 16], [11], [1.5]), # len(kernel), len(sigma) |
| 121 | + ([1, 1, 16, 16], [1, 1, 16, 16], [11, 0], [1.5, 1.5]), # invalid kernel input |
| 122 | + ([1, 1, 16, 16], [1, 1, 16, 16], [11, 10], [1.5, 1.5]), # invalid kernel input |
| 123 | + ([1, 1, 16, 16], [1, 1, 16, 16], [11, -11], [1.5, 1.5]), # invalid kernel input |
| 124 | + ([1, 1, 16, 16], [1, 1, 16, 16], [11, 11], [1.5, 0]), # invalid sigma input |
| 125 | + ([1, 1, 16, 16], [1, 1, 16, 16], [11, 0], [1.5, -1.5]), # invalid sigma input |
| 126 | + ], |
| 127 | +) |
| 128 | +def test_uqi_invalid_inputs(pred, target, kernel, sigma): |
| 129 | + pred_t = torch.rand(pred) |
| 130 | + target_t = torch.rand(target, dtype=torch.float64) |
| 131 | + with pytest.raises(TypeError): |
| 132 | + universal_image_quality_index(pred_t, target_t) |
| 133 | + |
| 134 | + pred = torch.rand(pred) |
| 135 | + target = torch.rand(target) |
| 136 | + with pytest.raises(ValueError): |
| 137 | + universal_image_quality_index(pred, target, kernel, sigma) |
| 138 | + |
| 139 | + |
| 140 | +def test_uqi_unequal_kernel_size(): |
| 141 | + """Test the case where kernel_size[0] != kernel_size[1]""" |
| 142 | + preds = torch.tensor( |
| 143 | + [ |
| 144 | + [ |
| 145 | + [ |
| 146 | + [1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0], |
| 147 | + [1.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0], |
| 148 | + [1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], |
| 149 | + [0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0], |
| 150 | + [0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0], |
| 151 | + [0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 1.0], |
| 152 | + [1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0], |
| 153 | + ] |
| 154 | + ] |
| 155 | + ] |
| 156 | + ) |
| 157 | + target = torch.tensor( |
| 158 | + [ |
| 159 | + [ |
| 160 | + [ |
| 161 | + [1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0], |
| 162 | + [0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0], |
| 163 | + [1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0], |
| 164 | + [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0], |
| 165 | + [1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0], |
| 166 | + [0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0], |
| 167 | + [0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0], |
| 168 | + ] |
| 169 | + ] |
| 170 | + ] |
| 171 | + ) |
| 172 | + # kernel order matters |
| 173 | + torch.allclose(universal_image_quality_index(preds, target, kernel_size=(3, 5)), torch.tensor(0.10662283)) |
| 174 | + torch.allclose(universal_image_quality_index(preds, target, kernel_size=(5, 3)), torch.tensor(0.10662283)) |
0 commit comments