From 9a784382be53567c7878d778402d0e8e44aaf993 Mon Sep 17 00:00:00 2001 From: jack <136876878@qq.com> Date: Fri, 24 Jul 2020 12:03:55 +0800 Subject: [PATCH] [cherry-pick] fix cpp precision --- deploy/cpp/docs/linux_build.md | 2 +- deploy/cpp/src/main.cc | 2 +- deploy/cpp/src/object_detector.cc | 16 ++++++++++------ deploy/python/infer.py | 2 +- deploy/python/visualize.py | 2 +- 5 files changed, 14 insertions(+), 10 deletions(-) diff --git a/deploy/cpp/docs/linux_build.md b/deploy/cpp/docs/linux_build.md index 321ec5645..1ff2e158c 100644 --- a/deploy/cpp/docs/linux_build.md +++ b/deploy/cpp/docs/linux_build.md @@ -1,7 +1,7 @@ # Linux平台编译指南 ## 说明 -本文档在 `Linux`平台使用`GCC 4.8.5` 和 `GCC 4.9.4`测试过,如果需要使用更高G++版本编译使用,则需要重新编译Paddle预测库,请参考: [从源码编译Paddle预测库](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/advanced_guide/inference_deployment/inference/build_and_install_lib_cn.html)。 +本文档在 `Linux`平台使用`GCC 4.8.5` 和 `GCC 4.9.4`测试过,如果需要使用更高G++版本编译使用,则需要重新编译Paddle预测库,请参考: [从源码编译Paddle预测库](https://www.paddlepaddle.org.cn/documentation/docs/zh/develop/advanced_guide/inference_deployment/inference/build_and_install_lib_cn.html)。本文档使用的预置的opencv库是在ubuntu 16.04上用gcc4.8编译的,如果需要在ubuntu 16.04以外的系统环境编译,那么需自行编译opencv库。 ## 前置条件 * G++ 4.8.2 ~ 4.9.4 diff --git a/deploy/cpp/src/main.cc b/deploy/cpp/src/main.cc index 2926dfa3f..94e74022a 100644 --- a/deploy/cpp/src/main.cc +++ b/deploy/cpp/src/main.cc @@ -94,7 +94,7 @@ void PredictImage(const std::string& image_path, std::vector result; det->Predict(im, &result); for (const auto& item : result) { - printf("class=%d confidence=%.2f rect=[%d %d %d %d]\n", + printf("class=%d confidence=%.4f rect=[%d %d %d %d]\n", item.class_id, item.confidence, item.rect[0], diff --git a/deploy/cpp/src/object_detector.cc b/deploy/cpp/src/object_detector.cc index c8aaedeb4..bffd5cc55 100644 --- a/deploy/cpp/src/object_detector.cc +++ b/deploy/cpp/src/object_detector.cc @@ -11,8 +11,10 @@ // 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 "include/object_detector.h" +#include +// for setprecision +#include +#include "include/object_detector.h" namespace PaddleDetection { @@ -48,7 +50,7 @@ void ObjectDetector::LoadModel(const std::string& model_dir, precision, false, false); - } + } } else { config.DisableGpu(); } @@ -71,13 +73,15 @@ cv::Mat VisualizeResult(const cv::Mat& img, cv::Rect roi = cv::Rect(results[i].rect[0], results[i].rect[2], w, h); // Configure color and text size - std::string text = lable_list[results[i].class_id]; + std::ostringstream oss; + oss << std::setiosflags(std::ios::fixed) << std::setprecision(4); + oss << lable_list[results[i].class_id] << " "; + oss << results[i].confidence; + std::string text = oss.str(); int c1 = colormap[3 * results[i].class_id + 0]; int c2 = colormap[3 * results[i].class_id + 1]; int c3 = colormap[3 * results[i].class_id + 2]; cv::Scalar roi_color = cv::Scalar(c1, c2, c3); - text += " "; - text += std::to_string(static_cast(results[i].confidence * 100)) + "%"; int font_face = cv::FONT_HERSHEY_COMPLEX_SMALL; double font_scale = 0.5f; float thickness = 0.5; diff --git a/deploy/python/infer.py b/deploy/python/infer.py index 0003acd44..0b2d5bdff 100644 --- a/deploy/python/infer.py +++ b/deploy/python/infer.py @@ -456,7 +456,7 @@ class Detector(): expect_boxes = (np_boxes[:, 1] > threshold) & (np_boxes[:, 0] > -1) np_boxes = np_boxes[expect_boxes, :] for box in np_boxes: - print('class_id:{:d}, confidence:{:.2f},' + print('class_id:{:d}, confidence:{:.4f},' 'left_top:[{:.2f},{:.2f}],' ' right_bottom:[{:.2f},{:.2f}]'.format( int(box[0]), box[1], box[2], box[3], box[4], box[5])) diff --git a/deploy/python/visualize.py b/deploy/python/visualize.py index 075a3619f..e4d5f6ac7 100644 --- a/deploy/python/visualize.py +++ b/deploy/python/visualize.py @@ -180,7 +180,7 @@ def draw_box(im, np_boxes, labels): fill=color) # draw label - text = "{} {:.2f}".format(labels[clsid], score) + text = "{} {:.4f}".format(labels[clsid], score) tw, th = draw.textsize(text) draw.rectangle( [(xmin + 1, ymin - th), (xmin + tw + 1, ymin)], fill=color) -- GitLab