提交 c6064306 编写于 作者: C chenguowei01

update fluid to beta

上级 04fa1750
...@@ -21,13 +21,11 @@ import math ...@@ -21,13 +21,11 @@ import math
import numpy as np import numpy as np
import paddle import paddle
import paddle.nn as nn
import paddle.nn.functional as F
from paddle.nn import SyncBatchNorm as BatchNorm from paddle.nn import SyncBatchNorm as BatchNorm
from paddle.nn import Conv2d from paddle.nn import Conv2d, Linear, Dropout
from paddle.nn import AdaptiveAvgPool2d, MaxPool2d, AvgPool2d
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 Pool2D, Linear, Dropout
from paddleseg.utils import utils from paddleseg.utils import utils
from paddleseg.models.common import layer_utils from paddleseg.models.common import layer_utils
...@@ -38,7 +36,7 @@ __all__ = [ ...@@ -38,7 +36,7 @@ __all__ = [
] ]
class ConvBNLayer(fluid.dygraph.Layer): class ConvBNLayer(nn.Layer):
def __init__( def __init__(
self, self,
in_channels, in_channels,
...@@ -54,12 +52,8 @@ class ConvBNLayer(fluid.dygraph.Layer): ...@@ -54,12 +52,8 @@ class ConvBNLayer(fluid.dygraph.Layer):
super(ConvBNLayer, self).__init__() super(ConvBNLayer, self).__init__()
self.is_vd_mode = is_vd_mode self.is_vd_mode = is_vd_mode
self._pool2d_avg = Pool2D( self._pool2d_avg = AvgPool2d(
pool_size=2, kernel_size=2, stride=2, padding=0, ceil_mode=True)
pool_stride=2,
pool_padding=0,
pool_type='avg',
ceil_mode=True)
self._conv = Conv2d( self._conv = Conv2d(
in_channels=in_channels, in_channels=in_channels,
out_channels=out_channels, out_channels=out_channels,
...@@ -68,16 +62,12 @@ class ConvBNLayer(fluid.dygraph.Layer): ...@@ -68,16 +62,12 @@ class ConvBNLayer(fluid.dygraph.Layer):
padding=(kernel_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,
weight_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(out_channels)
out_channels,
weight_attr=ParamAttr(name=bn_name + '_scale'),
bias_attr=ParamAttr(bn_name + '_offset'))
self._act_op = layer_utils.Activation(act=act) self._act_op = layer_utils.Activation(act=act)
def forward(self, inputs): def forward(self, inputs):
...@@ -90,7 +80,7 @@ class ConvBNLayer(fluid.dygraph.Layer): ...@@ -90,7 +80,7 @@ class ConvBNLayer(fluid.dygraph.Layer):
return y return y
class BottleneckBlock(fluid.dygraph.Layer): class BottleneckBlock(nn.Layer):
def __init__(self, def __init__(self,
in_channels, in_channels,
out_channels, out_channels,
...@@ -143,8 +133,7 @@ class BottleneckBlock(fluid.dygraph.Layer): ...@@ -143,8 +133,7 @@ class BottleneckBlock(fluid.dygraph.Layer):
# If given dilation rate > 1, using corresponding padding # If given dilation rate > 1, using corresponding padding
if self.dilation > 1: if self.dilation > 1:
padding = self.dilation padding = self.dilation
y = fluid.layers.pad( y = F.pad(y, [0, 0, 0, 0, padding, padding, padding, padding])
y, [0, 0, 0, 0, padding, padding, padding, padding])
##################################################################### #####################################################################
conv1 = self.conv1(y) conv1 = self.conv1(y)
conv2 = self.conv2(conv1) conv2 = self.conv2(conv1)
...@@ -154,12 +143,11 @@ class BottleneckBlock(fluid.dygraph.Layer): ...@@ -154,12 +143,11 @@ class BottleneckBlock(fluid.dygraph.Layer):
else: else:
short = self.short(inputs) short = self.short(inputs)
y = fluid.layers.elementwise_add(x=short, y=conv2) y = paddle.elementwise_add(x=short, y=conv2, act='relu')
layer_helper = LayerHelper(self.full_name(), act='relu') return y
return layer_helper.append_activation(y)
class BasicBlock(fluid.dygraph.Layer): class BasicBlock(nn.Layer):
def __init__(self, def __init__(self,
in_channels, in_channels,
out_channels, out_channels,
...@@ -202,13 +190,12 @@ class BasicBlock(fluid.dygraph.Layer): ...@@ -202,13 +190,12 @@ class BasicBlock(fluid.dygraph.Layer):
short = inputs short = inputs
else: else:
short = self.short(inputs) short = self.short(inputs)
y = fluid.layers.elementwise_add(x=short, y=conv1) y = paddle.elementwise_add(x=short, y=conv1, act='relu')
layer_helper = LayerHelper(self.full_name(), act='relu') return y
return layer_helper.append_activation(y)
class ResNet_vd(fluid.dygraph.Layer): class ResNet_vd(nn.Layer):
def __init__(self, def __init__(self,
backbone_pretrained=None, backbone_pretrained=None,
layers=50, layers=50,
...@@ -264,8 +251,7 @@ class ResNet_vd(fluid.dygraph.Layer): ...@@ -264,8 +251,7 @@ class ResNet_vd(fluid.dygraph.Layer):
stride=1, stride=1,
act='relu', act='relu',
name="conv1_3") name="conv1_3")
self.pool2d_max = Pool2D( self.pool2d_max = MaxPool2d(kernel_size=3, stride=2, padding=1)
pool_size=3, pool_stride=2, pool_padding=1, pool_type='max')
# self.block_list = [] # self.block_list = []
self.stage_list = [] self.stage_list = []
...@@ -330,23 +316,6 @@ class ResNet_vd(fluid.dygraph.Layer): ...@@ -330,23 +316,6 @@ class ResNet_vd(fluid.dygraph.Layer):
shortcut = True shortcut = True
self.stage_list.append(block_list) self.stage_list.append(block_list)
self.pool2d_avg = Pool2D(
pool_size=7, pool_type='avg', global_pooling=True)
self.pool2d_avg_channels = num_channels[-1] * 2
stdv = 1.0 / math.sqrt(self.pool2d_avg_channels * 1.0)
self.out = Linear(
self.pool2d_avg_channels,
class_dim,
param_attr=ParamAttr(
initializer=fluid.initializer.Uniform(-stdv, stdv),
name="fc_0.w_0"),
bias_attr=ParamAttr(name="fc_0.b_0"))
self.init_weight(backbone_pretrained)
def forward(self, inputs): def forward(self, inputs):
y = self.conv1_1(inputs) y = self.conv1_1(inputs)
y = self.conv1_2(y) y = self.conv1_2(y)
...@@ -358,33 +327,10 @@ class ResNet_vd(fluid.dygraph.Layer): ...@@ -358,33 +327,10 @@ class ResNet_vd(fluid.dygraph.Layer):
for i, stage in enumerate(self.stage_list): for i, stage in enumerate(self.stage_list):
for j, block in enumerate(stage): for j, block in enumerate(stage):
y = block(y) y = block(y)
#print("stage {} block {}".format(i+1, j+1), y.shape)
feat_list.append(y) feat_list.append(y)
y = self.pool2d_avg(y)
y = fluid.layers.reshape(y, shape=[-1, self.pool2d_avg_channels])
y = self.out(y)
return feat_list return feat_list
# def init_weight(self, pretrained_model=None):
# if pretrained_model is not None:
# if os.path.exists(pretrained_model):
# utils.load_pretrained_model(self, pretrained_model)
def init_weight(self, pretrained_model=None):
"""
Initialize the parameters of model parts.
Args:
pretrained_model ([str], optional): the path of pretrained model. Defaults to None.
"""
if pretrained_model is not None:
if os.path.exists(pretrained_model):
utils.load_pretrained_model(self, pretrained_model)
else:
raise Exception('Pretrained model is not found: {}'.format(
pretrained_model))
@manager.BACKBONES.add_component @manager.BACKBONES.add_component
def ResNet18_vd(**args): def ResNet18_vd(**args):
......
...@@ -16,13 +16,9 @@ import math ...@@ -16,13 +16,9 @@ import math
import os import os
import paddle import paddle
import paddle.nn as nn
import paddle.nn.functional as F
from paddle.nn import Conv2d from paddle.nn import Conv2d
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 Pool2D, Linear
from paddle.fluid.initializer import Normal
from paddle.nn import SyncBatchNorm as BatchNorm from paddle.nn import SyncBatchNorm as BatchNorm
from paddleseg.cvlibs import manager from paddleseg.cvlibs import manager
...@@ -39,7 +35,7 @@ __all__ = [ ...@@ -39,7 +35,7 @@ __all__ = [
@manager.MODELS.add_component @manager.MODELS.add_component
class FCN(fluid.dygraph.Layer): class FCN(nn.Layer):
""" """
Fully Convolutional Networks for Semantic Segmentation. Fully Convolutional Networks for Semantic Segmentation.
https://arxiv.org/abs/1411.4038 https://arxiv.org/abs/1411.4038
...@@ -96,7 +92,7 @@ class FCN(fluid.dygraph.Layer): ...@@ -96,7 +92,7 @@ class FCN(fluid.dygraph.Layer):
x = fea_list[self.backbone_indices[0]] x = fea_list[self.backbone_indices[0]]
x = self.conv_last_2(x) x = self.conv_last_2(x)
logit = self.conv_last_1(x) logit = self.conv_last_1(x)
logit = fluid.layers.resize_bilinear(logit, input_shape) logit = F.resize_bilinear(logit, input_shape)
return [logit] return [logit]
def init_weight(self): def init_weight(self):
...@@ -128,7 +124,7 @@ class FCN(fluid.dygraph.Layer): ...@@ -128,7 +124,7 @@ class FCN(fluid.dygraph.Layer):
logger.warning('No pretrained model to load, train from scratch') logger.warning('No pretrained model to load, train from scratch')
class ConvBNLayer(fluid.dygraph.Layer): class ConvBNLayer(nn.Layer):
def __init__(self, def __init__(self,
in_channels, in_channels,
out_channels, out_channels,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册