2021-05-27 13:36:33 +08:00
|
|
|
# copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
|
2021-05-27 12:10:37 +08:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2021-05-27 13:50:12 +08:00
|
|
|
from __future__ import absolute_import, division, print_function
|
2021-05-27 12:10:37 +08:00
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
import paddle
|
|
|
|
from paddle import ParamAttr
|
|
|
|
import paddle.nn as nn
|
|
|
|
from paddle.nn import Conv2D, BatchNorm, Linear, Dropout
|
|
|
|
from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D
|
|
|
|
from paddle.nn.initializer import KaimingNormal
|
|
|
|
import math
|
|
|
|
|
|
|
|
from ppcls.arch.backbone.base.theseus_layer import TheseusLayer
|
|
|
|
|
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
"MobileNetV1_x0_25", "MobileNetV1_x0_5", "MobileNetV1_x0_75", "MobileNetV1"
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class ConvBNLayer(TheseusLayer):
|
|
|
|
def __init__(self,
|
|
|
|
num_channels,
|
|
|
|
filter_size,
|
|
|
|
num_filters,
|
|
|
|
stride,
|
|
|
|
padding,
|
|
|
|
channels=None,
|
|
|
|
num_groups=1,
|
2021-05-27 14:00:39 +08:00
|
|
|
act='relu'):
|
2021-05-27 12:10:37 +08:00
|
|
|
super(ConvBNLayer, self).__init__()
|
|
|
|
|
|
|
|
self._conv = Conv2D(
|
|
|
|
in_channels=num_channels,
|
|
|
|
out_channels=num_filters,
|
|
|
|
kernel_size=filter_size,
|
|
|
|
stride=stride,
|
|
|
|
padding=padding,
|
|
|
|
groups=num_groups,
|
|
|
|
weight_attr=ParamAttr(
|
2021-05-27 14:00:39 +08:00
|
|
|
initializer=KaimingNormal()),
|
2021-05-27 12:10:37 +08:00
|
|
|
bias_attr=False)
|
|
|
|
|
|
|
|
self._batch_norm = BatchNorm(
|
|
|
|
num_filters,
|
2021-05-27 14:00:39 +08:00
|
|
|
act=act)
|
2021-05-27 12:10:37 +08:00
|
|
|
|
2021-05-27 13:50:12 +08:00
|
|
|
def forward(self, x):
|
|
|
|
x = self._conv(x)
|
|
|
|
x = self._batch_norm(x)
|
|
|
|
return x
|
2021-05-27 12:10:37 +08:00
|
|
|
|
|
|
|
|
|
|
|
class DepthwiseSeparable(TheseusLayer):
|
|
|
|
def __init__(self,
|
|
|
|
num_channels,
|
|
|
|
num_filters1,
|
|
|
|
num_filters2,
|
|
|
|
num_groups,
|
|
|
|
stride,
|
2021-05-27 14:00:39 +08:00
|
|
|
scale):
|
2021-05-27 12:10:37 +08:00
|
|
|
super(DepthwiseSeparable, self).__init__()
|
|
|
|
|
|
|
|
self._depthwise_conv = ConvBNLayer(
|
|
|
|
num_channels=num_channels,
|
|
|
|
num_filters=int(num_filters1 * scale),
|
|
|
|
filter_size=3,
|
|
|
|
stride=stride,
|
|
|
|
padding=1,
|
2021-05-27 14:00:39 +08:00
|
|
|
num_groups=int(num_groups * scale))
|
2021-05-27 12:10:37 +08:00
|
|
|
|
|
|
|
self._pointwise_conv = ConvBNLayer(
|
|
|
|
num_channels=int(num_filters1 * scale),
|
|
|
|
filter_size=1,
|
|
|
|
num_filters=int(num_filters2 * scale),
|
|
|
|
stride=1,
|
2021-05-27 14:00:39 +08:00
|
|
|
padding=0)
|
2021-05-27 12:10:37 +08:00
|
|
|
|
2021-05-27 13:50:12 +08:00
|
|
|
def forward(self, x):
|
|
|
|
x = self._depthwise_conv(x)
|
|
|
|
x = self._pointwise_conv(x)
|
|
|
|
return x
|
2021-05-27 12:10:37 +08:00
|
|
|
|
|
|
|
|
|
|
|
class MobileNet(TheseusLayer):
|
|
|
|
def __init__(self, scale=1.0, class_dim=1000):
|
|
|
|
super(MobileNet, self).__init__()
|
|
|
|
self.scale = scale
|
|
|
|
self.block_list = []
|
|
|
|
|
|
|
|
self.conv1 = ConvBNLayer(
|
|
|
|
num_channels=3,
|
|
|
|
filter_size=3,
|
|
|
|
channels=3,
|
|
|
|
num_filters=int(32 * scale),
|
|
|
|
stride=2,
|
2021-05-27 14:00:39 +08:00
|
|
|
padding=1)
|
2021-05-27 12:10:37 +08:00
|
|
|
|
|
|
|
conv2_1 = self.add_sublayer(
|
|
|
|
"conv2_1",
|
|
|
|
sublayer=DepthwiseSeparable(
|
|
|
|
num_channels=int(32 * scale),
|
|
|
|
num_filters1=32,
|
|
|
|
num_filters2=64,
|
|
|
|
num_groups=32,
|
|
|
|
stride=1,
|
2021-05-27 14:00:39 +08:00
|
|
|
scale=scale))
|
2021-05-27 12:10:37 +08:00
|
|
|
self.block_list.append(conv2_1)
|
|
|
|
|
|
|
|
conv2_2 = self.add_sublayer(
|
|
|
|
"conv2_2",
|
|
|
|
sublayer=DepthwiseSeparable(
|
|
|
|
num_channels=int(64 * scale),
|
|
|
|
num_filters1=64,
|
|
|
|
num_filters2=128,
|
|
|
|
num_groups=64,
|
|
|
|
stride=2,
|
2021-05-27 14:00:39 +08:00
|
|
|
scale=scale))
|
2021-05-27 12:10:37 +08:00
|
|
|
self.block_list.append(conv2_2)
|
|
|
|
|
|
|
|
conv3_1 = self.add_sublayer(
|
|
|
|
"conv3_1",
|
|
|
|
sublayer=DepthwiseSeparable(
|
|
|
|
num_channels=int(128 * scale),
|
|
|
|
num_filters1=128,
|
|
|
|
num_filters2=128,
|
|
|
|
num_groups=128,
|
|
|
|
stride=1,
|
2021-05-27 14:00:39 +08:00
|
|
|
scale=scale))
|
2021-05-27 12:10:37 +08:00
|
|
|
self.block_list.append(conv3_1)
|
|
|
|
|
|
|
|
conv3_2 = self.add_sublayer(
|
|
|
|
"conv3_2",
|
|
|
|
sublayer=DepthwiseSeparable(
|
|
|
|
num_channels=int(128 * scale),
|
|
|
|
num_filters1=128,
|
|
|
|
num_filters2=256,
|
|
|
|
num_groups=128,
|
|
|
|
stride=2,
|
2021-05-27 14:00:39 +08:00
|
|
|
scale=scale))
|
2021-05-27 12:10:37 +08:00
|
|
|
self.block_list.append(conv3_2)
|
|
|
|
|
|
|
|
conv4_1 = self.add_sublayer(
|
|
|
|
"conv4_1",
|
|
|
|
sublayer=DepthwiseSeparable(
|
|
|
|
num_channels=int(256 * scale),
|
|
|
|
num_filters1=256,
|
|
|
|
num_filters2=256,
|
|
|
|
num_groups=256,
|
|
|
|
stride=1,
|
2021-05-27 14:00:39 +08:00
|
|
|
scale=scale))
|
2021-05-27 12:10:37 +08:00
|
|
|
self.block_list.append(conv4_1)
|
|
|
|
|
|
|
|
conv4_2 = self.add_sublayer(
|
|
|
|
"conv4_2",
|
|
|
|
sublayer=DepthwiseSeparable(
|
|
|
|
num_channels=int(256 * scale),
|
|
|
|
num_filters1=256,
|
|
|
|
num_filters2=512,
|
|
|
|
num_groups=256,
|
|
|
|
stride=2,
|
2021-05-27 14:00:39 +08:00
|
|
|
scale=scale))
|
2021-05-27 12:10:37 +08:00
|
|
|
self.block_list.append(conv4_2)
|
|
|
|
|
|
|
|
for i in range(5):
|
|
|
|
conv5 = self.add_sublayer(
|
|
|
|
"conv5_" + str(i + 1),
|
|
|
|
sublayer=DepthwiseSeparable(
|
|
|
|
num_channels=int(512 * scale),
|
|
|
|
num_filters1=512,
|
|
|
|
num_filters2=512,
|
|
|
|
num_groups=512,
|
|
|
|
stride=1,
|
2021-05-27 14:00:39 +08:00
|
|
|
scale=scale))
|
2021-05-27 12:10:37 +08:00
|
|
|
self.block_list.append(conv5)
|
|
|
|
|
|
|
|
conv5_6 = self.add_sublayer(
|
|
|
|
"conv5_6",
|
|
|
|
sublayer=DepthwiseSeparable(
|
|
|
|
num_channels=int(512 * scale),
|
|
|
|
num_filters1=512,
|
|
|
|
num_filters2=1024,
|
|
|
|
num_groups=512,
|
|
|
|
stride=2,
|
2021-05-27 14:00:39 +08:00
|
|
|
scale=scale))
|
2021-05-27 12:10:37 +08:00
|
|
|
self.block_list.append(conv5_6)
|
|
|
|
|
|
|
|
conv6 = self.add_sublayer(
|
|
|
|
"conv6",
|
|
|
|
sublayer=DepthwiseSeparable(
|
|
|
|
num_channels=int(1024 * scale),
|
|
|
|
num_filters1=1024,
|
|
|
|
num_filters2=1024,
|
|
|
|
num_groups=1024,
|
|
|
|
stride=1,
|
2021-05-27 14:00:39 +08:00
|
|
|
scale=scale))
|
2021-05-27 12:10:37 +08:00
|
|
|
self.block_list.append(conv6)
|
|
|
|
|
|
|
|
self.pool2d_avg = AdaptiveAvgPool2D(1)
|
|
|
|
|
|
|
|
self.out = Linear(
|
|
|
|
int(1024 * scale),
|
|
|
|
class_dim,
|
2021-05-27 14:00:39 +08:00
|
|
|
weight_attr=ParamAttr(initializer=KaimingNormal()))
|
2021-05-27 12:10:37 +08:00
|
|
|
|
2021-05-27 13:50:12 +08:00
|
|
|
def forward(self, x):
|
|
|
|
x = self.conv1(x)
|
2021-05-27 12:10:37 +08:00
|
|
|
for block in self.block_list:
|
2021-05-27 13:50:12 +08:00
|
|
|
x = block(x)
|
|
|
|
x = self.pool2d_avg(x)
|
|
|
|
x = paddle.flatten(x, start_axis=1, stop_axis=-1)
|
|
|
|
x = self.out(x)
|
|
|
|
return x
|
2021-05-27 12:10:37 +08:00
|
|
|
|
|
|
|
|
|
|
|
def MobileNetV1_x0_25(**args):
|
|
|
|
model = MobileNet(scale=0.25, **args)
|
|
|
|
return model
|
|
|
|
|
|
|
|
|
|
|
|
def MobileNetV1_x0_5(**args):
|
|
|
|
model = MobileNet(scale=0.5, **args)
|
|
|
|
return model
|
|
|
|
|
|
|
|
|
|
|
|
def MobileNetV1_x0_75(**args):
|
|
|
|
model = MobileNet(scale=0.75, **args)
|
|
|
|
return model
|
|
|
|
|
|
|
|
|
|
|
|
def MobileNetV1(**args):
|
|
|
|
model = MobileNet(scale=1.0, **args)
|
|
|
|
return model
|