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_psnr.py 3.1 kB

5 years ago
5 years ago
5 years ago
5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 psnr
  17. """
  18. import numpy as np
  19. import pytest
  20. import mindspore.nn as nn
  21. from mindspore.common import dtype as mstype
  22. from mindspore.common.api import _executor
  23. from mindspore import Tensor
  24. class PSNRNet(nn.Cell):
  25. def __init__(self, max_val=1.0):
  26. super(PSNRNet, self).__init__()
  27. self.net = nn.PSNR(max_val)
  28. def construct(self, img1, img2):
  29. return self.net(img1, img2)
  30. def test_compile_psnr():
  31. max_val = 1.0
  32. net = PSNRNet(max_val)
  33. img1 = Tensor(np.random.random((8, 3, 16, 16)))
  34. img2 = Tensor(np.random.random((8, 3, 16, 16)))
  35. _executor.compile(net, img1, img2)
  36. def test_compile_psnr_grayscale():
  37. max_val = 255
  38. net = PSNRNet(max_val)
  39. img1 = Tensor(np.random.randint(0, 256, (8, 1, 16, 16), np.uint8))
  40. img2 = Tensor(np.random.randint(0, 256, (8, 1, 16, 16), np.uint8))
  41. _executor.compile(net, img1, img2)
  42. def test_psnr_max_val_negative():
  43. max_val = -1
  44. with pytest.raises(ValueError):
  45. net = PSNRNet(max_val)
  46. def test_psnr_max_val_bool():
  47. max_val = True
  48. with pytest.raises(TypeError):
  49. net = PSNRNet(max_val)
  50. def test_psnr_max_val_zero():
  51. max_val = 0
  52. with pytest.raises(ValueError):
  53. net = PSNRNet(max_val)
  54. def test_psnr_different_shape():
  55. shape_1 = (8, 3, 16, 16)
  56. shape_2 = (8, 3, 8, 8)
  57. img1 = Tensor(np.random.random(shape_1))
  58. img2 = Tensor(np.random.random(shape_2))
  59. net = PSNRNet()
  60. with pytest.raises(ValueError):
  61. _executor.compile(net, img1, img2)
  62. def test_psnr_different_dtype():
  63. dtype_1 = mstype.float32
  64. dtype_2 = mstype.float16
  65. img1 = Tensor(np.random.random((8, 3, 16, 16)), dtype=dtype_1)
  66. img2 = Tensor(np.random.random((8, 3, 16, 16)), dtype=dtype_2)
  67. net = PSNRNet()
  68. with pytest.raises(TypeError):
  69. _executor.compile(net, img1, img2)
  70. def test_psnr_invalid_5d_input():
  71. shape_1 = (8, 3, 16, 16)
  72. shape_2 = (8, 3, 8, 8)
  73. invalid_shape = (8, 3, 16, 16, 1)
  74. img1 = Tensor(np.random.random(shape_1))
  75. invalid_img1 = Tensor(np.random.random(invalid_shape))
  76. img2 = Tensor(np.random.random(shape_2))
  77. invalid_img2 = Tensor(np.random.random(invalid_shape))
  78. net = PSNRNet()
  79. with pytest.raises(ValueError):
  80. _executor.compile(net, invalid_img1, img2)
  81. with pytest.raises(ValueError):
  82. _executor.compile(net, img1, invalid_img2)
  83. with pytest.raises(ValueError):
  84. _executor.compile(net, invalid_img1, invalid_img2)