Skip to content

FasterRCNN Anchor Generator Op #11218

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

Merged
merged 12 commits into from
Jul 2, 2018
Merged

Conversation

xingyuanbu
Copy link
Contributor

Anchor Generator Op
Match with detectron

@qingqing01 qingqing01 requested review from kuke and qingqing01 June 6, 2018 04:27
void Make() override {
AddInput("Input",
"(Tensor, default Tensor<float>), "
"the input feature data of AnchorGeneratorOp. "
Copy link
Contributor

Choose a reason for hiding this comment

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

the input feature data of AnchorGeneratorOp ->

the input feature is tensor with a rank of 4.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

revised

"The layout is NCHW.");
AddOutput("Anchors",
"(Tensor, default Tensor<float>), the output anchors of "
"AnchorGeneratorOp. The layout is [H, W, num_anchors, 4]. "
Copy link
Contributor

Choose a reason for hiding this comment

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

the output anchors of AnchorGeneratorOp ->

the output is a tensor with a rank of 4.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

revised

"AnchorGeneratorOp. The layout is [H, W, num_anchors, 4]. "
"H is the height of input, W is the width of input, num_anchors "
"is the box count of each position. "
"Each anchor is in (x1, y1, x2, y2) format");
Copy link
Contributor

Choose a reason for hiding this comment

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

x1, y1, x2, y2 -> xmin, ymin, xmax, ymax ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

revised

"Each anchor is in (x1, y1, x2, y2) format");
AddOutput("Variances",
"(Tensor, default Tensor<float>), the expanded variances of "
"AnchorGeneratorOp. The layout is [H, W, num_anchors, 4]. "
Copy link
Contributor

Choose a reason for hiding this comment

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

of AnchorGeneratorOp 这种话没有意义。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

revised

"H is the height of input, W is the width of input, num_anchors "
"is the box count of each position. "
"Each variance is in (x, y, w, h) format "
"which is unrelated with Anchor's");
Copy link
Contributor

Choose a reason for hiding this comment

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

Each variance is in (x, y, w, h) format which is unrelated with Anchor's

这句话不太懂。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

revised


def anchor_generator(input,
anchor_sizes=[64, 128, 256, 512],
aspect_ratios=[0.5, 1.0, 2.0],
Copy link
Contributor

Choose a reason for hiding this comment

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

anchor_sizes=None
aspect_ratios=None
stride=None

用户会依据图片大小调整的这些变量,不要设置缺省值较好些?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

revised


self.anchor_sizes = [64, 128, 256, 512]
self.anchor_sizes = np.array(self.anchor_sizes).astype(
'float32').tolist()
Copy link
Contributor

Choose a reason for hiding this comment

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

self.anchor_sizes = [64., 128., 256., 512.]

即可,去掉line 57.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

revised

'float32').tolist()
self.aspect_ratios = [0.5, 1, 2]
self.aspect_ratios = np.array(self.aspect_ratios).astype(
'float32').tolist()
Copy link
Contributor

Choose a reason for hiding this comment

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

同上,没有必要转成np.array再转回来吧。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

revised

out_var = np.tile(self.variances, (self.layer_h, self.layer_w,
self.num_anchors, 1))
self.out_anchors = out_anchors.astype('float32')
self.out_var = out_var.astype('float32')
Copy link
Contributor

Choose a reason for hiding this comment

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

test_prior_box_op.py 本身就写的有点乱,写法不要按照这个来。

这里把anchor生成的代码写到class外,写成一个函数吧。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

revised

def set_data(self):
self.init_test_params()
self.init_test_input()
self.init_test_output()
Copy link
Contributor

Choose a reason for hiding this comment

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

这里的单测只有一个,输入、输出的初始化不是为了不同的单测复用,就没必要写成两个函数,里面的内容可以直接展开到这里吧。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

感觉不展开继续使用init_test_output这样的方式更清晰,更模块化一些。我已经将anchor生成代码写到class外了,你可以看一看现在的风格是否恰当~

"""
**Anchor generator operator**

Generate prior boxes for SSD(Single Shot MultiBox Detector) algorithm.
Copy link
Contributor

Choose a reason for hiding this comment

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

Not for SSD.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

revised

Each position of the input produce N prior boxes, N is determined by
the count of min_sizes, max_sizes and aspect_ratios, The size of the
box is in range(min_size, max_size) interval, which is generated in
sequence according to the aspect_ratios.
Copy link
Contributor

Choose a reason for hiding this comment

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

There is no min_sizes, max_sizes here, please update the doc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

revised

Copy link
Contributor Author

Choose a reason for hiding this comment

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

revised

Args:
input(Variable): The Input feature map, the format is NCHW.
anchor_sizes(list|tuple|float value): Anchor sizes of generated anchors.
Defalut: [64, 128, 256, 512]
Copy link
Contributor

Choose a reason for hiding this comment

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

list|tuple|float value -> list|tuple|float

There is no default value in the interface. And need to explain what is anchor size?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

revised

def anchor_generator(input,
anchor_sizes=None,
aspect_ratios=None,
variance=[0.1, 0.1, 0.2, 0.2],
Copy link
Contributor

Choose a reason for hiding this comment

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

这里的默认值是常用值吗? 否则,不要设置。 暴露给用户的接口要谨慎。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

variance=[0.1, 0.1, 0.2, 0.2] is a common value, and is rarely to be set to another value.

anchor_sizes(list|tuple|float value): Anchor sizes of generated anchors.
Defalut: [64, 128, 256, 512]
aspect_ratios(list|tuple|float value): The aspect ratios of generated
anchors. Default: [0.5, 1.0, 2.0].
Copy link
Contributor

Choose a reason for hiding this comment

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

same as above.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

revised

The layout is [H, W, num_anchors, 4].
H is the height of input, W is the width of input,
num_anchors is the box count of each position.
Each anchor is in (x1, y1, x2, y2) format.
Copy link
Contributor

Choose a reason for hiding this comment

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

x1, y1, x2, y2 -> xmin, ymin, xmax, ymax? 以及说明下,是否normalize?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

revised

aspect_ratios=[0.5, 1.0, 2.0],
variance=[0.1, 0.1, 0.2, 0.2],
stride=[16.0, 16.0],
offset=0.5)
Copy link
Contributor

Choose a reason for hiding this comment

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

Examples .. code-block:: python 前后注意留空格,否则文档格式不正确。

     Examples:
       
        .. code-block:: python

        anchor, variance = anchor_generator(
            # ... 下面代码注意缩进

Copy link
Contributor Author

Choose a reason for hiding this comment

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

revised


AddAttr<std::vector<float>>(
"anchor_sizes",
"(vector<float>) List of RPN anchor sizes "
Copy link
Contributor

Choose a reason for hiding this comment

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

List of RPN anchor sizes

need more details, RPN用全拼。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

revised

"(float) "
"Anchor center offset, with a default of 0.5")
.SetDefault(0.5);
AddComment(R"DOC(
Copy link
Contributor

@qingqing01 qingqing01 Jun 29, 2018

Choose a reason for hiding this comment

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

上面是否有默认值和Python端保持一致?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

0.5 is the default value both in python and C++


void InferShape(framework::InferShapeContext* ctx) const override {
PADDLE_ENFORCE(ctx->HasInput("Input"),
"Input(Input) of AnchorGeneratorOp should not be null.");
Copy link
Contributor

Choose a reason for hiding this comment

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

Also need to check outputs.

PADDLE_ENFORCE(ctx->HasInput("Anchors"), "xxxx");
PADDLE_ENFORCE(ctx->HasInput("Variances"), "xxxx");

Copy link
Contributor Author

Choose a reason for hiding this comment

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

revised

@@ -909,3 +910,95 @@ def _is_list_or_tuple_and_equal(data, length, err_info):
box.stop_gradient = True
var.stop_gradient = True
return mbox_locs_concat, mbox_confs_concat, box, var


def anchor_generator(input,
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

revised

@qingqing01 qingqing01 merged commit 5056d3e into PaddlePaddle:develop Jul 2, 2018
kuke pushed a commit to kuke/Paddle that referenced this pull request Aug 25, 2018
* Add anchor generator operator for Faster-RCNN.
* Add unittest testing.
* Add Python API.
auto input_dims = ctx->GetInputDim("Input");
PADDLE_ENFORCE(input_dims.size() == 4, "The layout of input is NCHW.");

auto anchor_sizes = ctx->Attrs().Get<std::vector<float>>("anchor_sizes");
Copy link
Collaborator

Choose a reason for hiding this comment

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

auto --> auto&

PADDLE_ENFORCE(input_dims.size() == 4, "The layout of input is NCHW.");

auto anchor_sizes = ctx->Attrs().Get<std::vector<float>>("anchor_sizes");
auto aspect_ratios = ctx->Attrs().Get<std::vector<float>>("aspect_ratios");
Copy link
Collaborator

Choose a reason for hiding this comment

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

auto --> auto&


auto anchor_sizes = ctx->Attrs().Get<std::vector<float>>("anchor_sizes");
auto aspect_ratios = ctx->Attrs().Get<std::vector<float>>("aspect_ratios");
auto stride = ctx->Attrs().Get<std::vector<float>>("stride");
Copy link
Collaborator

Choose a reason for hiding this comment

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

auto --> auto&

auto anchor_sizes = ctx->Attrs().Get<std::vector<float>>("anchor_sizes");
auto aspect_ratios = ctx->Attrs().Get<std::vector<float>>("aspect_ratios");
auto stride = ctx->Attrs().Get<std::vector<float>>("stride");
auto variances = ctx->Attrs().Get<std::vector<float>>("variances");
Copy link
Collaborator

Choose a reason for hiding this comment

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

auto --> auto&

size_t num_anchors = aspect_ratios.size() * anchor_sizes.size();

std::vector<int64_t> dim_vec(4);
dim_vec[0] = input_dims[2];
Copy link
Collaborator

Choose a reason for hiding this comment

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

Check input_dims.size() >= 4


std::vector<int64_t> dim_vec(4);
dim_vec[0] = input_dims[2];
dim_vec[1] = input_dims[3];
Copy link
Collaborator

Choose a reason for hiding this comment

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

Check input_dims.size() >= 4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants