-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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 mine_hard_examples operator #7679
Changes from 1 commit
c5a14ed
8190552
00280ba
ff5570c
62dc593
4284b85
8137dd9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. */ | ||
|
||
#include "paddle/operators/mine_hard_examples_op.h" | ||
|
||
namespace paddle { | ||
namespace operators { | ||
|
||
class MineHardExamplesOp : public framework::OperatorWithKernel { | ||
public: | ||
using framework::OperatorWithKernel::OperatorWithKernel; | ||
|
||
protected: | ||
void InferShape(framework::InferShapeContext *ctx) const override { | ||
PADDLE_ENFORCE(ctx->HasInput("ClsLoss"), | ||
"Input(ClsLoss) of MineHardExamplesOp should not be null."); | ||
PADDLE_ENFORCE( | ||
ctx->HasInput("MatchIndics"), | ||
"Input(MatchIndics) of MineHardExamplesOp should not be null."); | ||
PADDLE_ENFORCE(ctx->HasInput("MatchDis"), | ||
"Input(MatchDis) of MineHardExamplesOp should not be null."); | ||
PADDLE_ENFORCE( | ||
ctx->HasOutput("NegIndics"), | ||
"Output(NegIndics) of MineHardExamplesOp should not be null."); | ||
PADDLE_ENFORCE( | ||
ctx->HasOutput("UpdatedMatchIndics"), | ||
"Output(UpdatedMatchIndics) of MineHardExamplesOp should not be null."); | ||
|
||
auto cls_loss_dims = ctx->GetInputDim("ClsLoss"); | ||
auto idx_dims = ctx->GetInputDim("MatchIndics"); | ||
auto dis_dims = ctx->GetInputDim("MatchDis"); | ||
|
||
PADDLE_ENFORCE_EQ(cls_loss_dims.size(), 2UL, | ||
"The shape of ClsLoss is [N, Np]."); | ||
PADDLE_ENFORCE_EQ(idx_dims.size(), 2UL, | ||
"The shape of MatchIndics is [N, Np]."); | ||
PADDLE_ENFORCE_EQ(dis_dims.size(), 2UL, | ||
"The shape of MatchDis is [N, Np]."); | ||
|
||
if (ctx->HasInput("LocLoss")) { | ||
auto loc_loss_dims = ctx->GetInputDim("LocLoss"); | ||
PADDLE_ENFORCE_EQ(loc_loss_dims.size(), 2UL, | ||
"The shape of LocLoss is [N, Np]."); | ||
PADDLE_ENFORCE_EQ(cls_loss_dims[0], loc_loss_dims[0], | ||
"Batch size of ClsLoss and LocLoss must be the same."); | ||
PADDLE_ENFORCE_EQ( | ||
cls_loss_dims[1], loc_loss_dims[1], | ||
"Prior box number of ClsLoss and LocLoss must be the same."); | ||
} | ||
|
||
PADDLE_ENFORCE_EQ( | ||
cls_loss_dims[0], idx_dims[0], | ||
"Batch size of ClsLoss and MatchIndics must be the same."); | ||
PADDLE_ENFORCE_EQ( | ||
cls_loss_dims[1], idx_dims[1], | ||
"Prior box number of ClsLoss and MatchIndics must be the same."); | ||
|
||
PADDLE_ENFORCE_EQ(cls_loss_dims[0], dis_dims[0], | ||
"Batch size of ClsLoss and MatchDis must be the same."); | ||
PADDLE_ENFORCE_EQ( | ||
cls_loss_dims[1], idx_dims[1], | ||
"Prior box number of ClsLoss and MatchDis must be the same."); | ||
|
||
auto mining_type = | ||
GetMiningType(ctx->Attrs().Get<std::string>("mining_type")); | ||
|
||
PADDLE_ENFORCE_NE(mining_type, MiningType::kNone, | ||
"mining_type must be hard_example or max_negative"); | ||
|
||
if (mining_type == MiningType::kMaxNegative) { | ||
auto neg_pos_ratio = ctx->Attrs().Get<float>("neg_pos_ratio"); | ||
auto neg_dis_threshold = ctx->Attrs().Get<float>("neg_dis_threshold"); | ||
PADDLE_ENFORCE_GT( | ||
neg_pos_ratio, 0.0f, | ||
"neg_pos_ratio must greater than zero in max_negative mode"); | ||
PADDLE_ENFORCE_GT( | ||
neg_dis_threshold, 0.0f, | ||
"neg_dis_threshold must greater than zero in max_negative mode"); | ||
} else if (mining_type == MiningType::kHardExample) { | ||
auto sample_size = ctx->Attrs().Get<int>("sample_size"); | ||
PADDLE_ENFORCE_GT( | ||
sample_size, 0, | ||
"sample_size must greater than zero in hard_example mode"); | ||
} | ||
|
||
ctx->SetOutputDim("UpdatedMatchIndics", idx_dims); | ||
} | ||
|
||
protected: | ||
framework::OpKernelType GetExpectedKernelType( | ||
const framework::ExecutionContext &ctx) const override { | ||
return framework::OpKernelType( | ||
framework::ToDataType(ctx.Input<framework::Tensor>("ClsLoss")->type()), | ||
ctx.device_context()); | ||
} | ||
}; | ||
|
||
class MineHardExamplesOpMaker : public framework::OpProtoAndCheckerMaker { | ||
public: | ||
MineHardExamplesOpMaker(OpProto *proto, OpAttrChecker *op_checker) | ||
: OpProtoAndCheckerMaker(proto, op_checker) { | ||
AddInput( | ||
"ClsLoss", | ||
"(Tensor, default Tensor<float>), The classification loss wit shape " | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
"[N, Np], N is the batch size and Np is the number of prior box."); | ||
AddInput("LocLoss", | ||
"(Tensor, optional, default Tensor<float>), The localization loss " | ||
"wit shape [N, Np], N is the batch size and Np is the number of " | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wit -> with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
"prior box.") | ||
.AsDispensable(); | ||
AddInput("MatchIndics", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo error: MatchIndics -> MatchIndices , fix it in all files. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
"(Tensor, Tensor<int>), Matched indices with shape [N, Np], N is " | ||
"the batch size and Np is the number of prior box. " | ||
"MatchIndics[i][j] equal -1 means box[j] does not match any " | ||
"entity, otherwise means Box[j] is matched to row."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no
If MatchIndics[i][j] is -1, it means the j-th prior box in i-th instance does not match any ground-truth box.
please also modify this sentence. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
AddInput("MatchDis", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MatchDis -> MatchDist ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
"(Tensor, default Tensor<float>) Matched indices with shape [N, " | ||
"Np], N is the batch size and Np is the number of prior box."); | ||
AddAttr<float>("neg_pos_ratio", | ||
"(float) The ratio of the negative box to the positive " | ||
"box. Use only when mining_type is equal to max_negative.") | ||
.SetDefault(1.0); | ||
AddAttr<float>("neg_dis_threshold", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dis -> dist? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
"(float) The negative box dis value threshold. " | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The negative overlap upper bound for the unmatched predictions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
"Use only when mining_type is equal to max_negative.") | ||
.SetDefault(0.5); | ||
AddAttr<int>("sample_size", | ||
"(float) The max sample size of negative box. Use only when " | ||
"mining_type is equal to hard_example.") | ||
.SetDefault(0); | ||
AddAttr<std::string>("mining_type", | ||
"(float) The mining algorithm name, the value is " | ||
"hard_example or max_negative.") | ||
.SetDefault("max_negative") | ||
.InEnum({"hard_example", "max_negative"}); | ||
|
||
AddOutput("NegIndics", | ||
"(LoDTensor) The output of negative example indics.a lod tensor " | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
a LoDTensor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
"with shape [Neg, 1]. The size of lod[0] is batch size, " | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The size of lod[0] is batch size -> The size of lod[0] minus 1 is batch size There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
"and each element is the box index. " | ||
"For example, the batch size is 2, the lod is [[0, 1, 2]], " | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. box -> prior box ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
"the sample 0's box 1(MatchIndics[0][1]) is selected, " | ||
"and sample 1's box 0 is selected. The output NegIndics is " | ||
"[[1], [0]]."); | ||
|
||
AddOutput("UpdatedMatchIndics", | ||
"(Tensor) The output of updated MatchIndics, a tensor with " | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
"shape [N, M]. Only update when mining_type is equal to " | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The shape of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
"hard_example. The input MatchIndics elements will be update to " | ||
"-1 when it not in the highest loss list"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The elements in MatchIndics will be updated to -1 when it is in NegIndics. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comment has been changed |
||
|
||
AddComment(R"DOC( | ||
Mine hard examples Operator. | ||
This operator implements hard example mining to select a subset of negative box indics. | ||
For each image, selects the box with highest losses. subject to the condition that the box cannot have | ||
an MatchDis > neg_dis_threshold when mining_type is equals max_negative. The selected number is | ||
min(sample_size, max_negative_box_number) when mining_type is equals hard_example, | ||
or min(neg_pos_ratio * positive_box_number, max_negative_box_number) when mining_type is | ||
equals max_negative, where the max_negative_box_number is the count of MatchIndics elements with value -1. | ||
)DOC"); | ||
} | ||
}; | ||
} // namespace operators | ||
} // namespace paddle | ||
|
||
namespace ops = paddle::operators; | ||
REGISTER_OP_WITHOUT_GRADIENT(mine_hard_examples, ops::MineHardExamplesOp, | ||
ops::MineHardExamplesOpMaker); | ||
|
||
REGISTER_OP_CPU_KERNEL( | ||
mine_hard_examples, | ||
ops::MineHardExamplesKernel<paddle::platform::CPUDeviceContext, float>, | ||
ops::MineHardExamplesKernel<paddle::platform::CPUDeviceContext, double>); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. */ | ||
|
||
#pragma once | ||
#include "paddle/framework/op_registry.h" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only CPU implementation can be removed to .cc file. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
||
namespace paddle { | ||
namespace operators { | ||
|
||
enum MiningType { kNone = 0, kMaxNegative, kHardExample }; | ||
|
||
template <typename T> | ||
bool SortScoreDescend(const std::pair<float, T>& pair1, | ||
const std::pair<float, T>& pair2) { | ||
return pair1.first > pair2.first; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also add this function in multiclass_nms_op, we can make some common function in a common file in the future. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can unify it at the time of merge. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also add this function in multiclass_nms_op, we can make some common function in a common file in the future. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can unify it at the time of merge. |
||
|
||
inline bool IsEligibleMining(const MiningType mining_type, const int match_idx, | ||
const float match_dis, | ||
const float neg_dis_threshold) { | ||
if (mining_type == MiningType::kMaxNegative) { | ||
return match_idx == -1 && match_dis < neg_dis_threshold; | ||
} else if (mining_type == MiningType::kHardExample) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
MiningType GetMiningType(std::string str) { | ||
if (str == "max_negative") { | ||
return MiningType::kMaxNegative; | ||
} else if (str == "hard_example") { | ||
return MiningType::kHardExample; | ||
} else { | ||
return MiningType::kNone; | ||
} | ||
} | ||
|
||
template <typename DeviceContext, typename T> | ||
class MineHardExamplesKernel : public framework::OpKernel<T> { | ||
public: | ||
void Compute(const framework::ExecutionContext& ctx) const override { | ||
auto* in_cls_loss = ctx.Input<framework::Tensor>("ClsLoss"); | ||
auto* in_loc_loss = ctx.Input<framework::Tensor>("LocLoss"); | ||
auto* in_matched_indics = ctx.Input<framework::Tensor>("MatchIndics"); | ||
auto* in_match_dis = ctx.Input<framework::Tensor>("MatchDis"); | ||
float neg_pos_ratio = ctx.Attr<float>("neg_pos_ratio"); | ||
T neg_dis_threshold = static_cast<T>(ctx.Attr<float>("neg_dis_threshold")); | ||
int sample_size = ctx.Attr<int>("sample_size"); | ||
MiningType mining_type = | ||
GetMiningType(ctx.Attr<std::string>("mining_type")); | ||
|
||
auto out_neg_indics = ctx.Output<framework::LoDTensor>("NegIndics"); | ||
auto out_match_indics = ctx.Output<framework::Tensor>("UpdatedMatchIndics"); | ||
|
||
framework::Copy(*in_matched_indics, ctx.GetPlace(), out_match_indics); | ||
|
||
int batch_size = in_matched_indics->dims()[0]; | ||
int prior_num = in_matched_indics->dims()[1]; | ||
|
||
auto match_indices = framework::EigenMatrix<int>::From(*in_matched_indics); | ||
|
||
auto match_indices_et = | ||
framework::EigenMatrix<int>::From(*out_match_indics); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 如果没用Eigen计算,代码是不是可以不引入Eigen? 直接访问T*就好? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the code is easier to understand by using eigen. It's just a habit. |
||
|
||
auto match_dis = framework::EigenMatrix<float>::From(*in_match_dis); | ||
auto cls_loss = framework::EigenMatrix<float>::From(*in_cls_loss); | ||
auto loc_loss = framework::EigenMatrix<float>::From(*in_loc_loss); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. loc_loss是可选输入,在使用时需要判断是否存在:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done. |
||
|
||
std::vector<std::vector<int>> all_neg_indices; | ||
int all_neg_num = 0; | ||
for (int n = 0; n < batch_size; ++n) { | ||
std::vector<std::pair<float, size_t>> loss_idx; | ||
int neg_sel = 0; | ||
for (int m = 0; m < prior_num; ++m) { | ||
if (IsEligibleMining(mining_type, match_indices(n, m), match_dis(n, m), | ||
neg_dis_threshold)) { | ||
T loss = cls_loss(n, m); | ||
if (mining_type == MiningType::kHardExample) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if (mining_type == MiningType::kHardExample && in_loc_loss) { There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
loss = cls_loss(n, m) + loc_loss(n, m); | ||
} | ||
loss_idx.push_back(std::make_pair(loss, m)); | ||
++neg_sel; | ||
} | ||
} | ||
if (mining_type == MiningType::kMaxNegative) { | ||
int num_pos = 0; | ||
for (int m = 0; m < prior_num; ++m) { | ||
if (match_indices(n, m) != -1) ++num_pos; | ||
} | ||
neg_sel = std::min(static_cast<int>(num_pos * neg_pos_ratio), neg_sel); | ||
} else if (mining_type == MiningType::kHardExample) { | ||
neg_sel = std::min(sample_size, neg_sel); | ||
} | ||
std::sort(loss_idx.begin(), loss_idx.end(), SortScoreDescend<int>); | ||
std::set<int> sel_indices; | ||
std::vector<int> neg_indices; | ||
for (int n = 0; n < neg_sel; ++n) { | ||
sel_indices.insert(loss_idx[n].second); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
||
for (int m = 0; m < prior_num; ++m) { | ||
if (match_indices(n, m) > -1) { | ||
if (mining_type == MiningType::kHardExample && | ||
sel_indices.find(m) == sel_indices.end()) { | ||
match_indices_et(n, m) = -1; | ||
} | ||
} else { | ||
if (sel_indices.find(m) != sel_indices.end()) { | ||
neg_indices.push_back(m); | ||
} | ||
} | ||
} | ||
all_neg_indices.push_back(neg_indices); | ||
all_neg_num += neg_indices.size(); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The out_neg_indics_lod[0] can be calculated in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
||
framework::LoD out_neg_indics_lod; | ||
out_neg_indics_lod.resize(1); | ||
int neg_offset = 0; | ||
auto neg_data = out_neg_indics->mutable_data<int>( | ||
framework::make_ddim({all_neg_num, 1}), ctx.GetPlace()); | ||
out_neg_indics_lod[0].push_back(neg_offset); | ||
for (auto neg_indices : all_neg_indices) { | ||
for (auto neg_idx : neg_indices) { | ||
neg_data[neg_offset++] = neg_idx; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
out_neg_indics_lod[0].push_back(neg_offset); | ||
} | ||
out_neg_indics->set_lod(out_neg_indics_lod); | ||
return; | ||
} | ||
}; | ||
} // namespace operators | ||
|
||
} // namespace paddle |
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.
These checks also can be add in ProtoMaker, like https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/operators/lrn_op.cc#L160
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.
neg_pos_ratio and neg_dis_threshold are not always greater than 0.0f, just when mining_type is set to kMaxNegative.