未验证 提交 2d1a6f8d 编写于 作者: A Abhinav Arora 提交者: GitHub

Fix cpplint issues in Detection_map_op (#9969)

* Fix conv_op.h

* Fix conv_mkldnn_op

* Fix cpplint issues in detection_map_op
上级 d08791d1
...@@ -72,10 +72,10 @@ class ConvMKLDNNOpKernel : public paddle::framework::OpKernel<T> { ...@@ -72,10 +72,10 @@ class ConvMKLDNNOpKernel : public paddle::framework::OpKernel<T> {
auto dst_md = platform::MKLDNNMemDesc( auto dst_md = platform::MKLDNNMemDesc(
dst_tz, mkldnn::memory::data_type::f32, mkldnn::memory::format::nchw); dst_tz, mkldnn::memory::data_type::f32, mkldnn::memory::format::nchw);
auto src_memory = auto src_memory = mkldnn::memory({src_md, mkldnn_engine},
mkldnn::memory({src_md, mkldnn_engine}, (void*)input_data); reinterpret_cast<void*>(input_data));
auto weights_memory = auto weights_memory = mkldnn::memory({weights_md, mkldnn_engine},
mkldnn::memory({weights_md, mkldnn_engine}, (void*)filter_data); reinterpret_cast<void*>(filter_data));
auto dst_memory = mkldnn::memory({dst_md, mkldnn_engine}, output_data); auto dst_memory = mkldnn::memory({dst_md, mkldnn_engine}, output_data);
std::shared_ptr<mkldnn::convolution_forward::primitive_desc> conv_pd = std::shared_ptr<mkldnn::convolution_forward::primitive_desc> conv_pd =
...@@ -180,8 +180,9 @@ class ConvMKLDNNGradOpKernel : public paddle::framework::OpKernel<T> { ...@@ -180,8 +180,9 @@ class ConvMKLDNNGradOpKernel : public paddle::framework::OpKernel<T> {
dst_tz, mkldnn::memory::data_type::f32, mkldnn::memory::format::nchw); dst_tz, mkldnn::memory::data_type::f32, mkldnn::memory::format::nchw);
// create memory // create memory
auto diff_dst_memory = mkldnn::memory({diff_weights_md, mkldnn_engine}, auto diff_dst_memory =
(void*)output_grad_data); mkldnn::memory({diff_weights_md, mkldnn_engine},
reinterpret_cast<void*>(output_grad_data));
// Retrieve conv_pd from device context // Retrieve conv_pd from device context
auto conv_pd = auto conv_pd =
std::static_pointer_cast<mkldnn::convolution_forward::primitive_desc>( std::static_pointer_cast<mkldnn::convolution_forward::primitive_desc>(
...@@ -198,10 +199,11 @@ class ConvMKLDNNGradOpKernel : public paddle::framework::OpKernel<T> { ...@@ -198,10 +199,11 @@ class ConvMKLDNNGradOpKernel : public paddle::framework::OpKernel<T> {
mkldnn_engine); mkldnn_engine);
// create memory // create memory
auto diff_weights_memory = mkldnn::memory( auto diff_weights_memory =
{diff_weights_md, mkldnn_engine}, (void*)filter_grad_data); mkldnn::memory({diff_weights_md, mkldnn_engine},
auto src_memory = reinterpret_cast<void*>(filter_grad_data));
mkldnn::memory({src_md, mkldnn_engine}, (void*)input_data); auto src_memory = mkldnn::memory({src_md, mkldnn_engine},
reinterpret_cast<void*>(input_data));
// create backward conv primitive for weights // create backward conv primitive for weights
auto conv_bwd_weights_prim = mkldnn::convolution_backward_weights( auto conv_bwd_weights_prim = mkldnn::convolution_backward_weights(
...@@ -221,9 +223,10 @@ class ConvMKLDNNGradOpKernel : public paddle::framework::OpKernel<T> { ...@@ -221,9 +223,10 @@ class ConvMKLDNNGradOpKernel : public paddle::framework::OpKernel<T> {
// create memory // create memory
auto diff_src_memory = auto diff_src_memory =
mkldnn::memory({diff_src_md, mkldnn_engine}, (void*)input_grad_data); mkldnn::memory({diff_src_md, mkldnn_engine},
auto weights_memory = reinterpret_cast<void*>(input_grad_data));
mkldnn::memory({weights_md, mkldnn_engine}, (void*)filter_data); auto weights_memory = mkldnn::memory(
{weights_md, mkldnn_engine}, reinterpret_cast<void*>(filter_data));
// create backward conv primitive for data // create backward conv primitive for data
auto conv_bwd_data_prim = mkldnn::convolution_backward_data( auto conv_bwd_data_prim = mkldnn::convolution_backward_data(
......
...@@ -14,6 +14,7 @@ limitations under the License. */ ...@@ -14,6 +14,7 @@ limitations under the License. */
#pragma once #pragma once
#include <vector>
#include "paddle/fluid/framework/eigen.h" #include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/operators/math/depthwise_conv.h" #include "paddle/fluid/operators/math/depthwise_conv.h"
...@@ -41,9 +42,10 @@ inline int ConvOutputSize(int input_size, int filter_size, int dilation, ...@@ -41,9 +42,10 @@ inline int ConvOutputSize(int input_size, int filter_size, int dilation,
return output_size; return output_size;
} }
inline bool IsExpand(std::vector<int64_t>& filter_dim, inline bool IsExpand(const std::vector<int64_t>& filter_dim,
std::vector<int>& strides, std::vector<int>& paddings, const std::vector<int>& strides,
std::vector<int>& dilations) { const std::vector<int>& paddings,
const std::vector<int>& dilations) {
bool filter_1 = true, strides_1 = true, padding_0 = true, dilation_1 = true; bool filter_1 = true, strides_1 = true, padding_0 = true, dilation_1 = true;
for (size_t j = 0; j < strides.size(); ++j) { for (size_t j = 0; j < strides.size(); ++j) {
filter_1 = filter_1 && (static_cast<int>(filter_dim[j + 2]) == 1); filter_1 = filter_1 && (static_cast<int>(filter_dim[j + 2]) == 1);
......
...@@ -13,6 +13,7 @@ See the License for the specific language governing permissions and ...@@ -13,6 +13,7 @@ See the License for the specific language governing permissions and
limitations under the License. */ limitations under the License. */
#include "paddle/fluid/operators/detection_map_op.h" #include "paddle/fluid/operators/detection_map_op.h"
#include <string>
namespace paddle { namespace paddle {
namespace operators { namespace operators {
......
...@@ -13,6 +13,11 @@ See the License for the specific language governing permissions and ...@@ -13,6 +13,11 @@ See the License for the specific language governing permissions and
limitations under the License. */ limitations under the License. */
#pragma once #pragma once
#include <algorithm>
#include <map>
#include <string>
#include <utility>
#include <vector>
#include "paddle/fluid/framework/eigen.h" #include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/framework/op_registry.h"
...@@ -82,7 +87,7 @@ class DetectionMAPOpKernel : public framework::OpKernel<T> { ...@@ -82,7 +87,7 @@ class DetectionMAPOpKernel : public framework::OpKernel<T> {
std::vector<std::map<int, std::vector<Box>>> gt_boxes; std::vector<std::map<int, std::vector<Box>>> gt_boxes;
std::vector<std::map<int, std::vector<std::pair<T, Box>>>> detect_boxes; std::vector<std::map<int, std::vector<std::pair<T, Box>>>> detect_boxes;
GetBoxes(*in_label, *in_detect, gt_boxes, detect_boxes); GetBoxes(*in_label, *in_detect, &gt_boxes, detect_boxes);
std::map<int, int> label_pos_count; std::map<int, int> label_pos_count;
std::map<int, std::vector<std::pair<T, int>>> true_pos; std::map<int, std::vector<std::pair<T, int>>> true_pos;
...@@ -95,20 +100,20 @@ class DetectionMAPOpKernel : public framework::OpKernel<T> { ...@@ -95,20 +100,20 @@ class DetectionMAPOpKernel : public framework::OpKernel<T> {
} }
if (in_pos_count != nullptr && state) { if (in_pos_count != nullptr && state) {
GetInputPos(*in_pos_count, *in_true_pos, *in_false_pos, label_pos_count, GetInputPos(*in_pos_count, *in_true_pos, *in_false_pos, &label_pos_count,
true_pos, false_pos, class_num); &true_pos, &false_pos, class_num);
} }
CalcTrueAndFalsePositive(gt_boxes, detect_boxes, evaluate_difficult, CalcTrueAndFalsePositive(gt_boxes, detect_boxes, evaluate_difficult,
overlap_threshold, label_pos_count, true_pos, overlap_threshold, &label_pos_count, &true_pos,
false_pos); &false_pos);
int background_label = ctx.Attr<int>("background_label"); int background_label = ctx.Attr<int>("background_label");
T map = CalcMAP(ap_type, label_pos_count, true_pos, false_pos, T map = CalcMAP(ap_type, label_pos_count, true_pos, false_pos,
background_label); background_label);
GetOutputPos(ctx, label_pos_count, true_pos, false_pos, *out_pos_count, GetOutputPos(ctx, label_pos_count, true_pos, false_pos, out_pos_count,
*out_true_pos, *out_false_pos, class_num); out_true_pos, out_false_pos, class_num);
T* map_data = out_map->mutable_data<T>(ctx.GetPlace()); T* map_data = out_map->mutable_data<T>(ctx.GetPlace());
map_data[0] = map; map_data[0] = map;
...@@ -155,7 +160,7 @@ class DetectionMAPOpKernel : public framework::OpKernel<T> { ...@@ -155,7 +160,7 @@ class DetectionMAPOpKernel : public framework::OpKernel<T> {
void GetBoxes(const framework::LoDTensor& input_label, void GetBoxes(const framework::LoDTensor& input_label,
const framework::LoDTensor& input_detect, const framework::LoDTensor& input_detect,
std::vector<std::map<int, std::vector<Box>>>& gt_boxes, std::vector<std::map<int, std::vector<Box>>>* gt_boxes,
std::vector<std::map<int, std::vector<std::pair<T, Box>>>>& std::vector<std::map<int, std::vector<std::pair<T, Box>>>>&
detect_boxes) const { detect_boxes) const {
auto labels = framework::EigenTensor<T, 2>::From(input_label); auto labels = framework::EigenTensor<T, 2>::From(input_label);
...@@ -179,7 +184,7 @@ class DetectionMAPOpKernel : public framework::OpKernel<T> { ...@@ -179,7 +184,7 @@ class DetectionMAPOpKernel : public framework::OpKernel<T> {
box.is_difficult = true; box.is_difficult = true;
boxes[label].push_back(box); boxes[label].push_back(box);
} }
gt_boxes.push_back(boxes); gt_boxes->push_back(boxes);
} }
auto detect_index = detect_lod[0]; auto detect_index = detect_lod[0];
...@@ -200,9 +205,9 @@ class DetectionMAPOpKernel : public framework::OpKernel<T> { ...@@ -200,9 +205,9 @@ class DetectionMAPOpKernel : public framework::OpKernel<T> {
const std::map<int, int>& label_pos_count, const std::map<int, int>& label_pos_count,
const std::map<int, std::vector<std::pair<T, int>>>& true_pos, const std::map<int, std::vector<std::pair<T, int>>>& true_pos,
const std::map<int, std::vector<std::pair<T, int>>>& false_pos, const std::map<int, std::vector<std::pair<T, int>>>& false_pos,
framework::Tensor& output_pos_count, framework::Tensor* output_pos_count,
framework::LoDTensor& output_true_pos, framework::LoDTensor* output_true_pos,
framework::LoDTensor& output_false_pos, const int class_num) const { framework::LoDTensor* output_false_pos, const int class_num) const {
int true_pos_count = 0; int true_pos_count = 0;
int false_pos_count = 0; int false_pos_count = 0;
for (auto it = true_pos.begin(); it != true_pos.end(); ++it) { for (auto it = true_pos.begin(); it != true_pos.end(); ++it) {
...@@ -214,12 +219,12 @@ class DetectionMAPOpKernel : public framework::OpKernel<T> { ...@@ -214,12 +219,12 @@ class DetectionMAPOpKernel : public framework::OpKernel<T> {
false_pos_count += fp.size(); false_pos_count += fp.size();
} }
int* pos_count_data = output_pos_count.mutable_data<int>( int* pos_count_data = output_pos_count->mutable_data<int>(
framework::make_ddim({class_num, 1}), ctx.GetPlace()); framework::make_ddim({class_num, 1}), ctx.GetPlace());
T* true_pos_data = output_true_pos.mutable_data<T>( T* true_pos_data = output_true_pos->mutable_data<T>(
framework::make_ddim({true_pos_count, 2}), ctx.GetPlace()); framework::make_ddim({true_pos_count, 2}), ctx.GetPlace());
T* false_pos_data = output_false_pos.mutable_data<T>( T* false_pos_data = output_false_pos->mutable_data<T>(
framework::make_ddim({false_pos_count, 2}), ctx.GetPlace()); framework::make_ddim({false_pos_count, 2}), ctx.GetPlace());
true_pos_count = 0; true_pos_count = 0;
false_pos_count = 0; false_pos_count = 0;
...@@ -261,21 +266,21 @@ class DetectionMAPOpKernel : public framework::OpKernel<T> { ...@@ -261,21 +266,21 @@ class DetectionMAPOpKernel : public framework::OpKernel<T> {
framework::LoD false_pos_lod; framework::LoD false_pos_lod;
false_pos_lod.emplace_back(false_pos_starts); false_pos_lod.emplace_back(false_pos_starts);
output_true_pos.set_lod(true_pos_lod); output_true_pos->set_lod(true_pos_lod);
output_false_pos.set_lod(false_pos_lod); output_false_pos->set_lod(false_pos_lod);
return; return;
} }
void GetInputPos(const framework::Tensor& input_pos_count, void GetInputPos(const framework::Tensor& input_pos_count,
const framework::LoDTensor& input_true_pos, const framework::LoDTensor& input_true_pos,
const framework::LoDTensor& input_false_pos, const framework::LoDTensor& input_false_pos,
std::map<int, int>& label_pos_count, std::map<int, int>* label_pos_count,
std::map<int, std::vector<std::pair<T, int>>>& true_pos, std::map<int, std::vector<std::pair<T, int>>>* true_pos,
std::map<int, std::vector<std::pair<T, int>>>& false_pos, std::map<int, std::vector<std::pair<T, int>>>* false_pos,
const int class_num) const { const int class_num) const {
const int* pos_count_data = input_pos_count.data<int>(); const int* pos_count_data = input_pos_count.data<int>();
for (int i = 0; i < class_num; ++i) { for (int i = 0; i < class_num; ++i) {
label_pos_count[i] = pos_count_data[i]; (*label_pos_count)[i] = pos_count_data[i];
} }
auto SetData = [](const framework::LoDTensor& pos_tensor, auto SetData = [](const framework::LoDTensor& pos_tensor,
...@@ -291,8 +296,8 @@ class DetectionMAPOpKernel : public framework::OpKernel<T> { ...@@ -291,8 +296,8 @@ class DetectionMAPOpKernel : public framework::OpKernel<T> {
} }
}; };
SetData(input_true_pos, true_pos); SetData(input_true_pos, *true_pos);
SetData(input_false_pos, false_pos); SetData(input_false_pos, *false_pos);
return; return;
} }
...@@ -301,9 +306,9 @@ class DetectionMAPOpKernel : public framework::OpKernel<T> { ...@@ -301,9 +306,9 @@ class DetectionMAPOpKernel : public framework::OpKernel<T> {
const std::vector<std::map<int, std::vector<std::pair<T, Box>>>>& const std::vector<std::map<int, std::vector<std::pair<T, Box>>>>&
detect_boxes, detect_boxes,
bool evaluate_difficult, float overlap_threshold, bool evaluate_difficult, float overlap_threshold,
std::map<int, int>& label_pos_count, std::map<int, int>* label_pos_count,
std::map<int, std::vector<std::pair<T, int>>>& true_pos, std::map<int, std::vector<std::pair<T, int>>>* true_pos,
std::map<int, std::vector<std::pair<T, int>>>& false_pos) const { std::map<int, std::vector<std::pair<T, int>>>* false_pos) const {
int batch_size = gt_boxes.size(); int batch_size = gt_boxes.size();
for (int n = 0; n < batch_size; ++n) { for (int n = 0; n < batch_size; ++n) {
auto image_gt_boxes = gt_boxes[n]; auto image_gt_boxes = gt_boxes[n];
...@@ -320,10 +325,10 @@ class DetectionMAPOpKernel : public framework::OpKernel<T> { ...@@ -320,10 +325,10 @@ class DetectionMAPOpKernel : public framework::OpKernel<T> {
continue; continue;
} }
int label = it->first; int label = it->first;
if (label_pos_count.find(label) == label_pos_count.end()) { if (label_pos_count->find(label) == label_pos_count->end()) {
label_pos_count[label] = count; (*label_pos_count)[label] = count;
} else { } else {
label_pos_count[label] += count; (*label_pos_count)[label] += count;
} }
} }
} }
...@@ -338,8 +343,8 @@ class DetectionMAPOpKernel : public framework::OpKernel<T> { ...@@ -338,8 +343,8 @@ class DetectionMAPOpKernel : public framework::OpKernel<T> {
int label = it->first; int label = it->first;
for (size_t i = 0; i < pred_boxes.size(); ++i) { for (size_t i = 0; i < pred_boxes.size(); ++i) {
auto score = pred_boxes[i].first; auto score = pred_boxes[i].first;
true_pos[label].push_back(std::make_pair(score, 0)); (*true_pos)[label].push_back(std::make_pair(score, 0));
false_pos[label].push_back(std::make_pair(score, 1)); (*false_pos)[label].push_back(std::make_pair(score, 1));
} }
} }
continue; continue;
...@@ -351,8 +356,8 @@ class DetectionMAPOpKernel : public framework::OpKernel<T> { ...@@ -351,8 +356,8 @@ class DetectionMAPOpKernel : public framework::OpKernel<T> {
if (image_gt_boxes.find(label) == image_gt_boxes.end()) { if (image_gt_boxes.find(label) == image_gt_boxes.end()) {
for (size_t i = 0; i < pred_boxes.size(); ++i) { for (size_t i = 0; i < pred_boxes.size(); ++i) {
auto score = pred_boxes[i].first; auto score = pred_boxes[i].first;
true_pos[label].push_back(std::make_pair(score, 0)); (*true_pos)[label].push_back(std::make_pair(score, 0));
false_pos[label].push_back(std::make_pair(score, 1)); (*false_pos)[label].push_back(std::make_pair(score, 1));
} }
continue; continue;
} }
...@@ -381,17 +386,17 @@ class DetectionMAPOpKernel : public framework::OpKernel<T> { ...@@ -381,17 +386,17 @@ class DetectionMAPOpKernel : public framework::OpKernel<T> {
(!evaluate_difficult && !matched_bboxes[max_idx].is_difficult); (!evaluate_difficult && !matched_bboxes[max_idx].is_difficult);
if (match_evaluate_difficult) { if (match_evaluate_difficult) {
if (!visited[max_idx]) { if (!visited[max_idx]) {
true_pos[label].push_back(std::make_pair(score, 1)); (*true_pos)[label].push_back(std::make_pair(score, 1));
false_pos[label].push_back(std::make_pair(score, 0)); (*false_pos)[label].push_back(std::make_pair(score, 0));
visited[max_idx] = true; visited[max_idx] = true;
} else { } else {
true_pos[label].push_back(std::make_pair(score, 0)); (*true_pos)[label].push_back(std::make_pair(score, 0));
false_pos[label].push_back(std::make_pair(score, 1)); (*false_pos)[label].push_back(std::make_pair(score, 1));
} }
} }
} else { } else {
true_pos[label].push_back(std::make_pair(score, 0)); (*true_pos)[label].push_back(std::make_pair(score, 0));
false_pos[label].push_back(std::make_pair(score, 1)); (*false_pos)[label].push_back(std::make_pair(score, 1));
} }
} }
} }
......
...@@ -14,6 +14,7 @@ limitations under the License. */ ...@@ -14,6 +14,7 @@ limitations under the License. */
#include <algorithm> #include <algorithm>
#include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/operators/edit_distance_op.h"
#include "paddle/fluid/operators/math/math_function.h" #include "paddle/fluid/operators/math/math_function.h"
#include "paddle/fluid/platform/cuda_helper.h" #include "paddle/fluid/platform/cuda_helper.h"
#include "paddle/fluid/platform/gpu_info.h" #include "paddle/fluid/platform/gpu_info.h"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册