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

5 years ago
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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.api import _executor
  22. from mindspore import Tensor
  23. class PSNRNet(nn.Cell):
  24. def __init__(self, max_val=1.0):
  25. super(PSNRNet, self).__init__()
  26. self.net = nn.PSNR(max_val)
  27. def construct(self, img1, img2):
  28. return self.net(img1, img2)
  29. def test_compile_psnr():
  30. max_val = 1.0
  31. net = PSNRNet(max_val)
  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_psnr_grayscale():
  36. max_val = 255
  37. net = PSNRNet(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_psnr_max_val_negative():
  42. max_val = -1
  43. with pytest.raises(ValueError):
  44. net = PSNRNet(max_val)
  45. def test_psnr_max_val_bool():
  46. max_val = True
  47. with pytest.raises(TypeError):
  48. net = PSNRNet(max_val)
  49. def test_psnr_max_val_zero():
  50. max_val = 0
  51. with pytest.raises(ValueError):
  52. net = PSNRNet(max_val)