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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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.common.dtype as mstype
  21. import mindspore.nn as nn
  22. from mindspore import Tensor
  23. from mindspore.common.api import _executor
  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)), mstype.float32)
  33. img2 = Tensor(np.random.random((8, 3, 16, 16)), mstype.float32)
  34. _executor.compile(net, img1, img2)
  35. def test_ssim_max_val_negative():
  36. max_val = -1
  37. with pytest.raises(ValueError):
  38. _ = SSIMNet(max_val)
  39. def test_ssim_max_val_bool():
  40. max_val = True
  41. with pytest.raises(TypeError):
  42. _ = SSIMNet(max_val)
  43. def test_ssim_max_val_zero():
  44. max_val = 0
  45. with pytest.raises(ValueError):
  46. _ = SSIMNet(max_val)
  47. def test_ssim_filter_size_float():
  48. with pytest.raises(TypeError):
  49. _ = SSIMNet(filter_size=1.1)
  50. def test_ssim_filter_size_zero():
  51. with pytest.raises(ValueError):
  52. _ = SSIMNet(filter_size=0)
  53. def test_ssim_filter_sigma_zero():
  54. with pytest.raises(ValueError):
  55. _ = SSIMNet(filter_sigma=0.0)
  56. def test_ssim_filter_sigma_negative():
  57. with pytest.raises(ValueError):
  58. _ = SSIMNet(filter_sigma=-0.1)
  59. def test_ssim_different_shape():
  60. shape_1 = (8, 3, 16, 16)
  61. shape_2 = (8, 3, 8, 8)
  62. img1 = Tensor(np.random.random(shape_1))
  63. img2 = Tensor(np.random.random(shape_2))
  64. net = SSIMNet()
  65. with pytest.raises(ValueError):
  66. _executor.compile(net, img1, img2)
  67. def test_ssim_different_dtype():
  68. dtype_1 = mstype.float32
  69. dtype_2 = mstype.float16
  70. img1 = Tensor(np.random.random((8, 3, 16, 16)), dtype=dtype_1)
  71. img2 = Tensor(np.random.random((8, 3, 16, 16)), dtype=dtype_2)
  72. net = SSIMNet()
  73. with pytest.raises(TypeError):
  74. _executor.compile(net, img1, img2)
  75. def test_ssim_invalid_5d_input():
  76. shape_1 = (8, 3, 16, 16)
  77. shape_2 = (8, 3, 8, 8)
  78. invalid_shape = (8, 3, 16, 16, 1)
  79. img1 = Tensor(np.random.random(shape_1))
  80. invalid_img1 = Tensor(np.random.random(invalid_shape))
  81. img2 = Tensor(np.random.random(shape_2))
  82. invalid_img2 = Tensor(np.random.random(invalid_shape))
  83. net = SSIMNet()
  84. with pytest.raises(ValueError):
  85. _executor.compile(net, invalid_img1, img2)
  86. with pytest.raises(ValueError):
  87. _executor.compile(net, img1, invalid_img2)
  88. with pytest.raises(ValueError):
  89. _executor.compile(net, invalid_img1, invalid_img2)