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 5.9 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. def test_slice_float64():
  57. data = Tensor(np.array([[[1, 1, 1], [2, 2, 2]],
  58. [[3, 3, 3], [4, 4, 4]],
  59. [[5, 5, 5], [6, 6, 6]]]).astype(np.float64))
  60. slice_op = P.Slice()
  61. output = slice_op(data, (1, 0, 0), (1, 1, 3))
  62. expect = [[[3.0, 3.0, 3.0]]]
  63. assert (output.asnumpy() == expect).all()
  64. class Slice3(nn.Cell):
  65. def __init__(self):
  66. super(Slice3, self).__init__()
  67. self.relu = nn.ReLU()
  68. def construct(self, x):
  69. return (x[..., -1], x[..., 2:1:-1], x[1:3:1, 0, ...], x[-1, 0, ...])
  70. @pytest.mark.level0
  71. @pytest.mark.platform_x86_cpu
  72. @pytest.mark.env_onecard
  73. def test_slice3():
  74. inputx = np.random.rand(4, 4, 4, 4).astype(np.float32)
  75. x = Tensor(inputx)
  76. slice_op = Slice3()
  77. output = slice_op(x)
  78. assert (output[0].asnumpy() == inputx[..., -1]).all()
  79. assert (output[1].asnumpy() == inputx[..., 2:1:-1]).all()
  80. assert (output[2].asnumpy() == inputx[1:3:1, 0, ...]).all()
  81. assert (output[3].asnumpy() == inputx[-1, 0, ...]).all()
  82. class Slice4(nn.Cell):
  83. def __init__(self):
  84. super(Slice4, self).__init__()
  85. self.relu = nn.ReLU()
  86. def construct(self, x):
  87. return x[:10:1, :, 2:3:1]
  88. @pytest.mark.level0
  89. @pytest.mark.platform_x86_cpu
  90. @pytest.mark.env_onecard
  91. def test_slice4():
  92. inputx = np.random.rand(4, 4, 4).astype(np.float32)
  93. x = Tensor(inputx)
  94. slice_op = Slice4()
  95. output = slice_op(x)
  96. assert (output.asnumpy() == inputx[:10:1, :, 2:3:1]).all()
  97. class Slice5(nn.Cell):
  98. def __init__(self, begin, size):
  99. super(Slice5, self).__init__()
  100. self.relu = nn.ReLU()
  101. self.slice = P.Slice()
  102. self.begin = begin
  103. self.size = size
  104. def construct(self, x):
  105. return self.slice(x, self.begin, self.size)
  106. @pytest.mark.level0
  107. @pytest.mark.platform_x86_cpu
  108. @pytest.mark.env_onecard
  109. def test_slice5():
  110. inputx = np.arange(3 * 5 * 4).reshape(3, 5, 4).astype(np.float32)
  111. x = Tensor(inputx)
  112. begin = (0, 1, 0)
  113. size = (3, 4, 4)
  114. slice_op = Slice5(begin, size)
  115. output = slice_op(x)
  116. assert (output.asnumpy() == inputx[0:3:1, 1:5:1, 0:4:1]).all()
  117. class Slice6(nn.Cell):
  118. def __init__(self):
  119. super(Slice6, self).__init__()
  120. self.relu = nn.ReLU()
  121. def construct(self, x):
  122. return (x[-10:], x[-5:10:2, :, :], x[-10:10:1, :, -10:10:1])
  123. @pytest.mark.level0
  124. @pytest.mark.platform_x86_cpu
  125. @pytest.mark.env_onecard
  126. def test_slice6():
  127. inputx = np.random.rand(4, 4, 4).astype(np.float32)
  128. x = Tensor(inputx)
  129. slice_op = Slice6()
  130. output = slice_op(x)
  131. assert (output[0].asnumpy() == inputx[-10:]).all()
  132. assert (output[1].asnumpy() == inputx[-5:10:2, :, :]).all()
  133. assert (output[2].asnumpy() == inputx[-10:10:1, :, -10:10:1]).all()
  134. class StridedSlice(nn.Cell):
  135. def __init__(self, begin, end, stride):
  136. super(StridedSlice, self).__init__()
  137. self.begin = begin
  138. self.end = end
  139. self.stride = stride
  140. self.stride_slice = P.StridedSlice()
  141. def construct(self, x):
  142. return self.stride_slice(x, self.begin, self.end, self.stride)
  143. @pytest.mark.level0
  144. @pytest.mark.platform_x86_cpu
  145. @pytest.mark.env_onecard
  146. def test_strided_slice_bool_type():
  147. input_x = Tensor([[[False, False, True], [False, True, False]], [[False, True, False], [True, False, False]],
  148. [[False, True, True], [True, False, True]]], mstype.bool_)
  149. begin = (1, 0, 0)
  150. end = (2, 1, 3)
  151. stride = (1, 1, 1)
  152. slice_op = StridedSlice(begin, end, stride)
  153. output = slice_op(input_x)
  154. expected_output = np.array([False, True, False])
  155. assert (output.asnumpy() == expected_output).all()
  156. if __name__ == '__main__':
  157. test_slice()
  158. test_slice2()
  159. test_slice3()
  160. test_slice4()
  161. test_slice5()
  162. test_slice6()
  163. test_strided_slice_bool_type()