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

modify format

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