提交 8ff9d3fb 编写于 作者: W wqz960

fix weight name

上级 ac50d65d
...@@ -37,8 +37,7 @@ class GhostNet(): ...@@ -37,8 +37,7 @@ class GhostNet():
def net(self, input, class_dim=1000): def net(self, input, class_dim=1000):
# build first layer: # build first layer:
output_channel = int(self._make_divisible(16 * self.scale, 4)) output_channel = int(self._make_divisible(16 * self.scale, 4))
x = self.conv_bn_layer( x = self.conv_bn_layer(input=input,
input=input,
num_filters=output_channel, num_filters=output_channel,
filter_size=3, filter_size=3,
stride=2, stride=2,
...@@ -49,48 +48,39 @@ class GhostNet(): ...@@ -49,48 +48,39 @@ class GhostNet():
idx = 0 idx = 0
for k, exp_size, c, use_se, s in self.cfgs: for k, exp_size, c, use_se, s in self.cfgs:
output_channel = int(self._make_divisible(c * self.scale, 4)) output_channel = int(self._make_divisible(c * self.scale, 4))
hidden_channel = int( hidden_channel = int(self._make_divisible(exp_size * self.scale, 4))
self._make_divisible(exp_size * self.scale, 4)) x = self.ghost_bottleneck(input=x,
x = self.ghost_bottleneck(
inp=x,
hidden_dim=hidden_channel, hidden_dim=hidden_channel,
oup=output_channel, output=output_channel,
kernel_size=k, kernel_size=k,
stride=s, stride=s,
use_se=use_se, use_se=use_se,
name="ghost_bottle_" + str(idx)) name="_ghostbottleneck_" + str(idx))
idx += 1 idx += 1
# build last several layers # build last several layers
output_channel = int( output_channel = int(self._make_divisible(exp_size * self.scale, 4))
self._make_divisible(exp_size * self.scale, 4)) x = self.conv_bn_layer(input=x,
x = self.conv_bn_layer(
input=x,
num_filters=output_channel, num_filters=output_channel,
filter_size=1, filter_size=1,
stride=1, stride=1,
groups=1, groups=1,
act="relu", act="relu",
name="conv2") name="conv_last")
x = fluid.layers.pool2d( x = fluid.layers.pool2d(input=x, pool_type='avg', global_pooling=True)
input=x, pool_type='avg', global_pooling=True)
output_channel = 1280 output_channel = 1280
stdv = 1.0 / math.sqrt(x.shape[1] * 1.0) stdv = 1.0 / math.sqrt(x.shape[1] * 1.0)
out = self.conv_bn_layer( out = self.conv_bn_layer(input=x,
input=x,
num_filters=output_channel, num_filters=output_channel,
filter_size=1, filter_size=1,
stride=1, stride=1,
groups=1,
act="relu", act="relu",
name="fc_0") name="fc_0")
out = fluid.layers.dropout(x=out, dropout_prob=0.2) out = fluid.layers.dropout(x=out, dropout_prob=0.2)
stdv = 1.0 / math.sqrt(out.shape[1] * 1.0) stdv = 1.0 / math.sqrt(out.shape[1] * 1.0)
out = fluid.layers.fc( out = fluid.layers.fc(input=out,
input=out,
size=class_dim, size=class_dim,
param_attr=ParamAttr( param_attr=ParamAttr(name="fc_1_weights",
name="fc_1_weight",
initializer=fluid.initializer.Uniform(-stdv, stdv)), initializer=fluid.initializer.Uniform(-stdv, stdv)),
bias_attr=ParamAttr(name="fc_1_offset")) bias_attr=ParamAttr(name="fc_1_offset"))
...@@ -119,8 +109,7 @@ class GhostNet(): ...@@ -119,8 +109,7 @@ class GhostNet():
groups=1, groups=1,
act=None, act=None,
name=None): name=None):
x = fluid.layers.conv2d( x = fluid.layers.conv2d(input=input,
input=input,
num_filters=num_filters, num_filters=num_filters,
filter_size=filter_size, filter_size=filter_size,
stride=stride, stride=stride,
...@@ -130,149 +119,135 @@ class GhostNet(): ...@@ -130,149 +119,135 @@ class GhostNet():
param_attr=ParamAttr( param_attr=ParamAttr(
initializer=fluid.initializer.MSRA(), name=name + "_weights"), initializer=fluid.initializer.MSRA(), name=name + "_weights"),
bias_attr=False) bias_attr=False)
bn_name = name+"_bn"
x = fluid.layers.batch_norm( x = fluid.layers.batch_norm(input=x,
input=x,
act=act, act=act,
param_attr=ParamAttr( param_attr=ParamAttr(
name=name + "_bn_scale", name=bn_name+"_scale",
regularizer=fluid.regularizer.L2DecayRegularizer( regularizer=fluid.regularizer.L2DecayRegularizer(
regularization_coeff=0.0)), regularization_coeff=0.0)),
bias_attr=ParamAttr( bias_attr=ParamAttr(
name=name + "_bn_offset", name=bn_name+"_offset",
regularizer=fluid.regularizer.L2DecayRegularizer( regularizer=fluid.regularizer.L2DecayRegularizer(
regularization_coeff=0.0)), regularization_coeff=0.0)),
moving_mean_name=name + "_bn_mean", moving_mean_name=bn_name+"_mean",
moving_variance_name=name + "_bn_variance") moving_variance_name=name+"_variance")
return x return x
def se_layer(self, input, num_channels, reduction_ratio=4, name=None): def se_block(self, input, num_channels, reduction_ratio=4, name=None):
pool = fluid.layers.pool2d( pool = fluid.layers.pool2d(input=input, pool_type='avg', global_pooling=True, use_cudnn=False)
input=input, pool_size=0, pool_type='avg', global_pooling=True)
stdv = 1.0 / math.sqrt(pool.shape[1] * 1.0) stdv = 1.0 / math.sqrt(pool.shape[1] * 1.0)
squeeze = fluid.layers.fc( squeeze = fluid.layers.fc(input=pool,
input=pool,
size=num_channels // reduction_ratio, size=num_channels // reduction_ratio,
act='relu', act='relu',
param_attr=fluid.param_attr.ParamAttr( param_attr=fluid.param_attr.ParamAttr(
initializer=fluid.initializer.Uniform(-stdv, stdv), initializer=fluid.initializer.Uniform(-stdv, stdv),
name=name + '_sqz_weights'), name=name + '_1_weights'),
bias_attr=ParamAttr(name=name + '_sqz_offset')) bias_attr=ParamAttr(name=name + '_1_offset'))
stdv = 1.0 / math.sqrt(squeeze.shape[1] * 1.0) stdv = 1.0 / math.sqrt(squeeze.shape[1] * 1.0)
excitation = fluid.layers.fc( excitation = fluid.layers.fc(input=squeeze,
input=squeeze,
size=num_channels, size=num_channels,
act=None, act=None,
param_attr=fluid.param_attr.ParamAttr( param_attr=fluid.param_attr.ParamAttr(
initializer=fluid.initializer.Uniform(-stdv, stdv), initializer=fluid.initializer.Uniform(-stdv, stdv),
name=name + '_exc_weights'), name=name + '_2_weights'),
bias_attr=ParamAttr(name=name + '_exc_offset')) bias_attr=ParamAttr(name=name + '_2_offset'))
excitation = fluid.layers.clip( excitation = fluid.layers.clip(x=excitation, min=0, max=1)
x=excitation, min=0, max=1)
se_scale = fluid.layers.elementwise_mul(x=input, y=excitation, axis=0) se_scale = fluid.layers.elementwise_mul(x=input, y=excitation, axis=0)
return se_scale return se_scale
def depthwise_conv(self, def depthwise_conv(self,
inp, input,
oup, output,
kernel_size, kernel_size,
stride=1, stride=1,
relu=False, relu=False,
name=None): name=None):
return self.conv_bn_layer( return self.conv_bn_layer(input=input,
input=inp, num_filters=output,
num_filters=oup,
filter_size=kernel_size, filter_size=kernel_size,
stride=stride, stride=stride,
groups=inp.shape[1], groups=input.shape[1],
act="relu" if relu else None, act="relu" if relu else None,
name=name + "_dw") name=name + "_depthwise")
def ghost_module(self, def ghost_module(self,
inp, input,
oup, output,
kernel_size=1, kernel_size=1,
ratio=2, ratio=2,
dw_size=3, dw_size=3,
stride=1, stride=1,
relu=True, relu=True,
name=None): name=None):
self.oup = oup self.output = output
init_channels = int(math.ceil(oup / ratio)) init_channels = int(math.ceil(output / ratio))
new_channels = int(init_channels * (ratio - 1)) new_channels = int(init_channels * (ratio - 1))
primary_conv = self.conv_bn_layer( primary_conv = self.conv_bn_layer(input=input,
input=inp,
num_filters=init_channels, num_filters=init_channels,
filter_size=kernel_size, filter_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")
cheap_operation = self.conv_bn_layer( cheap_operation = self.conv_bn_layer(input=primary_conv,
input=primary_conv,
num_filters=new_channels, num_filters=new_channels,
filter_size=dw_size, filter_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,
name=name + "_cheap_operation") name=name + "_cheap_operation")
out = fluid.layers.concat( out = fluid.layers.concat([primary_conv, cheap_operation], axis=1)
[primary_conv, cheap_operation], axis=1)
return out return out
def ghost_bottleneck(self, def ghost_bottleneck(self,
inp, input,
hidden_dim, hidden_dim,
oup, output,
kernel_size, kernel_size,
stride, stride,
use_se, use_se,
name=None): name=None):
inp_channels = inp.shape[1] inp_channels = input.shape[1]
x = self.ghost_module( x = self.ghost_module(input=input,
inp=inp, output=hidden_dim,
oup=hidden_dim,
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:
x = self.depthwise_conv( x = self.depthwise_conv(input=x,
inp=x, output=hidden_dim,
oup=hidden_dim,
kernel_size=kernel_size, kernel_size=kernel_size,
stride=stride, stride=stride,
relu=False, relu=False,
name=name + "_dw2") name=name + "_depthwise")
if use_se: if use_se:
x = self.se_layer( x = self.se_block(input=x, num_channels=hidden_dim, name=name+"_se")
input=x, num_channels=hidden_dim, name=name + "se_layer") x = self.ghost_module(input=x,
x = self.ghost_module( output=output,
inp=x,
oup=oup,
kernel_size=1, kernel_size=1,
relu=False, relu=False,
name=name + "ghost_module_2") name=name + "_ghost_module_2")
if stride == 1 and inp_channels == oup: if stride == 1 and inp_channels == output:
shortcut = inp shortcut = input
else: else:
shortcut = self.depthwise_conv( shortcut = self.depthwise_conv(input=input,
inp=inp, output=inp_channels,
oup=inp_channels,
kernel_size=kernel_size, kernel_size=kernel_size,
stride=stride, stride=stride,
relu=False, relu=False,
name=name + "shortcut_depthwise_conv") name=name + "_shortcut_depthwise")
shortcut = self.conv_bn_layer( shortcut = self.conv_bn_layer(input=shortcut,
input=shortcut, num_filters=output,
num_filters=oup,
filter_size=1, filter_size=1,
stride=1, stride=1,
groups=1, groups=1,
act=None, act=None,
name=name + "shortcut_conv_bn") name=name + "_shortcut_conv")
return fluid.layers.elementwise_add( return fluid.layers.elementwise_add(x=x,
x=x, y=shortcut, axis=-1, act=None) y=shortcut,
axis=-1)
def GhostNet_x0_5(): def GhostNet_x0_5():
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册