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_slice_op.py 4.6 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 numpy as np
  16. import pytest
  17. import mindspore.context as context
  18. import mindspore.nn as nn
  19. from mindspore import Tensor
  20. from mindspore.common import dtype as mstype
  21. from mindspore.ops import operations as P
  22. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  23. class Slice(nn.Cell):
  24. def __init__(self):
  25. super(Slice, self).__init__()
  26. self.slice = P.Slice()
  27. def construct(self, x):
  28. return self.slice(x, (0, 1, 0), (2, 1, 3))
  29. @pytest.mark.level0
  30. @pytest.mark.platform_x86_cpu
  31. @pytest.mark.env_onecard
  32. def test_slice():
  33. x = Tensor(
  34. np.array([[[1, -1, 1], [2, -2, 2]], [[3, -3, 3], [4, -4, 4]], [[5, -5, 5], [6, -6, 6]]]), mstype.float32)
  35. expect = [[[2., -2., 2.]],
  36. [[4., -4., 4.]]]
  37. slice_op = Slice()
  38. output = slice_op(x)
  39. assert (output.asnumpy() == expect).all()
  40. class Slice2(nn.Cell):
  41. def __init__(self):
  42. super(Slice2, self).__init__()
  43. self.slice = P.Slice()
  44. def construct(self, x):
  45. return self.slice(x, (1, 0, 0), (1, 2, 3))
  46. @pytest.mark.level0
  47. @pytest.mark.platform_x86_cpu
  48. @pytest.mark.env_onecard
  49. def test_slice2():
  50. x = Tensor(np.arange(3 * 2 * 3).reshape(3, 2, 3), mstype.float32)
  51. expect = [[[6., 7., 8.],
  52. [9., 10., 11.]]]
  53. slice_op = Slice2()
  54. output = slice_op(x)
  55. assert (output.asnumpy() == expect).all()
  56. class Slice3(nn.Cell):
  57. def __init__(self):
  58. super(Slice3, self).__init__()
  59. self.relu = nn.ReLU()
  60. def construct(self, x):
  61. return (x[..., -1], x[..., 2:1:-1], x[1:3:1, 0, ...], x[-1, 0, ...])
  62. @pytest.mark.level0
  63. @pytest.mark.platform_x86_cpu
  64. @pytest.mark.env_onecard
  65. def test_slice3():
  66. inputx = np.random.rand(4, 4, 4, 4).astype(np.float32)
  67. x = Tensor(inputx)
  68. slice_op = Slice3()
  69. output = slice_op(x)
  70. assert (output[0].asnumpy() == inputx[..., -1]).all()
  71. assert (output[1].asnumpy() == inputx[..., 2:1:-1]).all()
  72. assert (output[2].asnumpy() == inputx[1:3:1, 0, ...]).all()
  73. assert (output[3].asnumpy() == inputx[-1, 0, ...]).all()
  74. class Slice4(nn.Cell):
  75. def __init__(self):
  76. super(Slice4, self).__init__()
  77. self.relu = nn.ReLU()
  78. def construct(self, x):
  79. return x[:10:1, :, 2:3:1]
  80. @pytest.mark.level0
  81. @pytest.mark.platform_x86_cpu
  82. @pytest.mark.env_onecard
  83. def test_slice4():
  84. inputx = np.random.rand(4, 4, 4).astype(np.float32)
  85. x = Tensor(inputx)
  86. slice_op = Slice4()
  87. output = slice_op(x)
  88. assert (output.asnumpy() == inputx[:10:1, :, 2:3:1]).all()
  89. class Slice5(nn.Cell):
  90. def __init__(self, begin, size):
  91. super(Slice5, self).__init__()
  92. self.relu = nn.ReLU()
  93. self.slice = P.Slice()
  94. self.begin = begin
  95. self.size = size
  96. def construct(self, x):
  97. return self.slice(x, self.begin, self.size)
  98. @pytest.mark.level0
  99. @pytest.mark.platform_x86_cpu
  100. @pytest.mark.env_onecard
  101. def test_slice5():
  102. inputx = np.arange(3 * 5 * 4).reshape(3, 5, 4).astype(np.float32)
  103. x = Tensor(inputx)
  104. begin = (0, 1, 0)
  105. size = (3, 4, 4)
  106. slice_op = Slice5(begin, size)
  107. output = slice_op(x)
  108. assert (output.asnumpy() == inputx[0:3:1, 1:5:1, 0:4:1]).all()
  109. class Slice6(nn.Cell):
  110. def __init__(self):
  111. super(Slice6, self).__init__()
  112. self.relu = nn.ReLU()
  113. def construct(self, x):
  114. return (x[-10:], x[-5:10:2, :, :], x[-10:10:1, :, -10:10:1])
  115. @pytest.mark.level0
  116. @pytest.mark.platform_x86_cpu
  117. @pytest.mark.env_onecard
  118. def test_slice6():
  119. inputx = np.random.rand(4, 4, 4).astype(np.float32)
  120. x = Tensor(inputx)
  121. slice_op = Slice6()
  122. output = slice_op(x)
  123. assert (output[0].asnumpy() == inputx[-10:]).all()
  124. assert (output[1].asnumpy() == inputx[-5:10:2, :, :]).all()
  125. assert (output[2].asnumpy() == inputx[-10:10:1, :, -10:10:1]).all()
  126. if __name__ == '__main__':
  127. test_slice()
  128. test_slice2()
  129. test_slice3()
  130. test_slice4()
  131. test_slice5()
  132. test_slice6()