未验证 提交 032230c6 编写于 作者: D dyning 提交者: GitHub

Merge pull request #261 from littletomatodonkey/dyg/opt_code

improve dygraph model
......@@ -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
......@@ -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
......
......@@ -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
......
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
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
......@@ -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
......
......@@ -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
......
......@@ -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
......
......@@ -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
......
......@@ -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):
......
......@@ -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):
......
......@@ -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):
......
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)
......@@ -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):
......
......@@ -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):
......
......@@ -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):
......
......@@ -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):
......
......@@ -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):
......
......@@ -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):
......
......@@ -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
......
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)
......
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)
......
......@@ -31,12 +31,12 @@ def check_version():
Log error and exit when the installed version of paddlepaddle is
not satisfied.
"""
err = "PaddlePaddle version 2.0.0 or higher is required, " \
err = "PaddlePaddle version 1.8.0 or higher is required, " \
"or a suitable develop version is satisfied as well. \n" \
"Please make sure the version is good with your code." \
try:
fluid.require_version('2.0.0')
fluid.require_version('1.8.0')
except Exception:
logger.error(err)
sys.exit(1)
......
......@@ -64,14 +64,18 @@ def print_dict(d, delimiter=0):
placeholder = "-" * 60
for k, v in sorted(d.items()):
if isinstance(v, dict):
logger.info("{}{} : ".format(delimiter * " ", logger.coloring(k, "HEADER")))
logger.info("{}{} : ".format(delimiter * " ",
logger.coloring(k, "HEADER")))
print_dict(v, delimiter + 4)
elif isinstance(v, list) and len(v) >= 1 and isinstance(v[0], dict):
logger.info("{}{} : ".format(delimiter * " ", logger.coloring(str(k),"HEADER")))
logger.info("{}{} : ".format(delimiter * " ",
logger.coloring(str(k), "HEADER")))
for value in v:
print_dict(value, delimiter + 4)
else:
logger.info("{}{} : {}".format(delimiter * " ", logger.coloring(k,"HEADER"), logger.coloring(v,"OKGREEN")))
logger.info("{}{} : {}".format(delimiter * " ",
logger.coloring(k, "HEADER"),
logger.coloring(v, "OKGREEN")))
if k.isupper():
logger.info(placeholder)
......@@ -138,7 +142,9 @@ def override(dl, ks, v):
override(dl[k], ks[1:], v)
else:
if len(ks) == 1:
assert ks[0] in dl, ('{} is not exist in {}'.format(ks[0], dl))
# assert ks[0] in dl, ('{} is not exist in {}'.format(ks[0], dl))
if not ks[0] in dl:
logger.warning('A new filed ({}) detected!'.format(ks[0], dl))
dl[ks[0]] = str2num(v)
else:
override(dl[ks[0]], ks[1:], v)
......
......@@ -45,10 +45,7 @@ def _mkdir_if_not_exist(path):
raise OSError('Failed to mkdir {}'.format(path))
def load_dygraph_pretrain(
model,
path=None,
load_static_weights=False, ):
def load_dygraph_pretrain(model, path=None, load_static_weights=False):
if not (os.path.isdir(path) or os.path.exists(path + '.pdparams')):
raise ValueError("Model pretrain path {} does not "
"exists.".format(path))
......@@ -72,6 +69,32 @@ def load_dygraph_pretrain(
return
def load_distillation_model(model, pretrained_model, load_static_weights):
logger.info("In distillation mode, teacher model will be "
"loaded firstly before student model.")
assert len(pretrained_model
) == 2, "pretrained_model length should be 2 but got {}".format(
len(pretrained_model))
assert len(
load_static_weights
) == 2, "load_static_weights length should be 2 but got {}".format(
len(load_static_weights))
load_dygraph_pretrain(
model.teacher,
path=pretrained_model[0],
load_static_weights=load_static_weights[0])
logger.info(
logger.coloring("Finish initing teacher model from {}".format(
pretrained_model), "HEADER"))
load_dygraph_pretrain(
model.student,
path=pretrained_model[1],
load_static_weights=load_static_weights[1])
logger.info(
logger.coloring("Finish initing student model from {}".format(
pretrained_model), "HEADER"))
def init_model(config, net, optimizer=None):
"""
load model from checkpoint or pretrained_model
......@@ -94,18 +117,17 @@ def init_model(config, net, optimizer=None):
load_static_weights = config.get('load_static_weights', False)
use_distillation = config.get('use_distillation', False)
if pretrained_model:
if not isinstance(pretrained_model, list):
pretrained_model = [pretrained_model]
if not isinstance(load_static_weights, list):
load_static_weights = [load_static_weights] * len(pretrained_model)
for idx, pretrained in enumerate(pretrained_model):
load_static = load_static_weights[idx]
model = net
if use_distillation and not load_static:
model = net.teacher
if isinstance(pretrained_model,
list): # load distillation pretrained model
if not isinstance(load_static_weights, list):
load_static_weights = [load_static_weights] * len(
pretrained_model)
load_distillation_model(net, pretrained_model, load_static_weights)
else: # common load
load_dygraph_pretrain(
model, path=pretrained, load_static_weights=load_static)
net,
path=pretrained_model,
load_static_weights=load_static_weights)
logger.info(
logger.coloring("Finish initing model from {}".format(
pretrained_model), "HEADER"))
......
......@@ -35,8 +35,6 @@ from ppcls.utils.misc import AverageMeter
from ppcls.utils import logger
from paddle.fluid.dygraph.base import to_variable
from paddle.fluid.incubate.fleet.collective import fleet
from paddle.fluid.incubate.fleet.collective import DistributedStrategy
def create_dataloader():
......@@ -243,43 +241,6 @@ def create_optimizer(config, parameter_list=None):
return opt(lr, parameter_list)
def dist_optimizer(config, optimizer):
"""
Create a distributed optimizer based on a normal optimizer
Args:
config(dict):
optimizer(): a normal optimizer
Returns:
optimizer: a distributed optimizer
"""
exec_strategy = fluid.ExecutionStrategy()
exec_strategy.num_threads = 3
exec_strategy.num_iteration_per_drop_scope = 10
dist_strategy = DistributedStrategy()
dist_strategy.nccl_comm_num = 1
dist_strategy.fuse_all_reduce_ops = True
dist_strategy.exec_strategy = exec_strategy
optimizer = fleet.distributed_optimizer(optimizer, strategy=dist_strategy)
return optimizer
def mixed_precision_optimizer(config, optimizer):
use_fp16 = config.get('use_fp16', False)
amp_scale_loss = config.get('amp_scale_loss', 1.0)
use_dynamic_loss_scaling = config.get('use_dynamic_loss_scaling', False)
if use_fp16:
optimizer = fluid.contrib.mixed_precision.decorate(
optimizer,
init_loss_scaling=amp_scale_loss,
use_dynamic_loss_scaling=use_dynamic_loss_scaling)
return optimizer
def create_feeds(batch, use_mix):
image = batch[0]
if use_mix:
......@@ -307,26 +268,22 @@ def run(dataloader, config, net, optimizer=None, epoch=0, mode='train'):
Returns:
"""
print_interval = config.get("print_interval", 10)
use_mix = config.get("use_mix", False) and mode == "train"
if use_mix:
metric_list = OrderedDict([
("loss", AverageMeter('loss', '7.4f')),
("lr", AverageMeter(
'lr', 'f', need_avg=False)),
("batch_time", AverageMeter('elapse', '.3f')),
('reader_time', AverageMeter('reader', '.3f')),
])
else:
metric_list = [
("loss", AverageMeter('loss', '7.4f')),
("lr", AverageMeter(
'lr', 'f', need_avg=False)),
("batch_time", AverageMeter('elapse', '.3f')),
('reader_time', AverageMeter('reader', '.3f')),
]
if not use_mix:
topk_name = 'top{}'.format(config.topk)
metric_list = OrderedDict([
("loss", AverageMeter('loss', '7.4f')),
("top1", AverageMeter('top1', '.4f')),
(topk_name, AverageMeter(topk_name, '.4f')),
("lr", AverageMeter(
'lr', 'f', need_avg=False)),
("batch_time", AverageMeter('elapse', '.3f')),
('reader_time', AverageMeter('reader', '.3f')),
])
metric_list.insert(1, (topk_name, AverageMeter(topk_name, '.4f')))
metric_list.insert(1, ("top1", AverageMeter("top1", '.4f')))
metric_list = OrderedDict(metric_list)
tic = time.time()
for idx, batch in enumerate(dataloader()):
......@@ -354,17 +311,19 @@ def run(dataloader, config, net, optimizer=None, epoch=0, mode='train'):
tic = time.time()
fetchs_str = ' '.join([str(m.value) for m in metric_list.values()])
if mode == 'eval':
logger.info("{:s} step:{:<4d} {:s}s".format(mode, idx, fetchs_str))
else:
epoch_str = "epoch:{:<3d}".format(epoch)
step_str = "{:s} step:{:<4d}".format(mode, idx)
logger.info("{:s} {:s} {:s}s".format(
logger.coloring(epoch_str, "HEADER")
if idx == 0 else epoch_str,
logger.coloring(step_str, "PURPLE"),
logger.coloring(fetchs_str, 'OKGREEN')))
if idx % print_interval == 0:
if mode == 'eval':
logger.info("{:s} step:{:<4d} {:s}s".format(mode, idx,
fetchs_str))
else:
epoch_str = "epoch:{:<3d}".format(epoch)
step_str = "{:s} step:{:<4d}".format(mode, idx)
logger.info("{:s} {:s} {:s}s".format(
logger.coloring(epoch_str, "HEADER")
if idx == 0 else epoch_str,
logger.coloring(step_str, "PURPLE"),
logger.coloring(fetchs_str, 'OKGREEN')))
end_str = ' '.join([str(m.mean) for m in metric_list.values()] +
[metric_list['batch_time'].total])
......
......@@ -5,4 +5,5 @@ export PYTHONPATH=$PWD:$PYTHONPATH
python -m paddle.distributed.launch \
--selected_gpus="0,1,2,3" \
tools/train.py \
-c ./configs/ResNet/ResNet50.yaml
-c ./configs/ResNet/ResNet50.yaml \
-o print_interval=10
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册