Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions python/paddle/fluid/layers/nn.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
'lrn',
'pad',
'label_smooth',
'roi_pool',
]


Expand Down Expand Up @@ -3759,3 +3760,44 @@ def label_smooth(label,
outputs={"Out": smooth_label},
attrs={"epsilon": float(epsilon)})
return smooth_label


def roi_pool(input, rois, pooled_height=1, pooled_width=1, spatial_scale=1.0):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"""
RoI pooling, its purpose is to perform max pooling on inputs of nonuniform sizes to obtain fixed-size feature maps (e.g. 7*7).
The operator has three steps:
1.Dividing each region proposal into equal-sized sections with the pooled_width and pooled_height
2.Finding the largest value in each section
3.Copying these max values to the output buffer

Args:
input (Variable): The input for roi pooling.
rois (Variable): ROIs (Regions of Interest) to pool over. It should be a 2-D tensor of shape (num_rois, 5). It given as [[batch_id, x1, y1, x2, y2], ...], where batch_id is the id of the data, (x1, y1) is the top left coordinates, and (x2, y2) is the bottom right coordinates.
pooled_height (integer): The pooled output height. Default: 1
pooled_width (integer): The pooled output width. Default: 1
spatial_scale (float): Multiplicative spatial scale factor. To translate ROI coords from their input scale to the scale used when pooling. Default: 1.0

Returns:
pool_out (Variable): The output of ROIPoolOp is a 4-D tensor with shape (num_rois, channels, pooled_h, pooled_w).
argmaxes (Variable): Argmaxes corresponding to indices in input with shape (num_rois, channels, pooled_h, pooled_w).

Examples:
.. code-block:: python
# assuming we have input x_feas, rois x_rois, pooled_height ph, pooled_width pw and spatial_scale scale.
pool_out, argmaxes = fluid.layers.roi_pool(input=x_feas, rois=x_rois, pooled_height=ph, pooled_width=pw, spatial_scale=scale)
"""
helper = LayerHelper('roi_pool', **locals())
dtype = helper.input_dtype()
pool_out = helper.create_tmp_variable(dtype)
Copy link
Contributor

@qingqing01 qingqing01 Apr 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also need to create variable for Argmax.

helper.append_op(
type="pool2d",
inputs={"X": input,
"ROIs": rois},
outputs={"Out": pool_out,
"Argmax": argmaxes},
attrs={
"pooled_height": pooled_height,
"pooled_width": pooled_width,
"spatial_scale": spatial_scale
})
return pool_out, argmaxes
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

一般ROI pool 之后的op需要argmaxes么?如果不需要,就不用return了吧。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不需要的

11 changes: 11 additions & 0 deletions python/paddle/fluid/tests/unittests/test_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,17 @@ def test_topk(self):
self.assertIsNotNone(indices)
print(str(program))

def test_roi_pool(self):
print("test_roi_pool")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

program = Program()
with program_guard(program):
x = layers.data(name="x", shape=[10, 256, 30, 30], dtype="float32")
rois = layers.data(name="rois", shape=[20, 5], dtype="float32")
output, argmaxes = layers.roi_pool(x, rois, 7, 7, 0.6)
self.assertIsNotNone(output)
self.assertIsNotNone(argmaxes)
print(str(program))


if __name__ == '__main__':
unittest.main()