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
|
2021-05-28 14:51:07 +08:00
|
|
|
from paddle.nn import Conv2D, BatchNorm, Linear, ReLU, Flatten
|
2021-05-28 14:04:03 +08:00
|
|
|
from paddle.nn import AdaptiveAvgPool2D
|
2021-05-27 12:10:37 +08:00
|
|
|
from paddle.nn.initializer import KaimingNormal
|
|
|
|
|
|
|
|
from ppcls.arch.backbone.base.theseus_layer import TheseusLayer
|
2021-05-28 13:28:11 +08:00
|
|
|
from ppcls.utils.save_load import load_dygraph_pretrain_from, load_dygraph_pretrain_from_url
|
2021-05-27 12:10:37 +08:00
|
|
|
|
2021-05-28 13:28:11 +08:00
|
|
|
|
|
|
|
MODEL_URLS = {
|
|
|
|
"MobileNetV1_x0_25": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/MobileNetV1_x0_25_pretrained.pdparams",
|
2021-05-28 14:27:37 +08:00
|
|
|
"MobileNetV1_x0_5": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/MobileNetV1_x0_5_pretrained.pdparams",
|
2021-05-28 13:28:11 +08:00
|
|
|
"MobileNetV1_x0_75": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/MobileNetV1_x0_75_pretrained.pdparams",
|
|
|
|
"MobileNetV1": "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/MobileNetV1_pretrained.pdparams",
|
|
|
|
}
|
|
|
|
|
|
|
|
__all__ = MODEL_URLS.keys()
|
|
|
|
|
2021-05-27 16:22:52 +08:00
|
|
|
|
2021-05-27 12:10:37 +08:00
|
|
|
class ConvBNLayer(TheseusLayer):
|
|
|
|
def __init__(self,
|
|
|
|
num_channels,
|
|
|
|
filter_size,
|
|
|
|
num_filters,
|
|
|
|
stride,
|
|
|
|
padding,
|
2021-05-27 16:22:52 +08:00
|
|
|
num_groups=1):
|
2021-05-27 12:10:37 +08:00
|
|
|
super(ConvBNLayer, self).__init__()
|
|
|
|
|
2021-05-28 14:04:03 +08:00
|
|
|
self.conv = Conv2D(
|
2021-05-27 12:10:37 +08:00
|
|
|
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)
|
|
|
|
|
2021-05-28 14:04:03 +08:00
|
|
|
self.bn = BatchNorm(
|
2021-05-27 16:22:52 +08:00
|
|
|
num_filters)
|
|
|
|
|
2021-05-28 14:04:03 +08:00
|
|
|
self.relu = ReLU()
|
2021-05-27 12:10:37 +08:00
|
|
|
|
2021-05-27 13:50:12 +08:00
|
|
|
def forward(self, x):
|
2021-05-28 14:04:03 +08:00
|
|
|
x = self.conv(x)
|
|
|
|
x = self.bn(x)
|
|
|
|
x = self.relu(x)
|
2021-05-27 13:50:12 +08:00
|
|
|
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__()
|
|
|
|
|
2021-05-28 14:04:03 +08:00
|
|
|
self.depthwise_conv = ConvBNLayer(
|
2021-05-27 12:10:37 +08:00
|
|
|
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
|
|
|
|
2021-05-28 14:04:03 +08:00
|
|
|
self.pointwise_conv = ConvBNLayer(
|
2021-05-27 12:10:37 +08:00
|
|
|
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):
|
2021-05-28 14:04:03 +08:00
|
|
|
x = self.depthwise_conv(x)
|
|
|
|
x = self.pointwise_conv(x)
|
2021-05-27 13:50:12 +08:00
|
|
|
return x
|
2021-05-27 12:10:37 +08:00
|
|
|
|
|
|
|
|
|
|
|
class MobileNet(TheseusLayer):
|
2021-05-28 13:28:11 +08:00
|
|
|
def __init__(self, scale=1.0, class_num=1000, pretrained=False):
|
2021-05-27 12:10:37 +08:00
|
|
|
super(MobileNet, self).__init__()
|
|
|
|
self.scale = scale
|
2021-05-28 13:28:11 +08:00
|
|
|
self.pretrained = pretrained
|
2021-05-27 12:10:37 +08:00
|
|
|
|
2021-05-28 13:28:11 +08:00
|
|
|
self.conv = ConvBNLayer(
|
2021-05-27 12:10:37 +08:00
|
|
|
num_channels=3,
|
|
|
|
filter_size=3,
|
|
|
|
num_filters=int(32 * scale),
|
|
|
|
stride=2,
|
2021-05-27 14:00:39 +08:00
|
|
|
padding=1)
|
2021-05-27 16:22:52 +08:00
|
|
|
|
2021-05-28 14:21:01 +08:00
|
|
|
#num_channels, num_filters1, num_filters2, num_groups, stride
|
2021-05-28 14:50:21 +08:00
|
|
|
self.cfg = [[int(32 * scale), 32, 64, 32, 1],
|
|
|
|
[int(64 * scale), 64, 128, 64, 2],
|
|
|
|
[int(128 * scale), 128, 128, 128, 1],
|
|
|
|
[int(128 * scale), 128, 256, 128, 2],
|
|
|
|
[int(256 * scale), 256, 256, 256, 1],
|
|
|
|
[int(256 * scale), 256, 512, 256, 2],
|
|
|
|
[int(512 * scale), 512, 512, 512, 1],
|
|
|
|
[int(512 * scale), 512, 512, 512, 1],
|
|
|
|
[int(512 * scale), 512, 512, 512, 1],
|
|
|
|
[int(512 * scale), 512, 512, 512, 1],
|
|
|
|
[int(512 * scale), 512, 512, 512, 1],
|
|
|
|
[int(512 * scale), 512, 1024, 512, 2],
|
|
|
|
[int(1024 * scale), 1024, 1024, 1024, 1]]
|
2021-05-27 16:22:52 +08:00
|
|
|
|
|
|
|
self.blocks = nn.Sequential(*[
|
|
|
|
DepthwiseSeparable(
|
|
|
|
num_channels=params[0],
|
|
|
|
num_filters1=params[1],
|
|
|
|
num_filters2=params[2],
|
|
|
|
num_groups=params[3],
|
|
|
|
stride=params[4],
|
|
|
|
scale=scale) for params in self.cfg])
|
2021-05-27 12:10:37 +08:00
|
|
|
|
2021-05-28 13:28:11 +08:00
|
|
|
self.avg_pool = AdaptiveAvgPool2D(1)
|
2021-05-28 14:04:03 +08:00
|
|
|
self.flatten = Flatten(start_axis=1, stop_axis=-1)
|
2021-05-27 12:10:37 +08:00
|
|
|
|
2021-05-28 13:33:24 +08:00
|
|
|
self.fc = Linear(
|
2021-05-27 12:10:37 +08:00
|
|
|
int(1024 * scale),
|
2021-05-28 10:36:48 +08:00
|
|
|
class_num,
|
2021-05-27 14:00:39 +08:00
|
|
|
weight_attr=ParamAttr(initializer=KaimingNormal()))
|
2021-05-27 19:13:03 +08:00
|
|
|
|
2021-05-27 13:50:12 +08:00
|
|
|
def forward(self, x):
|
2021-05-28 13:28:11 +08:00
|
|
|
x = self.conv(x)
|
2021-05-27 16:22:52 +08:00
|
|
|
x = self.blocks(x)
|
2021-05-28 13:28:11 +08:00
|
|
|
x = self.avg_pool(x)
|
2021-05-27 19:13:03 +08:00
|
|
|
x = self.flatten(x)
|
2021-05-28 13:33:24 +08:00
|
|
|
x = self.fc(x)
|
2021-05-27 13:50:12 +08:00
|
|
|
return x
|
2021-05-27 12:10:37 +08:00
|
|
|
|
|
|
|
|
|
|
|
def MobileNetV1_x0_25(**args):
|
2021-05-28 10:36:48 +08:00
|
|
|
"""
|
|
|
|
MobileNetV1_x0_25
|
|
|
|
Args:
|
|
|
|
pretrained: bool=False. If `True` load pretrained parameters, `False` otherwise.
|
|
|
|
kwargs:
|
|
|
|
class_num: int=1000. Output dim of last fc layer.
|
|
|
|
Returns:
|
|
|
|
model: nn.Layer. Specific `MobileNetV1_x0_25` model depends on args.
|
|
|
|
"""
|
2021-05-27 12:10:37 +08:00
|
|
|
model = MobileNet(scale=0.25, **args)
|
2021-05-28 13:28:11 +08:00
|
|
|
if isinstance(model.pretrained, bool):
|
|
|
|
if model.pretrained is True:
|
|
|
|
load_dygraph_pretrain_from_url(model, MODEL_URLS["MobileNetV1_x0_25"])
|
|
|
|
elif isinstance(model.pretrained, str):
|
|
|
|
load_dygraph_pretrain(model, model.pretrained)
|
|
|
|
else:
|
|
|
|
raise RuntimeError(
|
|
|
|
"pretrained type is not available. Please use `string` or `boolean` type")
|
2021-05-27 12:10:37 +08:00
|
|
|
return model
|
|
|
|
|
|
|
|
|
|
|
|
def MobileNetV1_x0_5(**args):
|
2021-05-28 10:36:48 +08:00
|
|
|
"""
|
|
|
|
MobileNetV1_x0_5
|
|
|
|
Args:
|
|
|
|
pretrained: bool=False. If `True` load pretrained parameters, `False` otherwise.
|
|
|
|
kwargs:
|
|
|
|
class_num: int=1000. Output dim of last fc layer.
|
|
|
|
Returns:
|
|
|
|
model: nn.Layer. Specific `MobileNetV1_x0_5` model depends on args.
|
|
|
|
"""
|
2021-05-27 12:10:37 +08:00
|
|
|
model = MobileNet(scale=0.5, **args)
|
2021-05-28 13:28:11 +08:00
|
|
|
if isinstance(model.pretrained, bool):
|
|
|
|
if model.pretrained is True:
|
|
|
|
load_dygraph_pretrain_from_url(model, MODEL_URLS["MobileNetV1_x0_5"])
|
|
|
|
elif isinstance(model.pretrained, str):
|
|
|
|
load_dygraph_pretrain(model, model.pretrained)
|
|
|
|
else:
|
|
|
|
raise RuntimeError(
|
|
|
|
"pretrained type is not available. Please use `string` or `boolean` type")
|
2021-05-27 12:10:37 +08:00
|
|
|
return model
|
|
|
|
|
|
|
|
|
|
|
|
def MobileNetV1_x0_75(**args):
|
2021-05-28 10:36:48 +08:00
|
|
|
"""
|
|
|
|
MobileNetV1_x0_75
|
|
|
|
Args:
|
|
|
|
pretrained: bool=False. If `True` load pretrained parameters, `False` otherwise.
|
|
|
|
kwargs:
|
|
|
|
class_num: int=1000. Output dim of last fc layer.
|
|
|
|
Returns:
|
|
|
|
model: nn.Layer. Specific `MobileNetV1_x0_75` model depends on args.
|
|
|
|
"""
|
2021-05-27 12:10:37 +08:00
|
|
|
model = MobileNet(scale=0.75, **args)
|
2021-05-28 13:28:11 +08:00
|
|
|
if isinstance(model.pretrained, bool):
|
|
|
|
if model.pretrained is True:
|
|
|
|
load_dygraph_pretrain_from_url(model, MODEL_URLS["MobileNetV1_x0_75"])
|
|
|
|
elif isinstance(model.pretrained, str):
|
|
|
|
load_dygraph_pretrain(model, model.pretrained)
|
|
|
|
else:
|
|
|
|
raise RuntimeError(
|
|
|
|
"pretrained type is not available. Please use `string` or `boolean` type")
|
2021-05-27 12:10:37 +08:00
|
|
|
return model
|
|
|
|
|
|
|
|
|
|
|
|
def MobileNetV1(**args):
|
2021-05-28 10:36:48 +08:00
|
|
|
"""
|
|
|
|
MobileNetV1
|
|
|
|
Args:
|
|
|
|
pretrained: bool=False. If `True` load pretrained parameters, `False` otherwise.
|
|
|
|
kwargs:
|
|
|
|
class_num: int=1000. Output dim of last fc layer.
|
|
|
|
Returns:
|
|
|
|
model: nn.Layer. Specific `MobileNetV1` model depends on args.
|
|
|
|
"""
|
2021-05-27 12:10:37 +08:00
|
|
|
model = MobileNet(scale=1.0, **args)
|
2021-05-28 13:28:11 +08:00
|
|
|
if isinstance(model.pretrained, bool):
|
|
|
|
if model.pretrained is True:
|
|
|
|
load_dygraph_pretrain_from_url(model, MODEL_URLS["MobileNetV1"])
|
|
|
|
elif isinstance(model.pretrained, str):
|
|
|
|
load_dygraph_pretrain(model, model.pretrained)
|
|
|
|
else:
|
|
|
|
raise RuntimeError(
|
|
|
|
"pretrained type is not available. Please use `string` or `boolean` type")
|
2021-05-27 12:10:37 +08:00
|
|
|
return model
|
2021-05-27 16:22:52 +08:00
|
|
|
|
|
|
|
|