提交 07bb46ef 编写于 作者: W weishengyu

modify format

上级 44889ba4
...@@ -25,21 +25,21 @@ from paddle.nn.initializer import Uniform ...@@ -25,21 +25,21 @@ from paddle.nn.initializer import Uniform
class ConvBNLayer(nn.Layer): class ConvBNLayer(nn.Layer):
def __init__( def __init__(
self, self,
num_channels, in_channels,
num_filters, out_channels,
filter_size, kernel_size,
stride=1, stride=1,
groups=1, groups=1,
act=None, act="relu",
name=None name=None
): ):
super(ConvBNLayer, self).__init__() super(ConvBNLayer, self).__init__()
self._conv = Conv2d( self._conv = Conv2d(
in_channels=num_channels, in_channels=in_channels,
out_channels=num_filters, out_channels=out_channels,
kernel_size=filter_size, kernel_size=kernel_size,
stride=stride, stride=stride,
padding=(filter_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(initializer=nn.initializer.MSRA(), name=name + "_weights"),
bias_attr=False bias_attr=False
...@@ -48,16 +48,10 @@ class ConvBNLayer(nn.Layer): ...@@ -48,16 +48,10 @@ class ConvBNLayer(nn.Layer):
# 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_filters, num_channels=out_channels,
act=act, act=act,
param_attr=ParamAttr( param_attr=ParamAttr(name=bn_name + "_scale", regularizer=L2DecayRegularizer(regularization_coeff=0.0)),
name=bn_name + "_scale", bias_attr=ParamAttr(name=bn_name + "_offset", regularizer=L2DecayRegularizer(regularization_coeff=0.0)),
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=bn_name + "_variance" moving_variance_name=bn_name + "_variance"
) )
...@@ -83,20 +77,14 @@ class SEBlock(nn.Layer): ...@@ -83,20 +77,14 @@ class SEBlock(nn.Layer):
self.squeeze = Linear( self.squeeze = Linear(
num_channels, num_channels,
med_ch, med_ch,
weight_attr=ParamAttr( weight_attr=ParamAttr(initializer=Uniform(-stdv, stdv), name=name + "_1_weights"),
initializer=Uniform(-stdv, stdv),
name=name + "_1_weights"
),
bias_attr=ParamAttr(name=name + "_1_offset") 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( weight_attr=ParamAttr(initializer=Uniform(-stdv, stdv), name=name+"_2_weights"),
initializer=Uniform(-stdv, stdv),
name=name+"_2_weights"
),
bias_attr=ParamAttr(name=name+"_2_offset") bias_attr=ParamAttr(name=name+"_2_offset")
) )
...@@ -107,10 +95,7 @@ class SEBlock(nn.Layer): ...@@ -107,10 +95,7 @@ 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 = paddle.reshape(excitation, shape=[-1, self._num_channels, 1, 1])
excitation,
shape=[-1, self._num_channels, 1, 1]
)
out = inputs * excitation out = inputs * excitation
return out return out
...@@ -118,7 +103,7 @@ class SEBlock(nn.Layer): ...@@ -118,7 +103,7 @@ class SEBlock(nn.Layer):
class GhostModule(nn.Layer): class GhostModule(nn.Layer):
def __init__( def __init__(
self, self,
num_channels, in_channels,
output_channels, output_channels,
kernel_size=1, kernel_size=1,
ratio=2, ratio=2,
...@@ -131,18 +116,18 @@ class GhostModule(nn.Layer): ...@@ -131,18 +116,18 @@ class GhostModule(nn.Layer):
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))
self.primary_conv = ConvBNLayer( self.primary_conv = ConvBNLayer(
num_channels=num_channels, in_channels=in_channels,
num_filters=init_channels, out_channels=init_channels,
filter_size=kernel_size, kernel_size=kernel_size,
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(
num_channels=init_channels, in_channels=init_channels,
num_filters=new_channels, out_channels=new_channels,
filter_size=dw_size, kernel_size=dw_size,
stride=1, stride=1,
groups=init_channels, groups=init_channels,
act="relu" if relu else None, act="relu" if relu else None,
...@@ -159,7 +144,7 @@ class GhostModule(nn.Layer): ...@@ -159,7 +144,7 @@ class GhostModule(nn.Layer):
class GhostBottleneck(nn.Layer): class GhostBottleneck(nn.Layer):
def __init__( def __init__(
self, self,
num_channels, in_channels,
hidden_dim, hidden_dim,
output_channels, output_channels,
kernel_size, kernel_size,
...@@ -170,10 +155,10 @@ class GhostBottleneck(nn.Layer): ...@@ -170,10 +155,10 @@ class GhostBottleneck(nn.Layer):
super(GhostBottleneck, self).__init__() super(GhostBottleneck, self).__init__()
self._stride = stride self._stride = stride
self._use_se = use_se self._use_se = use_se
self._num_channels = num_channels self._num_channels = in_channels
self._output_channels = output_channels self._output_channels = output_channels
self.ghost_module_1 = GhostModule( self.ghost_module_1 = GhostModule(
num_channels=num_channels, in_channels=in_channels,
output_channels=hidden_dim, output_channels=hidden_dim,
kernel_size=1, kernel_size=1,
stride=1, stride=1,
...@@ -182,9 +167,9 @@ class GhostBottleneck(nn.Layer): ...@@ -182,9 +167,9 @@ class GhostBottleneck(nn.Layer):
) )
if stride == 2: if stride == 2:
self.depthwise_conv = ConvBNLayer( self.depthwise_conv = ConvBNLayer(
num_channels=hidden_dim, in_channels=hidden_dim,
num_filters=hidden_dim, out_channels=hidden_dim,
filter_size=kernel_size, kernel_size=kernel_size,
stride=stride, stride=stride,
groups=hidden_dim, groups=hidden_dim,
act=None, act=None,
...@@ -196,26 +181,26 @@ class GhostBottleneck(nn.Layer): ...@@ -196,26 +181,26 @@ class GhostBottleneck(nn.Layer):
name=name + "_se" name=name + "_se"
) )
self.ghost_module_2 = GhostModule( self.ghost_module_2 = GhostModule(
num_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 num_channels != output_channels: if stride != 1 or in_channels != output_channels:
self.shortcut_depthwise = ConvBNLayer( self.shortcut_depthwise = ConvBNLayer(
num_channels=num_channels, in_channels=in_channels,
num_filters=num_channels, out_channels=in_channels,
filter_size=kernel_size, kernel_size=kernel_size,
stride=stride, stride=stride,
groups=num_channels, groups=in_channels,
act=None, act=None,
name=name + "_shortcut_depthwise" # In the old version, name was name + "_shortcut_depthwise_depthwise" name=name + "_shortcut_depthwise" # In the old version, name was name + "_shortcut_depthwise_depthwise"
) )
self.shortcut_conv = ConvBNLayer( self.shortcut_conv = ConvBNLayer(
num_channels=num_channels, in_channels=in_channels,
num_filters=output_channels, out_channels=output_channels,
filter_size=1, kernel_size=1,
stride=1, stride=1,
groups=1, groups=1,
act=None, act=None,
...@@ -262,9 +247,9 @@ class GhostNet(nn.Layer): ...@@ -262,9 +247,9 @@ class GhostNet(nn.Layer):
self.scale = scale self.scale = scale
output_channels = int(self._make_divisible(16 * self.scale, 4)) output_channels = int(self._make_divisible(16 * self.scale, 4))
self.conv1 = ConvBNLayer( self.conv1 = ConvBNLayer(
num_channels=3, in_channels=3,
num_filters=output_channels, out_channels=output_channels,
filter_size=3, kernel_size=3,
stride=2, stride=2,
groups=1, groups=1,
act="relu", act="relu",
...@@ -274,13 +259,13 @@ class GhostNet(nn.Layer): ...@@ -274,13 +259,13 @@ class GhostNet(nn.Layer):
idx = 0 idx = 0
self.ghost_bottleneck_list = [] self.ghost_bottleneck_list = []
for k, exp_size, c, use_se, s in self.cfgs: for k, exp_size, c, use_se, s in self.cfgs:
num_channels = output_channels in_channels = output_channels
output_channels = int(self._make_divisible(c * self.scale, 4)) output_channels = int(self._make_divisible(c * self.scale, 4))
hidden_dim = int(self._make_divisible(exp_size, self.scale, 4)) hidden_dim = int(self._make_divisible(exp_size, self.scale, 4))
ghost_bottleneck = self.add_sublayer( ghost_bottleneck = self.add_sublayer(
name="_ghostbottleneck_" + str(idx), name="_ghostbottleneck_" + str(idx),
sublayer=GhostBottleneck( sublayer=GhostBottleneck(
num_channels=num_channels, in_channels=in_channels,
hidden_dim=hidden_dim, hidden_dim=hidden_dim,
output_channels=output_channels, output_channels=output_channels,
kernel_size=k, kernel_size=k,
...@@ -292,25 +277,24 @@ class GhostNet(nn.Layer): ...@@ -292,25 +277,24 @@ class GhostNet(nn.Layer):
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
num_channels = output_channels in_channels = output_channels
output_channels = int(self._make_divisible(exp_size * self.scale, 4)) output_channels = int(self._make_divisible(exp_size * self.scale, 4))
self.conv_last = ConvBNLayer( self.conv_last = ConvBNLayer(
num_channels=num_channels, in_channels=in_channels,
num_filters=output_channels, out_channels=output_channels,
filter_size=1, kernel_size=1,
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)
num_channels = output_channels in_channels = output_channels
self._num_channels = num_channels
self._fc0_output_channels = 1280 self._fc0_output_channels = 1280
self.fc_0 = ConvBNLayer( self.fc_0 = ConvBNLayer(
num_channels=num_channels, in_channels=in_channels,
num_filters=self._fc0_output_channels, out_channels=self._fc0_output_channels,
filter_size=1, kernel_size=1,
stride=1, stride=1,
act="relu", act="relu",
name="fc_0" name="fc_0"
...@@ -320,10 +304,7 @@ class GhostNet(nn.Layer): ...@@ -320,10 +304,7 @@ class GhostNet(nn.Layer):
self.fc_1 = Linear( self.fc_1 = Linear(
self._fc0_output_channels, self._fc0_output_channels,
class_dim, class_dim,
weight_attr=ParamAttr( weight_attr=ParamAttr(name="fc_1_weights", initializer=Uniform(-stdv, stdv)),
name="fc_1_weights",
initializer=Uniform(-stdv, stdv)
),
bias_attr=ParamAttr(name="fc_1_offset") bias_attr=ParamAttr(name="fc_1_offset")
) )
...@@ -367,4 +348,4 @@ def GhostNet_x1_0(**args): ...@@ -367,4 +348,4 @@ def GhostNet_x1_0(**args):
def GhostNet_x1_3(**args): def GhostNet_x1_3(**args):
model = GhostNet(scale=1.3) model = GhostNet(scale=1.3)
return model return model
\ No newline at end of file
...@@ -19,7 +19,7 @@ from __future__ import print_function ...@@ -19,7 +19,7 @@ from __future__ import print_function
from paddle import ParamAttr, reshape, transpose, concat, split from paddle import ParamAttr, reshape, transpose, concat, split
from paddle.nn import Layer, Conv2d, MaxPool2d, AdaptiveAvgPool2d, BatchNorm, Linear from paddle.nn import Layer, Conv2d, MaxPool2d, AdaptiveAvgPool2d, BatchNorm, Linear
from paddle.nn.initializer import MSRA from paddle.nn.initializer import MSRA
from paddle.nn.functional import relu, swish from paddle.nn.functional import swish
__all__ = [ __all__ = [
...@@ -53,11 +53,10 @@ class ConvBNLayer(Layer): ...@@ -53,11 +53,10 @@ class ConvBNLayer(Layer):
stride, stride,
padding, padding,
groups=1, groups=1,
act=relu, act=None,
name=None, name=None,
): ):
super(ConvBNLayer, self).__init__() super(ConvBNLayer, self).__init__()
self._act = act
self._conv = Conv2d( self._conv = Conv2d(
in_channels=in_channels, in_channels=in_channels,
out_channels=out_channels, out_channels=out_channels,
...@@ -66,12 +65,14 @@ class ConvBNLayer(Layer): ...@@ -66,12 +65,14 @@ class ConvBNLayer(Layer):
padding=padding, padding=padding,
groups=groups, groups=groups,
weight_attr=ParamAttr(initializer=MSRA(), name=name + "_weights"), weight_attr=ParamAttr(initializer=MSRA(), name=name + "_weights"),
bias_attr=False) bias_attr=False
)
self._batch_norm = BatchNorm( self._batch_norm = BatchNorm(
out_channels, out_channels,
param_attr=ParamAttr(name=name + "_bn_scale"), param_attr=ParamAttr(name=name + "_bn_scale"),
bias_attr=ParamAttr(name=name + "_bn_offset"), bias_attr=ParamAttr(name=name + "_bn_offset"),
act=act,
moving_mean_name=name + "_bn_mean", moving_mean_name=name + "_bn_mean",
moving_variance_name=name + "_bn_variance" moving_variance_name=name + "_bn_variance"
) )
...@@ -79,18 +80,18 @@ class ConvBNLayer(Layer): ...@@ -79,18 +80,18 @@ class ConvBNLayer(Layer):
def forward(self, inputs): def forward(self, inputs):
y = self._conv(inputs) y = self._conv(inputs)
y = self._batch_norm(y) y = self._batch_norm(y)
if self._act:
y = self._act(y)
return y return y
class InvertedResidual(Layer): class InvertedResidual(Layer):
def __init__(self, def __init__(
in_channels, self,
out_channels, in_channels,
stride, out_channels,
act=relu, stride,
name=None): act="relu",
name=None
):
super(InvertedResidual, self).__init__() super(InvertedResidual, self).__init__()
self._conv_pw = ConvBNLayer( self._conv_pw = ConvBNLayer(
in_channels=in_channels // 2, in_channels=in_channels // 2,
...@@ -124,25 +125,23 @@ class InvertedResidual(Layer): ...@@ -124,25 +125,23 @@ class InvertedResidual(Layer):
) )
def forward(self, inputs): def forward(self, inputs):
x1, x2 = split( x1, x2 = split(inputs, num_or_sections=[inputs.shape[1] // 2, inputs.shape[1] // 2], axis=1)
inputs,
num_or_sections=[inputs.shape[1] // 2, inputs.shape[1] // 2],
axis=1)
x2 = self._conv_pw(x2) x2 = self._conv_pw(x2)
x2 = self._conv_dw(x2) x2 = self._conv_dw(x2)
x2 = self._conv_linear(x2) x2 = self._conv_linear(x2)
out = concat([x1, x2], axis=1) out = concat([x1, x2], axis=1)
return channel_shuffle(out, 2) return channel_shuffle(out, 2)
class InvertedResidualDS(Layer): class InvertedResidualDS(Layer):
def __init__(self, def __init__(
in_channels, self,
out_channels, in_channels,
stride, out_channels,
act=relu, stride,
name=None): act="relu",
name=None
):
super(InvertedResidualDS, self).__init__() super(InvertedResidualDS, self).__init__()
# branch1 # branch1
...@@ -201,7 +200,6 @@ class InvertedResidualDS(Layer): ...@@ -201,7 +200,6 @@ class InvertedResidualDS(Layer):
def forward(self, inputs): def forward(self, inputs):
x1 = self._conv_dw_1(inputs) x1 = self._conv_dw_1(inputs)
x1 = self._conv_linear_1(x1) x1 = self._conv_linear_1(x1)
x2 = self._conv_pw_2(inputs) x2 = self._conv_pw_2(inputs)
x2 = self._conv_dw_2(x2) x2 = self._conv_dw_2(x2)
x2 = self._conv_linear_2(x2) x2 = self._conv_linear_2(x2)
...@@ -211,7 +209,7 @@ class InvertedResidualDS(Layer): ...@@ -211,7 +209,7 @@ class InvertedResidualDS(Layer):
class ShuffleNet(Layer): class ShuffleNet(Layer):
def __init__(self, class_dim=1000, scale=1.0, act=relu): def __init__(self, class_dim=1000, scale=1.0, act="relu"):
super(ShuffleNet, self).__init__() super(ShuffleNet, self).__init__()
self.scale = scale self.scale = scale
self.class_dim = class_dim self.class_dim = class_dim
...@@ -240,7 +238,8 @@ class ShuffleNet(Layer): ...@@ -240,7 +238,8 @@ class ShuffleNet(Layer):
stride=2, stride=2,
padding=1, padding=1,
act=act, act=act,
name='stage1_conv') name='stage1_conv'
)
self._max_pool = MaxPool2d( self._max_pool = MaxPool2d(
kernel_size=3, kernel_size=3,
stride=2, stride=2,
...@@ -259,7 +258,9 @@ class ShuffleNet(Layer): ...@@ -259,7 +258,9 @@ class ShuffleNet(Layer):
out_channels=stage_out_channels[stage_id + 2], out_channels=stage_out_channels[stage_id + 2],
stride=2, stride=2,
act=act, act=act,
name=str(stage_id + 2) + '_' + str(i + 1))) name=str(stage_id + 2) + '_' + str(i + 1)
)
)
else: else:
block = self.add_sublayer( block = self.add_sublayer(
name=str(stage_id + 2) + '_' + str(i + 1), name=str(stage_id + 2) + '_' + str(i + 1),
...@@ -268,7 +269,9 @@ class ShuffleNet(Layer): ...@@ -268,7 +269,9 @@ class ShuffleNet(Layer):
out_channels=stage_out_channels[stage_id + 2], out_channels=stage_out_channels[stage_id + 2],
stride=1, stride=1,
act=act, act=act,
name=str(stage_id + 2) + '_' + str(i + 1))) name=str(stage_id + 2) + '_' + str(i + 1)
)
)
self._block_list.append(block) self._block_list.append(block)
# 3. last_conv # 3. last_conv
self._last_conv = ConvBNLayer( self._last_conv = ConvBNLayer(
...@@ -278,7 +281,8 @@ class ShuffleNet(Layer): ...@@ -278,7 +281,8 @@ class ShuffleNet(Layer):
stride=1, stride=1,
padding=0, padding=0,
act=act, act=act,
name='conv5') name='conv5'
)
# 4. pool # 4. pool
self._pool2d_avg = AdaptiveAvgPool2d(1) self._pool2d_avg = AdaptiveAvgPool2d(1)
self._out_c = stage_out_channels[-1] self._out_c = stage_out_channels[-1]
...@@ -287,7 +291,8 @@ class ShuffleNet(Layer): ...@@ -287,7 +291,8 @@ class ShuffleNet(Layer):
stage_out_channels[-1], stage_out_channels[-1],
class_dim, class_dim,
weight_attr=ParamAttr(name='fc6_weights'), weight_attr=ParamAttr(name='fc6_weights'),
bias_attr=ParamAttr(name='fc6_offset')) bias_attr=ParamAttr(name='fc6_offset')
)
def forward(self, inputs): def forward(self, inputs):
y = self._conv1(inputs) y = self._conv1(inputs)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册