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_image_gradients.py 2.0 kB

5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. """ test image gradients """
  16. import numpy as np
  17. import pytest
  18. import mindspore.common.dtype as mstype
  19. import mindspore.context as context
  20. import mindspore.nn as nn
  21. from mindspore import Tensor
  22. from mindspore.common.api import _executor
  23. from mindspore.common.api import ms_function
  24. context.set_context(device_target="Ascend")
  25. class Net(nn.Cell):
  26. def __init__(self):
  27. super(Net, self).__init__()
  28. self.image_gradients = nn.ImageGradients()
  29. @ms_function
  30. def construct(self, x):
  31. return self.image_gradients(x)
  32. def test_compile():
  33. # input shape 1 x 1 x 2 x 2
  34. image = Tensor(np.array([[[[1, 2], [3, 4]]]]), dtype=mstype.int32)
  35. net = Net()
  36. _executor.compile(net, image)
  37. def test_compile_multi_channel():
  38. # input shape 4 x 2 x 2 x 2
  39. dtype = mstype.int32
  40. image = Tensor(np.array([[[[1, 2], [3, 4]], [[5, 6], [7, 8]]],
  41. [[[3, 5], [7, 9]], [[11, 13], [15, 17]]],
  42. [[[5, 10], [15, 20]], [[25, 30], [35, 40]]],
  43. [[[10, 20], [30, 40]], [[50, 60], [70, 80]]]]), dtype=dtype)
  44. net = Net()
  45. _executor.compile(net, image)
  46. def test_invalid_5d_input():
  47. dtype = mstype.float32
  48. image = Tensor(np.random.random([4, 1, 16, 16, 1]), dtype=dtype)
  49. net = Net()
  50. with pytest.raises(ValueError):
  51. _executor.compile(net, image)