• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2023 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# ============================================================================
15import numpy as np
16import pytest
17import mindspore as ms
18import mindspore.nn as nn
19from mindspore import Tensor
20
21
22@pytest.mark.level2
23@pytest.mark.platform_x86_cpu
24@pytest.mark.platform_arm_cpu
25@pytest.mark.platform_x86_gpu_training
26@pytest.mark.platform_arm_ascend_training
27@pytest.mark.platform_x86_ascend_training
28@pytest.mark.env_onecard
29@pytest.mark.parametrize('mode', [ms.GRAPH_MODE, ms.PYNATIVE_MODE])
30def test_upsample_area_3d(mode):
31    """
32    Feature: upsample
33    Description: Verify the result of upsample area mode
34                1. 3D size
35                2. 3D scale_factor
36    Expectation: success
37    """
38    ms.set_context(mode=mode)
39
40    # 1. 3D(1, 3, 4)
41    input_3d = np.array([[[0.476130, 0.196372, 0.320748, 0.574267],
42                          [0.558406, 0.186530, 0.144793, 0.017598],
43                          [0.043675, 0.360826, 0.367078, 0.607198]]], dtype=np.float32)
44    except_3d_1 = np.array([[[0.336251, 0.447508],
45                             [0.372468, 0.081196],
46                             [0.202250, 0.487138]]], dtype=np.float32)
47    size = 2
48    net = nn.Upsample(mode="area", size=size)
49    output_3d_1 = net(Tensor(input_3d))
50    assert np.allclose(output_3d_1.asnumpy(), except_3d_1, atol=1e-3, rtol=1e-3)
51
52    # 2. 3D(1, 3, 4) scale_factor=0.3
53    except_3d_2 = np.array([[[0.391879],
54                             [0.226832],
55                             [0.344694]]], dtype=np.float32)
56    scale_factor = 0.3
57    net = nn.Upsample(mode="area", scale_factor=scale_factor)
58    output_3d_2 = net(Tensor(input_3d))
59    assert np.allclose(output_3d_2.asnumpy(), except_3d_2, atol=1e-3, rtol=1e-3)
60
61
62@pytest.mark.level2
63@pytest.mark.platform_x86_cpu
64@pytest.mark.platform_arm_cpu
65@pytest.mark.platform_x86_gpu_training
66@pytest.mark.platform_arm_ascend_training
67@pytest.mark.platform_x86_ascend_training
68@pytest.mark.env_onecard
69@pytest.mark.parametrize('mode', [ms.GRAPH_MODE, ms.PYNATIVE_MODE])
70def test_upsample_bilinear(mode):
71    """
72    Feature: upsample
73    Description: Verify the result of upsample bilinear mode
74                1. 4D size
75                2. 4D size align_corners=True
76                3. 4D scale_factor recompute_scale_factor=True
77    Expectation: success
78    """
79    ms.set_context(mode=mode)
80    input_4d = np.array([[[[0.003088, 0.313131, 0.481231, 0.326219, 0.190293],
81                           [0.711616, 0.58399, 0.718121, 0.258823, 0.121847],
82                           [0.781316, 0.591508, 0.858185, 0.091935, 0.444639]],
83                          [[0.389884, 0.894497, 0.471427, 0.188708, 0.557449],
84                           [0.998047, 0.380719, 0.570574, 0.722258, 0.997173],
85                           [0.195751, 0.050744, 0.002008, 0.482685, 0.708559]]]],
86                        dtype=np.float32)
87    size = (5, 3)
88
89    # 1. 4D size=(5, 3)
90    except_4d_1 = np.array([[[[0.106436, 0.481231, 0.235602],
91                              [0.331491, 0.575987, 0.208363],
92                              [0.669074, 0.718121, 0.167506],
93                              [0.698458, 0.802159, 0.263245],
94                              [0.718047, 0.858185, 0.327071]],
95                             [[0.558088, 0.471427, 0.434535],
96                              [0.651761, 0.511086, 0.622935],
97                              [0.792271, 0.570574, 0.905535],
98                              [0.405358, 0.229434, 0.742174],
99                              [0.147415, 0.002008, 0.633268]]]], dtype=np.float32)
100    net = nn.Upsample(mode="bilinear", size=size)
101    output_4d_1 = net(Tensor(input_4d))
102    assert np.allclose(output_4d_1.asnumpy(), except_4d_1, atol=1e-5, rtol=1e-5)
103
104    # 2. 4D size=(5, 3), align_corners=True
105    except_4d_2 = np.array([[[[0.003088, 0.481231, 0.190293],
106                              [0.357352, 0.599676, 0.15607],
107                              [0.711616, 0.718121, 0.121847],
108                              [0.746466, 0.788153, 0.283243],
109                              [0.781316, 0.858185, 0.444639]],
110
111                             [[0.389884, 0.471427, 0.557449],
112                              [0.693965, 0.521001, 0.777311],
113                              [0.998047, 0.570574, 0.997173],
114                              [0.596899, 0.286291, 0.852866],
115                              [0.195751, 0.002008, 0.708559]]]], dtype=np.float32)
116    net = nn.Upsample(mode="bilinear", size=size, align_corners=True)
117    output_4d_2 = net(Tensor(input_4d))
118    assert np.allclose(output_4d_2.asnumpy(), except_4d_2, atol=1e-5, rtol=1e-5)
119
120    # 3. scale_factor=(0.5, 1.5), recompute_scale_factor=True
121    scale_factor = (0.5, 1.5)
122    except_4d_3 = np.array([[[[0.711616, 0.638687, 0.622313, 0.718121, 0.390051, 0.200119,
123                               0.121847]],
124
125                             [[0.998047, 0.645288, 0.434963, 0.570574, 0.67892, 0.840079,
126                               0.997173]]]], dtype=np.float32)
127    net = nn.Upsample(mode="bilinear", scale_factor=scale_factor, recompute_scale_factor=True)
128    output_4d_3 = net(Tensor(input_4d))
129    assert np.allclose(output_4d_3.asnumpy(), except_4d_3, atol=1e-5, rtol=1e-5)
130