提交 1a685144 编写于 作者: S sweetsky0901

for xxYY to xx_yy

上级 dc7ddcb0
...@@ -18,8 +18,7 @@ namespace operators { ...@@ -18,8 +18,7 @@ namespace operators {
class DetectionOutputOpMaker : public framework::OpProtoAndCheckerMaker { class DetectionOutputOpMaker : public framework::OpProtoAndCheckerMaker {
public: public:
DetectionOutputOpMaker(framework::OpProto* proto, DetectionOutputOpMaker(OpProto* proto, OpAttrChecker* op_checker)
framework::OpAttrChecker* op_checker)
: OpProtoAndCheckerMaker(proto, op_checker) { : OpProtoAndCheckerMaker(proto, op_checker) {
AddInput("Loc", AddInput("Loc",
"(Tensor) The input tensor of detection_output operator." "(Tensor) The input tensor of detection_output operator."
...@@ -86,5 +85,5 @@ REGISTER_OP_WITHOUT_GRADIENT(detection_output, ops::DetectionOutputOp, ...@@ -86,5 +85,5 @@ REGISTER_OP_WITHOUT_GRADIENT(detection_output, ops::DetectionOutputOp,
ops::DetectionOutputOpMaker); ops::DetectionOutputOpMaker);
REGISTER_OP_CPU_KERNEL( REGISTER_OP_CPU_KERNEL(
detection_output, detection_output,
ops::Detection_output_Kernel<paddle::platform::CPUDeviceContext, float>, ops::DetectionOutputKernel<paddle::platform::CPUDeviceContext, float>,
ops::Detection_output_Kernel<paddle::platform::CPUDeviceContext, double>); ops::DetectionOutputKernel<paddle::platform::CPUDeviceContext, double>);
...@@ -17,5 +17,5 @@ limitations under the License. */ ...@@ -17,5 +17,5 @@ limitations under the License. */
namespace ops = paddle::operators; namespace ops = paddle::operators;
REGISTER_OP_CUDA_KERNEL( REGISTER_OP_CUDA_KERNEL(
detection_output, detection_output,
ops::Detection_output_Kernel<paddle::platform::CUDADeviceContext, float>, ops::DetectionOutputKernel<paddle::platform::CUDADeviceContext, float>,
ops::Detection_output_Kernel<paddle::platform::CUDADeviceContext, double>); ops::DetectionOutputKernel<paddle::platform::CUDADeviceContext, double>);
...@@ -48,7 +48,7 @@ inline void transpose_fun(const framework::ExecutionContext& context, ...@@ -48,7 +48,7 @@ inline void transpose_fun(const framework::ExecutionContext& context,
} }
} }
template <typename DeviceContext, typename T> template <typename DeviceContext, typename T>
class Detection_output_Kernel : public framework::OpKernel<T> { class DetectionOutputKernel : public framework::OpKernel<T> {
public: public:
void Compute(const framework::ExecutionContext& context) const override { void Compute(const framework::ExecutionContext& context) const override {
const framework::Tensor* in_loc = context.Input<framework::Tensor>("Loc"); const framework::Tensor* in_loc = context.Input<framework::Tensor>("Loc");
...@@ -119,22 +119,22 @@ class Detection_output_Kernel : public framework::OpKernel<T> { ...@@ -119,22 +119,22 @@ class Detection_output_Kernel : public framework::OpKernel<T> {
size_t prior_offset = i * 8; size_t prior_offset = i * 8;
size_t loc_pred_offset = n * num_priors * 4 + i * 4; size_t loc_pred_offset = n * num_priors * 4 + i * 4;
std::vector<math::BBox<T>> prior_bbox_vec; std::vector<math::BBox<T>> prior_bbox_vec;
math::getBBoxFromPriorData<T>(priorbox_data + prior_offset, 1, math::get_bbox_from_priorData<T>(priorbox_data + prior_offset, 1,
prior_bbox_vec); prior_bbox_vec);
std::vector<std::vector<T>> prior_bbox_var; std::vector<std::vector<T>> prior_bbox_var;
math::getBBoxVarFromPriorData<T>(priorbox_data + prior_offset, 1, math::get_bbox_var_from_prior_data<T>(priorbox_data + prior_offset, 1,
prior_bbox_var); prior_bbox_var);
std::vector<T> loc_pred_data; std::vector<T> loc_pred_data;
for (size_t j = 0; j < 4; ++j) for (size_t j = 0; j < 4; ++j)
loc_pred_data.push_back(*(loc_data + loc_pred_offset + j)); loc_pred_data.push_back(*(loc_data + loc_pred_offset + j));
math::BBox<T> bbox = math::decodeBBoxWithVar<T>( math::BBox<T> bbox = math::decode_bbox_with_var<T>(
prior_bbox_vec[0], prior_bbox_var[0], loc_pred_data); prior_bbox_vec[0], prior_bbox_var[0], loc_pred_data);
decoded_bboxes.push_back(bbox); decoded_bboxes.push_back(bbox);
} }
all_decoded_bboxes.push_back(decoded_bboxes); all_decoded_bboxes.push_back(decoded_bboxes);
} }
std::vector<std::map<size_t, std::vector<size_t>>> all_indices; std::vector<std::map<size_t, std::vector<size_t>>> all_indices;
int num_kept = math::getDetectionIndices<T>( int num_kept = math::get_detection_indices<T>(
conf_data, num_priors, num_classes, background_label_id, batch_size, conf_data, num_priors, num_classes, background_label_id, batch_size,
confidence_threshold, nms_top_k, nms_threshold, top_k, confidence_threshold, nms_top_k, nms_threshold, top_k,
all_decoded_bboxes, &all_indices); all_decoded_bboxes, &all_indices);
...@@ -154,11 +154,11 @@ class Detection_output_Kernel : public framework::OpKernel<T> { ...@@ -154,11 +154,11 @@ class Detection_output_Kernel : public framework::OpKernel<T> {
out_cpu.mutable_data<T>(out->dims(), platform::CPUPlace()); out_cpu.mutable_data<T>(out->dims(), platform::CPUPlace());
out_data = out_cpu.data<T>(); out_data = out_cpu.data<T>();
} }
math::getDetectionOutput<T>(conf_data, num_kept, num_priors, num_classes, math::get_detection_output<T>(conf_data, num_kept, num_priors, num_classes,
batch_size, all_indices, all_decoded_bboxes, batch_size, all_indices, all_decoded_bboxes,
out_data); out_data);
if (platform::is_gpu_place(context.GetPlace())) { if (platform::is_gpu_place(context.GetPlace())) {
framework::CopyFrom(out_cpu, platform::GPUPlace(), framework::CopyFrom(out_cpu, platform::CUDAPlace(),
context.device_context(), out); context.device_context(), out);
} }
} }
......
...@@ -51,31 +51,31 @@ struct BBox { ...@@ -51,31 +51,31 @@ struct BBox {
// KNCHW ==> NHWC // KNCHW ==> NHWC
// template <typename T> // template <typename T>
template <typename T> template <typename T>
void getBBoxFromPriorData(const T* prior_data, const size_t num_bboxes, void get_bbox_from_priorData(const T* prior_data, const size_t num_bboxes,
std::vector<BBox<T>>& bbox_vec); std::vector<BBox<T>>& bbox_vec);
template <typename T> template <typename T>
void getBBoxVarFromPriorData(const T* prior_data, const size_t num, void get_bbox_var_from_prior_data(const T* prior_data, const size_t num,
std::vector<std::vector<T>>& var_vec); std::vector<std::vector<T>>& var_vec);
template <typename T> template <typename T>
BBox<T> decodeBBoxWithVar(BBox<T>& prior_bbox, BBox<T> decode_bbox_with_var(BBox<T>& prior_bbox,
const std::vector<T>& prior_bbox_var, const std::vector<T>& prior_bbox_var,
const std::vector<T>& loc_pred_data); const std::vector<T>& loc_pred_data);
template <typename T1, typename T2> template <typename T1, typename T2>
bool sortScorePairDescend(const std::pair<T1, T2>& pair1, bool sort_score_pair_descend(const std::pair<T1, T2>& pair1,
const std::pair<T1, T2>& pair2); const std::pair<T1, T2>& pair2);
template <typename T> template <typename T>
bool sortScorePairDescend(const std::pair<T, BBox<T>>& pair1, bool sort_score_pair_descend(const std::pair<T, BBox<T>>& pair1,
const std::pair<T, BBox<T>>& pair2); const std::pair<T, BBox<T>>& pair2);
template <typename T> template <typename T>
T jaccardOverlap(const BBox<T>& bbox1, const BBox<T>& bbox2); T jaccard_overlap(const BBox<T>& bbox1, const BBox<T>& bbox2);
template <typename T> template <typename T>
void applyNMSFast(const std::vector<BBox<T>>& bboxes, const T* conf_score_data, void apply_nms_fast(const std::vector<BBox<T>>& bboxes,
size_t class_idx, size_t top_k, T conf_threshold, const T* conf_score_data, size_t class_idx, size_t top_k,
T nms_threshold, size_t num_priors, size_t num_classes, T conf_threshold, T nms_threshold, size_t num_priors,
std::vector<size_t>* indices); size_t num_classes, std::vector<size_t>* indices);
template <typename T> template <typename T>
int getDetectionIndices( int get_detection_indices(
const T* conf_data, const size_t num_priors, const size_t num_classes, const T* conf_data, const size_t num_priors, const size_t num_classes,
const size_t background_label_id, const size_t batch_size, const size_t background_label_id, const size_t batch_size,
const T conf_threshold, const size_t nms_top_k, const T nms_threshold, const T conf_threshold, const size_t nms_top_k, const T nms_threshold,
...@@ -85,13 +85,13 @@ int getDetectionIndices( ...@@ -85,13 +85,13 @@ int getDetectionIndices(
template <typename T> template <typename T>
BBox<T> clipBBox(const BBox<T>& bbox); BBox<T> clipBBox(const BBox<T>& bbox);
template <typename T> template <typename T>
void getDetectionOutput( void get_detection_output(
const T* conf_data, const size_t num_kept, const size_t num_priors, const T* conf_data, const size_t num_kept, const size_t num_priors,
const size_t num_classes, const size_t batch_size, const size_t num_classes, const size_t batch_size,
const std::vector<std::map<size_t, std::vector<size_t>>>& all_indices, const std::vector<std::map<size_t, std::vector<size_t>>>& all_indices,
const std::vector<std::vector<BBox<T>>>& all_decoded_bboxes, T* out_data); const std::vector<std::vector<BBox<T>>>& all_decoded_bboxes, T* out_data);
template <typename T> template <typename T>
void getBBoxFromPriorData(const T* prior_data, const size_t num_bboxes, void get_bbox_from_priorData(const T* prior_data, const size_t num_bboxes,
std::vector<BBox<T>>& bbox_vec) { std::vector<BBox<T>>& bbox_vec) {
size_t out_offset = bbox_vec.size(); size_t out_offset = bbox_vec.size();
bbox_vec.resize(bbox_vec.size() + num_bboxes); bbox_vec.resize(bbox_vec.size() + num_bboxes);
...@@ -105,7 +105,7 @@ void getBBoxFromPriorData(const T* prior_data, const size_t num_bboxes, ...@@ -105,7 +105,7 @@ void getBBoxFromPriorData(const T* prior_data, const size_t num_bboxes,
} }
} }
template <typename T> template <typename T>
void getBBoxVarFromPriorData(const T* prior_data, const size_t num, void get_bbox_var_from_prior_data(const T* prior_data, const size_t num,
std::vector<std::vector<T>>& var_vec) { std::vector<std::vector<T>>& var_vec) {
size_t out_offset = var_vec.size(); size_t out_offset = var_vec.size();
var_vec.resize(var_vec.size() + num); var_vec.resize(var_vec.size() + num);
...@@ -119,7 +119,7 @@ void getBBoxVarFromPriorData(const T* prior_data, const size_t num, ...@@ -119,7 +119,7 @@ void getBBoxVarFromPriorData(const T* prior_data, const size_t num,
} }
} }
template <typename T> template <typename T>
BBox<T> decodeBBoxWithVar(BBox<T>& prior_bbox, BBox<T> decode_bbox_with_var(BBox<T>& prior_bbox,
const std::vector<T>& prior_bbox_var, const std::vector<T>& prior_bbox_var,
const std::vector<T>& loc_pred_data) { const std::vector<T>& loc_pred_data) {
T prior_bbox_width = prior_bbox.get_width(); T prior_bbox_width = prior_bbox.get_width();
...@@ -147,12 +147,12 @@ BBox<T> decodeBBoxWithVar(BBox<T>& prior_bbox, ...@@ -147,12 +147,12 @@ BBox<T> decodeBBoxWithVar(BBox<T>& prior_bbox,
return decoded_bbox; return decoded_bbox;
} }
template <typename T1, typename T2> template <typename T1, typename T2>
bool sortScorePairDescend(const std::pair<T1, T2>& pair1, bool sort_score_pair_descend(const std::pair<T1, T2>& pair1,
const std::pair<T1, T2>& pair2) { const std::pair<T1, T2>& pair2) {
return pair1.first > pair2.first; return pair1.first > pair2.first;
} }
template <typename T> template <typename T>
T jaccardOverlap(const BBox<T>& bbox1, const BBox<T>& bbox2) { T jaccard_overlap(const BBox<T>& bbox1, const BBox<T>& bbox2) {
if (bbox2.x_min > bbox1.x_max || bbox2.x_max < bbox1.x_min || if (bbox2.x_min > bbox1.x_max || bbox2.x_max < bbox1.x_min ||
bbox2.y_min > bbox1.y_max || bbox2.y_max < bbox1.y_min) { bbox2.y_min > bbox1.y_max || bbox2.y_max < bbox1.y_min) {
return 0.0; return 0.0;
...@@ -174,10 +174,10 @@ T jaccardOverlap(const BBox<T>& bbox1, const BBox<T>& bbox2) { ...@@ -174,10 +174,10 @@ T jaccardOverlap(const BBox<T>& bbox1, const BBox<T>& bbox2) {
} }
template <typename T> template <typename T>
void applyNMSFast(const std::vector<BBox<T>>& bboxes, const T* conf_score_data, void apply_nms_fast(const std::vector<BBox<T>>& bboxes,
size_t class_idx, size_t top_k, T conf_threshold, const T* conf_score_data, size_t class_idx, size_t top_k,
T nms_threshold, size_t num_priors, size_t num_classes, T conf_threshold, T nms_threshold, size_t num_priors,
std::vector<size_t>* indices) { size_t num_classes, std::vector<size_t>* indices) {
std::vector<std::pair<T, size_t>> scores; std::vector<std::pair<T, size_t>> scores;
for (size_t i = 0; i < num_priors; ++i) { for (size_t i = 0; i < num_priors; ++i) {
size_t conf_offset = i * num_classes + class_idx; size_t conf_offset = i * num_classes + class_idx;
...@@ -185,7 +185,7 @@ void applyNMSFast(const std::vector<BBox<T>>& bboxes, const T* conf_score_data, ...@@ -185,7 +185,7 @@ void applyNMSFast(const std::vector<BBox<T>>& bboxes, const T* conf_score_data,
scores.push_back(std::make_pair(conf_score_data[conf_offset], i)); scores.push_back(std::make_pair(conf_score_data[conf_offset], i));
} }
std::stable_sort(scores.begin(), scores.end(), std::stable_sort(scores.begin(), scores.end(),
sortScorePairDescend<T, size_t>); sort_score_pair_descend<T, size_t>);
if (top_k > 0 && top_k < scores.size()) scores.resize(top_k); if (top_k > 0 && top_k < scores.size()) scores.resize(top_k);
while (scores.size() > 0) { while (scores.size() > 0) {
const size_t idx = scores.front().second; const size_t idx = scores.front().second;
...@@ -193,7 +193,7 @@ void applyNMSFast(const std::vector<BBox<T>>& bboxes, const T* conf_score_data, ...@@ -193,7 +193,7 @@ void applyNMSFast(const std::vector<BBox<T>>& bboxes, const T* conf_score_data,
for (size_t i = 0; i < indices->size(); ++i) { for (size_t i = 0; i < indices->size(); ++i) {
if (keep) { if (keep) {
const size_t saved_idx = (*indices)[i]; const size_t saved_idx = (*indices)[i];
T overlap = jaccardOverlap<T>(bboxes[idx], bboxes[saved_idx]); T overlap = jaccard_overlap<T>(bboxes[idx], bboxes[saved_idx]);
keep = overlap <= nms_threshold; keep = overlap <= nms_threshold;
} else { } else {
break; break;
...@@ -204,7 +204,7 @@ void applyNMSFast(const std::vector<BBox<T>>& bboxes, const T* conf_score_data, ...@@ -204,7 +204,7 @@ void applyNMSFast(const std::vector<BBox<T>>& bboxes, const T* conf_score_data,
} }
} }
template <typename T> template <typename T>
int getDetectionIndices( int get_detection_indices(
const T* conf_data, const size_t num_priors, const size_t num_classes, const T* conf_data, const size_t num_priors, const size_t num_classes,
const size_t background_label_id, const size_t batch_size, const size_t background_label_id, const size_t batch_size,
const T conf_threshold, const size_t nms_top_k, const T nms_threshold, const T conf_threshold, const size_t nms_top_k, const T nms_threshold,
...@@ -219,7 +219,7 @@ int getDetectionIndices( ...@@ -219,7 +219,7 @@ int getDetectionIndices(
size_t conf_offset = n * num_priors * num_classes; size_t conf_offset = n * num_priors * num_classes;
for (size_t c = 0; c < num_classes; ++c) { for (size_t c = 0; c < num_classes; ++c) {
if (c == background_label_id) continue; if (c == background_label_id) continue;
applyNMSFast<T>(decoded_bboxes, conf_data + conf_offset, c, nms_top_k, apply_nms_fast<T>(decoded_bboxes, conf_data + conf_offset, c, nms_top_k,
conf_threshold, nms_threshold, num_priors, num_classes, conf_threshold, nms_threshold, num_priors, num_classes,
&(indices[c])); &(indices[c]));
num_detected += indices[c].size(); num_detected += indices[c].size();
...@@ -237,7 +237,7 @@ int getDetectionIndices( ...@@ -237,7 +237,7 @@ int getDetectionIndices(
} }
} }
std::sort(score_index_pairs.begin(), score_index_pairs.end(), std::sort(score_index_pairs.begin(), score_index_pairs.end(),
sortScorePairDescend<T, std::pair<size_t, size_t>>); sort_score_pair_descend<T, std::pair<size_t, size_t>>);
score_index_pairs.resize(top_k); score_index_pairs.resize(top_k);
std::map<size_t, std::vector<size_t>> new_indices; std::map<size_t, std::vector<size_t>> new_indices;
for (size_t i = 0; i < score_index_pairs.size(); ++i) { for (size_t i = 0; i < score_index_pairs.size(); ++i) {
...@@ -266,7 +266,7 @@ BBox<T> clipBBox(const BBox<T>& bbox) { ...@@ -266,7 +266,7 @@ BBox<T> clipBBox(const BBox<T>& bbox) {
return clipped_bbox; return clipped_bbox;
} }
template <typename T> template <typename T>
void getDetectionOutput( void get_detection_output(
const T* conf_data, const size_t num_kept, const size_t num_priors, const T* conf_data, const size_t num_kept, const size_t num_priors,
const size_t num_classes, const size_t batch_size, const size_t num_classes, const size_t batch_size,
const std::vector<std::map<size_t, std::vector<size_t>>>& all_indices, const std::vector<std::map<size_t, std::vector<size_t>>>& all_indices,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册