提交 428b4ef3 编写于 作者: R Renwb1991 提交者: qingqing01

caffe2fluid:add power layer and fix bugs in priorbox and pool layers (#1508)

上级 5c9deb5e
...@@ -15,6 +15,7 @@ import detection_out ...@@ -15,6 +15,7 @@ import detection_out
import normalize import normalize
import select import select
import crop import crop
import power
import reduction import reduction
#custom layer import ends #custom layer import ends
......
""" a custom layer for 'power', maybe we should implement this in standard way.
more info can be found here: http://caffe.berkeleyvision.org/tutorial/layers/power.html
"""
from .register import register
def power_shape(input_shape, shape=None):
""" calculate the output shape of this layer using input shape
Args:
@input_shape (list of num): a list of number which represents the input shape
Returns:
@output_shape (list of num): a list of numbers represent the output shape
"""
return input_shape
def power_layer(input, name, power=1.0, scale=1.0, shift=0.0):
""" build a layer of type 'Power' using fluid
Args:
@input (variables): input fluid variable for this layer
@name (str): name for this layer
@power (float): parameter from caffe's Power layer
@scale (float): parameter from caffe's Power layer
@shift (float): parameter from caffe's Power layer
Returns:
output (variable): output variable for this layer
"""
import paddle.fluid as fluid
scale_out = fluid.layers.scale(
input, scale=scale, bias=shift, bias_after_scale=True)
output = fluid.layers.pow(scale_out, factor=power)
return output
register(kind='Power', shape=power_shape, layer=power_layer)
...@@ -31,12 +31,12 @@ def priorbox_shape(input_shapes, min_size, max_size=None, aspect_ratio=None): ...@@ -31,12 +31,12 @@ def priorbox_shape(input_shapes, min_size, max_size=None, aspect_ratio=None):
def priorbox_layer(inputs, def priorbox_layer(inputs,
name, name,
min_size, min_size,
step,
max_size=None, max_size=None,
aspect_ratio=None, aspect_ratio=None,
flip=True, variance=[0.1, 0.1, 0.2, 0.2],
flip=False,
clip=False, clip=False,
variance=[], step=0.0,
offset=0.5): offset=0.5):
""" build a layer of type 'Priorbox' using fluid """ build a layer of type 'Priorbox' using fluid
...@@ -52,6 +52,8 @@ def priorbox_layer(inputs, ...@@ -52,6 +52,8 @@ def priorbox_layer(inputs,
assert len(inputs) == 2, "invalid inputs for Priorbox[%s]" % (name) assert len(inputs) == 2, "invalid inputs for Priorbox[%s]" % (name)
input = inputs[0] input = inputs[0]
image = inputs[1] image = inputs[1]
steps = tuple(step) if type(step) is list or type(step) is tuple else (step,
step)
box, variance_ = fluid.layers.prior_box( box, variance_ = fluid.layers.prior_box(
input, input,
image, image,
...@@ -60,7 +62,8 @@ def priorbox_layer(inputs, ...@@ -60,7 +62,8 @@ def priorbox_layer(inputs,
aspect_ratio, aspect_ratio,
variance, variance,
flip, flip,
clip, (step, step), clip,
steps,
offset, offset,
min_max_aspect_ratios_order=True) min_max_aspect_ratios_order=True)
""" """
......
...@@ -38,7 +38,7 @@ LAYER_DESCRIPTORS = { ...@@ -38,7 +38,7 @@ LAYER_DESCRIPTORS = {
'MultinomialLogisticLoss': shape_scalar, 'MultinomialLogisticLoss': shape_scalar,
'MVN': shape_not_implemented, 'MVN': shape_not_implemented,
'Pooling': shape_pool, 'Pooling': shape_pool,
'Power': shape_identity, 'Power': shape_power,
'ReLU': shape_identity, 'ReLU': shape_identity,
'PReLU': shape_identity, 'PReLU': shape_identity,
'Scale': shape_identity, 'Scale': shape_identity,
......
...@@ -280,8 +280,17 @@ class Network(object): ...@@ -280,8 +280,17 @@ class Network(object):
param_attr=fluid.ParamAttr(name=prefix + 'negslope')) param_attr=fluid.ParamAttr(name=prefix + 'negslope'))
return output return output
def pool(self, pool_type, input, k_h, k_w, s_h, s_w, ceil_mode, padding, def pool(self,
name): pool_type,
input,
k_h,
k_w,
s_h,
s_w,
ceil_mode,
padding,
name,
exclusive=True):
# Get the number of channels in the input # Get the number of channels in the input
in_hw = input.shape[2:] in_hw = input.shape[2:]
k_hw = [k_h, k_w] k_hw = [k_h, k_w]
...@@ -295,7 +304,8 @@ class Network(object): ...@@ -295,7 +304,8 @@ class Network(object):
pool_stride=s_hw, pool_stride=s_hw,
pool_padding=padding, pool_padding=padding,
ceil_mode=ceil_mode, ceil_mode=ceil_mode,
pool_type=pool_type) pool_type=pool_type,
exclusive=exclusive)
return output return output
@layer @layer
......
...@@ -67,6 +67,10 @@ def shape_crop(node): ...@@ -67,6 +67,10 @@ def shape_crop(node):
raise KaffeError('crop function had been defined in customer_layers') raise KaffeError('crop function had been defined in customer_layers')
def shape_power(node):
raise KaffeError('power function had been defined in customer_layers')
def shape_data(node): def shape_data(node):
if node.output_shape: if node.output_shape:
# Old-style input specification # Old-style input specification
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册