mirror of
https://github.com/PaddlePaddle/PaddleClas.git
synced 2025-06-03 21:55:06 +08:00
formatting
This commit is contained in:
parent
e0859f7498
commit
2ec1d73ece
@ -1,16 +1,16 @@
|
|||||||
#copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
|
# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
|
||||||
#
|
#
|
||||||
#Licensed under the Apache License, Version 2.0 (the "License");
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
#you may not use this file except in compliance with the License.
|
# you may not use this file except in compliance with the License.
|
||||||
#You may obtain a copy of the License at
|
# You may obtain a copy of the License at
|
||||||
#
|
#
|
||||||
# http://www.apache.org/licenses/LICENSE-2.0
|
# http://www.apache.org/licenses/LICENSE-2.0
|
||||||
#
|
#
|
||||||
#Unless required by applicable law or agreed to in writing, software
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
#distributed under the License is distributed on an "AS IS" BASIS,
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
#See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
#limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
import math
|
import math
|
||||||
import paddle
|
import paddle
|
||||||
@ -23,16 +23,14 @@ from paddle.nn.initializer import Uniform
|
|||||||
|
|
||||||
|
|
||||||
class ConvBNLayer(nn.Layer):
|
class ConvBNLayer(nn.Layer):
|
||||||
def __init__(
|
def __init__(self,
|
||||||
self,
|
|
||||||
in_channels,
|
in_channels,
|
||||||
out_channels,
|
out_channels,
|
||||||
kernel_size,
|
kernel_size,
|
||||||
stride=1,
|
stride=1,
|
||||||
groups=1,
|
groups=1,
|
||||||
act="relu",
|
act="relu",
|
||||||
name=None
|
name=None):
|
||||||
):
|
|
||||||
super(ConvBNLayer, self).__init__()
|
super(ConvBNLayer, self).__init__()
|
||||||
self._conv = Conv2d(
|
self._conv = Conv2d(
|
||||||
in_channels=in_channels,
|
in_channels=in_channels,
|
||||||
@ -41,19 +39,24 @@ class ConvBNLayer(nn.Layer):
|
|||||||
stride=stride,
|
stride=stride,
|
||||||
padding=(kernel_size - 1) // 2,
|
padding=(kernel_size - 1) // 2,
|
||||||
groups=groups,
|
groups=groups,
|
||||||
weight_attr=ParamAttr(initializer=nn.initializer.MSRA(), name=name + "_weights"),
|
weight_attr=ParamAttr(
|
||||||
bias_attr=False
|
initializer=nn.initializer.MSRA(), name=name + "_weights"),
|
||||||
)
|
bias_attr=False)
|
||||||
bn_name = name + "_bn"
|
bn_name = name + "_bn"
|
||||||
|
|
||||||
# In the old version, moving_variance_name was name + "_variance"
|
# In the old version, moving_variance_name was name + "_variance"
|
||||||
self._batch_norm = BatchNorm(
|
self._batch_norm = BatchNorm(
|
||||||
num_channels=out_channels,
|
num_channels=out_channels,
|
||||||
act=act,
|
act=act,
|
||||||
param_attr=ParamAttr(name=bn_name + "_scale", regularizer=L2DecayRegularizer(regularization_coeff=0.0)),
|
param_attr=ParamAttr(
|
||||||
bias_attr=ParamAttr(name=bn_name + "_offset", regularizer=L2DecayRegularizer(regularization_coeff=0.0)),
|
name=bn_name + "_scale",
|
||||||
|
regularizer=L2DecayRegularizer(regularization_coeff=0.0)),
|
||||||
|
bias_attr=ParamAttr(
|
||||||
|
name=bn_name + "_offset",
|
||||||
|
regularizer=L2DecayRegularizer(regularization_coeff=0.0)),
|
||||||
moving_mean_name=bn_name + "_mean",
|
moving_mean_name=bn_name + "_mean",
|
||||||
moving_variance_name=name + "_variance" # wrong due to an old typo, will be fixed later.
|
moving_variance_name=name +
|
||||||
|
"_variance" # wrong due to an old typo, will be fixed later.
|
||||||
)
|
)
|
||||||
|
|
||||||
def forward(self, inputs):
|
def forward(self, inputs):
|
||||||
@ -63,12 +66,7 @@ class ConvBNLayer(nn.Layer):
|
|||||||
|
|
||||||
|
|
||||||
class SEBlock(nn.Layer):
|
class SEBlock(nn.Layer):
|
||||||
def __init__(
|
def __init__(self, num_channels, reduction_ratio=4, name=None):
|
||||||
self,
|
|
||||||
num_channels,
|
|
||||||
reduction_ratio=4,
|
|
||||||
name=None
|
|
||||||
):
|
|
||||||
super(SEBlock, self).__init__()
|
super(SEBlock, self).__init__()
|
||||||
self.pool2d_gap = AdaptiveAvgPool2d(1)
|
self.pool2d_gap = AdaptiveAvgPool2d(1)
|
||||||
self._num_channels = num_channels
|
self._num_channels = num_channels
|
||||||
@ -77,16 +75,16 @@ class SEBlock(nn.Layer):
|
|||||||
self.squeeze = Linear(
|
self.squeeze = Linear(
|
||||||
num_channels,
|
num_channels,
|
||||||
med_ch,
|
med_ch,
|
||||||
weight_attr=ParamAttr(initializer=Uniform(-stdv, stdv), name=name + "_1_weights"),
|
weight_attr=ParamAttr(
|
||||||
bias_attr=ParamAttr(name=name + "_1_offset")
|
initializer=Uniform(-stdv, stdv), name=name + "_1_weights"),
|
||||||
)
|
bias_attr=ParamAttr(name=name + "_1_offset"))
|
||||||
stdv = 1.0 / math.sqrt(med_ch * 1.0)
|
stdv = 1.0 / math.sqrt(med_ch * 1.0)
|
||||||
self.excitation = Linear(
|
self.excitation = Linear(
|
||||||
med_ch,
|
med_ch,
|
||||||
num_channels,
|
num_channels,
|
||||||
weight_attr=ParamAttr(initializer=Uniform(-stdv, stdv), name=name+"_2_weights"),
|
weight_attr=ParamAttr(
|
||||||
bias_attr=ParamAttr(name=name+"_2_offset")
|
initializer=Uniform(-stdv, stdv), name=name + "_2_weights"),
|
||||||
)
|
bias_attr=ParamAttr(name=name + "_2_offset"))
|
||||||
|
|
||||||
def forward(self, inputs):
|
def forward(self, inputs):
|
||||||
pool = self.pool2d_gap(inputs)
|
pool = self.pool2d_gap(inputs)
|
||||||
@ -95,14 +93,14 @@ class SEBlock(nn.Layer):
|
|||||||
squeeze = F.relu(squeeze)
|
squeeze = F.relu(squeeze)
|
||||||
excitation = self.excitation(squeeze)
|
excitation = self.excitation(squeeze)
|
||||||
excitation = paddle.fluid.layers.clip(x=excitation, min=0, max=1)
|
excitation = paddle.fluid.layers.clip(x=excitation, min=0, max=1)
|
||||||
excitation = paddle.reshape(excitation, shape=[-1, self._num_channels, 1, 1])
|
excitation = paddle.reshape(
|
||||||
|
excitation, shape=[-1, self._num_channels, 1, 1])
|
||||||
out = inputs * excitation
|
out = inputs * excitation
|
||||||
return out
|
return out
|
||||||
|
|
||||||
|
|
||||||
class GhostModule(nn.Layer):
|
class GhostModule(nn.Layer):
|
||||||
def __init__(
|
def __init__(self,
|
||||||
self,
|
|
||||||
in_channels,
|
in_channels,
|
||||||
output_channels,
|
output_channels,
|
||||||
kernel_size=1,
|
kernel_size=1,
|
||||||
@ -110,8 +108,7 @@ class GhostModule(nn.Layer):
|
|||||||
dw_size=3,
|
dw_size=3,
|
||||||
stride=1,
|
stride=1,
|
||||||
relu=True,
|
relu=True,
|
||||||
name=None
|
name=None):
|
||||||
):
|
|
||||||
super(GhostModule, self).__init__()
|
super(GhostModule, self).__init__()
|
||||||
init_channels = int(math.ceil(output_channels / ratio))
|
init_channels = int(math.ceil(output_channels / ratio))
|
||||||
new_channels = int(init_channels * (ratio - 1))
|
new_channels = int(init_channels * (ratio - 1))
|
||||||
@ -122,8 +119,7 @@ class GhostModule(nn.Layer):
|
|||||||
stride=stride,
|
stride=stride,
|
||||||
groups=1,
|
groups=1,
|
||||||
act="relu" if relu else None,
|
act="relu" if relu else None,
|
||||||
name=name + "_primary_conv"
|
name=name + "_primary_conv")
|
||||||
)
|
|
||||||
self.cheap_operation = ConvBNLayer(
|
self.cheap_operation = ConvBNLayer(
|
||||||
in_channels=init_channels,
|
in_channels=init_channels,
|
||||||
out_channels=new_channels,
|
out_channels=new_channels,
|
||||||
@ -131,8 +127,7 @@ class GhostModule(nn.Layer):
|
|||||||
stride=1,
|
stride=1,
|
||||||
groups=init_channels,
|
groups=init_channels,
|
||||||
act="relu" if relu else None,
|
act="relu" if relu else None,
|
||||||
name=name + "_cheap_operation"
|
name=name + "_cheap_operation")
|
||||||
)
|
|
||||||
|
|
||||||
def forward(self, inputs):
|
def forward(self, inputs):
|
||||||
x = self.primary_conv(inputs)
|
x = self.primary_conv(inputs)
|
||||||
@ -142,16 +137,14 @@ class GhostModule(nn.Layer):
|
|||||||
|
|
||||||
|
|
||||||
class GhostBottleneck(nn.Layer):
|
class GhostBottleneck(nn.Layer):
|
||||||
def __init__(
|
def __init__(self,
|
||||||
self,
|
|
||||||
in_channels,
|
in_channels,
|
||||||
hidden_dim,
|
hidden_dim,
|
||||||
output_channels,
|
output_channels,
|
||||||
kernel_size,
|
kernel_size,
|
||||||
stride,
|
stride,
|
||||||
use_se,
|
use_se,
|
||||||
name=None
|
name=None):
|
||||||
):
|
|
||||||
super(GhostBottleneck, self).__init__()
|
super(GhostBottleneck, self).__init__()
|
||||||
self._stride = stride
|
self._stride = stride
|
||||||
self._use_se = use_se
|
self._use_se = use_se
|
||||||
@ -163,8 +156,7 @@ class GhostBottleneck(nn.Layer):
|
|||||||
kernel_size=1,
|
kernel_size=1,
|
||||||
stride=1,
|
stride=1,
|
||||||
relu=True,
|
relu=True,
|
||||||
name=name+"_ghost_module_1"
|
name=name + "_ghost_module_1")
|
||||||
)
|
|
||||||
if stride == 2:
|
if stride == 2:
|
||||||
self.depthwise_conv = ConvBNLayer(
|
self.depthwise_conv = ConvBNLayer(
|
||||||
in_channels=hidden_dim,
|
in_channels=hidden_dim,
|
||||||
@ -173,20 +165,17 @@ class GhostBottleneck(nn.Layer):
|
|||||||
stride=stride,
|
stride=stride,
|
||||||
groups=hidden_dim,
|
groups=hidden_dim,
|
||||||
act=None,
|
act=None,
|
||||||
name=name+"_depthwise_depthwise" # looks strange due to an old typo, will be fixed later.
|
name=name +
|
||||||
|
"_depthwise_depthwise" # looks strange due to an old typo, will be fixed later.
|
||||||
)
|
)
|
||||||
if use_se:
|
if use_se:
|
||||||
self.se_block = SEBlock(
|
self.se_block = SEBlock(num_channels=hidden_dim, name=name + "_se")
|
||||||
num_channels=hidden_dim,
|
|
||||||
name=name + "_se"
|
|
||||||
)
|
|
||||||
self.ghost_module_2 = GhostModule(
|
self.ghost_module_2 = GhostModule(
|
||||||
in_channels=hidden_dim,
|
in_channels=hidden_dim,
|
||||||
output_channels=output_channels,
|
output_channels=output_channels,
|
||||||
kernel_size=1,
|
kernel_size=1,
|
||||||
relu=False,
|
relu=False,
|
||||||
name=name + "_ghost_module_2"
|
name=name + "_ghost_module_2")
|
||||||
)
|
|
||||||
if stride != 1 or in_channels != output_channels:
|
if stride != 1 or in_channels != output_channels:
|
||||||
self.shortcut_depthwise = ConvBNLayer(
|
self.shortcut_depthwise = ConvBNLayer(
|
||||||
in_channels=in_channels,
|
in_channels=in_channels,
|
||||||
@ -195,7 +184,8 @@ class GhostBottleneck(nn.Layer):
|
|||||||
stride=stride,
|
stride=stride,
|
||||||
groups=in_channels,
|
groups=in_channels,
|
||||||
act=None,
|
act=None,
|
||||||
name=name + "_shortcut_depthwise_depthwise" # looks strange due to an old typo, will be fixed later.
|
name=name +
|
||||||
|
"_shortcut_depthwise_depthwise" # looks strange due to an old typo, will be fixed later.
|
||||||
)
|
)
|
||||||
self.shortcut_conv = ConvBNLayer(
|
self.shortcut_conv = ConvBNLayer(
|
||||||
in_channels=in_channels,
|
in_channels=in_channels,
|
||||||
@ -204,8 +194,7 @@ class GhostBottleneck(nn.Layer):
|
|||||||
stride=1,
|
stride=1,
|
||||||
groups=1,
|
groups=1,
|
||||||
act=None,
|
act=None,
|
||||||
name=name + "_shortcut_conv"
|
name=name + "_shortcut_conv")
|
||||||
)
|
|
||||||
|
|
||||||
def forward(self, inputs):
|
def forward(self, inputs):
|
||||||
x = self.ghost_module_1(inputs)
|
x = self.ghost_module_1(inputs)
|
||||||
@ -253,8 +242,7 @@ class GhostNet(nn.Layer):
|
|||||||
stride=2,
|
stride=2,
|
||||||
groups=1,
|
groups=1,
|
||||||
act="relu",
|
act="relu",
|
||||||
name="conv1"
|
name="conv1")
|
||||||
)
|
|
||||||
# build inverted residual blocks
|
# build inverted residual blocks
|
||||||
idx = 0
|
idx = 0
|
||||||
self.ghost_bottleneck_list = []
|
self.ghost_bottleneck_list = []
|
||||||
@ -271,9 +259,7 @@ class GhostNet(nn.Layer):
|
|||||||
kernel_size=k,
|
kernel_size=k,
|
||||||
stride=s,
|
stride=s,
|
||||||
use_se=use_se,
|
use_se=use_se,
|
||||||
name="_ghostbottleneck_" + str(idx)
|
name="_ghostbottleneck_" + str(idx)))
|
||||||
)
|
|
||||||
)
|
|
||||||
self.ghost_bottleneck_list.append(ghost_bottleneck)
|
self.ghost_bottleneck_list.append(ghost_bottleneck)
|
||||||
idx += 1
|
idx += 1
|
||||||
# build last several layers
|
# build last several layers
|
||||||
@ -286,8 +272,7 @@ class GhostNet(nn.Layer):
|
|||||||
stride=1,
|
stride=1,
|
||||||
groups=1,
|
groups=1,
|
||||||
act="relu",
|
act="relu",
|
||||||
name="conv_last"
|
name="conv_last")
|
||||||
)
|
|
||||||
self.pool2d_gap = AdaptiveAvgPool2d(1)
|
self.pool2d_gap = AdaptiveAvgPool2d(1)
|
||||||
in_channels = output_channels
|
in_channels = output_channels
|
||||||
self._fc0_output_channels = 1280
|
self._fc0_output_channels = 1280
|
||||||
@ -297,16 +282,15 @@ class GhostNet(nn.Layer):
|
|||||||
kernel_size=1,
|
kernel_size=1,
|
||||||
stride=1,
|
stride=1,
|
||||||
act="relu",
|
act="relu",
|
||||||
name="fc_0"
|
name="fc_0")
|
||||||
)
|
|
||||||
self.dropout = nn.Dropout(p=0.2)
|
self.dropout = nn.Dropout(p=0.2)
|
||||||
stdv = 1.0 / math.sqrt(self._fc0_output_channels * 1.0)
|
stdv = 1.0 / math.sqrt(self._fc0_output_channels * 1.0)
|
||||||
self.fc_1 = Linear(
|
self.fc_1 = Linear(
|
||||||
self._fc0_output_channels,
|
self._fc0_output_channels,
|
||||||
class_dim,
|
class_dim,
|
||||||
weight_attr=ParamAttr(name="fc_1_weights", initializer=Uniform(-stdv, stdv)),
|
weight_attr=ParamAttr(
|
||||||
bias_attr=ParamAttr(name="fc_1_offset")
|
name="fc_1_weights", initializer=Uniform(-stdv, stdv)),
|
||||||
)
|
bias_attr=ParamAttr(name="fc_1_offset"))
|
||||||
|
|
||||||
def forward(self, inputs):
|
def forward(self, inputs):
|
||||||
x = self.conv1(inputs)
|
x = self.conv1(inputs)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user