-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
Conversation
void Make() override { | ||
AddInput("Input", | ||
"(Tensor, default Tensor<float>), " | ||
"the input feature data of AnchorGeneratorOp. " |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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]. " |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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"); |
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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]. " |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
of AnchorGeneratorOp 这种话没有意义。
There was a problem hiding this comment.
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"); |
There was a problem hiding this comment.
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
这句话不太懂。
There was a problem hiding this comment.
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], |
There was a problem hiding this comment.
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
用户会依据图片大小调整的这些变量,不要设置缺省值较好些?
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
同上,没有必要转成np.array再转回来吧。
There was a problem hiding this comment.
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') |
There was a problem hiding this comment.
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外,写成一个函数吧。
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里的单测只有一个,输入、输出的初始化不是为了不同的单测复用,就没必要写成两个函数,里面的内容可以直接展开到这里吧。
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not for SSD.
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
revised
There was a problem hiding this comment.
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] |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里的默认值是常用值吗? 否则,不要设置。 暴露给用户的接口要谨慎。
There was a problem hiding this comment.
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]. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as above.
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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(
# ... 下面代码注意缩进
There was a problem hiding this comment.
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 " |
There was a problem hiding this comment.
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用全拼。
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
上面是否有默认值和Python端保持一致?
There was a problem hiding this comment.
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."); |
There was a problem hiding this comment.
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");
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need unit testing for the interface in https://github.com/PaddlePaddle/Paddle/blob/develop/python/paddle/fluid/tests/test_detection.py
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
revised
* 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"); |
There was a problem hiding this comment.
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"); |
There was a problem hiding this comment.
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"); |
There was a problem hiding this comment.
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"); |
There was a problem hiding this comment.
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]; |
There was a problem hiding this comment.
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]; |
There was a problem hiding this comment.
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
Anchor Generator Op
Match with detectron