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""" 16test pooling api 17""" 18import numpy as np 19import pytest 20 21import mindspore.nn as nn 22from mindspore import Tensor 23 24 25def test_avgpool2d(): 26 """ test_avgpool2d """ 27 kernel_size = 3 28 stride = 2 29 avg_pool = nn.AvgPool2d(kernel_size, stride) 30 assert avg_pool.kernel_size == 3 31 assert avg_pool.stride == 2 32 input_data = Tensor(np.random.randint(0, 255, [1, 3, 6, 6]) * 0.1) 33 output = avg_pool(input_data) 34 output_np = output.asnumpy() 35 assert isinstance(output_np[0][0][0][0], (np.float32, np.float64)) 36 37 38def test_avgpool2d_error_input(): 39 """ test_avgpool2d_error_input """ 40 kernel_size = 5 41 stride = 2.3 42 with pytest.raises(TypeError): 43 nn.AvgPool2d(kernel_size, stride) 44 45 46def test_maxpool2d(): 47 """ test_maxpool2d """ 48 kernel_size = 3 49 stride = 3 50 51 max_pool = nn.MaxPool2d(kernel_size, stride, pad_mode='SAME') 52 assert max_pool.kernel_size == 3 53 assert max_pool.stride == 3 54 input_data = Tensor(np.random.randint(0, 255, [1, 3, 6, 6]) * 0.1) 55 output = max_pool(input_data) 56 output_np = output.asnumpy() 57 assert isinstance(output_np[0][0][0][0], (np.float32, np.float64)) 58