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 4.2 kB

5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. import mindspore.common.dtype as mstype
  22. from mindspore.common.api import _executor
  23. from mindspore import Tensor
  24. class SSIMNet(nn.Cell):
  25. def __init__(self, max_val=1.0, filter_size=11, filter_sigma=1.5, k1=0.01, k2=0.03):
  26. super(SSIMNet, self).__init__()
  27. self.net = nn.SSIM(max_val, filter_size, filter_sigma, k1, k2)
  28. def construct(self, img1, img2):
  29. return self.net(img1, img2)
  30. def test_compile():
  31. net = SSIMNet()
  32. img1 = Tensor(np.random.random((8, 3, 16, 16)))
  33. img2 = Tensor(np.random.random((8, 3, 16, 16)))
  34. _executor.compile(net, img1, img2)
  35. def test_compile_grayscale():
  36. max_val = 255
  37. net = SSIMNet(max_val = max_val)
  38. img1 = Tensor(np.random.randint(0, 256, (8, 1, 16, 16), np.uint8))
  39. img2 = Tensor(np.random.randint(0, 256, (8, 1, 16, 16), np.uint8))
  40. _executor.compile(net, img1, img2)
  41. def test_ssim_max_val_negative():
  42. max_val = -1
  43. with pytest.raises(ValueError):
  44. net = SSIMNet(max_val)
  45. def test_ssim_max_val_bool():
  46. max_val = True
  47. with pytest.raises(TypeError):
  48. net = SSIMNet(max_val)
  49. def test_ssim_max_val_zero():
  50. max_val = 0
  51. with pytest.raises(ValueError):
  52. net = SSIMNet(max_val)
  53. def test_ssim_filter_size_float():
  54. with pytest.raises(TypeError):
  55. net = SSIMNet(filter_size=1.1)
  56. def test_ssim_filter_size_zero():
  57. with pytest.raises(ValueError):
  58. net = SSIMNet(filter_size=0)
  59. def test_ssim_filter_sigma_zero():
  60. with pytest.raises(ValueError):
  61. net = SSIMNet(filter_sigma=0.0)
  62. def test_ssim_filter_sigma_negative():
  63. with pytest.raises(ValueError):
  64. net = SSIMNet(filter_sigma=-0.1)
  65. def test_ssim_k1_k2_wrong_value():
  66. with pytest.raises(ValueError):
  67. net = SSIMNet(k1=1.1)
  68. with pytest.raises(ValueError):
  69. net = SSIMNet(k1=1.0)
  70. with pytest.raises(ValueError):
  71. net = SSIMNet(k1=0.0)
  72. with pytest.raises(ValueError):
  73. net = SSIMNet(k1=-1.0)
  74. with pytest.raises(ValueError):
  75. net = SSIMNet(k2=1.1)
  76. with pytest.raises(ValueError):
  77. net = SSIMNet(k2=1.0)
  78. with pytest.raises(ValueError):
  79. net = SSIMNet(k2=0.0)
  80. with pytest.raises(ValueError):
  81. net = SSIMNet(k2=-1.0)
  82. def test_ssim_different_shape():
  83. shape_1 = (8, 3, 16, 16)
  84. shape_2 = (8, 3, 8, 8)
  85. img1 = Tensor(np.random.random(shape_1))
  86. img2 = Tensor(np.random.random(shape_2))
  87. net = SSIMNet()
  88. with pytest.raises(ValueError):
  89. _executor.compile(net, img1, img2)
  90. def test_ssim_different_dtype():
  91. dtype_1 = mstype.float32
  92. dtype_2 = mstype.float16
  93. img1 = Tensor(np.random.random((8, 3, 16, 16)), dtype=dtype_1)
  94. img2 = Tensor(np.random.random((8, 3, 16, 16)), dtype=dtype_2)
  95. net = SSIMNet()
  96. with pytest.raises(TypeError):
  97. _executor.compile(net, img1, img2)
  98. def test_ssim_invalid_5d_input():
  99. shape_1 = (8, 3, 16, 16)
  100. shape_2 = (8, 3, 8, 8)
  101. invalid_shape = (8, 3, 16, 16, 1)
  102. img1 = Tensor(np.random.random(shape_1))
  103. invalid_img1 = Tensor(np.random.random(invalid_shape))
  104. img2 = Tensor(np.random.random(shape_2))
  105. invalid_img2 = Tensor(np.random.random(invalid_shape))
  106. net = SSIMNet()
  107. with pytest.raises(ValueError):
  108. _executor.compile(net, invalid_img1, img2)
  109. with pytest.raises(ValueError):
  110. _executor.compile(net, img1, invalid_img2)
  111. with pytest.raises(ValueError):
  112. _executor.compile(net, invalid_img1, invalid_img2)