Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add roi_align yaml and unittest #41402

Merged
merged 2 commits into from
Apr 5, 2022
Merged
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
9 changes: 7 additions & 2 deletions python/paddle/fluid/tests/unittests/test_roi_align_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from __future__ import print_function

import paddle
import unittest
import numpy as np
import math
Expand All @@ -30,6 +31,7 @@ def set_data(self):
self.inputs = {
'X': self.x,
'ROIs': (self.rois[:, 1:5], self.rois_lod),
'RoisNum': self.boxes_num
}
self.attrs = {
'spatial_scale': self.spatial_scale,
Expand Down Expand Up @@ -170,16 +172,19 @@ def make_rois(self):
rois.append(roi)
self.rois_num = len(rois)
self.rois = np.array(rois).astype("float64")
self.boxes_num = np.array(
[bno + 1 for bno in range(self.batch_size)]).astype('int32')

def setUp(self):
self.op_type = "roi_align"
self.python_api = lambda x, boxes, boxes_num, pooled_height, pooled_width, spatial_scale, sampling_ratio, aligned: paddle.vision.ops.roi_align(x, boxes, boxes_num, (pooled_height, pooled_width), spatial_scale, sampling_ratio, aligned)
self.set_data()

def test_check_output(self):
self.check_output()
self.check_output(check_eager=True)

def test_check_grad(self):
self.check_grad(['X'], 'Out')
self.check_grad(['X'], 'Out', check_eager=True)


class TestROIAlignInLodOp(TestROIAlignOp):
Expand Down
12 changes: 11 additions & 1 deletion python/paddle/utils/code_gen/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1344,7 +1344,7 @@
param : [n, dtype]
data_type : dtype
backend : place

- api : reciprocal
args : (Tensor x)
output : Tensor
Expand Down Expand Up @@ -1386,6 +1386,16 @@
intermediate : xshape
backward: reshape_grad

- api : roi_align
args : (Tensor x, Tensor boxes, Tensor boxes_num, int pooled_height, int pooled_width, float spatial_scale, int sampling_ratio, bool aligned)
output : Tensor
infer_meta :
func : RoiAlignInferMeta
kernel :
func : roi_align
optional : boxes_num
backward : roi_align_grad

- api : roll
args : (Tensor x, IntArray shifts, int64_t[] axis)
output : Tensor(out)
Expand Down
13 changes: 12 additions & 1 deletion python/paddle/utils/code_gen/backward.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@
param : [x]
kernel :
func : expand_as_grad

- backward_api : expm1_grad
forward : expm1 (Tensor x) -> Tensor(out)
args : (Tensor out, Tensor out_grad)
Expand Down Expand Up @@ -994,6 +994,17 @@
backend: out_grad
layout: out_grad

- backward_api : roi_align_grad
forward : roi_align (Tensor x, Tensor boxes, Tensor boxes_num, int pooled_height, int pooled_width, float spatial_scale, int sampling_ratio, bool aligned) -> Tensor(out)
args : (Tensor x, Tensor boxes, Tensor boxes_num, Tensor out_grad, int pooled_height, int pooled_width, float spatial_scale, int sampling_ratio, bool aligned)
output : Tensor(x_grad)
infer_meta :
func : UnchangedInferMeta
param : [x]
kernel :
func : roi_align_grad
optional : boxes_num

- backward_api : roll_grad
forward : roll(Tensor x, IntArray shifts, int64_t[] axis) -> Tensor(out)
args : (Tensor x, Tensor out_grad, IntArray shifts, int64_t[] axis)
Expand Down
1 change: 1 addition & 0 deletions python/paddle/utils/code_gen/backward_api_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ def source_include(header_file_path):
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/api/include/api.h"
#include "paddle/phi/infermeta/backward.h"
#include "paddle/phi/infermeta/unary.h"

#include "paddle/fluid/eager/api/utils/global_utils.h"
#include "paddle/fluid/platform/profiler/event_tracing.h"
Expand Down
9 changes: 7 additions & 2 deletions python/paddle/vision/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from ..fluid.layers import nn, utils
from ..nn import Layer, Conv2D, Sequential, ReLU, BatchNorm2D
from ..fluid.initializer import Normal
from ..fluid.framework import _non_static_mode, in_dygraph_mode
from ..fluid.framework import _non_static_mode, in_dygraph_mode, _in_legacy_dygraph
from paddle.common_ops_import import *
from paddle import _C_ops

Expand Down Expand Up @@ -1223,7 +1223,12 @@ def roi_align(x,
output_size = (output_size, output_size)

pooled_height, pooled_width = output_size
if _non_static_mode():
if in_dygraph_mode():
assert boxes_num is not None, "boxes_num should not be None in dygraph mode."
return _C_ops.final_state_roi_align(x, boxes, boxes_num, pooled_height,
pooled_width, spatial_scale,
sampling_ratio, aligned)
if _in_legacy_dygraph():
assert boxes_num is not None, "boxes_num should not be None in dygraph mode."
align_out = _C_ops.roi_align(
x, boxes, boxes_num, "pooled_height", pooled_height, "pooled_width",
Expand Down
2 changes: 1 addition & 1 deletion tools/infrt/skipped_phi_api.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"phi_apis":["conj", "nll_loss", "flatten", "expand_as", "dropout"],
"phi_apis":["conj", "nll_loss", "flatten", "expand_as", "dropout", "roi_align"],
"phi_kernels":["equal_all"]
}