提交 2bdf8a9b 编写于 作者: littletomatodonkey's avatar littletomatodonkey

fix hrnet

上级 d2970094
...@@ -18,9 +18,11 @@ from __future__ import print_function ...@@ -18,9 +18,11 @@ 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 import paddle.nn.functional as F
from paddle.nn import Conv2d, Pool2D, BatchNorm, Linear
from paddle.nn.initializer import Uniform
import math import math
...@@ -44,7 +46,7 @@ __all__ = [ ...@@ -44,7 +46,7 @@ __all__ = [
] ]
class ConvBNLayer(fluid.dygraph.Layer): class ConvBNLayer(nn.Layer):
def __init__(self, def __init__(self,
num_channels, num_channels,
num_filters, num_filters,
...@@ -55,15 +57,14 @@ class ConvBNLayer(fluid.dygraph.Layer): ...@@ -55,15 +57,14 @@ class ConvBNLayer(fluid.dygraph.Layer):
name=None): name=None):
super(ConvBNLayer, self).__init__() super(ConvBNLayer, self).__init__()
self._conv = Conv2D( self._conv = Conv2d(
num_channels=num_channels, in_channels=num_channels,
num_filters=num_filters, out_channels=num_filters,
filter_size=filter_size, kernel_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)
bn_name = name + '_bn' bn_name = name + '_bn'
self._batch_norm = BatchNorm( self._batch_norm = BatchNorm(
...@@ -80,7 +81,7 @@ class ConvBNLayer(fluid.dygraph.Layer): ...@@ -80,7 +81,7 @@ class ConvBNLayer(fluid.dygraph.Layer):
return y return y
class Layer1(fluid.dygraph.Layer): class Layer1(nn.Layer):
def __init__(self, num_channels, has_se=False, name=None): def __init__(self, num_channels, has_se=False, name=None):
super(Layer1, self).__init__() super(Layer1, self).__init__()
...@@ -105,7 +106,7 @@ class Layer1(fluid.dygraph.Layer): ...@@ -105,7 +106,7 @@ class Layer1(fluid.dygraph.Layer):
return conv return conv
class TransitionLayer(fluid.dygraph.Layer): class TransitionLayer(nn.Layer):
def __init__(self, in_channels, out_channels, name=None): def __init__(self, in_channels, out_channels, name=None):
super(TransitionLayer, self).__init__() super(TransitionLayer, self).__init__()
...@@ -148,7 +149,7 @@ class TransitionLayer(fluid.dygraph.Layer): ...@@ -148,7 +149,7 @@ class TransitionLayer(fluid.dygraph.Layer):
return outs return outs
class Branches(fluid.dygraph.Layer): class Branches(nn.Layer):
def __init__(self, def __init__(self,
block_num, block_num,
in_channels, in_channels,
...@@ -183,7 +184,7 @@ class Branches(fluid.dygraph.Layer): ...@@ -183,7 +184,7 @@ class Branches(fluid.dygraph.Layer):
return outs return outs
class BottleneckBlock(fluid.dygraph.Layer): class BottleneckBlock(nn.Layer):
def __init__(self, def __init__(self,
num_channels, num_channels,
num_filters, num_filters,
...@@ -243,11 +244,11 @@ class BottleneckBlock(fluid.dygraph.Layer): ...@@ -243,11 +244,11 @@ class BottleneckBlock(fluid.dygraph.Layer):
if self.has_se: if self.has_se:
conv3 = self.se(conv3) conv3 = self.se(conv3)
y = fluid.layers.elementwise_add(x=conv3, y=residual, act="relu") y = paddle.elementwise_add(x=conv3, y=residual, 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,
...@@ -301,11 +302,11 @@ class BasicBlock(fluid.dygraph.Layer): ...@@ -301,11 +302,11 @@ class BasicBlock(fluid.dygraph.Layer):
if self.has_se: if self.has_se:
conv2 = self.se(conv2) conv2 = self.se(conv2)
y = fluid.layers.elementwise_add(x=conv2, y=residual, act="relu") y = paddle.elementwise_add(x=conv2, y=residual, 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__()
...@@ -320,8 +321,7 @@ class SELayer(fluid.dygraph.Layer): ...@@ -320,8 +321,7 @@ class SELayer(fluid.dygraph.Layer):
med_ch, med_ch,
act="relu", act="relu",
param_attr=ParamAttr( param_attr=ParamAttr(
initializer=fluid.initializer.Uniform(-stdv, stdv), initializer=Uniform(-stdv, stdv), name=name + "_sqz_weights"),
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)
...@@ -330,22 +330,21 @@ class SELayer(fluid.dygraph.Layer): ...@@ -330,22 +330,21 @@ class SELayer(fluid.dygraph.Layer):
num_filters, num_filters,
act="sigmoid", act="sigmoid",
param_attr=ParamAttr( param_attr=ParamAttr(
initializer=fluid.initializer.Uniform(-stdv, stdv), initializer=Uniform(-stdv, stdv), name=name + "_exc_weights"),
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)
excitation = self.excitation(squeeze) excitation = self.excitation(squeeze)
excitation = fluid.layers.reshape( 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 Stage(fluid.dygraph.Layer): class Stage(nn.Layer):
def __init__(self, def __init__(self,
num_channels, num_channels,
num_modules, num_modules,
...@@ -386,7 +385,7 @@ class Stage(fluid.dygraph.Layer): ...@@ -386,7 +385,7 @@ class Stage(fluid.dygraph.Layer):
return out return out
class HighResolutionModule(fluid.dygraph.Layer): class HighResolutionModule(nn.Layer):
def __init__(self, def __init__(self,
num_channels, num_channels,
num_filters, num_filters,
...@@ -414,7 +413,7 @@ class HighResolutionModule(fluid.dygraph.Layer): ...@@ -414,7 +413,7 @@ class HighResolutionModule(fluid.dygraph.Layer):
return out return out
class FuseLayers(fluid.dygraph.Layer): class FuseLayers(nn.Layer):
def __init__(self, def __init__(self,
in_channels, in_channels,
out_channels, out_channels,
...@@ -482,8 +481,8 @@ class FuseLayers(fluid.dygraph.Layer): ...@@ -482,8 +481,8 @@ class FuseLayers(fluid.dygraph.Layer):
y = self.residual_func_list[residual_func_idx](input[j]) y = self.residual_func_list[residual_func_idx](input[j])
residual_func_idx += 1 residual_func_idx += 1
y = fluid.layers.resize_nearest(input=y, scale=2**(j - i)) y = F.resize_nearest(input=y, scale=2**(j - i))
residual = fluid.layers.elementwise_add( residual = paddle.elementwise_add(
x=residual, y=y, act=None) x=residual, y=y, act=None)
elif j < i: elif j < i:
y = input[j] y = input[j]
...@@ -491,16 +490,16 @@ class FuseLayers(fluid.dygraph.Layer): ...@@ -491,16 +490,16 @@ class FuseLayers(fluid.dygraph.Layer):
y = self.residual_func_list[residual_func_idx](y) y = self.residual_func_list[residual_func_idx](y)
residual_func_idx += 1 residual_func_idx += 1
residual = fluid.layers.elementwise_add( residual = paddle.elementwise_add(
x=residual, y=y, act=None) x=residual, y=y, act=None)
residual = fluid.layers.relu(residual) residual = F.relu(residual)
outs.append(residual) outs.append(residual)
return outs return outs
class LastClsOut(fluid.dygraph.Layer): class LastClsOut(nn.Layer):
def __init__(self, def __init__(self,
num_channel_list, num_channel_list,
has_se, has_se,
...@@ -528,7 +527,7 @@ class LastClsOut(fluid.dygraph.Layer): ...@@ -528,7 +527,7 @@ class LastClsOut(fluid.dygraph.Layer):
return outs return outs
class HRNet(fluid.dygraph.Layer): class HRNet(nn.Layer):
def __init__(self, width=18, has_se=False, class_dim=1000): def __init__(self, width=18, has_se=False, class_dim=1000):
super(HRNet, self).__init__() super(HRNet, self).__init__()
...@@ -630,9 +629,8 @@ class HRNet(fluid.dygraph.Layer): ...@@ -630,9 +629,8 @@ class HRNet(fluid.dygraph.Layer):
self.out = Linear( self.out = Linear(
2048, 2048,
class_dim, class_dim,
param_attr=ParamAttr( weight_attr=ParamAttr(
initializer=fluid.initializer.Uniform(-stdv, stdv), initializer=Uniform(-stdv, stdv), name="fc_weights"),
name="fc_weights"),
bias_attr=ParamAttr(name="fc_offset")) bias_attr=ParamAttr(name="fc_offset"))
def forward(self, input): def forward(self, input):
...@@ -658,7 +656,7 @@ class HRNet(fluid.dygraph.Layer): ...@@ -658,7 +656,7 @@ class HRNet(fluid.dygraph.Layer):
y = self.conv_last(y) y = self.conv_last(y)
y = self.pool2d_avg(y) y = self.pool2d_avg(y)
y = fluid.layers.reshape(y, shape=[0, -1]) y = paddle.reshape(y, shape=[0, -1])
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.
先完成此消息的编辑!
想要评论请 注册