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# ============================================================================ 15import mindspore.nn as nn 16from mindspore.ops import operations as P 17 18 19class AlexNet(nn.Cell): 20 def __init__(self, num_classes=10): 21 super(AlexNet, self).__init__() 22 self.batch_size = 32 23 self.conv1 = nn.Conv2d(3, 96, 11, stride=4, pad_mode="valid") 24 self.conv2 = nn.Conv2d(96, 256, 5, stride=1, pad_mode="same") 25 self.conv3 = nn.Conv2d(256, 384, 3, stride=1, pad_mode="same") 26 self.conv4 = nn.Conv2d(384, 384, 3, stride=1, pad_mode="same") 27 self.conv5 = nn.Conv2d(384, 256, 3, stride=1, pad_mode="same") 28 self.relu = P.ReLU() 29 self.max_pool2d = nn.MaxPool2d(kernel_size=3, stride=2) 30 self.flatten = nn.Flatten() 31 self.fc1 = nn.Dense(66256, 4096) 32 self.fc2 = nn.Dense(4096, 4096) 33 self.fc3 = nn.Dense(4096, num_classes) 34 35 def construct(self, x): 36 x = self.conv1(x) 37 x = self.relu(x) 38 x = self.max_pool2d(x) 39 x = self.conv2(x) 40 x = self.relu(x) 41 x = self.max_pool2d(x) 42 x = self.conv3(x) 43 x = self.relu(x) 44 x = self.conv4(x) 45 x = self.relu(x) 46 x = self.conv5(x) 47 x = self.relu(x) 48 x = self.max_pool2d(x) 49 x = self.flatten(x) 50 x = self.fc1(x) 51 x = self.relu(x) 52 x = self.fc2(x) 53 x = self.relu(x) 54 x = self.fc3(x) 55 return x 56