• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"""LeNet."""
16import mindspore.nn as nn
17
18
19class LeNet5(nn.Cell):
20    """
21    Lenet network
22
23    Args:
24        num_class (int): Num classes. Default: 10.
25
26    Returns:
27        Tensor, output tensor
28    Examples:
29        >>> LeNet(num_class=10)
30
31    """
32
33    def __init__(self, num_class=10, channel=1):
34        super(LeNet5, self).__init__()
35        self.type = "fusion"
36        self.num_class = num_class
37
38        # change `nn.Conv2d` to `nn.Conv2dBnAct`
39        self.conv1 = nn.Conv2dBnAct(channel, 6, 5, pad_mode='valid', activation='relu')
40        self.conv2 = nn.Conv2dBnAct(6, 16, 5, pad_mode='valid', activation='relu')
41        # change `nn.Dense` to `nn.DenseBnAct`
42        self.fc1 = nn.DenseBnAct(16 * 5 * 5, 120, activation='relu')
43        self.fc2 = nn.DenseBnAct(120, 84, activation='relu')
44        self.fc3 = nn.DenseBnAct(84, self.num_class)
45
46        self.max_pool2d = nn.MaxPool2d(kernel_size=2, stride=2)
47        self.flatten = nn.Flatten()
48
49    def construct(self, x):
50        x = self.conv1(x)
51        x = self.max_pool2d(x)
52        x = self.conv2(x)
53        x = self.max_pool2d(x)
54        x = self.flatten(x)
55        x = self.fc1(x)
56        x = self.fc2(x)
57        x = self.fc3(x)
58        return x
59