diff --git a/modules/dnn/src/layers/detection_output_layer.cpp b/modules/dnn/src/layers/detection_output_layer.cpp index 2e381b2e1d94ccd7498a3dab0a7a8deebce16944..ae4774a993158942b269c51e64e2c09b8f3b465c 100644 --- a/modules/dnn/src/layers/detection_output_layer.cpp +++ b/modules/dnn/src/layers/detection_output_layer.cpp @@ -240,6 +240,9 @@ public: if (numKept == 0) { + // Set confidences to zeros. + Range ranges[] = {Range::all(), Range::all(), Range::all(), Range(2, 3)}; + outputs[0](ranges).setTo(0); return; } int outputShape[] = {1, 1, (int)numKept, 7}; diff --git a/modules/dnn/test/test_caffe_importer.cpp b/modules/dnn/test/test_caffe_importer.cpp index 02ed4167b35b6f2ae6bd09450269e4d5e9edca1b..a1837aeb301cd3b9c3dceeb78b8a1f25a9fe2150 100644 --- a/modules/dnn/test/test_caffe_importer.cpp +++ b/modules/dnn/test/test_caffe_importer.cpp @@ -139,6 +139,35 @@ TEST(Reproducibility_SSD, Accuracy) normAssert(ref, out); } +TEST(Reproducibility_MobileNet_SSD, Accuracy) +{ + const string proto = findDataFile("dnn/MobileNetSSD_deploy.prototxt", false); + const string model = findDataFile("dnn/MobileNetSSD_deploy.caffemodel", false); + Net net = readNetFromCaffe(proto, model); + + Mat sample = imread(_tf("street.png")); + + Mat inp = blobFromImage(sample, 1.0f / 127.5, Size(300, 300), Scalar(127.5, 127.5, 127.5), false); + net.setInput(inp); + Mat out = net.forward(); + + Mat ref = blobFromNPY(_tf("mobilenet_ssd_caffe_out.npy")); + normAssert(ref, out); + + // Check that detections aren't preserved. + inp.setTo(0.0f); + net.setInput(inp); + out = net.forward(); + + const int numDetections = out.size[2]; + ASSERT_NE(numDetections, 0); + for (int i = 0; i < numDetections; ++i) + { + float confidence = out.ptr(0, 0, i)[2]; + ASSERT_EQ(confidence, 0); + } +} + TEST(Reproducibility_ResNet50, Accuracy) { Net net = readNetFromCaffe(findDataFile("dnn/ResNet-50-deploy.prototxt", false),