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""" test_initializer_fuzz """ 16import pytest 17 18import mindspore.nn as nn 19from mindspore import Model 20 21 22class Net(nn.Cell): 23 """ Net definition """ 24 25 def __init__(self, in_str): 26 a, b, c, d, e, f, g, h = in_str.strip().split() 27 a = int(a) 28 b = int(b) 29 c = int(b) 30 d = int(b) 31 e = int(b) 32 f = int(b) 33 g = int(b) 34 h = int(b) 35 36 super(Net, self).__init__() 37 self.conv = nn.Conv2d(a, b, c, pad_mode="valid") 38 self.bn = nn.BatchNorm2d(d) 39 self.relu = nn.ReLU() 40 self.flatten = nn.Flatten() 41 self.fc = nn.Dense(e * f * g, h) 42 43 def construct(self, x): 44 x = self.conv(x) 45 x = self.bn(x) 46 x = self.relu(x) 47 x = self.flatten(x) 48 out = self.fc(x) 49 return out 50 51 52class LeNet5(nn.Cell): 53 """ LeNet5 definition """ 54 55 def __init__(self, in_str): 56 super(LeNet5, self).__init__() 57 58 a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15 = in_str.strip().split() 59 a1 = int(a1) 60 a2 = int(a2) 61 a3 = int(a3) 62 a4 = int(a4) 63 a5 = int(a5) 64 a6 = int(a6) 65 a7 = int(a7) 66 a8 = int(a8) 67 a9 = int(a9) 68 a10 = int(a10) 69 a11 = int(a11) 70 a12 = int(a12) 71 a13 = int(a13) 72 a14 = int(a14) 73 a15 = int(a15) 74 75 self.conv1 = nn.Conv2d(a1, a2, a3, pad_mode="valid") 76 self.conv2 = nn.Conv2d(a4, a5, a6, pad_mode="valid") 77 self.fc1 = nn.Dense(a7 * a8 * a9, a10) 78 self.fc2 = nn.Dense(a11, a12) 79 self.fc3 = nn.Dense(a13, a14) 80 self.relu = nn.ReLU() 81 self.max_pool2d = nn.MaxPool2d(kernel_size=a15) 82 self.flatten = nn.Flatten() 83 84 def construct(self, x): 85 x = self.max_pool2d(self.relu(self.conv1(x))) 86 x = self.max_pool2d(self.relu(self.conv2(x))) 87 x = self.flatten(x) 88 x = self.relu(self.fc1(x)) 89 x = self.relu(self.fc2(x)) 90 x = self.fc3(x) 91 return x 92 93 94def test_shape_error(): 95 """ for fuzz test""" 96 in_str = "3 6 5 6 -6 5 16 5 5 120 120 84 84 3 2" 97 with pytest.raises(ValueError): 98 net = LeNet5(in_str) # neural network 99 Model(net) 100