test_nms.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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(BatchedNMS, Accuracy)
  31. {
  32. //reference results obtained using tf.image.non_max_suppression with iou_threshold=0.5
  33. std::string dataPath = findDataFile("dnn/batched_nms_reference.yml");
  34. FileStorage fs(dataPath, FileStorage::READ);
  35. std::vector<Rect> bboxes;
  36. std::vector<float> scores;
  37. std::vector<int> idxs;
  38. std::vector<int> ref_indices;
  39. fs["boxes"] >> bboxes;
  40. fs["probs"] >> scores;
  41. fs["idxs"] >> idxs;
  42. fs["output"] >> ref_indices;
  43. const float nms_thresh = .5f;
  44. const float score_thresh = .05f;
  45. std::vector<int> indices;
  46. cv::dnn::NMSBoxesBatched(bboxes, scores, idxs, score_thresh, nms_thresh, indices);
  47. ASSERT_EQ(ref_indices.size(), indices.size());
  48. std::sort(indices.begin(), indices.end());
  49. std::sort(ref_indices.begin(), ref_indices.end());
  50. for(size_t i = 0; i < indices.size(); i++)
  51. ASSERT_EQ(indices[i], ref_indices[i]);
  52. }
  53. TEST(SoftNMS, Accuracy)
  54. {
  55. //reference results are obtained using TF v2.7 tf.image.non_max_suppression_with_scores
  56. std::string dataPath = findDataFile("dnn/soft_nms_reference.yml");
  57. FileStorage fs(dataPath, FileStorage::READ);
  58. std::vector<Rect> bboxes;
  59. std::vector<float> scores;
  60. std::vector<int> ref_indices;
  61. std::vector<float> ref_updated_scores;
  62. fs["boxes"] >> bboxes;
  63. fs["probs"] >> scores;
  64. fs["indices"] >> ref_indices;
  65. fs["updated_scores"] >> ref_updated_scores;
  66. std::vector<float> updated_scores;
  67. const float score_thresh = .01f;
  68. const float nms_thresh = .5f;
  69. std::vector<int> indices;
  70. const size_t top_k = 0;
  71. const float sigma = 1.; // sigma in TF is being multiplied by 2, so 0.5 should be passed there
  72. cv::dnn::softNMSBoxes(bboxes, scores, updated_scores, score_thresh, nms_thresh, indices, top_k, sigma);
  73. ASSERT_EQ(ref_indices.size(), indices.size());
  74. for(size_t i = 0; i < indices.size(); i++)
  75. {
  76. ASSERT_EQ(indices[i], ref_indices[i]);
  77. }
  78. ASSERT_EQ(ref_updated_scores.size(), updated_scores.size());
  79. for(size_t i = 0; i < updated_scores.size(); i++)
  80. {
  81. EXPECT_NEAR(updated_scores[i], ref_updated_scores[i], 1e-7);
  82. }
  83. }
  84. }} // namespace