44
44
#include " test_precomp.hpp"
45
45
#include < opencv2/dnn/shape_utils.hpp>
46
46
#include < algorithm>
47
+ #include < opencv2/core/ocl.hpp>
48
+ #include < opencv2/ts/ocl_test.hpp>
47
49
48
50
namespace cvtest
49
51
{
@@ -69,6 +71,64 @@ TEST(Test_Darknet, read_yolo_voc)
69
71
ASSERT_FALSE (net.empty ());
70
72
}
71
73
74
+ OCL_TEST (Reproducibility_TinyYoloVoc, Accuracy)
75
+ {
76
+ Net net;
77
+ {
78
+ const string cfg = findDataFile (" dnn/tiny-yolo-voc.cfg" , false );
79
+ const string model = findDataFile (" dnn/tiny-yolo-voc.weights" , false );
80
+ net = readNetFromDarknet (cfg, model);
81
+ ASSERT_FALSE (net.empty ());
82
+ }
83
+
84
+ net.setPreferableBackend (DNN_BACKEND_DEFAULT);
85
+ net.setPreferableTarget (DNN_TARGET_OPENCL);
86
+
87
+ // dog416.png is dog.jpg that resized to 416x416 in the lossless PNG format
88
+ Mat sample = imread (_tf (" dog416.png" ));
89
+ ASSERT_TRUE (!sample.empty ());
90
+
91
+ Size inputSize (416 , 416 );
92
+
93
+ if (sample.size () != inputSize)
94
+ resize (sample, sample, inputSize);
95
+
96
+ net.setInput (blobFromImage (sample, 1 / 255 .F ), " data" );
97
+ Mat out = net.forward (" detection_out" );
98
+
99
+ Mat detection;
100
+ const float confidenceThreshold = 0.24 ;
101
+
102
+ for (int i = 0 ; i < out.rows ; i++) {
103
+ const int probability_index = 5 ;
104
+ const int probability_size = out.cols - probability_index;
105
+ float *prob_array_ptr = &out.at <float >(i, probability_index);
106
+ size_t objectClass = std::max_element (prob_array_ptr, prob_array_ptr + probability_size) - prob_array_ptr;
107
+ float confidence = out.at <float >(i, (int )objectClass + probability_index);
108
+
109
+ if (confidence > confidenceThreshold)
110
+ detection.push_back (out.row (i));
111
+ }
112
+
113
+ // obtained by: ./darknet detector test ./cfg/voc.data ./cfg/tiny-yolo-voc.cfg ./tiny-yolo-voc.weights -thresh 0.24 ./dog416.png
114
+ // There are 2 objects (6-car, 11-dog) with 25 values for each:
115
+ // { relative_center_x, relative_center_y, relative_width, relative_height, unused_t0, probability_for_each_class[20] }
116
+ float ref_array[] = {
117
+ 0 .736762F , 0 .239551F , 0 .315440F , 0 .160779F , 0 .761977F , 0 .000000F , 0 .000000F , 0 .000000F , 0 .000000F ,
118
+ 0 .000000F , 0 .000000F , 0 .761967F , 0 .000000F , 0 .000000F , 0 .000000F , 0 .000000F , 0 .000000F ,
119
+ 0 .000000F , 0 .000000F , 0 .000000F , 0 .000000F , 0 .000000F , 0 .000000F , 0 .000000F , 0 .000000F ,
120
+
121
+ 0 .287486F , 0 .653731F , 0 .315579F , 0 .534527F , 0 .782737F , 0 .000000F , 0 .000000F , 0 .000000F , 0 .000000F ,
122
+ 0 .000000F , 0 .000000F , 0 .000000F , 0 .000000F , 0 .000000F , 0 .000000F , 0 .000000F , 0 .780595F ,
123
+ 0 .000000F , 0 .000000F , 0 .000000F , 0 .000000F , 0 .000000F , 0 .000000F , 0 .000000F , 0 .000000F
124
+ };
125
+
126
+ const int number_of_objects = 2 ;
127
+ Mat ref (number_of_objects, sizeof (ref_array) / (number_of_objects * sizeof (float )), CV_32FC1, &ref_array);
128
+
129
+ normAssert (ref, detection);
130
+ }
131
+
72
132
TEST (Reproducibility_TinyYoloVoc, Accuracy)
73
133
{
74
134
Net net;
0 commit comments