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

fix weight name

上级 ac50d65d
...@@ -47,4 +47,4 @@ from .ghostnet import GhostNet_x0_5, GhostNet_x1_0, GhostNet_x1_3 ...@@ -47,4 +47,4 @@ from .ghostnet import GhostNet_x0_5, GhostNet_x1_0, GhostNet_x1_3
# distillation model # distillation model
from .distillation_models import ResNet50_vd_distill_MobileNetV3_large_x1_0, ResNeXt101_32x16d_wsl_distill_ResNet50_vd from .distillation_models import ResNet50_vd_distill_MobileNetV3_large_x1_0, ResNeXt101_32x16d_wsl_distill_ResNet50_vd
from .csp_resnet import CSPResNet50_leaky from .csp_resnet import CSPResNet50_leaky
\ No newline at end of file
...@@ -37,65 +37,55 @@ class GhostNet(): ...@@ -37,65 +37,55 @@ 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, groups=1,
groups=1, act="relu",
act="relu", name="conv1")
name="conv1")
# build inverted residual blocks # build inverted residual blocks
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( hidden_dim=hidden_channel,
inp=x, output=output_channel,
hidden_dim=hidden_channel, kernel_size=k,
oup=output_channel, stride=s,
kernel_size=k, use_se=use_se,
stride=s, name="_ghostbottleneck_" + str(idx))
use_se=use_se,
name="ghost_bottle_" + 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( num_filters=output_channel,
input=x, filter_size=1,
num_filters=output_channel, stride=1,
filter_size=1, groups=1,
stride=1, act="relu",
groups=1, name="conv_last")
act="relu", x = fluid.layers.pool2d(input=x, pool_type='avg', global_pooling=True)
name="conv2")
x = fluid.layers.pool2d(
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, act="relu",
groups=1, name="fc_0")
act="relu",
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(name="fc_1_weights",
param_attr=ParamAttr( initializer=fluid.initializer.Uniform(-stdv, stdv)),
name="fc_1_weight", bias_attr=ParamAttr(name="fc_1_offset"))
initializer=fluid.initializer.Uniform(-stdv, stdv)),
bias_attr=ParamAttr(name="fc_1_offset"))
return out
return out
def _make_divisible(self, v, divisor, min_value=None): def _make_divisible(self, v, divisor, min_value=None):
""" """
This function is taken from the original tf repo. This function is taken from the original tf repo.
...@@ -119,160 +109,145 @@ class GhostNet(): ...@@ -119,160 +109,145 @@ 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, padding=(filter_size - 1) // 2,
padding=(filter_size - 1) // 2, groups=groups,
groups=groups, act=None,
act=None, 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(input=x,
x = fluid.layers.batch_norm( act=act,
input=x, param_attr=ParamAttr(
act=act, name=bn_name+"_scale",
param_attr=ParamAttr( regularizer=fluid.regularizer.L2DecayRegularizer(
name=name + "_bn_scale", regularization_coeff=0.0)),
regularizer=fluid.regularizer.L2DecayRegularizer( bias_attr=ParamAttr(
regularization_coeff=0.0)), name=bn_name+"_offset",
bias_attr=ParamAttr( regularizer=fluid.regularizer.L2DecayRegularizer(
name=name + "_bn_offset", regularization_coeff=0.0)),
regularizer=fluid.regularizer.L2DecayRegularizer( moving_mean_name=bn_name+"_mean",
regularization_coeff=0.0)), moving_variance_name=name+"_variance")
moving_mean_name=name + "_bn_mean",
moving_variance_name=name + "_bn_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 + '_1_weights'),
name=name + '_sqz_weights'), bias_attr=ParamAttr(name=name + '_1_offset'))
bias_attr=ParamAttr(name=name + '_sqz_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 + '_2_weights'),
name=name + '_exc_weights'), bias_attr=ParamAttr(name=name + '_2_offset'))
bias_attr=ParamAttr(name=name + '_exc_offset')) excitation = fluid.layers.clip(x=excitation, min=0, max=1)
excitation = fluid.layers.clip(
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=input.shape[1],
groups=inp.shape[1], act="relu" if relu else None,
act="relu" if relu else None, name=name + "_depthwise")
name=name + "_dw")
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(input=primary_conv,
cheap_operation = self.conv_bn_layer( num_filters=new_channels,
input=primary_conv, filter_size=dw_size,
num_filters=new_channels, stride=1,
filter_size=dw_size, groups=init_channels,
stride=1, act="relu" if relu else None,
groups=init_channels, name=name + "_cheap_operation")
act="relu" if relu else None, out = fluid.layers.concat([primary_conv, cheap_operation], axis=1)
name=name + "_cheap_operation")
out = fluid.layers.concat(
[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 + "_depthwise")
name=name + "_dw2")
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, kernel_size=1,
oup=oup, relu=False,
kernel_size=1, name=name + "_ghost_module_2")
relu=False, if stride == 1 and inp_channels == output:
name=name + "ghost_module_2") shortcut = input
if stride == 1 and inp_channels == oup:
shortcut = inp
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")
name=name + "shortcut_depthwise_conv") shortcut = self.conv_bn_layer(input=shortcut,
shortcut = self.conv_bn_layer( num_filters=output,
input=shortcut, filter_size=1,
num_filters=oup, stride=1,
filter_size=1, groups=1,
stride=1, act=None,
groups=1, name=name + "_shortcut_conv")
act=None, return fluid.layers.elementwise_add(x=x,
name=name + "shortcut_conv_bn") y=shortcut,
return fluid.layers.elementwise_add( axis=-1)
x=x, y=shortcut, axis=-1, act=None)
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.
先完成此消息的编辑!
想要评论请 注册