test_nms.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. //
  5. // Copyright (C) 2017, Intel Corporation, all rights reserved.
  6. // Third party copyrights are property of their respective owners.
  7. #include "test_precomp.hpp"
  8. namespace opencv_test { namespace {
  9. TEST(NMS, Accuracy)
  10. {
  11. //reference results obtained using tf.image.non_max_suppression with iou_threshold=0.5
  12. std::string dataPath = findDataFile("dnn/nms_reference.yml");
  13. FileStorage fs(dataPath, FileStorage::READ);
  14. std::vector<Rect> bboxes;
  15. std::vector<float> scores;
  16. std::vector<int> ref_indices;
  17. fs["boxes"] >> bboxes;
  18. fs["probs"] >> scores;
  19. fs["output"] >> ref_indices;
  20. const float nms_thresh = .5f;
  21. const float score_thresh = .01f;
  22. std::vector<int> indices;
  23. cv::dnn::NMSBoxes(bboxes, scores, score_thresh, nms_thresh, indices);
  24. ASSERT_EQ(ref_indices.size(), indices.size());
  25. std::sort(indices.begin(), indices.end());
  26. std::sort(ref_indices.begin(), ref_indices.end());
  27. for(size_t i = 0; i < indices.size(); i++)
  28. ASSERT_EQ(indices[i], ref_indices[i]);
  29. }
  30. TEST(SoftNMS, Accuracy)
  31. {
  32. //reference results are obtained using TF v2.7 tf.image.non_max_suppression_with_scores
  33. std::string dataPath = findDataFile("dnn/soft_nms_reference.yml");
  34. FileStorage fs(dataPath, FileStorage::READ);
  35. std::vector<Rect> bboxes;
  36. std::vector<float> scores;
  37. std::vector<int> ref_indices;
  38. std::vector<float> ref_updated_scores;
  39. fs["boxes"] >> bboxes;
  40. fs["probs"] >> scores;
  41. fs["indices"] >> ref_indices;
  42. fs["updated_scores"] >> ref_updated_scores;
  43. std::vector<float> updated_scores;
  44. const float score_thresh = .01f;
  45. const float nms_thresh = .5f;
  46. std::vector<int> indices;
  47. const size_t top_k = 0;
  48. const float sigma = 1.; // sigma in TF is being multiplied by 2, so 0.5 should be passed there
  49. cv::dnn::softNMSBoxes(bboxes, scores, updated_scores, score_thresh, nms_thresh, indices, top_k, sigma);
  50. ASSERT_EQ(ref_indices.size(), indices.size());
  51. for(size_t i = 0; i < indices.size(); i++)
  52. {
  53. ASSERT_EQ(indices[i], ref_indices[i]);
  54. }
  55. ASSERT_EQ(ref_updated_scores.size(), updated_scores.size());
  56. for(size_t i = 0; i < updated_scores.size(); i++)
  57. {
  58. EXPECT_NEAR(updated_scores[i], ref_updated_scores[i], 1e-7);
  59. }
  60. }
  61. }} // namespace