1# Copyright 2021 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 16 17import mindspore.nn as nn 18from mindspore import Tensor 19from mindspore.ops import operations as P 20 21class LeNet(nn.Cell): 22 def __init__(self): 23 super(LeNet, self).__init__() 24 self.relu = P.ReLU() 25 self.batch_size = 1 26 weight1 = Tensor(np.ones([6, 3, 5, 5]).astype(np.float32) * 0.01) 27 weight2 = Tensor(np.ones([16, 6, 5, 5]).astype(np.float32) * 0.01) 28 self.conv1 = nn.Conv2d(3, 6, (5, 5), weight_init=weight1, stride=1, padding=0, pad_mode='valid') 29 self.conv2 = nn.Conv2d(6, 16, (5, 5), weight_init=weight2, pad_mode='valid', stride=1, padding=0) 30 self.pool = nn.MaxPool2d(kernel_size=2, stride=2, pad_mode="valid") 31 32 self.reshape = P.Reshape() 33 self.reshape1 = P.Reshape() 34 35 self.fc1 = nn.Dense(400, 120) 36 self.fc2 = nn.Dense(120, 84) 37 self.fc3 = nn.Dense(84, 10) 38 39 def construct(self, input_x): 40 output = self.conv1(input_x) 41 output = self.relu(output) 42 output = self.pool(output) 43 output = self.conv2(output) 44 output = self.relu(output) 45 output = self.pool(output) 46 output = self.reshape(output, (self.batch_size, -1)) 47 output = self.fc1(output) 48 output = self.fc2(output) 49 output = self.fc3(output) 50 return output 51