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_mirror_pad.py 3.3 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. import pytest
  16. import numpy as np
  17. import mindspore
  18. import mindspore.nn as nn
  19. import mindspore.context as context
  20. from mindspore import Tensor
  21. from mindspore.ops.composite import GradOperation
  22. @pytest.mark.level0
  23. @pytest.mark.platform_x86_gpu_training
  24. @pytest.mark.env_onecard
  25. def test_mirror_pad():
  26. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  27. test1_arr_in = [[[[1, 2, 3], [4, 5, 6], [7, 8, 9]]]]
  28. test_1_paddings = ((0, 0), (0, 0), (1, 1), (2, 2))
  29. test1_arr_exp = [[[[6, 5, 4, 5, 6, 5, 4], [3, 2, 1, 2, 3, 2, 1], [6, 5, 4, 5, 6, 5, 4],
  30. [9, 8, 7, 8, 9, 8, 7], [6, 5, 4, 5, 6, 5, 4]]]]
  31. test2_arr_in = [[[[1, 2, 3], [4, 5, 6], [7, 8, 9]]]]
  32. test_2_paddings = ((0, 0), (0, 0), (1, 1), (2, 2))
  33. test2_arr_exp = [[[[2, 1, 1, 2, 3, 3, 2], [2, 1, 1, 2, 3, 3, 2], [5, 4, 4, 5, 6, 6, 5],
  34. [8, 7, 7, 8, 9, 9, 8], [8, 7, 7, 8, 9, 9, 8]]]]
  35. reflectOp = nn.Pad(mode='REFLECT', paddings=test_1_paddings)
  36. symmOp = nn.Pad(mode='SYMMETRIC', paddings=test_2_paddings)
  37. x_test_1 = Tensor(np.array(test1_arr_in), dtype=mindspore.float32)
  38. x_test_2 = Tensor(np.array(test2_arr_in), dtype=mindspore.float32)
  39. y_test_1 = reflectOp(x_test_1).asnumpy()
  40. y_test_2 = symmOp(x_test_2).asnumpy()
  41. print(np.array(test1_arr_in))
  42. print(y_test_1)
  43. np.testing.assert_equal(np.array(test1_arr_exp), y_test_1)
  44. np.testing.assert_equal(np.array(test2_arr_exp), y_test_2)
  45. class Grad(nn.Cell):
  46. def __init__(self, network):
  47. super(Grad, self).__init__()
  48. self.grad = GradOperation(get_all=True, sens_param=True)
  49. self.network = network
  50. def construct(self, input_, output_grad):
  51. return self.grad(self.network)(input_, output_grad)
  52. class Net(nn.Cell):
  53. def __init__(self):
  54. super(Net, self).__init__()
  55. self.pad = nn.Pad(mode="REFLECT", paddings=((0, 0), (0, 0), (1, 0), (0, 2)))
  56. def construct(self, x):
  57. return self.pad(x)
  58. @pytest.mark.level0
  59. @pytest.mark.platform_x86_gpu_training
  60. @pytest.mark.env_onecard
  61. def test_mirror_pad_backprop():
  62. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  63. test_arr_in = [[[[1, 2, 3], [4, 5, 6], [7, 8, 9]]]] # size -> 3*3
  64. test_arr_in = Tensor(test_arr_in, dtype=mindspore.float32)
  65. dy = (np.ones((1, 1, 4, 5)) * 0.1).astype(np.float32)
  66. expected_dx = np.array([[[[0.2, 0.2, 0.1],
  67. [0.4, 0.4, 0.2],
  68. [0.2, 0.2, 0.1]]]])
  69. net = Grad(Net())
  70. dx = net(test_arr_in, Tensor(dy))
  71. dx = dx[0].asnumpy()
  72. np.testing.assert_array_almost_equal(dx, expected_dx)