• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2019-2021 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
16import numpy as np
17import pytest
18
19import mindspore.context as context
20import mindspore.nn as nn
21from mindspore import Tensor
22from mindspore.ops import operations as P
23
24
25class Slice(nn.Cell):
26    def __init__(self):
27        super(Slice, self).__init__()
28        self.slice = P.Slice()
29
30    def construct(self, x):
31        return self.slice(x, (0, 1, 0), (2, 1, 3))
32
33
34@pytest.mark.level0
35@pytest.mark.platform_x86_gpu_training
36@pytest.mark.env_onecard
37def test_slice():
38    x = Tensor(
39        np.array([[[1, -1, 1], [2, -2, 2]], [[3, -3, 3], [4, -4, 4]], [[5, -5, 5], [6, -6, 6]]]).astype(np.float32))
40    expect = [[[2., -2., 2.]],
41              [[4., -4., 4.]]]
42
43    context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
44    slice_op = Slice()
45    output = slice_op(x)
46    assert (output.asnumpy() == expect).all()
47
48
49class SliceNet(nn.Cell):
50    def __init__(self):
51        super(SliceNet, self).__init__()
52        self.slice = P.Slice()
53
54    def construct(self, x):
55        return self.slice(x, (0, 11, 0, 0), (32, 7, 224, 224))
56
57
58@pytest.mark.level0
59@pytest.mark.platform_x86_gpu_training
60@pytest.mark.env_onecard
61def test_slice_4d():
62    x_np = np.random.randn(32, 24, 224, 224).astype(np.float32)
63    output_np = x_np[:, 11:18, :, :]
64
65    context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
66    x_ms = Tensor(x_np)
67    net = SliceNet()
68    output_ms = net(x_ms)
69
70    assert (output_ms.asnumpy() == output_np).all()
71
72
73class Slice5DNet(nn.Cell):
74    def __init__(self):
75        super(Slice5DNet, self).__init__()
76        self.slice = P.Slice()
77
78    def construct(self, x):
79        return self.slice(x, (0, 11, 1, 2, 3), (32, 7, 14, 10, 221))
80
81
82@pytest.mark.level0
83@pytest.mark.platform_x86_gpu_training
84@pytest.mark.env_onecard
85def test_slice_5d():
86    x_np = np.random.randn(32, 32, 24, 224, 224).astype(np.float32)
87    output_np = x_np[:, 11:18, 1:15, 2:12, 3:224]
88
89    context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
90    x_ms = Tensor(x_np)
91    net = Slice5DNet()
92    output_ms = net(x_ms)
93
94    assert (output_ms.asnumpy() == output_np).all()
95
96
97@pytest.mark.level0
98@pytest.mark.platform_x86_gpu_training
99@pytest.mark.env_onecard
100def test_slice_float64():
101    x = Tensor(
102        np.array([[[1, -1, 1], [2, -2, 2]], [[3, -3, 3], [4, -4, 4]], [[5, -5, 5], [6, -6, 6]]]).astype(np.float64))
103    expect = np.array([[[2., -2., 2.]],
104                       [[4., -4., 4.]]]).astype(np.float64)
105
106    context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
107    slice_op = Slice()
108    output = slice_op(x)
109    assert (output.asnumpy() == expect).all()
110