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 19 20import mindspore.nn as nn 21from mindspore import Tensor 22from mindspore.common.api import _cell_graph_executor 23 24 25class AvgNet(nn.Cell): 26 def __init__(self, 27 kernel_size, 28 stride=None): 29 super(AvgNet, self).__init__() 30 self.avgpool = nn.AvgPool2d(kernel_size, stride) 31 32 def construct(self, x): 33 return self.avgpool(x) 34 35 36def test_compile_avg(): 37 net = AvgNet(3, 1) 38 x = Tensor(np.ones([1, 3, 16, 50]).astype(np.float32)) 39 _cell_graph_executor.compile(net, x) 40 41 42class MaxNet(nn.Cell): 43 """ MaxNet definition """ 44 45 def __init__(self, 46 kernel_size, 47 stride=None, 48 padding=0): 49 _ = padding 50 super(MaxNet, self).__init__() 51 self.maxpool = nn.MaxPool2d(kernel_size, 52 stride) 53 54 def construct(self, x): 55 return self.maxpool(x) 56 57 58def test_compile_max(): 59 net = MaxNet(3, stride=1, padding=0) 60 x = Tensor(np.random.randint(0, 255, [1, 3, 6, 6]).astype(np.float32)) 61 _cell_graph_executor.compile(net, x) 62 63 64class Avg1dNet(nn.Cell): 65 def __init__(self, 66 kernel_size, 67 stride=None): 68 super(Avg1dNet, self).__init__() 69 self.avg1d = nn.AvgPool1d(kernel_size, stride) 70 71 def construct(self, x): 72 return self.avg1d(x) 73 74 75def test_avg1d(): 76 net = Avg1dNet(6, 1) 77 input_ = Tensor(np.random.randint(0, 255, [1, 3, 6]).astype(np.float32)) 78 _cell_graph_executor.compile(net, input_) 79