You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_ssim.py 2.9 kB

5 years ago
5 years ago
5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  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. # ============================================================================
  15. """
  16. test ssim
  17. """
  18. import numpy as np
  19. import pytest
  20. import mindspore.nn as nn
  21. from mindspore.common.api import _executor
  22. from mindspore import Tensor
  23. class SSIMNet(nn.Cell):
  24. def __init__(self, max_val=1.0, filter_size=11, filter_sigma=1.5, k1=0.01, k2=0.03):
  25. super(SSIMNet, self).__init__()
  26. self.net = nn.SSIM(max_val, filter_size, filter_sigma, k1, k2)
  27. def construct(self, img1, img2):
  28. return self.net(img1, img2)
  29. def test_compile():
  30. net = SSIMNet()
  31. img1 = Tensor(np.random.random((8, 3, 16, 16)))
  32. img2 = Tensor(np.random.random((8, 3, 16, 16)))
  33. _executor.compile(net, img1, img2)
  34. def test_compile_grayscale():
  35. max_val = 255
  36. net = SSIMNet(max_val = max_val)
  37. img1 = Tensor(np.random.randint(0, 256, (8, 1, 16, 16), np.uint8))
  38. img2 = Tensor(np.random.randint(0, 256, (8, 1, 16, 16), np.uint8))
  39. _executor.compile(net, img1, img2)
  40. def test_ssim_max_val_negative():
  41. max_val = -1
  42. with pytest.raises(ValueError):
  43. net = SSIMNet(max_val)
  44. def test_ssim_max_val_bool():
  45. max_val = True
  46. with pytest.raises(TypeError):
  47. net = SSIMNet(max_val)
  48. def test_ssim_max_val_zero():
  49. max_val = 0
  50. with pytest.raises(ValueError):
  51. net = SSIMNet(max_val)
  52. def test_ssim_filter_size_float():
  53. with pytest.raises(TypeError):
  54. net = SSIMNet(filter_size=1.1)
  55. def test_ssim_filter_size_zero():
  56. with pytest.raises(ValueError):
  57. net = SSIMNet(filter_size=0)
  58. def test_ssim_filter_sigma_zero():
  59. with pytest.raises(ValueError):
  60. net = SSIMNet(filter_sigma=0.0)
  61. def test_ssim_filter_sigma_negative():
  62. with pytest.raises(ValueError):
  63. net = SSIMNet(filter_sigma=-0.1)
  64. def test_ssim_k1_k2_wrong_value():
  65. with pytest.raises(ValueError):
  66. net = SSIMNet(k1=1.1)
  67. with pytest.raises(ValueError):
  68. net = SSIMNet(k1=1.0)
  69. with pytest.raises(ValueError):
  70. net = SSIMNet(k1=0.0)
  71. with pytest.raises(ValueError):
  72. net = SSIMNet(k1=-1.0)
  73. with pytest.raises(ValueError):
  74. net = SSIMNet(k2=1.1)
  75. with pytest.raises(ValueError):
  76. net = SSIMNet(k2=1.0)
  77. with pytest.raises(ValueError):
  78. net = SSIMNet(k2=0.0)
  79. with pytest.raises(ValueError):
  80. net = SSIMNet(k2=-1.0)