提交 251e47c1 编写于 作者: littletomatodonkey's avatar littletomatodonkey

fix resnext_wsl

上级 43a89a95
...@@ -34,5 +34,6 @@ from .shufflenet_v2 import ShuffleNetV2_x0_25, ShuffleNetV2_x0_33, ShuffleNetV2_ ...@@ -34,5 +34,6 @@ from .shufflenet_v2 import ShuffleNetV2_x0_25, ShuffleNetV2_x0_33, ShuffleNetV2_
from .alexnet import AlexNet from .alexnet import AlexNet
from .inception_v4 import InceptionV4 from .inception_v4 import InceptionV4
from .xception_deeplab import Xception41_deeplab, Xception65_deeplab, Xception71_deeplab from .xception_deeplab import Xception41_deeplab, Xception65_deeplab, Xception71_deeplab
from .resnext101_wsl import ResNeXt101_32x8d_wsl, ResNeXt101_32x16d_wsl, ResNeXt101_32x32d_wsl, ResNeXt101_32x48d_wsl
from .distillation_models import ResNet50_vd_distill_MobileNetV3_large_x1_0 from .distillation_models import ResNet50_vd_distill_MobileNetV3_large_x1_0
...@@ -202,8 +202,7 @@ class Res2Net_vd(nn.Layer): ...@@ -202,8 +202,7 @@ class Res2Net_vd(nn.Layer):
stride=1, stride=1,
act='relu', act='relu',
name="conv1_3") name="conv1_3")
self.pool2d_max = MaxPool2d( self.pool2d_max = MaxPool2d(kernel_size=3, stride=2, padding=1)
kernel_size=3, stride=2, padding=1, ceil_mode=True)
self.block_list = [] self.block_list = []
for block in range(len(depth)): for block in range(len(depth)):
......
...@@ -17,9 +17,12 @@ from __future__ import print_function ...@@ -17,9 +17,12 @@ from __future__ import print_function
import numpy as np import numpy as np
import paddle import paddle
import paddle.fluid as fluid from paddle import ParamAttr
from paddle.fluid.param_attr import ParamAttr import paddle.nn as nn
from paddle.fluid.dygraph.nn import Conv2D, Pool2D, BatchNorm, Linear, Dropout import paddle.nn.functional as F
from paddle.nn import Conv2d, BatchNorm, Linear, Dropout
from paddle.nn import AdaptiveAvgPool2d, MaxPool2d, AvgPool2d
from paddle.nn.initializer import Uniform
import math import math
...@@ -29,7 +32,7 @@ __all__ = [ ...@@ -29,7 +32,7 @@ __all__ = [
] ]
class ConvBNLayer(fluid.dygraph.Layer): class ConvBNLayer(nn.Layer):
def __init__( def __init__(
self, self,
num_channels, num_channels,
...@@ -43,21 +46,17 @@ class ConvBNLayer(fluid.dygraph.Layer): ...@@ -43,21 +46,17 @@ 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, self._conv = Conv2d(
pool_type='avg', in_channels=num_channels,
ceil_mode=True) out_channels=num_filters,
self._conv = Conv2D( kernel_size=filter_size,
num_channels=num_channels,
num_filters=num_filters,
filter_size=filter_size,
stride=stride, stride=stride,
padding=(filter_size - 1) // 2, padding=(filter_size - 1) // 2,
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
...@@ -79,7 +78,7 @@ class ConvBNLayer(fluid.dygraph.Layer): ...@@ -79,7 +78,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,
num_channels, num_channels,
num_filters, num_filters,
...@@ -136,11 +135,11 @@ class BottleneckBlock(fluid.dygraph.Layer): ...@@ -136,11 +135,11 @@ class BottleneckBlock(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=scale, act='relu') y = paddle.elementwise_add(x=short, y=scale, act='relu')
return y return y
class BasicBlock(fluid.dygraph.Layer): class BasicBlock(nn.Layer):
def __init__(self, def __init__(self,
num_channels, num_channels,
num_filters, num_filters,
...@@ -191,15 +190,15 @@ class BasicBlock(fluid.dygraph.Layer): ...@@ -191,15 +190,15 @@ 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=scale, act='relu') y = paddle.elementwise_add(x=short, y=scale, act='relu')
return y return y
class SELayer(fluid.dygraph.Layer): class SELayer(nn.Layer):
def __init__(self, num_channels, num_filters, reduction_ratio, name=None): def __init__(self, num_channels, num_filters, reduction_ratio, name=None):
super(SELayer, self).__init__() super(SELayer, self).__init__()
self.pool2d_gap = Pool2D(pool_type='avg', global_pooling=True) self.pool2d_gap = AdaptiveAvgPool2d(1)
self._num_channels = num_channels self._num_channels = num_channels
...@@ -208,34 +207,32 @@ class SELayer(fluid.dygraph.Layer): ...@@ -208,34 +207,32 @@ class SELayer(fluid.dygraph.Layer):
self.squeeze = Linear( self.squeeze = Linear(
num_channels, num_channels,
med_ch, med_ch,
act="relu", weight_attr=ParamAttr(
param_attr=ParamAttr( initializer=Uniform(-stdv, stdv), name=name + "_sqz_weights"),
initializer=fluid.initializer.Uniform(-stdv, stdv),
name=name + "_sqz_weights"),
bias_attr=ParamAttr(name=name + '_sqz_offset')) bias_attr=ParamAttr(name=name + '_sqz_offset'))
stdv = 1.0 / math.sqrt(med_ch * 1.0) stdv = 1.0 / math.sqrt(med_ch * 1.0)
self.excitation = Linear( self.excitation = Linear(
med_ch, med_ch,
num_filters, num_filters,
act="sigmoid", weight_attr=ParamAttr(
param_attr=ParamAttr( initializer=Uniform(-stdv, stdv), name=name + "_exc_weights"),
initializer=fluid.initializer.Uniform(-stdv, stdv),
name=name + "_exc_weights"),
bias_attr=ParamAttr(name=name + '_exc_offset')) bias_attr=ParamAttr(name=name + '_exc_offset'))
def forward(self, input): def forward(self, input):
pool = self.pool2d_gap(input) pool = self.pool2d_gap(input)
pool = fluid.layers.reshape(pool, shape=[-1, self._num_channels]) pool = paddle.reshape(pool, shape=[-1, self._num_channels])
squeeze = self.squeeze(pool) squeeze = self.squeeze(pool)
squeeze = F.relu(squeeze)
excitation = self.excitation(squeeze) excitation = self.excitation(squeeze)
excitation = fluid.layers.reshape( excitation = F.sigmoid(excitation)
excitation = paddle.reshape(
excitation, shape=[-1, self._num_channels, 1, 1]) excitation, shape=[-1, self._num_channels, 1, 1])
out = input * excitation out = input * excitation
return out return out
class SE_ResNet_vd(fluid.dygraph.Layer): class SE_ResNet_vd(nn.Layer):
def __init__(self, layers=50, class_dim=1000): def __init__(self, layers=50, class_dim=1000):
super(SE_ResNet_vd, self).__init__() super(SE_ResNet_vd, self).__init__()
...@@ -280,8 +277,7 @@ class SE_ResNet_vd(fluid.dygraph.Layer): ...@@ -280,8 +277,7 @@ class SE_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 = []
if layers >= 50: if layers >= 50:
...@@ -325,8 +321,7 @@ class SE_ResNet_vd(fluid.dygraph.Layer): ...@@ -325,8 +321,7 @@ class SE_ResNet_vd(fluid.dygraph.Layer):
self.block_list.append(basic_block) self.block_list.append(basic_block)
shortcut = True shortcut = True
self.pool2d_avg = Pool2D( self.pool2d_avg = AdaptiveAvgPool2d(1)
pool_size=7, pool_type='avg', global_pooling=True)
self.pool2d_avg_channels = num_channels[-1] * 2 self.pool2d_avg_channels = num_channels[-1] * 2
...@@ -335,9 +330,8 @@ class SE_ResNet_vd(fluid.dygraph.Layer): ...@@ -335,9 +330,8 @@ class SE_ResNet_vd(fluid.dygraph.Layer):
self.out = Linear( self.out = Linear(
self.pool2d_avg_channels, self.pool2d_avg_channels,
class_dim, class_dim,
param_attr=ParamAttr( weight_attr=ParamAttr(
initializer=fluid.initializer.Uniform(-stdv, stdv), initializer=Uniform(-stdv, stdv), name="fc6_weights"),
name="fc6_weights"),
bias_attr=ParamAttr(name="fc6_offset")) bias_attr=ParamAttr(name="fc6_offset"))
def forward(self, inputs): def forward(self, inputs):
...@@ -348,7 +342,7 @@ class SE_ResNet_vd(fluid.dygraph.Layer): ...@@ -348,7 +342,7 @@ class SE_ResNet_vd(fluid.dygraph.Layer):
for block in self.block_list: for block in self.block_list:
y = block(y) y = block(y)
y = self.pool2d_avg(y) y = self.pool2d_avg(y)
y = fluid.layers.reshape(y, shape=[-1, self.pool2d_avg_channels]) y = paddle.reshape(y, shape=[-1, self.pool2d_avg_channels])
y = self.out(y) y = self.out(y)
return y return y
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册