Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/dnn/test/test_darknet_importer.cpp
16339 views
1
/*M///////////////////////////////////////////////////////////////////////////////////////
2
//
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4
//
5
// By downloading, copying, installing or using the software you agree to this license.
6
// If you do not agree to this license, do not download, install,
7
// copy or use the software.
8
//
9
//
10
// License Agreement
11
// For Open Source Computer Vision Library
12
// (3-clause BSD License)
13
//
14
// Copyright (C) 2017, Intel Corporation, all rights reserved.
15
// Third party copyrights are property of their respective owners.
16
//
17
// Redistribution and use in source and binary forms, with or without modification,
18
// are permitted provided that the following conditions are met:
19
//
20
// * Redistributions of source code must retain the above copyright notice,
21
// this list of conditions and the following disclaimer.
22
//
23
// * Redistributions in binary form must reproduce the above copyright notice,
24
// this list of conditions and the following disclaimer in the documentation
25
// and/or other materials provided with the distribution.
26
//
27
// * Neither the names of the copyright holders nor the names of the contributors
28
// may be used to endorse or promote products derived from this software
29
// without specific prior written permission.
30
//
31
// This software is provided by the copyright holders and contributors "as is" and
32
// any express or implied warranties, including, but not limited to, the implied
33
// warranties of merchantability and fitness for a particular purpose are disclaimed.
34
// In no event shall copyright holders or contributors be liable for any direct,
35
// indirect, incidental, special, exemplary, or consequential damages
36
// (including, but not limited to, procurement of substitute goods or services;
37
// loss of use, data, or profits; or business interruption) however caused
38
// and on any theory of liability, whether in contract, strict liability,
39
// or tort (including negligence or otherwise) arising in any way out of
40
// the use of this software, even if advised of the possibility of such damage.
41
//
42
//M*/
43
44
#include "test_precomp.hpp"
45
#include "npy_blob.hpp"
46
#include <opencv2/dnn/shape_utils.hpp>
47
48
namespace opencv_test { namespace {
49
50
template<typename TString>
51
static std::string _tf(TString filename)
52
{
53
return (getOpenCVExtraDir() + "/dnn/") + filename;
54
}
55
56
static std::vector<String> getOutputsNames(const Net& net)
57
{
58
std::vector<String> names;
59
std::vector<int> outLayers = net.getUnconnectedOutLayers();
60
std::vector<String> layersNames = net.getLayerNames();
61
names.resize(outLayers.size());
62
for (size_t i = 0; i < outLayers.size(); ++i)
63
names[i] = layersNames[outLayers[i] - 1];
64
return names;
65
}
66
67
TEST(Test_Darknet, read_tiny_yolo_voc)
68
{
69
Net net = readNetFromDarknet(_tf("tiny-yolo-voc.cfg"));
70
ASSERT_FALSE(net.empty());
71
}
72
73
TEST(Test_Darknet, read_yolo_voc)
74
{
75
Net net = readNetFromDarknet(_tf("yolo-voc.cfg"));
76
ASSERT_FALSE(net.empty());
77
}
78
79
TEST(Test_Darknet, read_yolo_voc_stream)
80
{
81
Mat ref;
82
Mat sample = imread(_tf("dog416.png"));
83
Mat inp = blobFromImage(sample, 1.0/255, Size(416, 416), Scalar(), true, false);
84
const std::string cfgFile = findDataFile("dnn/yolo-voc.cfg", false);
85
const std::string weightsFile = findDataFile("dnn/yolo-voc.weights", false);
86
// Import by paths.
87
{
88
Net net = readNetFromDarknet(cfgFile, weightsFile);
89
net.setInput(inp);
90
net.setPreferableBackend(DNN_BACKEND_OPENCV);
91
ref = net.forward();
92
}
93
// Import from bytes array.
94
{
95
std::string cfg, weights;
96
readFileInMemory(cfgFile, cfg);
97
readFileInMemory(weightsFile, weights);
98
99
Net net = readNetFromDarknet(&cfg[0], cfg.size(), &weights[0], weights.size());
100
net.setInput(inp);
101
net.setPreferableBackend(DNN_BACKEND_OPENCV);
102
Mat out = net.forward();
103
normAssert(ref, out);
104
}
105
}
106
107
class Test_Darknet_layers : public DNNTestLayer
108
{
109
public:
110
void testDarknetLayer(const std::string& name, bool hasWeights = false)
111
{
112
std::string cfg = findDataFile("dnn/darknet/" + name + ".cfg", false);
113
std::string model = "";
114
if (hasWeights)
115
model = findDataFile("dnn/darknet/" + name + ".weights", false);
116
Mat inp = blobFromNPY(findDataFile("dnn/darknet/" + name + "_in.npy", false));
117
Mat ref = blobFromNPY(findDataFile("dnn/darknet/" + name + "_out.npy", false));
118
119
checkBackend(&inp, &ref);
120
121
Net net = readNet(cfg, model);
122
net.setPreferableBackend(backend);
123
net.setPreferableTarget(target);
124
net.setInput(inp);
125
Mat out = net.forward();
126
normAssert(out, ref, "", default_l1, default_lInf);
127
}
128
};
129
130
class Test_Darknet_nets : public DNNTestLayer
131
{
132
public:
133
// Test object detection network from Darknet framework.
134
void testDarknetModel(const std::string& cfg, const std::string& weights,
135
const std::vector<std::vector<int> >& refClassIds,
136
const std::vector<std::vector<float> >& refConfidences,
137
const std::vector<std::vector<Rect2d> >& refBoxes,
138
double scoreDiff, double iouDiff, float confThreshold = 0.24, float nmsThreshold = 0.4)
139
{
140
checkBackend();
141
142
Mat img1 = imread(_tf("dog416.png"));
143
Mat img2 = imread(_tf("street.png"));
144
std::vector<Mat> samples(2);
145
samples[0] = img1; samples[1] = img2;
146
147
// determine test type, whether batch or single img
148
int batch_size = refClassIds.size();
149
CV_Assert(batch_size == 1 || batch_size == 2);
150
samples.resize(batch_size);
151
152
Mat inp = blobFromImages(samples, 1.0/255, Size(416, 416), Scalar(), true, false);
153
154
Net net = readNet(findDataFile("dnn/" + cfg, false),
155
findDataFile("dnn/" + weights, false));
156
net.setPreferableBackend(backend);
157
net.setPreferableTarget(target);
158
net.setInput(inp);
159
std::vector<Mat> outs;
160
net.forward(outs, getOutputsNames(net));
161
162
for (int b = 0; b < batch_size; ++b)
163
{
164
std::vector<int> classIds;
165
std::vector<float> confidences;
166
std::vector<Rect2d> boxes;
167
for (int i = 0; i < outs.size(); ++i)
168
{
169
Mat out;
170
if (batch_size > 1){
171
// get the sample slice from 3D matrix (batch, box, classes+5)
172
Range ranges[3] = {Range(b, b+1), Range::all(), Range::all()};
173
out = outs[i](ranges).reshape(1, outs[i].size[1]);
174
}else{
175
out = outs[i];
176
}
177
for (int j = 0; j < out.rows; ++j)
178
{
179
Mat scores = out.row(j).colRange(5, out.cols);
180
double confidence;
181
Point maxLoc;
182
minMaxLoc(scores, 0, &confidence, 0, &maxLoc);
183
184
if (confidence > confThreshold) {
185
float* detection = out.ptr<float>(j);
186
double centerX = detection[0];
187
double centerY = detection[1];
188
double width = detection[2];
189
double height = detection[3];
190
boxes.push_back(Rect2d(centerX - 0.5 * width, centerY - 0.5 * height,
191
width, height));
192
confidences.push_back(confidence);
193
classIds.push_back(maxLoc.x);
194
}
195
}
196
}
197
198
// here we need NMS of boxes
199
std::vector<int> indices;
200
NMSBoxes(boxes, confidences, confThreshold, nmsThreshold, indices);
201
202
std::vector<int> nms_classIds;
203
std::vector<float> nms_confidences;
204
std::vector<Rect2d> nms_boxes;
205
206
for (size_t i = 0; i < indices.size(); ++i)
207
{
208
int idx = indices[i];
209
Rect2d box = boxes[idx];
210
float conf = confidences[idx];
211
int class_id = classIds[idx];
212
nms_boxes.push_back(box);
213
nms_confidences.push_back(conf);
214
nms_classIds.push_back(class_id);
215
}
216
217
normAssertDetections(refClassIds[b], refConfidences[b], refBoxes[b], nms_classIds,
218
nms_confidences, nms_boxes, format("batch size %d, sample %d\n", batch_size, b).c_str(), confThreshold, scoreDiff, iouDiff);
219
}
220
}
221
222
void testDarknetModel(const std::string& cfg, const std::string& weights,
223
const std::vector<int>& refClassIds,
224
const std::vector<float>& refConfidences,
225
const std::vector<Rect2d>& refBoxes,
226
double scoreDiff, double iouDiff, float confThreshold = 0.24, float nmsThreshold = 0.4)
227
{
228
testDarknetModel(cfg, weights,
229
std::vector<std::vector<int> >(1, refClassIds),
230
std::vector<std::vector<float> >(1, refConfidences),
231
std::vector<std::vector<Rect2d> >(1, refBoxes),
232
scoreDiff, iouDiff, confThreshold, nmsThreshold);
233
}
234
235
void testDarknetModel(const std::string& cfg, const std::string& weights,
236
const cv::Mat& ref, double scoreDiff, double iouDiff,
237
float confThreshold = 0.24, float nmsThreshold = 0.4)
238
{
239
CV_Assert(ref.cols == 7);
240
std::vector<std::vector<int> > refClassIds;
241
std::vector<std::vector<float> > refScores;
242
std::vector<std::vector<Rect2d> > refBoxes;
243
for (int i = 0; i < ref.rows; ++i)
244
{
245
int batchId = static_cast<int>(ref.at<float>(i, 0));
246
int classId = static_cast<int>(ref.at<float>(i, 1));
247
float score = ref.at<float>(i, 2);
248
float left = ref.at<float>(i, 3);
249
float top = ref.at<float>(i, 4);
250
float right = ref.at<float>(i, 5);
251
float bottom = ref.at<float>(i, 6);
252
Rect2d box(left, top, right - left, bottom - top);
253
if (batchId >= refClassIds.size())
254
{
255
refClassIds.resize(batchId + 1);
256
refScores.resize(batchId + 1);
257
refBoxes.resize(batchId + 1);
258
}
259
refClassIds[batchId].push_back(classId);
260
refScores[batchId].push_back(score);
261
refBoxes[batchId].push_back(box);
262
}
263
testDarknetModel(cfg, weights, refClassIds, refScores, refBoxes,
264
scoreDiff, iouDiff, confThreshold, nmsThreshold);
265
}
266
};
267
268
TEST_P(Test_Darknet_nets, YoloVoc)
269
{
270
// batchId, classId, confidence, left, top, right, bottom
271
Mat ref = (Mat_<float>(6, 7) << 0, 6, 0.750469f, 0.577374f, 0.127391f, 0.902949f, 0.300809f, // a car
272
0, 1, 0.780879f, 0.270762f, 0.264102f, 0.732475f, 0.745412f, // a bicycle
273
0, 11, 0.901615f, 0.1386f, 0.338509f, 0.421337f, 0.938789f, // a dog
274
1, 14, 0.623813f, 0.183179f, 0.381921f, 0.247726f, 0.625847f, // a person
275
1, 6, 0.667770f, 0.446555f, 0.453578f, 0.499986f, 0.519167f, // a car
276
1, 6, 0.844947f, 0.637058f, 0.460398f, 0.828508f, 0.66427f); // a car
277
278
double scoreDiff = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 1e-2 : 8e-5;
279
double iouDiff = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 0.018 : 3e-4;
280
double nmsThreshold = (target == DNN_TARGET_MYRIAD) ? 0.397 : 0.4;
281
282
std::string config_file = "yolo-voc.cfg";
283
std::string weights_file = "yolo-voc.weights";
284
285
// batch size 1
286
testDarknetModel(config_file, weights_file, ref.rowRange(0, 3), scoreDiff, iouDiff);
287
288
// batch size 2
289
testDarknetModel(config_file, weights_file, ref, scoreDiff, iouDiff, 0.24, nmsThreshold);
290
}
291
292
TEST_P(Test_Darknet_nets, TinyYoloVoc)
293
{
294
// batchId, classId, confidence, left, top, right, bottom
295
Mat ref = (Mat_<float>(4, 7) << 0, 6, 0.761967f, 0.579042f, 0.159161f, 0.894482f, 0.31994f, // a car
296
0, 11, 0.780595f, 0.129696f, 0.386467f, 0.445275f, 0.920994f, // a dog
297
1, 6, 0.651450f, 0.460526f, 0.458019f, 0.522527f, 0.5341f, // a car
298
1, 6, 0.928758f, 0.651024f, 0.463539f, 0.823784f, 0.654998f); // a car
299
300
double scoreDiff = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 8e-3 : 8e-5;
301
double iouDiff = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 0.018 : 3e-4;
302
303
std::string config_file = "tiny-yolo-voc.cfg";
304
std::string weights_file = "tiny-yolo-voc.weights";
305
306
// batch size 1
307
testDarknetModel(config_file, weights_file, ref.rowRange(0, 2), scoreDiff, iouDiff);
308
309
// batch size 2
310
testDarknetModel(config_file, weights_file, ref, scoreDiff, iouDiff);
311
}
312
313
TEST_P(Test_Darknet_nets, YOLOv3)
314
{
315
// batchId, classId, confidence, left, top, right, bottom
316
Mat ref = (Mat_<float>(9, 7) << 0, 7, 0.952983f, 0.614622f, 0.150257f, 0.901369f, 0.289251f, // a truck
317
0, 1, 0.987908f, 0.150913f, 0.221933f, 0.742255f, 0.74626f, // a bicycle
318
0, 16, 0.998836f, 0.160024f, 0.389964f, 0.417885f, 0.943716f, // a dog (COCO)
319
1, 9, 0.384801f, 0.659824f, 0.372389f, 0.673926f, 0.429412f, // a traffic light
320
1, 9, 0.733283f, 0.376029f, 0.315694f, 0.401776f, 0.395165f, // a traffic light
321
1, 9, 0.785352f, 0.665503f, 0.373543f, 0.688893f, 0.439245f, // a traffic light
322
1, 0, 0.980052f, 0.195856f, 0.378454f, 0.258626f, 0.629258f, // a person
323
1, 2, 0.989633f, 0.450719f, 0.463353f, 0.496305f, 0.522258f, // a car
324
1, 2, 0.997412f, 0.647584f, 0.459939f, 0.821038f, 0.663947f); // a car
325
326
double scoreDiff = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 0.0047 : 8e-5;
327
double iouDiff = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 0.018 : 3e-4;
328
329
std::string config_file = "yolov3.cfg";
330
std::string weights_file = "yolov3.weights";
331
332
// batch size 1
333
testDarknetModel(config_file, weights_file, ref.rowRange(0, 3), scoreDiff, iouDiff);
334
335
if ((backend != DNN_BACKEND_INFERENCE_ENGINE || target != DNN_TARGET_MYRIAD) &&
336
(backend != DNN_BACKEND_INFERENCE_ENGINE || target != DNN_TARGET_OPENCL))
337
{
338
// batch size 2
339
testDarknetModel(config_file, weights_file, ref, scoreDiff, iouDiff);
340
}
341
}
342
343
INSTANTIATE_TEST_CASE_P(/**/, Test_Darknet_nets, dnnBackendsAndTargets());
344
345
TEST_P(Test_Darknet_layers, shortcut)
346
{
347
if (backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_CPU)
348
throw SkipTestException("");
349
testDarknetLayer("shortcut");
350
}
351
352
TEST_P(Test_Darknet_layers, upsample)
353
{
354
testDarknetLayer("upsample");
355
}
356
357
TEST_P(Test_Darknet_layers, avgpool_softmax)
358
{
359
testDarknetLayer("avgpool_softmax");
360
}
361
362
TEST_P(Test_Darknet_layers, region)
363
{
364
testDarknetLayer("region");
365
}
366
367
TEST_P(Test_Darknet_layers, reorg)
368
{
369
testDarknetLayer("reorg");
370
}
371
372
INSTANTIATE_TEST_CASE_P(/**/, Test_Darknet_layers, dnnBackendsAndTargets());
373
374
}} // namespace
375
376