Skip to content

Commit 99c4b76

Browse files
committed
dnn(test): add YOLOv4-tiny tests
1 parent 65dbbf7 commit 99c4b76

File tree

3 files changed

+85
-2
lines changed

3 files changed

+85
-2
lines changed

modules/dnn/perf/perf_net.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,19 @@ PERF_TEST_P_(DNNTestNetwork, YOLOv4)
216216
processNet("dnn/yolov4.weights", "dnn/yolov4.cfg", "", inp);
217217
}
218218

219+
PERF_TEST_P_(DNNTestNetwork, YOLOv4_tiny)
220+
{
221+
if (backend == DNN_BACKEND_HALIDE)
222+
throw SkipTestException("");
223+
if (target == DNN_TARGET_MYRIAD)
224+
throw SkipTestException("");
225+
Mat sample = imread(findDataFile("dnn/dog416.png"));
226+
cvtColor(sample, sample, COLOR_BGR2RGB);
227+
Mat inp;
228+
sample.convertTo(inp, CV_32FC3, 1.0f / 255, 0);
229+
processNet("dnn/yolov4-tiny.weights", "dnn/yolov4-tiny.cfg", "", inp);
230+
}
231+
219232
PERF_TEST_P_(DNNTestNetwork, EAST_text_detection)
220233
{
221234
if (backend == DNN_BACKEND_HALIDE)

modules/dnn/test/test_common.impl.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,12 @@ void normAssertDetections(
9696
const char *comment /*= ""*/, double confThreshold /*= 0.0*/,
9797
double scores_diff /*= 1e-5*/, double boxes_iou_diff /*= 1e-4*/)
9898
{
99+
ASSERT_FALSE(testClassIds.empty()) << "No detections";
99100
std::vector<bool> matchedRefBoxes(refBoxes.size(), false);
101+
std::vector<double> refBoxesIoUDiff(refBoxes.size(), 1.0);
100102
for (int i = 0; i < testBoxes.size(); ++i)
101103
{
104+
//cout << "Test[i=" << i << "]: score=" << testScores[i] << " id=" << testClassIds[i] << " box " << testBoxes[i] << endl;
102105
double testScore = testScores[i];
103106
if (testScore < confThreshold)
104107
continue;
@@ -115,6 +118,7 @@ void normAssertDetections(
115118
double interArea = (testBox & refBoxes[j]).area();
116119
double iou = interArea / (testBox.area() + refBoxes[j].area() - interArea);
117120
topIoU = std::max(topIoU, iou);
121+
refBoxesIoUDiff[j] = std::min(refBoxesIoUDiff[j], 1.0f - iou);
118122
if (1.0 - iou < boxes_iou_diff)
119123
{
120124
matched = true;
@@ -137,7 +141,9 @@ void normAssertDetections(
137141
if (!matchedRefBoxes[i] && refScores[i] > confThreshold)
138142
{
139143
std::cout << cv::format("Unmatched reference: class %d score %f box ",
140-
refClassIds[i], refScores[i]) << refBoxes[i] << std::endl;
144+
refClassIds[i], refScores[i]) << refBoxes[i]
145+
<< " IoU diff: " << refBoxesIoUDiff[i]
146+
<< std::endl;
141147
EXPECT_LE(refScores[i], confThreshold) << comment;
142148
}
143149
}

modules/dnn/test/test_darknet_importer.cpp

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,13 @@ class Test_Darknet_nets : public DNNTestLayer
254254

255255
}
256256

257+
if (cvIsNaN(iouDiff))
258+
{
259+
if (b == 0)
260+
std::cout << "Skip accuracy checks" << std::endl;
261+
continue;
262+
}
263+
257264
normAssertDetections(refClassIds[b], refConfidences[b], refBoxes[b], nms_classIds,
258265
nms_confidences, nms_boxes, format("batch size %d, sample %d\n", batch_size, b).c_str(), confThreshold, scoreDiff, iouDiff);
259266
}
@@ -449,7 +456,7 @@ TEST_P(Test_Darknet_nets_async, Accuracy)
449456
}
450457

451458
INSTANTIATE_TEST_CASE_P(/**/, Test_Darknet_nets_async, Combine(
452-
Values("yolo-voc", "tiny-yolo-voc", "yolov3", "yolov4"),
459+
Values("yolo-voc", "tiny-yolo-voc", "yolov3", "yolov4", "yolov4-tiny"),
453460
dnnBackendsAndTargets()
454461
));
455462

@@ -587,6 +594,63 @@ TEST_P(Test_Darknet_nets, YOLOv4)
587594
}
588595
}
589596

597+
TEST_P(Test_Darknet_nets, YOLOv4_tiny)
598+
{
599+
applyTestTag(
600+
target == DNN_TARGET_CPU ? CV_TEST_TAG_MEMORY_512MB : CV_TEST_TAG_MEMORY_1GB
601+
);
602+
603+
const double confThreshold = 0.5;
604+
// batchId, classId, confidence, left, top, right, bottom
605+
const int N0 = 2;
606+
const int N1 = 3;
607+
static const float ref_[/* (N0 + N1) * 7 */] = {
608+
0, 7, 0.85935f, 0.593484f, 0.141211f, 0.920356f, 0.291593f,
609+
0, 16, 0.795188f, 0.169207f, 0.386886f, 0.423753f, 0.933004f,
610+
611+
1, 2, 0.996832f, 0.653802f, 0.464573f, 0.815193f, 0.653292f,
612+
1, 2, 0.963325f, 0.451151f, 0.458915f, 0.496255f, 0.52241f,
613+
1, 0, 0.926244f, 0.194851f, 0.361743f, 0.260277f, 0.632364f,
614+
};
615+
Mat ref(N0 + N1, 7, CV_32FC1, (void*)ref_);
616+
617+
double scoreDiff = 0.01f;
618+
double iouDiff = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 0.15 : 0.01f;
619+
620+
std::string config_file = "yolov4-tiny.cfg";
621+
std::string weights_file = "yolov4-tiny.weights";
622+
623+
#if defined(INF_ENGINE_RELEASE)
624+
if (target == DNN_TARGET_MYRIAD) // bad accuracy
625+
iouDiff = std::numeric_limits<double>::quiet_NaN();
626+
if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target == DNN_TARGET_OPENCL)
627+
iouDiff = std::numeric_limits<double>::quiet_NaN();
628+
if ((backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 ||
629+
backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH) && DNN_TARGET_OPENCL_FP16)
630+
iouDiff = std::numeric_limits<double>::quiet_NaN();
631+
#endif
632+
633+
{
634+
SCOPED_TRACE("batch size 1");
635+
testDarknetModel(config_file, weights_file, ref.rowRange(0, N0), scoreDiff, iouDiff, confThreshold);
636+
}
637+
638+
{
639+
SCOPED_TRACE("batch size 2");
640+
testDarknetModel(config_file, weights_file, ref, scoreDiff, iouDiff, confThreshold);
641+
}
642+
643+
#if defined(INF_ENGINE_RELEASE)
644+
if (target == DNN_TARGET_MYRIAD) // bad accuracy
645+
applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
646+
if (backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && target == DNN_TARGET_OPENCL)
647+
applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
648+
if ((backend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 ||
649+
backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH) && DNN_TARGET_OPENCL_FP16)
650+
applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
651+
#endif
652+
}
653+
590654

591655
INSTANTIATE_TEST_CASE_P(/**/, Test_Darknet_nets, dnnBackendsAndTargets());
592656

0 commit comments

Comments
 (0)