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_central_crop.py 2.2 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 CentralCrop
  17. """
  18. import numpy as np
  19. import pytest
  20. import mindspore.nn as nn
  21. from mindspore import Tensor
  22. from mindspore.common import dtype as mstype
  23. from mindspore.common.api import _executor
  24. class CentralCropNet(nn.Cell):
  25. def __init__(self, central_fraction):
  26. super(CentralCropNet, self).__init__()
  27. self.net = nn.CentralCrop(central_fraction)
  28. def construct(self, image):
  29. return self.net(image)
  30. def test_compile_3d_central_crop():
  31. central_fraction = 0.2
  32. net = CentralCropNet(central_fraction)
  33. image = Tensor(np.random.random((3, 16, 16)), mstype.float32)
  34. _executor.compile(net, image)
  35. def test_compile_4d_central_crop():
  36. central_fraction = 0.5
  37. net = CentralCropNet(central_fraction)
  38. image = Tensor(np.random.random((8, 3, 16, 16)), mstype.float32)
  39. _executor.compile(net, image)
  40. def test_central_fraction_bool():
  41. central_fraction = True
  42. with pytest.raises(TypeError):
  43. _ = CentralCropNet(central_fraction)
  44. def test_central_crop_central_fraction_negative():
  45. central_fraction = -1.0
  46. with pytest.raises(ValueError):
  47. _ = CentralCropNet(central_fraction)
  48. def test_central_fraction_zero():
  49. central_fraction = 0.0
  50. with pytest.raises(ValueError):
  51. _ = CentralCropNet(central_fraction)
  52. def test_central_crop_invalid_5d_input():
  53. invalid_shape = (8, 3, 16, 16, 1)
  54. invalid_image = Tensor(np.random.random(invalid_shape))
  55. net = CentralCropNet(central_fraction=0.5)
  56. with pytest.raises(ValueError):
  57. _executor.compile(net, invalid_image)