diff --git a/ppcls/modeling/architectures/GhostNet_wqz.py b/ppcls/modeling/architectures/GhostNet_wqz.py new file mode 100644 index 0000000000000000000000000000000000000000..18f298a7b1d0f266bdeafda3bf7e2bd21cb199a3 --- /dev/null +++ b/ppcls/modeling/architectures/GhostNet_wqz.py @@ -0,0 +1,334 @@ +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + +import math + +import paddle +import paddle.fluid as fluid +from paddle.fluid.param_attr import ParamAttr +from paddle.fluid.initializer import MSRA +from paddle.fluid.contrib.model_stat import summary + +__all__ = ["GhostNet", "GhostNetV1"] + + +class GhostNet(): + def __init__(self, width_mult): + cfgs = [ + # k, t, c, SE, s + [3, 16, 16, 0, 1], + [3, 48, 24, 0, 2], + [3, 72, 24, 0, 1], + [5, 72, 40, 1, 2], + [5, 120, 40, 1, 1], + [3, 240, 80, 0, 2], + [3, 200, 80, 0, 1], + [3, 184, 80, 0, 1], + [3, 184, 80, 0, 1], + [3, 480, 112, 1, 1], + [3, 672, 112, 1, 1], + [5, 672, 160, 1, 2], + [5, 960, 160, 0, 1], + [5, 960, 160, 1, 1], + [5, 960, 160, 0, 1], + [5, 960, 160, 1, 1] + ] + self.cfgs = cfgs + self.width_mult = width_mult + + def _make_divisible(self, v, divisor, min_value=None): + """ + This function is taken from the original tf repo. + It ensures that all layers have a channel number that is divisible by 8 + It can be seen here: + https://github.com/tensorflow/models/blob/master/research/slim/nets/mobilenet/mobilenet.py + """ + if min_value is None: + min_value = divisor + new_v = max(min_value, int(v + divisor / 2) // divisor * divisor) + # Make sure that round down does not go down by more than 10%. + if new_v < 0.9 * v: + new_v += divisor + return new_v + + def conv_bn_layer(self, + input, + num_filters, + filter_size, + stride=1, + groups=1, + act=None, + name=None, + data_format="NCHW"): + print("conv bn, num_filters: {}, filter_size: {}, stride: {}".format( + num_filters, filter_size, stride)) + x = fluid.layers.conv2d( + input=input, + num_filters=num_filters, + filter_size=filter_size, + stride=stride, + padding=(filter_size - 1) // 2, + groups=groups, + act=None, + param_attr=ParamAttr( + initializer=fluid.initializer.MSRA(), name=name + "_weights"), + bias_attr=False, + name=name + "_conv_op", + data_format=data_format) + + x = fluid.layers.batch_norm( + input=x, + act=act, + name=name + "_bn", + param_attr=ParamAttr(name=name + "_bn_scale"), + bias_attr=ParamAttr(name=name + "_bn_offset"), + moving_mean_name=name + "_bn_mean", + moving_variance_name=name + "_bn_variance", + data_layout=data_format) + return x + + def SElayer(self, input, num_channels, reduction_ratio=4, name=None): + pool = fluid.layers.pool2d( + input=input, pool_size=0, pool_type='avg', global_pooling=True) + stdv = 1.0 / math.sqrt(pool.shape[1] * 1.0) + squeeze = fluid.layers.fc( + input=pool, + size=num_channels // reduction_ratio, + act='relu', + param_attr=fluid.param_attr.ParamAttr( + initializer=fluid.initializer.Uniform(-stdv, stdv), + name=name + '_sqz_weights'), + bias_attr=ParamAttr(name=name + '_sqz_offset')) + stdv = 1.0 / math.sqrt(squeeze.shape[1] * 1.0) + excitation = fluid.layers.fc( + input=squeeze, + size=num_channels, + act=None, + param_attr=fluid.param_attr.ParamAttr( + initializer=fluid.initializer.Uniform(-stdv, stdv), + name=name + '_exc_weights'), + bias_attr=ParamAttr(name=name + '_exc_offset')) + excitation = fluid.layers.clip( + x=excitation, min=0, max=1, name=name + '_clip') + scale = fluid.layers.elementwise_mul(x=input, y=excitation, axis=0) + return scale + + def depthwise_conv(self, + inp, + oup, + kernel_size, + stride=1, + relu=False, + name=None, + data_format="NCHW"): + return self.conv_bn_layer( + input=inp, + num_filters=oup, + filter_size=kernel_size, + stride=stride, + groups=inp.shape[1] if data_format == "NCHW" else inp.shape[-1], + act="relu" if relu else None, + name=name + "_dw", + data_format=data_format) + + def GhostModule(self, + inp, + oup, + kernel_size=1, + ratio=2, + dw_size=3, + stride=1, + relu=True, + name=None, + data_format="NCHW"): + self.oup = oup + init_channels = math.ceil(oup / ratio) + new_channels = init_channels * (ratio - 1) + primary_conv = self.conv_bn_layer( + input=inp, + num_filters=init_channels, + filter_size=kernel_size, + stride=stride, + groups=1, + act="relu" if relu else None, + name=name + "_primary_conv", + data_format="NCHW") + cheap_operation = self.conv_bn_layer( + input=primary_conv, + num_filters=new_channels, + filter_size=dw_size, + stride=1, + groups=init_channels, + act="relu" if relu else None, + name=name + "_cheap_operation", + data_format=data_format) + out = fluid.layers.concat( + [primary_conv, cheap_operation], axis=1, name=name + "_concat") + return out[:, :self.oup, :, :] + + def GhostBottleneck(self, + inp, + hidden_dim, + oup, + kernel_size, + stride, + use_se, + name=None, + data_format="NCHW"): + inp_channels = inp.shape[1] + x = self.GhostModule( + inp=inp, + oup=hidden_dim, + kernel_size=1, + stride=1, + relu=True, + name=name + "GhostBottle_1", + data_format="NCHW") + if stride == 2: + x = self.depthwise_conv( + inp=x, + oup=hidden_dim, + kernel_size=kernel_size, + stride=stride, + relu=False, + name=name + "_dw2", + data_format="NCHW") + if use_se: + x = self.SElayer( + input=x, num_channels=hidden_dim, name=name + "SElayer") + x = self.GhostModule( + inp=x, + oup=oup, + kernel_size=1, + relu=False, + name=name + "GhostModule_2") + if stride == 1 and inp_channels == oup: + shortcut = inp + else: + shortcut = self.depthwise_conv( + inp=inp, + oup=inp_channels, + kernel_size=kernel_size, + stride=stride, + relu=False, + name=name + "shortcut_depthwise_conv", + data_format="NCHW") + shortcut = self.conv_bn_layer( + input=shortcut, + num_filters=oup, + filter_size=1, + stride=1, + groups=1, + act=None, + name=name + "shortcut_conv_bn", + data_format="NCHW") + return fluid.layers.elementwise_add( + x=x, y=shortcut, axis=-1, act=None, name=name + "elementwise_add") + + def net(self, input, class_dim=1000): + # build first layer: + output_channel = self._make_divisible(16 * self.width_mult, 4) + x = self.conv_bn_layer( + input=input, + num_filters=output_channel, + filter_size=3, + stride=2, + groups=1, + act="relu", + name="firstlayer", + data_format="NCHW") + input_channel = output_channel + # build inverted residual blocks + idx = 0 + for k, exp_size, c, use_se, s in self.cfgs: + output_channel = self._make_divisible(c * self.width_mult, 4) + hidden_channel = self._make_divisible(exp_size * self.width_mult, + 4) + x = self.GhostBottleneck( + inp=x, + hidden_dim=hidden_channel, + oup=output_channel, + kernel_size=k, + stride=s, + use_se=use_se, + name="GhostBottle_" + str(idx), + data_format="NCHW") + input_channel = output_channel + idx += 1 + # build last several layers + output_channel = self._make_divisible(exp_size * self.width_mult, 4) + x = self.conv_bn_layer( + input=x, + num_filters=output_channel, + filter_size=1, + stride=1, + groups=1, + act="relu", + name="lastlayer", + data_format="NCHW") + x = fluid.layers.pool2d( + input=x, pool_type='avg', global_pooling=True, data_format="NCHW") + input_channel = output_channel + output_channel = 1280 + + stdv = 1.0 / math.sqrt(x.shape[1] * 1.0) + out = fluid.layers.conv2d( + input=x, + num_filters=output_channel, + filter_size=1, + groups=1, + param_attr=ParamAttr( + name="fc_0_w", + initializer=fluid.initializer.Uniform(-stdv, stdv)), + bias_attr=False, + name="fc_0") + out = fluid.layers.batch_norm( + input=out, + act="relu", + name="fc_0_bn", + param_attr=ParamAttr(name="fc_0_bn_scale"), + bias_attr=ParamAttr(name="fc_0_bn_offset"), + moving_mean_name="fc_0_bn_mean", + moving_variance_name="fc_0_bn_variance", + data_layout="NCHW") + out = fluid.layers.dropout(x=out, dropout_prob=0.2) + stdv = 1.0 / math.sqrt(out.shape[1] * 1.0) + out = fluid.layers.fc( + input=out, + size=class_dim, + param_attr=ParamAttr( + name="fc_1_w", + initializer=fluid.initializer.Uniform(-stdv, stdv)), + bias_attr=ParamAttr(name="fc_1_bias")) + return out + + +def GhostNet_0_5(): + model = GhostNet(width_mult=0.5) + return model + + +def GhostNet_1_0(): + model = GhostNet(width_mult=1.0) + return model + + +def GhostNet_1_3(): + model = GhostNet(width_mult=1.3) + return model + + +if __name__ == "__main__": + # from calc_flops import summary + + image = fluid.data(name='image', shape=[-1, 3, 224, 224], dtype='float32') + model = GhostNet_1_3() + out = model.net(input=image, class_dim=1000) + test_program = fluid.default_main_program().clone(for_test=True) + + place = fluid.CPUPlace() + exe = fluid.Executor(place) + exe.run(fluid.default_startup_program()) + + total_flops_params, is_quantize = summary(test_program) diff --git a/ppcls/modeling/architectures/__init__.py b/ppcls/modeling/architectures/__init__.py index 82d1b2a2e665e8933d058d2fb6d5346da02932f9..4daec57a122f77efa6631f953542b78614d90bbc 100644 --- a/ppcls/modeling/architectures/__init__.py +++ b/ppcls/modeling/architectures/__init__.py @@ -24,9 +24,11 @@ from .se_resnext_vd import SE_ResNeXt50_vd_32x4d, SE_ResNeXt50_vd_32x4d, SENet15 from .dpn import DPN68 from .densenet import DenseNet121 from .hrnet import HRNet_W18_C +from .efficientnet import EfficientNetB0 +from .googlenet import GoogLeNet from .mobilenet_v1 import MobileNetV1_x0_25, MobileNetV1_x0_5, MobileNetV1_x0_75, MobileNetV1 from .mobilenet_v2 import MobileNetV2_x0_25, MobileNetV2_x0_5, MobileNetV2_x0_75, MobileNetV2, MobileNetV2_x1_5, MobileNetV2_x2_0 from .mobilenet_v3 import MobileNetV3_small_x0_35, MobileNetV3_small_x0_5, MobileNetV3_small_x0_75, MobileNetV3_small_x1_0, MobileNetV3_small_x1_25, MobileNetV3_large_x0_35, MobileNetV3_large_x0_5, MobileNetV3_large_x0_75, MobileNetV3_large_x1_0, MobileNetV3_large_x1_25 from .shufflenet_v2 import ShuffleNetV2_x0_25, ShuffleNetV2_x0_33, ShuffleNetV2_x0_5, ShuffleNetV2, ShuffleNetV2_x1_5, ShuffleNetV2_x2_0, ShuffleNetV2_swish -from .distillation_models import ResNet50_vd_distill_MobileNetV3_large_x1_0 \ No newline at end of file +from .distillation_models import ResNet50_vd_distill_MobileNetV3_large_x1_0 diff --git a/ppcls/modeling/architectures/densenet.py b/ppcls/modeling/architectures/densenet.py index b0950064153bfd08fa68f2132746ae4f95c83f56..8a52c1e46dfabfbcb18af421e77b5bb87d2d6cc2 100644 --- a/ppcls/modeling/architectures/densenet.py +++ b/ppcls/modeling/architectures/densenet.py @@ -20,7 +20,6 @@ import numpy as np import paddle import paddle.fluid as fluid from paddle.fluid.param_attr import ParamAttr -from paddle.fluid.layer_helper import LayerHelper from paddle.fluid.dygraph.nn import Conv2D, Pool2D, BatchNorm, Linear, Dropout import math diff --git a/ppcls/modeling/architectures/dpn.py b/ppcls/modeling/architectures/dpn.py index 44fc4b5aceb149472f28b9e29a6758e304a2a65f..1810957b7af1438e499270f08466ca663edf6c4f 100644 --- a/ppcls/modeling/architectures/dpn.py +++ b/ppcls/modeling/architectures/dpn.py @@ -21,7 +21,6 @@ import sys import paddle import paddle.fluid as fluid from paddle.fluid.param_attr import ParamAttr -from paddle.fluid.layer_helper import LayerHelper from paddle.fluid.dygraph.nn import Conv2D, Pool2D, BatchNorm, Linear import math diff --git a/ppcls/modeling/architectures/efficientnet.py b/ppcls/modeling/architectures/efficientnet.py index 8fa292f2e64d22d4ff80605fe6121b36376359e1..6e9baccd33c36488a94db5dc7c9badd54c16836f 100644 --- a/ppcls/modeling/architectures/efficientnet.py +++ b/ppcls/modeling/architectures/efficientnet.py @@ -1,7 +1,6 @@ import paddle import paddle.fluid as fluid from paddle.fluid.param_attr import ParamAttr -from paddle.fluid.layer_helper import LayerHelper from paddle.fluid.dygraph.nn import Conv2D, Pool2D, BatchNorm, Linear, Dropout import math import collections @@ -491,6 +490,7 @@ class SEBlock(fluid.dygraph.Layer): num_squeezed_channels, oup, 1, + act="sigmoid", use_bias=True, padding_type=padding_type, name=name + "_se_expand") @@ -499,8 +499,6 @@ class SEBlock(fluid.dygraph.Layer): x = self._pool(inputs) x = self._conv1(x) x = self._conv2(x) - layer_helper = LayerHelper(self.full_name(), act='sigmoid') - x = layer_helper.append_activation(x) return fluid.layers.elementwise_mul(inputs, x) @@ -565,18 +563,17 @@ class MbConvBlock(fluid.dygraph.Layer): def forward(self, inputs): x = inputs - layer_helper = LayerHelper(self.full_name(), act='swish') if self.expand_ratio != 1: x = self._ecn(x) - x = layer_helper.append_activation(x) + x = fluid.layers.swish(x) x = self._dcn(x) - x = layer_helper.append_activation(x) + x = fluid.layers.swish(x) if self.has_se: x = self._se(x) x = self._pcn(x) if self.id_skip and \ - self.block_args.stride == 1 and \ - self.block_args.input_filters == self.block_args.output_filters: + self.block_args.stride == 1 and \ + self.block_args.input_filters == self.block_args.output_filters: if self.drop_connect_rate: x = _drop_connect(x, self.drop_connect_rate, self.is_test) x = fluid.layers.elementwise_add(x, inputs) @@ -697,8 +694,7 @@ class ExtractFeatures(fluid.dygraph.Layer): def forward(self, inputs): x = self._conv_stem(inputs) - layer_helper = LayerHelper(self.full_name(), act='swish') - x = layer_helper.append_activation(x) + x = fluid.layers.swish(x) for _mc_block in self.conv_seq: x = _mc_block(x) return x @@ -914,4 +910,4 @@ def EfficientNetB7(is_test=False, override_params=override_params, use_se=use_se, **args) - return model \ No newline at end of file + return model diff --git a/ppcls/modeling/architectures/googlenet.py b/ppcls/modeling/architectures/googlenet.py index 541195854b6f9782565895a32f3d4da4728b3816..e3cc664ea851516746ffbfc0f7226828aaed4bc1 100644 --- a/ppcls/modeling/architectures/googlenet.py +++ b/ppcls/modeling/architectures/googlenet.py @@ -1,18 +1,17 @@ import paddle import paddle.fluid as fluid from paddle.fluid.param_attr import ParamAttr -from paddle.fluid.layer_helper import LayerHelper from paddle.fluid.dygraph.nn import Conv2D, Pool2D, BatchNorm, Linear import math -__all__ = ['GoogLeNet_DY'] +__all__ = ['GoogLeNet'] + def xavier(channels, filter_size, name): stdv = (3.0 / (filter_size**2 * channels))**0.5 param_attr = ParamAttr( initializer=fluid.initializer.Uniform(-stdv, stdv), name=name + "_weights") - return param_attr @@ -90,8 +89,8 @@ class Inception(fluid.dygraph.Layer): convprj = self._convprj(pool) cat = fluid.layers.concat([conv1, conv3, conv5, convprj], axis=1) - layer_helper = LayerHelper(self.full_name(), act="relu") - return layer_helper.append_activation(cat) + cat = fluid.layers.relu(cat) + return cat class GoogleNetDY(fluid.dygraph.Layer): @@ -205,4 +204,4 @@ class GoogleNetDY(fluid.dygraph.Layer): def GoogLeNet(**args): model = GoogleNetDY(**args) - return model \ No newline at end of file + return model diff --git a/ppcls/modeling/architectures/hrnet.py b/ppcls/modeling/architectures/hrnet.py index 64d1f14869b1e991c64502ee987a9611c7febd11..e6e75cabd435bcd703afd54b94718b454ea2ab01 100644 --- a/ppcls/modeling/architectures/hrnet.py +++ b/ppcls/modeling/architectures/hrnet.py @@ -20,7 +20,6 @@ import numpy as np import paddle import paddle.fluid as fluid from paddle.fluid.param_attr import ParamAttr -from paddle.fluid.layer_helper import LayerHelper from paddle.fluid.dygraph.nn import Conv2D, Pool2D, BatchNorm, Linear import math @@ -495,8 +494,7 @@ class FuseLayers(fluid.dygraph.Layer): residual = fluid.layers.elementwise_add( x=residual, y=y, act=None) - layer_helper = LayerHelper(self.full_name(), act='relu') - residual = layer_helper.append_activation(residual) + residual = fluid.layers.relu(residual) outs.append(residual) return outs diff --git a/ppcls/modeling/architectures/mobilenet_v1.py b/ppcls/modeling/architectures/mobilenet_v1.py index 5dc229d4c4e215333320b49a7a47296b704d580c..a8b8ab6eaca4805ffcb5abb2c6b5d54e76744f54 100644 --- a/ppcls/modeling/architectures/mobilenet_v1.py +++ b/ppcls/modeling/architectures/mobilenet_v1.py @@ -20,7 +20,6 @@ import numpy as np import paddle import paddle.fluid as fluid from paddle.fluid.param_attr import ParamAttr -from paddle.fluid.layer_helper import LayerHelper from paddle.fluid.dygraph.nn import Conv2D, Pool2D, BatchNorm, Linear, Dropout from paddle.fluid.initializer import MSRA import math diff --git a/ppcls/modeling/architectures/mobilenet_v2.py b/ppcls/modeling/architectures/mobilenet_v2.py index f8a5e62d420daf2060d000fc372d0394badaa291..fb9e061afcbdb041fa9510adbf6cfee8151a3a78 100644 --- a/ppcls/modeling/architectures/mobilenet_v2.py +++ b/ppcls/modeling/architectures/mobilenet_v2.py @@ -20,7 +20,6 @@ import numpy as np import paddle import paddle.fluid as fluid from paddle.fluid.param_attr import ParamAttr -from paddle.fluid.layer_helper import LayerHelper from paddle.fluid.dygraph.nn import Conv2D, Pool2D, BatchNorm, Linear, Dropout import math diff --git a/ppcls/modeling/architectures/mobilenet_v3.py b/ppcls/modeling/architectures/mobilenet_v3.py index eb99dd21f5ad89b9b87f2f7222d41f06ce4d68e8..584b790f4b5edf0592577c30fc3b6087a48d7a97 100644 --- a/ppcls/modeling/architectures/mobilenet_v3.py +++ b/ppcls/modeling/architectures/mobilenet_v3.py @@ -20,7 +20,6 @@ import numpy as np import paddle import paddle.fluid as fluid from paddle.fluid.param_attr import ParamAttr -from paddle.fluid.layer_helper import LayerHelper from paddle.fluid.dygraph.nn import Conv2D, Pool2D, BatchNorm, Linear, Dropout import math diff --git a/ppcls/modeling/architectures/res2net.py b/ppcls/modeling/architectures/res2net.py index ed4dcd8056f608912b9e73b8bf4646f7acb87d6a..050d2ad7b3bc2686483f1c0109b62305e2eee9db 100644 --- a/ppcls/modeling/architectures/res2net.py +++ b/ppcls/modeling/architectures/res2net.py @@ -20,7 +20,6 @@ import numpy as np import paddle import paddle.fluid as fluid from paddle.fluid.param_attr import ParamAttr -from paddle.fluid.layer_helper import LayerHelper from paddle.fluid.dygraph.nn import Conv2D, Pool2D, BatchNorm, Linear, Dropout import math @@ -143,9 +142,8 @@ class BottleneckBlock(fluid.dygraph.Layer): short = inputs else: short = self.short(inputs) - y = fluid.layers.elementwise_add(x=short, y=conv2) - layer_helper = LayerHelper(self.full_name(), act='relu') - return layer_helper.append_activation(y) + y = fluid.layers.elementwise_add(x=short, y=conv2, act='relu') + return y class Res2Net(fluid.dygraph.Layer): diff --git a/ppcls/modeling/architectures/res2net_vd.py b/ppcls/modeling/architectures/res2net_vd.py index 1cc397e87f5341ea158fcd839a493fdb278e905d..21c4397d98c26bc2100b6e07605df2184120c6f8 100644 --- a/ppcls/modeling/architectures/res2net_vd.py +++ b/ppcls/modeling/architectures/res2net_vd.py @@ -20,7 +20,6 @@ import numpy as np import paddle import paddle.fluid as fluid from paddle.fluid.param_attr import ParamAttr -from paddle.fluid.layer_helper import LayerHelper from paddle.fluid.dygraph.nn import Conv2D, Pool2D, BatchNorm, Linear, Dropout import math @@ -47,7 +46,11 @@ class ConvBNLayer(fluid.dygraph.Layer): self.is_vd_mode = is_vd_mode self._pool2d_avg = Pool2D( - pool_size=2, pool_stride=2, pool_padding=0, pool_type='avg', ceil_mode=True) + pool_size=2, + pool_stride=2, + pool_padding=0, + pool_type='avg', + ceil_mode=True) self._conv = Conv2D( num_channels=num_channels, num_filters=num_filters, @@ -150,9 +153,8 @@ class BottleneckBlock(fluid.dygraph.Layer): short = inputs else: short = self.short(inputs) - y = fluid.layers.elementwise_add(x=short, y=conv2) - layer_helper = LayerHelper(self.full_name(), act='relu') - return layer_helper.append_activation(y) + y = fluid.layers.elementwise_add(x=short, y=conv2, act='relu') + return y class Res2Net_vd(fluid.dygraph.Layer): diff --git a/ppcls/modeling/architectures/resnet.py b/ppcls/modeling/architectures/resnet.py index 3e0f1187e7055eabc7655214a0e63a7b27be5575..e346e67434d69b9bae0ed333df969dffe2a01c1f 100644 --- a/ppcls/modeling/architectures/resnet.py +++ b/ppcls/modeling/architectures/resnet.py @@ -20,7 +20,6 @@ import numpy as np import paddle import paddle.fluid as fluid from paddle.fluid.param_attr import ParamAttr -from paddle.fluid.layer_helper import LayerHelper from paddle.fluid.dygraph.nn import Conv2D, Pool2D, BatchNorm, Linear, Dropout import math @@ -118,10 +117,8 @@ class BottleneckBlock(fluid.dygraph.Layer): else: short = self.short(inputs) - y = fluid.layers.elementwise_add(x=short, y=conv2) - - layer_helper = LayerHelper(self.full_name(), act="relu") - return layer_helper.append_activation(y) + y = fluid.layers.elementwise_add(x=short, y=conv2, act="relu") + return y class BasicBlock(fluid.dygraph.Layer): @@ -165,10 +162,8 @@ class BasicBlock(fluid.dygraph.Layer): short = inputs else: short = self.short(inputs) - y = fluid.layers.elementwise_add(x=short, y=conv1) - - layer_helper = LayerHelper(self.full_name(), act="relu") - return layer_helper.append_activation(y) + y = fluid.layers.elementwise_add(x=short, y=conv1, act="relu") + return y class ResNet(fluid.dygraph.Layer): diff --git a/ppcls/modeling/architectures/resnet_name.py b/ppcls/modeling/architectures/resnet_name.py deleted file mode 100644 index a7ac0a78743f0be1ecdd6a7841833d2e9ced4665..0000000000000000000000000000000000000000 --- a/ppcls/modeling/architectures/resnet_name.py +++ /dev/null @@ -1,213 +0,0 @@ -import numpy as np -import argparse -import ast -import paddle -import paddle.fluid as fluid -from paddle.fluid.param_attr import ParamAttr -from paddle.fluid.layer_helper import LayerHelper -from paddle.fluid.dygraph.nn import Conv2D, Pool2D, BatchNorm, Linear -from paddle.fluid.dygraph.base import to_variable - -from paddle.fluid import framework - -import math -import sys -import time - -class ConvBNLayer(fluid.dygraph.Layer): - def __init__(self, - num_channels, - num_filters, - filter_size, - stride=1, - groups=1, - act=None, - name=None): - super(ConvBNLayer, self).__init__() - - self._conv = Conv2D( - num_channels=num_channels, - num_filters=num_filters, - filter_size=filter_size, - stride=stride, - padding=(filter_size - 1) // 2, - groups=groups, - act=None, - param_attr=ParamAttr(name=name + "_weights"), - bias_attr=False) - if name == "conv1": - bn_name = "bn_" + name - else: - bn_name = "bn" + name[3:] - self._batch_norm = BatchNorm(num_filters, - act=act, - param_attr=ParamAttr(name=bn_name + '_scale'), - bias_attr=ParamAttr(bn_name + '_offset'), - moving_mean_name=bn_name + '_mean', - moving_variance_name=bn_name + '_variance') - - def forward(self, inputs): - y = self._conv(inputs) - y = self._batch_norm(y) - return y - - -class BottleneckBlock(fluid.dygraph.Layer): - def __init__(self, - num_channels, - num_filters, - stride, - shortcut=True, - name=None): - super(BottleneckBlock, self).__init__() - - self.conv0 = ConvBNLayer( - num_channels=num_channels, - num_filters=num_filters, - filter_size=1, - act='relu', - name=name+"_branch2a") - self.conv1 = ConvBNLayer( - num_channels=num_filters, - num_filters=num_filters, - filter_size=3, - stride=stride, - act='relu', - name=name+"_branch2b") - self.conv2 = ConvBNLayer( - num_channels=num_filters, - num_filters=num_filters * 4, - filter_size=1, - act=None, - name=name+"_branch2c") - - if not shortcut: - self.short = ConvBNLayer( - num_channels=num_channels, - num_filters=num_filters * 4, - filter_size=1, - stride=stride, - name=name + "_branch1") - - self.shortcut = shortcut - - self._num_channels_out = num_filters * 4 - - def forward(self, inputs): - y = self.conv0(inputs) - conv1 = self.conv1(y) - conv2 = self.conv2(conv1) - - if self.shortcut: - short = inputs - else: - short = self.short(inputs) - - y = fluid.layers.elementwise_add(x=short, y=conv2) - - layer_helper = LayerHelper(self.full_name(), act='relu') - return layer_helper.append_activation(y) - - -class ResNet(fluid.dygraph.Layer): - def __init__(self, layers=50, class_dim=1000): - super(ResNet, self).__init__() - - self.layers = layers - supported_layers = [50, 101, 152] - assert layers in supported_layers, \ - "supported layers are {} but input layer is {}".format(supported_layers, layers) - - if layers == 50: - depth = [3, 4, 6, 3] - elif layers == 101: - depth = [3, 4, 23, 3] - elif layers == 152: - depth = [3, 8, 36, 3] - num_channels = [64, 256, 512, 1024] - num_filters = [64, 128, 256, 512] - - self.conv = ConvBNLayer( - num_channels=3, - num_filters=64, - filter_size=7, - stride=2, - act='relu', - name="conv1") - self.pool2d_max = Pool2D( - pool_size=3, - pool_stride=2, - pool_padding=1, - pool_type='max') - - self.bottleneck_block_list = [] - for block in range(len(depth)): - shortcut = False - for i in range(depth[block]): - if layers in [101, 152] and block == 2: - if i == 0: - conv_name="res"+str(block+2)+"a" - else: - conv_name="res"+str(block+2)+"b"+str(i) - else: - conv_name="res"+str(block+2)+chr(97+i) - bottleneck_block = self.add_sublayer( - 'bb_%d_%d' % (block, i), - BottleneckBlock( - num_channels=num_channels[block] - if i == 0 else num_filters[block] * 4, - num_filters=num_filters[block], - stride=2 if i == 0 and block != 0 else 1, - shortcut=shortcut, - name=conv_name)) - self.bottleneck_block_list.append(bottleneck_block) - shortcut = True - - self.pool2d_avg = Pool2D( - pool_size=7, pool_type='avg', global_pooling=True) - - self.pool2d_avg_output = num_filters[len(num_filters) - 1] * 4 * 1 * 1 - - stdv = 1.0 / math.sqrt(2048 * 1.0) - - self.out = Linear(self.pool2d_avg_output, - class_dim, - param_attr=ParamAttr( - initializer=fluid.initializer.Uniform(-stdv, stdv), name="fc_0.w_0"), - bias_attr=ParamAttr(name="fc_0.b_0")) - - def forward(self, inputs): - y = self.conv(inputs) - y = self.pool2d_max(y) - for bottleneck_block in self.bottleneck_block_list: - y = bottleneck_block(y) - y = self.pool2d_avg(y) - y = fluid.layers.reshape(y, shape=[-1, self.pool2d_avg_output]) - y = self.out(y) - return y - - -def ResNet50(**args): - model = ResNet(layers=50, **args) - return model - - -def ResNet101(**args): - model = ResNet(layers=101, **args) - return model - - -def ResNet152(**args): - model = ResNet(layers=152, **args) - return model - - -if __name__ == "__main__": - import numpy as np - place = fluid.CPUPlace() - with fluid.dygraph.guard(place): - model = ResNet50() - img = np.random.uniform(0, 255, [1, 3, 224, 224]).astype('float32') - img = fluid.dygraph.to_variable(img) - res = model(img) - print(res.shape) diff --git a/ppcls/modeling/architectures/resnet_vc.py b/ppcls/modeling/architectures/resnet_vc.py index b568d0207f946fea589a49d9902a7d61fa478a83..74900b509ebe218dbeaa6fb69f7d0ac077a97380 100644 --- a/ppcls/modeling/architectures/resnet_vc.py +++ b/ppcls/modeling/architectures/resnet_vc.py @@ -20,7 +20,6 @@ import numpy as np import paddle import paddle.fluid as fluid from paddle.fluid.param_attr import ParamAttr -from paddle.fluid.layer_helper import LayerHelper from paddle.fluid.dygraph.nn import Conv2D, Pool2D, BatchNorm, Linear, Dropout import math @@ -120,10 +119,8 @@ class BottleneckBlock(fluid.dygraph.Layer): else: short = self.short(inputs) - y = fluid.layers.elementwise_add(x=short, y=conv2) - - layer_helper = LayerHelper(self.full_name(), act='relu') - return layer_helper.append_activation(y) + y = fluid.layers.elementwise_add(x=short, y=conv2, act='relu') + return y class BasicBlock(fluid.dygraph.Layer): @@ -167,10 +164,8 @@ class BasicBlock(fluid.dygraph.Layer): short = inputs else: short = self.short(inputs) - y = fluid.layers.elementwise_add(x=short, y=conv1) - - layer_helper = LayerHelper(self.full_name(), act='relu') - return layer_helper.append_activation(y) + y = fluid.layers.elementwise_add(x=short, y=conv1, act='relu') + return y class ResNet_vc(fluid.dygraph.Layer): diff --git a/ppcls/modeling/architectures/resnet_vd.py b/ppcls/modeling/architectures/resnet_vd.py index 9a7ba5fce1e61520dff3f00c9ce2c5124b1bb9a3..b285b9184cb9381bfd921042f5fc60dcb6c6f53d 100644 --- a/ppcls/modeling/architectures/resnet_vd.py +++ b/ppcls/modeling/architectures/resnet_vd.py @@ -20,7 +20,6 @@ import numpy as np import paddle import paddle.fluid as fluid from paddle.fluid.param_attr import ParamAttr -from paddle.fluid.layer_helper import LayerHelper from paddle.fluid.dygraph.nn import Conv2D, Pool2D, BatchNorm, Linear, Dropout import math @@ -130,10 +129,8 @@ class BottleneckBlock(fluid.dygraph.Layer): short = inputs else: short = self.short(inputs) - y = fluid.layers.elementwise_add(x=short, y=conv2) - - layer_helper = LayerHelper(self.full_name(), act='relu') - return layer_helper.append_activation(y) + y = fluid.layers.elementwise_add(x=short, y=conv2, act='relu') + return y class BasicBlock(fluid.dygraph.Layer): @@ -179,10 +176,8 @@ class BasicBlock(fluid.dygraph.Layer): short = inputs else: short = self.short(inputs) - y = fluid.layers.elementwise_add(x=short, y=conv1) - - layer_helper = LayerHelper(self.full_name(), act='relu') - return layer_helper.append_activation(y) + y = fluid.layers.elementwise_add(x=short, y=conv1, act='relu') + return y class ResNet_vd(fluid.dygraph.Layer): diff --git a/ppcls/modeling/architectures/resnext.py b/ppcls/modeling/architectures/resnext.py index c2200c6f35611708c48ada3b40bf24b1e559b2d0..3a4fe5cf46b2875d128a83f7246d2f4c07ea492e 100644 --- a/ppcls/modeling/architectures/resnext.py +++ b/ppcls/modeling/architectures/resnext.py @@ -20,7 +20,6 @@ import numpy as np import paddle import paddle.fluid as fluid from paddle.fluid.param_attr import ParamAttr -from paddle.fluid.layer_helper import LayerHelper from paddle.fluid.dygraph.nn import Conv2D, Pool2D, BatchNorm, Linear, Dropout import math @@ -122,10 +121,8 @@ class BottleneckBlock(fluid.dygraph.Layer): else: short = self.short(inputs) - y = fluid.layers.elementwise_add(x=short, y=conv2) - - layer_helper = LayerHelper(self.full_name(), act='relu') - return layer_helper.append_activation(y) + y = fluid.layers.elementwise_add(x=short, y=conv2, act='relu') + return y class ResNeXt(fluid.dygraph.Layer): diff --git a/ppcls/modeling/architectures/resnext_vd.py b/ppcls/modeling/architectures/resnext_vd.py index 0abb2419fc9b9b7dfe9fe3c1b8d4d460da2dd68f..0d6159059b030153e5ceef8243aac918b8b5db04 100644 --- a/ppcls/modeling/architectures/resnext_vd.py +++ b/ppcls/modeling/architectures/resnext_vd.py @@ -20,7 +20,6 @@ import numpy as np import paddle import paddle.fluid as fluid from paddle.fluid.param_attr import ParamAttr -from paddle.fluid.layer_helper import LayerHelper from paddle.fluid.dygraph.nn import Conv2D, Pool2D, BatchNorm, Linear, Dropout import math @@ -46,7 +45,11 @@ class ConvBNLayer(fluid.dygraph.Layer): self.is_vd_mode = is_vd_mode self._pool2d_avg = Pool2D( - pool_size=2, pool_stride=2, pool_padding=0, pool_type='avg', ceil_mode=True) + pool_size=2, + pool_stride=2, + pool_padding=0, + pool_type='avg', + ceil_mode=True) self._conv = Conv2D( num_channels=num_channels, num_filters=num_filters, @@ -131,10 +134,8 @@ class BottleneckBlock(fluid.dygraph.Layer): else: short = self.short(inputs) - y = fluid.layers.elementwise_add(x=short, y=conv2) - - layer_helper = LayerHelper(self.full_name(), act='relu') - return layer_helper.append_activation(y) + y = fluid.layers.elementwise_add(x=short, y=conv2, act='relu') + return y class ResNeXt(fluid.dygraph.Layer): diff --git a/ppcls/modeling/architectures/se_resnet_vd.py b/ppcls/modeling/architectures/se_resnet_vd.py index 7e25a80b2281fd3cf4e3e3de46df26648d808dee..f0fd33c71f7add805863545b5f0401ddb222b097 100644 --- a/ppcls/modeling/architectures/se_resnet_vd.py +++ b/ppcls/modeling/architectures/se_resnet_vd.py @@ -19,7 +19,6 @@ import numpy as np import paddle import paddle.fluid as fluid from paddle.fluid.param_attr import ParamAttr -from paddle.fluid.layer_helper import LayerHelper from paddle.fluid.dygraph.nn import Conv2D, Pool2D, BatchNorm, Linear, Dropout import math @@ -137,10 +136,8 @@ class BottleneckBlock(fluid.dygraph.Layer): short = inputs else: short = self.short(inputs) - y = fluid.layers.elementwise_add(x=short, y=scale) - - layer_helper = LayerHelper(self.full_name(), act='relu') - return layer_helper.append_activation(y) + y = fluid.layers.elementwise_add(x=short, y=scale, act='relu') + return y class BasicBlock(fluid.dygraph.Layer): @@ -194,10 +191,8 @@ class BasicBlock(fluid.dygraph.Layer): short = inputs else: short = self.short(inputs) - y = fluid.layers.elementwise_add(x=short, y=scale) - - layer_helper = LayerHelper(self.full_name(), act='relu') - return layer_helper.append_activation(y) + y = fluid.layers.elementwise_add(x=short, y=scale, act='relu') + return y class SELayer(fluid.dygraph.Layer): diff --git a/ppcls/modeling/architectures/se_resnext_vd.py b/ppcls/modeling/architectures/se_resnext_vd.py index 5b3b47eab0ed52f44f03c0feaabe1412f071f4b6..04db2a8a1ec7f9d6ec67ffd35e12a979cd35653c 100644 --- a/ppcls/modeling/architectures/se_resnext_vd.py +++ b/ppcls/modeling/architectures/se_resnext_vd.py @@ -20,7 +20,6 @@ import numpy as np import paddle import paddle.fluid as fluid from paddle.fluid.param_attr import ParamAttr -from paddle.fluid.layer_helper import LayerHelper from paddle.fluid.dygraph.nn import Conv2D, Pool2D, BatchNorm, Linear, Dropout import math @@ -131,10 +130,8 @@ class BottleneckBlock(fluid.dygraph.Layer): short = inputs else: short = self.short(inputs) - y = fluid.layers.elementwise_add(x=short, y=scale) - - layer_helper = LayerHelper(self.full_name(), act='relu') - return layer_helper.append_activation(y) + y = fluid.layers.elementwise_add(x=short, y=scale, act='relu') + return y class SELayer(fluid.dygraph.Layer): diff --git a/ppcls/modeling/architectures/shufflenet_v2.py b/ppcls/modeling/architectures/shufflenet_v2.py index 83979915cfc34130a18fbf180da8926fa7222dd4..dd3d7923355443a36f7670acc298c417729d2860 100644 --- a/ppcls/modeling/architectures/shufflenet_v2.py +++ b/ppcls/modeling/architectures/shufflenet_v2.py @@ -20,7 +20,6 @@ import numpy as np import paddle import paddle.fluid as fluid from paddle.fluid.param_attr import ParamAttr -from paddle.fluid.layer_helper import LayerHelper from paddle.fluid.dygraph.nn import Conv2D, Pool2D, BatchNorm, Linear, Dropout from paddle.fluid.initializer import MSRA import math diff --git a/ppcls/modeling/architectures/xception.py b/ppcls/modeling/architectures/xception.py index ac9c4864d1d6ed722240eec6f463e228dfdf8271..e05e8ca5e0c2c0f54d1a4348ab0de479d928219a 100644 --- a/ppcls/modeling/architectures/xception.py +++ b/ppcls/modeling/architectures/xception.py @@ -1,7 +1,6 @@ import paddle import paddle.fluid as fluid from paddle.fluid.param_attr import ParamAttr -from paddle.fluid.layer_helper import LayerHelper from paddle.fluid.dygraph.nn import Conv2D, Pool2D, BatchNorm, Linear import math @@ -99,11 +98,10 @@ class EntryFlowBottleneckBlock(fluid.dygraph.Layer): def forward(self, inputs): conv0 = inputs short = self._short(inputs) - layer_helper = LayerHelper(self.full_name(), act="relu") if self.relu_first: - conv0 = layer_helper.append_activation(conv0) + conv0 = fluid.layers.relu(conv0) conv1 = self._conv1(conv0) - conv2 = layer_helper.append_activation(conv1) + conv2 = fluid.layers.relu(conv1) conv2 = self._conv2(conv2) pool = self._pool(conv2) return fluid.layers.elementwise_add(x=short, y=pool) @@ -177,12 +175,11 @@ class MiddleFlowBottleneckBlock(fluid.dygraph.Layer): name=name + "_branch2c_weights") def forward(self, inputs): - layer_helper = LayerHelper(self.full_name(), act="relu") - conv0 = layer_helper.append_activation(inputs) + conv0 = fluid.layers.relu(inputs) conv0 = self._conv_0(conv0) - conv1 = layer_helper.append_activation(conv0) + conv1 = fluid.layers.relu(conv0) conv1 = self._conv_1(conv1) - conv2 = layer_helper.append_activation(conv1) + conv2 = fluid.layers.relu(conv1) conv2 = self._conv_2(conv2) return fluid.layers.elementwise_add(x=inputs, y=conv2) @@ -276,10 +273,9 @@ class ExitFlowBottleneckBlock(fluid.dygraph.Layer): def forward(self, inputs): short = self._short(inputs) - layer_helper = LayerHelper(self.full_name(), act="relu") - conv0 = layer_helper.append_activation(inputs) + conv0 = fluid.layers.relu(inputs) conv1 = self._conv_1(conv0) - conv2 = layer_helper.append_activation(conv1) + conv2 = fluid.layers.relu(conv1) conv2 = self._conv_2(conv2) pool = self._pool(conv2) return fluid.layers.elementwise_add(x=short, y=pool) @@ -306,12 +302,11 @@ class ExitFlow(fluid.dygraph.Layer): bias_attr=ParamAttr(name="fc_offset")) def forward(self, inputs): - layer_helper = LayerHelper(self.full_name(), act="relu") conv0 = self._conv_0(inputs) conv1 = self._conv_1(conv0) - conv1 = layer_helper.append_activation(conv1) + conv1 = fluid.layers.relu(conv1) conv2 = self._conv_2(conv1) - conv2 = layer_helper.append_activation(conv2) + conv2 = fluid.layers.relu(conv2) pool = self._pool(conv2) pool = fluid.layers.reshape(pool, [0, -1]) out = self._out(pool) diff --git a/ppcls/modeling/architectures/xception_deeplab.py b/ppcls/modeling/architectures/xception_deeplab.py index 48fcd638073179d7121110454eaf0d6350f15b07..c2c4cd90fbad01f61bf32a2c04c20018b946475c 100644 --- a/ppcls/modeling/architectures/xception_deeplab.py +++ b/ppcls/modeling/architectures/xception_deeplab.py @@ -1,7 +1,6 @@ import paddle import paddle.fluid as fluid from paddle.fluid.param_attr import ParamAttr -from paddle.fluid.layer_helper import LayerHelper from paddle.fluid.dygraph.nn import Conv2D, Pool2D, BatchNorm, Linear, Dropout __all__ = ["Xception41_deeplab", "Xception65_deeplab", "Xception71_deeplab"] @@ -226,13 +225,12 @@ class Xception_Block(fluid.dygraph.Layer): name=name + "/shortcut") def forward(self, inputs): - layer_helper = LayerHelper(self.full_name(), act='relu') if not self.activation_fn_in_separable_conv: - x = layer_helper.append_activation(inputs) + x = fluid.layers.relu(inputs) x = self._conv1(x) - x = layer_helper.append_activation(x) + x = fluid.layers.relu(x) x = self._conv2(x) - x = layer_helper.append_activation(x) + x = fluid.layers.relu(x) x = self._conv3(x) else: x = self._conv1(inputs) diff --git a/tools/run.sh b/tools/run.sh index ce9772eb88e882a9fcd5bb2b6ae118ea2846bd57..5e8043b1205f99dada7448d172f84cd0661df509 100755 --- a/tools/run.sh +++ b/tools/run.sh @@ -5,5 +5,5 @@ export PYTHONPATH=$PWD:$PYTHONPATH python -m paddle.distributed.launch \ --selected_gpus="0,1,2,3" \ tools/train.py \ - -c ./configs/ResNet/ResNet50_vd.yaml \ + -c ./configs/ResNet/ResNet50.yaml \ -o print_interval=10