提交 04fa1750 编写于 作者: C chenguowei01

update conv

上级 df15bf4d
...@@ -21,11 +21,13 @@ import math ...@@ -21,11 +21,13 @@ import math
import numpy as np import numpy as np
import paddle import paddle
from paddle.nn import SyncBatchNorm as BatchNorm
from paddle.nn import Conv2d
import paddle.fluid as fluid import paddle.fluid as fluid
from paddle.fluid.param_attr import ParamAttr from paddle.fluid.param_attr import ParamAttr
from paddle.fluid.layer_helper import LayerHelper from paddle.fluid.layer_helper import LayerHelper
from paddle.fluid.dygraph.nn import Conv2D, Pool2D, Linear, Dropout from paddle.fluid.dygraph.nn import Pool2D, Linear, Dropout
from paddle.nn import SyncBatchNorm as BatchNorm
from paddleseg.utils import utils from paddleseg.utils import utils
from paddleseg.models.common import layer_utils from paddleseg.models.common import layer_utils
...@@ -39,9 +41,9 @@ __all__ = [ ...@@ -39,9 +41,9 @@ __all__ = [
class ConvBNLayer(fluid.dygraph.Layer): class ConvBNLayer(fluid.dygraph.Layer):
def __init__( def __init__(
self, self,
num_channels, in_channels,
num_filters, out_channels,
filter_size, kernel_size,
stride=1, stride=1,
dilation=1, dilation=1,
groups=1, groups=1,
...@@ -58,23 +60,22 @@ class ConvBNLayer(fluid.dygraph.Layer): ...@@ -58,23 +60,22 @@ class ConvBNLayer(fluid.dygraph.Layer):
pool_padding=0, pool_padding=0,
pool_type='avg', pool_type='avg',
ceil_mode=True) ceil_mode=True)
self._conv = Conv2D( self._conv = Conv2d(
num_channels=num_channels, in_channels=in_channels,
num_filters=num_filters, out_channels=out_channels,
filter_size=filter_size, kernel_size=kernel_size,
stride=stride, stride=stride,
padding=(filter_size - 1) // 2 if dilation == 1 else 0, padding=(kernel_size - 1) // 2 if dilation == 1 else 0,
dilation=dilation, dilation=dilation,
groups=groups, groups=groups,
act=None, weight_attr=ParamAttr(name=name + "_weights"),
param_attr=ParamAttr(name=name + "_weights"),
bias_attr=False) bias_attr=False)
if name == "conv1": if name == "conv1":
bn_name = "bn_" + name bn_name = "bn_" + name
else: else:
bn_name = "bn" + name[3:] bn_name = "bn" + name[3:]
self._batch_norm = BatchNorm( self._batch_norm = BatchNorm(
num_filters, out_channels,
weight_attr=ParamAttr(name=bn_name + '_scale'), weight_attr=ParamAttr(name=bn_name + '_scale'),
bias_attr=ParamAttr(bn_name + '_offset')) bias_attr=ParamAttr(bn_name + '_offset'))
self._act_op = layer_utils.Activation(act=act) self._act_op = layer_utils.Activation(act=act)
...@@ -91,8 +92,8 @@ class ConvBNLayer(fluid.dygraph.Layer): ...@@ -91,8 +92,8 @@ class ConvBNLayer(fluid.dygraph.Layer):
class BottleneckBlock(fluid.dygraph.Layer): class BottleneckBlock(fluid.dygraph.Layer):
def __init__(self, def __init__(self,
num_channels, in_channels,
num_filters, out_channels,
stride, stride,
shortcut=True, shortcut=True,
if_first=False, if_first=False,
...@@ -101,34 +102,34 @@ class BottleneckBlock(fluid.dygraph.Layer): ...@@ -101,34 +102,34 @@ class BottleneckBlock(fluid.dygraph.Layer):
super(BottleneckBlock, self).__init__() super(BottleneckBlock, self).__init__()
self.conv0 = ConvBNLayer( self.conv0 = ConvBNLayer(
num_channels=num_channels, in_channels=in_channels,
num_filters=num_filters, out_channels=out_channels,
filter_size=1, kernel_size=1,
act='relu', act='relu',
name=name + "_branch2a") name=name + "_branch2a")
self.dilation = dilation self.dilation = dilation
self.conv1 = ConvBNLayer( self.conv1 = ConvBNLayer(
num_channels=num_filters, in_channels=out_channels,
num_filters=num_filters, out_channels=out_channels,
filter_size=3, kernel_size=3,
stride=stride, stride=stride,
act='relu', act='relu',
dilation=dilation, dilation=dilation,
name=name + "_branch2b") name=name + "_branch2b")
self.conv2 = ConvBNLayer( self.conv2 = ConvBNLayer(
num_channels=num_filters, in_channels=out_channels,
num_filters=num_filters * 4, out_channels=out_channels * 4,
filter_size=1, kernel_size=1,
act=None, act=None,
name=name + "_branch2c") name=name + "_branch2c")
if not shortcut: if not shortcut:
self.short = ConvBNLayer( self.short = ConvBNLayer(
num_channels=num_channels, in_channels=in_channels,
num_filters=num_filters * 4, out_channels=out_channels * 4,
filter_size=1, kernel_size=1,
stride=1, stride=1,
is_vd_mode=False if if_first or stride == 1 else True, is_vd_mode=False if if_first or stride == 1 else True,
name=name + "_branch1") name=name + "_branch1")
...@@ -160,8 +161,8 @@ class BottleneckBlock(fluid.dygraph.Layer): ...@@ -160,8 +161,8 @@ class BottleneckBlock(fluid.dygraph.Layer):
class BasicBlock(fluid.dygraph.Layer): class BasicBlock(fluid.dygraph.Layer):
def __init__(self, def __init__(self,
num_channels, in_channels,
num_filters, out_channels,
stride, stride,
shortcut=True, shortcut=True,
if_first=False, if_first=False,
...@@ -169,24 +170,24 @@ class BasicBlock(fluid.dygraph.Layer): ...@@ -169,24 +170,24 @@ class BasicBlock(fluid.dygraph.Layer):
super(BasicBlock, self).__init__() super(BasicBlock, self).__init__()
self.stride = stride self.stride = stride
self.conv0 = ConvBNLayer( self.conv0 = ConvBNLayer(
num_channels=num_channels, in_channels=in_channels,
num_filters=num_filters, out_channels=out_channels,
filter_size=3, kernel_size=3,
stride=stride, stride=stride,
act='relu', act='relu',
name=name + "_branch2a") name=name + "_branch2a")
self.conv1 = ConvBNLayer( self.conv1 = ConvBNLayer(
num_channels=num_filters, in_channels=out_channels,
num_filters=num_filters, out_channels=out_channels,
filter_size=3, kernel_size=3,
act=None, act=None,
name=name + "_branch2b") name=name + "_branch2b")
if not shortcut: if not shortcut:
self.short = ConvBNLayer( self.short = ConvBNLayer(
num_channels=num_channels, in_channels=in_channels,
num_filters=num_filters, out_channels=out_channels,
filter_size=1, kernel_size=1,
stride=1, stride=1,
is_vd_mode=False if if_first else True, is_vd_mode=False if if_first else True,
name=name + "_branch1") name=name + "_branch1")
...@@ -243,23 +244,23 @@ class ResNet_vd(fluid.dygraph.Layer): ...@@ -243,23 +244,23 @@ class ResNet_vd(fluid.dygraph.Layer):
dilation_dict = {3: 2} dilation_dict = {3: 2}
self.conv1_1 = ConvBNLayer( self.conv1_1 = ConvBNLayer(
num_channels=3, in_channels=3,
num_filters=32, out_channels=32,
filter_size=3, kernel_size=3,
stride=2, stride=2,
act='relu', act='relu',
name="conv1_1") name="conv1_1")
self.conv1_2 = ConvBNLayer( self.conv1_2 = ConvBNLayer(
num_channels=32, in_channels=32,
num_filters=32, out_channels=32,
filter_size=3, kernel_size=3,
stride=1, stride=1,
act='relu', act='relu',
name="conv1_2") name="conv1_2")
self.conv1_3 = ConvBNLayer( self.conv1_3 = ConvBNLayer(
num_channels=32, in_channels=32,
num_filters=64, out_channels=64,
filter_size=3, kernel_size=3,
stride=1, stride=1,
act='relu', act='relu',
name="conv1_3") name="conv1_3")
...@@ -296,9 +297,9 @@ class ResNet_vd(fluid.dygraph.Layer): ...@@ -296,9 +297,9 @@ class ResNet_vd(fluid.dygraph.Layer):
bottleneck_block = self.add_sublayer( bottleneck_block = self.add_sublayer(
'bb_%d_%d' % (block, i), 'bb_%d_%d' % (block, i),
BottleneckBlock( BottleneckBlock(
num_channels=num_channels[block] in_channels=num_channels[block]
if i == 0 else num_filters[block] * 4, if i == 0 else num_filters[block] * 4,
num_filters=num_filters[block], out_channels=num_filters[block],
stride=2 if i == 0 and block != 0 stride=2 if i == 0 and block != 0
and dilation_rate == 1 else 1, and dilation_rate == 1 else 1,
shortcut=shortcut, shortcut=shortcut,
...@@ -318,9 +319,9 @@ class ResNet_vd(fluid.dygraph.Layer): ...@@ -318,9 +319,9 @@ class ResNet_vd(fluid.dygraph.Layer):
basic_block = self.add_sublayer( basic_block = self.add_sublayer(
'bb_%d_%d' % (block, i), 'bb_%d_%d' % (block, i),
BasicBlock( BasicBlock(
num_channels=num_channels[block] in_channels=num_channels[block]
if i == 0 else num_filters[block], if i == 0 else num_filters[block],
num_filters=num_filters[block], out_channels=num_filters[block],
stride=2 if i == 0 and block != 0 else 1, stride=2 if i == 0 and block != 0 else 1,
shortcut=shortcut, shortcut=shortcut,
if_first=block == i == 0, if_first=block == i == 0,
......
...@@ -16,10 +16,12 @@ import math ...@@ -16,10 +16,12 @@ import math
import os import os
import paddle import paddle
from paddle.nn import Conv2d
import paddle.fluid as fluid import paddle.fluid as fluid
from paddle.fluid.param_attr import ParamAttr from paddle.fluid.param_attr import ParamAttr
from paddle.fluid.layer_helper import LayerHelper from paddle.fluid.layer_helper import LayerHelper
from paddle.fluid.dygraph.nn import Conv2D, Pool2D, Linear from paddle.fluid.dygraph.nn import Pool2D, Linear
from paddle.fluid.initializer import Normal from paddle.fluid.initializer import Normal
from paddle.nn import SyncBatchNorm as BatchNorm from paddle.nn import SyncBatchNorm as BatchNorm
...@@ -27,6 +29,7 @@ from paddleseg.cvlibs import manager ...@@ -27,6 +29,7 @@ from paddleseg.cvlibs import manager
from paddleseg import utils from paddleseg import utils
from paddleseg.cvlibs import param_init from paddleseg.cvlibs import param_init
from paddleseg.utils import logger from paddleseg.utils import logger
from paddleseg.models.common import layer_utils
__all__ = [ __all__ = [
"fcn_hrnet_w18_small_v1", "fcn_hrnet_w18_small_v2", "fcn_hrnet_w18", "fcn_hrnet_w18_small_v1", "fcn_hrnet_w18_small_v2", "fcn_hrnet_w18",
...@@ -74,14 +77,14 @@ class FCN(fluid.dygraph.Layer): ...@@ -74,14 +77,14 @@ class FCN(fluid.dygraph.Layer):
self.backbone = backbone self.backbone = backbone
self.conv_last_2 = ConvBNLayer( self.conv_last_2 = ConvBNLayer(
num_channels=backbone_channels[0], in_channels=backbone_channels[0],
num_filters=channels, out_channels=channels,
filter_size=1, kernel_size=1,
stride=1) stride=1)
self.conv_last_1 = Conv2D( self.conv_last_1 = Conv2d(
num_channels=channels, in_channels=channels,
num_filters=self.num_classes, out_channels=self.num_classes,
filter_size=1, kernel_size=1,
stride=1, stride=1,
padding=0) padding=0)
if self.training: if self.training:
...@@ -127,30 +130,29 @@ class FCN(fluid.dygraph.Layer): ...@@ -127,30 +130,29 @@ class FCN(fluid.dygraph.Layer):
class ConvBNLayer(fluid.dygraph.Layer): class ConvBNLayer(fluid.dygraph.Layer):
def __init__(self, def __init__(self,
num_channels, in_channels,
num_filters, out_channels,
filter_size, kernel_size,
stride=1, stride=1,
groups=1, groups=1,
act="relu"): act="relu"):
super(ConvBNLayer, self).__init__() super(ConvBNLayer, self).__init__()
self._conv = Conv2D( self._conv = Conv2d(
num_channels=num_channels, in_channels=in_channels,
num_filters=num_filters, out_channels=out_channels,
filter_size=filter_size, kernel_size=kernel_size,
stride=stride, stride=stride,
padding=(filter_size - 1) // 2, padding=(kernel_size - 1) // 2,
groups=groups, groups=groups,
bias_attr=False) bias_attr=False)
self._batch_norm = BatchNorm(num_filters) self._batch_norm = BatchNorm(out_channels)
self.act = act self.act = layer_utils.Activation(act=act)
def forward(self, input): def forward(self, input):
y = self._conv(input) y = self._conv(input)
y = self._batch_norm(y) y = self._batch_norm(y)
if self.act == 'relu': y = self.act(y)
y = fluid.layers.relu(y)
return y return y
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册