1# Copyright 2019 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 16from functools import reduce 17import numpy as np 18import pytest 19 20import mindspore.context as context 21import mindspore.nn as nn 22import mindspore.ops.operations as P 23from mindspore import Tensor 24 25context.set_context(mode=context.GRAPH_MODE, device_target="CPU") 26 27 28class Net_Pool(nn.Cell): 29 def __init__(self): 30 super(Net_Pool, self).__init__() 31 self.maxpool_fun = nn.MaxPool2d(kernel_size=2, stride=2, pad_mode="VALID") 32 33 def construct(self, x): 34 return self.maxpool_fun(x) 35 36 37class Net_Pool2(nn.Cell): 38 def __init__(self): 39 super(Net_Pool2, self).__init__() 40 self.maxpool_fun2 = nn.MaxPool2d(kernel_size=3, stride=2, pad_mode="SAME") 41 42 def construct(self, x): 43 return self.maxpool_fun2(x) 44 45 46@pytest.mark.level0 47@pytest.mark.platform_x86_cpu 48@pytest.mark.env_onecard 49def test_maxpool2d(): 50 x = Tensor(np.array([[[ 51 [0, 1, 2, 3, -4, -5], 52 [6, 7, 8, 9, -10, -11], 53 [12, 13, 14, -15, -16, -17], 54 [18, 19, 20, 21, 22, 23], 55 [24, 25, 26, 27, 28, 29], 56 [30, 31, 32, 33, 34, 35] 57 ]]]).astype(np.float32)) 58 maxpool2d = Net_Pool() 59 maxpool2d2 = Net_Pool2() 60 output2 = maxpool2d2(x) 61 output = maxpool2d(x) 62 expect_result = (np.array([[[ 63 [7, 9, -4], 64 [19, 21, 23], 65 [31, 33, 35] 66 ]]])) 67 expect_result2 = (np.array([[[ 68 [14, 14, -4], 69 [26, 28, 29], 70 [32, 34, 35] 71 ]]])) 72 print(output.asnumpy()) 73 assert (output.asnumpy() == expect_result).all() 74 print(output2.asnumpy()) 75 assert (output2.asnumpy() == expect_result2).all() 76 77 78@pytest.mark.level0 79@pytest.mark.platform_x86_cpu 80@pytest.mark.env_onecard 81def test_maxpool(): 82 x = Tensor(np.array([[[ 83 [0, 1, 2, 3, -4, -5], 84 [6, 7, 8, 9, -10, -11], 85 [12, 13, 14, -15, -16, -17], 86 [18, 19, 20, 21, 22, 23], 87 [24, 25, 26, 27, 28, 29], 88 [30, 31, 32, 33, 34, 35] 89 ]]]).astype(np.int16)) 90 maxpool2d = Net_Pool() 91 with pytest.raises(Exception): 92 maxpool2d(x) 93 94 95@pytest.mark.level0 96@pytest.mark.platform_x86_cpu 97@pytest.mark.env_onecard 98def test_max_pool3d_1(): 99 context.set_context(mode=context.GRAPH_MODE, device_target="CPU") 100 x_shape = (2, 3, 2, 3, 4) 101 kernel_size = (2, 2, 3) 102 strides = 1 103 pad_mode = 'VALID' 104 x_val = np.arange(reduce(lambda x, y: x * y, x_shape)) 105 x_ms = Tensor(x_val).reshape(x_shape).astype(np.float32) 106 output_ms = P.MaxPool3D(kernel_size=kernel_size, strides=strides, pad_mode=pad_mode)(x_ms) 107 expert_result = (np.array([[[[[18, 19], 108 [22, 23]]], 109 [[[42, 43], 110 [46, 47]]], 111 [[[66, 67], 112 [70, 71]]]], 113 [[[[90, 91], 114 [94, 95]]], 115 [[[114, 115], 116 [118, 119]]], 117 [[[138, 139], 118 [142, 143]]]]])) 119 assert (output_ms.asnumpy() == expert_result).all() 120 121 122@pytest.mark.level0 123@pytest.mark.platform_x86_cpu 124@pytest.mark.env_onecard 125def test_max_pool3d_2(): 126 context.set_context(mode=context.GRAPH_MODE, device_target="CPU") 127 x_shape = (2, 3, 2, 3, 4) 128 kernel_size = 2 129 strides = 1 130 pad_mode = 'VALID' 131 x_val = np.arange(reduce(lambda x, y: x * y, x_shape)) 132 x_ms = Tensor(x_val).reshape(x_shape).astype(np.float32) 133 output_ms = P.MaxPool3D(kernel_size=kernel_size, strides=strides, pad_mode=pad_mode)(x_ms) 134 expert_result = (np.array([[[[[17, 18, 19], 135 [21, 22, 23]]], 136 [[[41, 42, 43], 137 [45, 46, 47]]], 138 [[[65, 66, 67], 139 [69, 70, 71]]]], 140 [[[[89, 90, 91], 141 [93, 94, 95]]], 142 [[[113, 114, 115], 143 [117, 118, 119]]], 144 [[[137, 138, 139], 145 [141, 142, 143]]]]])) 146 assert (output_ms.asnumpy() == expert_result).all() 147 148 149@pytest.mark.level0 150@pytest.mark.platform_x86_cpu 151@pytest.mark.env_onecard 152def test_max_pool3d_3(): 153 context.set_context(mode=context.GRAPH_MODE, device_target="CPU") 154 x_shape = (2, 3, 2, 3, 4) 155 kernel_size = 2 156 strides = 3 157 pad_mode = 'VALID' 158 x_val = np.arange(reduce(lambda x, y: x * y, x_shape)) 159 x_ms = Tensor(x_val).reshape(x_shape).astype(np.float32) 160 output_ms = P.MaxPool3D(kernel_size=kernel_size, strides=strides, pad_mode=pad_mode)(x_ms) 161 expert_result = (np.array([[[[[17]]], 162 [[[41]]], 163 [[[65]]]], 164 [[[[89]]], 165 [[[113]]], 166 [[[137]]]]])) 167 assert (output_ms.asnumpy() == expert_result).all() 168 169 170@pytest.mark.level0 171@pytest.mark.platform_x86_cpu 172@pytest.mark.env_onecard 173def test_max_pool3d_4(): 174 context.set_context(mode=context.GRAPH_MODE, device_target="CPU") 175 x_shape = (2, 3, 2, 3, 4) 176 kernel_size = (2, 2, 3) 177 strides = 1 178 pad_mode = 'SAME' 179 x_val = np.arange(reduce(lambda x, y: x * y, x_shape)) 180 x_ms = Tensor(x_val).reshape(x_shape).astype(np.float32) 181 output_ms = P.MaxPool3D(kernel_size=kernel_size, strides=strides, pad_mode=pad_mode)(x_ms) 182 expert_result = (np.array([[[[[17, 18, 19, 19], 183 [21, 22, 23, 23], 184 [21, 22, 23, 23]], 185 [[17, 18, 19, 19], 186 [21, 22, 23, 23], 187 [21, 22, 23, 23]]], 188 [[[41, 42, 43, 43], 189 [45, 46, 47, 47], 190 [45, 46, 47, 47]], 191 [[41, 42, 43, 43], 192 [45, 46, 47, 47], 193 [45, 46, 47, 47]]], 194 [[[65, 66, 67, 67], 195 [69, 70, 71, 71], 196 [69, 70, 71, 71]], 197 [[65, 66, 67, 67], 198 [69, 70, 71, 71], 199 [69, 70, 71, 71]]]], 200 [[[[89, 90, 91, 91], 201 [93, 94, 95, 95], 202 [93, 94, 95, 95]], 203 [[89, 90, 91, 91], 204 [93, 94, 95, 95], 205 [93, 94, 95, 95]]], 206 [[[113, 114, 115, 115], 207 [117, 118, 119, 119], 208 [117, 118, 119, 119]], 209 [[113, 114, 115, 115], 210 [117, 118, 119, 119], 211 [117, 118, 119, 119]]], 212 [[[137, 138, 139, 139], 213 [141, 142, 143, 143], 214 [141, 142, 143, 143]], 215 [[137, 138, 139, 139], 216 [141, 142, 143, 143], 217 [141, 142, 143, 143]]]]])) 218 assert (output_ms.asnumpy() == expert_result).all() 219 220 221@pytest.mark.level0 222@pytest.mark.platform_x86_cpu 223@pytest.mark.env_onecard 224def test_max_pool3d_5(): 225 context.set_context(mode=context.PYNATIVE_MODE, device_target="CPU") 226 x_shape = (2, 3, 2, 3, 4) 227 kernel_size = (2, 2, 3) 228 strides = 1 229 pad_mode = 'SAME' 230 x_val = np.arange(reduce(lambda x, y: x * y, x_shape)) 231 x_ms = Tensor(x_val).reshape(x_shape).astype(np.float32) 232 output_ms = P.MaxPool3D(kernel_size=kernel_size, strides=strides, pad_mode=pad_mode)(x_ms) 233 expert_result = (np.array([[[[[17, 18, 19, 19], 234 [21, 22, 23, 23], 235 [21, 22, 23, 23]], 236 [[17, 18, 19, 19], 237 [21, 22, 23, 23], 238 [21, 22, 23, 23]]], 239 [[[41, 42, 43, 43], 240 [45, 46, 47, 47], 241 [45, 46, 47, 47]], 242 [[41, 42, 43, 43], 243 [45, 46, 47, 47], 244 [45, 46, 47, 47]]], 245 [[[65, 66, 67, 67], 246 [69, 70, 71, 71], 247 [69, 70, 71, 71]], 248 [[65, 66, 67, 67], 249 [69, 70, 71, 71], 250 [69, 70, 71, 71]]]], 251 [[[[89, 90, 91, 91], 252 [93, 94, 95, 95], 253 [93, 94, 95, 95]], 254 [[89, 90, 91, 91], 255 [93, 94, 95, 95], 256 [93, 94, 95, 95]]], 257 [[[113, 114, 115, 115], 258 [117, 118, 119, 119], 259 [117, 118, 119, 119]], 260 [[113, 114, 115, 115], 261 [117, 118, 119, 119], 262 [117, 118, 119, 119]]], 263 [[[137, 138, 139, 139], 264 [141, 142, 143, 143], 265 [141, 142, 143, 143]], 266 [[137, 138, 139, 139], 267 [141, 142, 143, 143], 268 [141, 142, 143, 143]]]]])) 269 assert (output_ms.asnumpy() == expert_result).all() 270