Skip to content

Commit b7ff9dd

Browse files
committed
Merge pull request opencv#9705 from AlexeyAB:dnn_darknet_yolo_v2
2 parents 0739f28 + ecc34dc commit b7ff9dd

File tree

11 files changed

+1764
-1
lines changed

11 files changed

+1764
-1
lines changed

modules/dnn/include/opencv2/dnn/all_layers.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,18 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN
527527
static Ptr<PriorBoxLayer> create(const LayerParams& params);
528528
};
529529

530+
class CV_EXPORTS ReorgLayer : public Layer
531+
{
532+
public:
533+
static Ptr<ReorgLayer> create(const LayerParams& params);
534+
};
535+
536+
class CV_EXPORTS RegionLayer : public Layer
537+
{
538+
public:
539+
static Ptr<RegionLayer> create(const LayerParams& params);
540+
};
541+
530542
class CV_EXPORTS DetectionOutputLayer : public Layer
531543
{
532544
public:

modules/dnn/include/opencv2/dnn/dnn.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,14 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN
612612
virtual ~Importer();
613613
};
614614

615+
/** @brief Reads a network model stored in <a href="https://pjreddie.com/darknet/">Darknet</a> model files.
616+
* @param cfgFile path to the .cfg file with text description of the network architecture.
617+
* @param darknetModel path to the .weights file with learned network.
618+
* @returns Network object that ready to do forward, throw an exception in failure cases.
619+
* @details This is shortcut consisting from DarknetImporter and Net::populateNet calls.
620+
*/
621+
CV_EXPORTS_W Net readNetFromDarknet(const String &cfgFile, const String &darknetModel = String());
622+
615623
/**
616624
* @deprecated Use @ref readNetFromCaffe instead.
617625
* @brief Creates the importer of <a href="http://caffe.berkeleyvision.org">Caffe</a> framework network.
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
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 "../precomp.hpp"
45+
46+
#include <iostream>
47+
#include <algorithm>
48+
#include <vector>
49+
#include <map>
50+
51+
#include "darknet_io.hpp"
52+
53+
54+
namespace cv {
55+
namespace dnn {
56+
CV__DNN_EXPERIMENTAL_NS_BEGIN
57+
58+
namespace
59+
{
60+
61+
class DarknetImporter : public Importer
62+
{
63+
darknet::NetParameter net;
64+
65+
public:
66+
67+
DarknetImporter() {}
68+
69+
DarknetImporter(const char *cfgFile, const char *darknetModel)
70+
{
71+
CV_TRACE_FUNCTION();
72+
73+
ReadNetParamsFromCfgFileOrDie(cfgFile, &net);
74+
75+
if (darknetModel && darknetModel[0])
76+
ReadNetParamsFromBinaryFileOrDie(darknetModel, &net);
77+
}
78+
79+
struct BlobNote
80+
{
81+
BlobNote(const std::string &_name, int _layerId, int _outNum) :
82+
name(_name), layerId(_layerId), outNum(_outNum) {}
83+
84+
std::string name;
85+
int layerId, outNum;
86+
};
87+
88+
std::vector<BlobNote> addedBlobs;
89+
std::map<String, int> layerCounter;
90+
91+
void populateNet(Net dstNet)
92+
{
93+
CV_TRACE_FUNCTION();
94+
95+
int layersSize = net.layer_size();
96+
layerCounter.clear();
97+
addedBlobs.clear();
98+
addedBlobs.reserve(layersSize + 1);
99+
100+
//setup input layer names
101+
{
102+
std::vector<String> netInputs(net.input_size());
103+
for (int inNum = 0; inNum < net.input_size(); inNum++)
104+
{
105+
addedBlobs.push_back(BlobNote(net.input(inNum), 0, inNum));
106+
netInputs[inNum] = net.input(inNum);
107+
}
108+
dstNet.setInputsNames(netInputs);
109+
}
110+
111+
for (int li = 0; li < layersSize; li++)
112+
{
113+
const darknet::LayerParameter &layer = net.layer(li);
114+
String name = layer.name();
115+
String type = layer.type();
116+
LayerParams layerParams = layer.getLayerParams();
117+
118+
int repetitions = layerCounter[name]++;
119+
if (repetitions)
120+
name += cv::format("_%d", repetitions);
121+
122+
int id = dstNet.addLayer(name, type, layerParams);
123+
124+
// iterate many bottoms layers (for example for: route -1, -4)
125+
for (int inNum = 0; inNum < layer.bottom_size(); inNum++)
126+
addInput(layer.bottom(inNum), id, inNum, dstNet, layer.name());
127+
128+
for (int outNum = 0; outNum < layer.top_size(); outNum++)
129+
addOutput(layer, id, outNum);
130+
}
131+
132+
addedBlobs.clear();
133+
}
134+
135+
void addOutput(const darknet::LayerParameter &layer, int layerId, int outNum)
136+
{
137+
const std::string &name = layer.top(outNum);
138+
139+
bool haveDups = false;
140+
for (int idx = (int)addedBlobs.size() - 1; idx >= 0; idx--)
141+
{
142+
if (addedBlobs[idx].name == name)
143+
{
144+
haveDups = true;
145+
break;
146+
}
147+
}
148+
149+
if (haveDups)
150+
{
151+
bool isInplace = layer.bottom_size() > outNum && layer.bottom(outNum) == name;
152+
if (!isInplace)
153+
CV_Error(Error::StsBadArg, "Duplicate blobs produced by multiple sources");
154+
}
155+
156+
addedBlobs.push_back(BlobNote(name, layerId, outNum));
157+
}
158+
159+
void addInput(const std::string &name, int layerId, int inNum, Net &dstNet, std::string nn)
160+
{
161+
int idx;
162+
for (idx = (int)addedBlobs.size() - 1; idx >= 0; idx--)
163+
{
164+
if (addedBlobs[idx].name == name)
165+
break;
166+
}
167+
168+
if (idx < 0)
169+
{
170+
CV_Error(Error::StsObjectNotFound, "Can't find output blob \"" + name + "\"");
171+
return;
172+
}
173+
174+
dstNet.connect(addedBlobs[idx].layerId, addedBlobs[idx].outNum, layerId, inNum);
175+
}
176+
177+
~DarknetImporter()
178+
{
179+
180+
}
181+
182+
};
183+
184+
}
185+
186+
Net readNetFromDarknet(const String &cfgFile, const String &darknetModel /*= String()*/)
187+
{
188+
DarknetImporter darknetImporter(cfgFile.c_str(), darknetModel.c_str());
189+
Net net;
190+
darknetImporter.populateNet(net);
191+
return net;
192+
}
193+
194+
CV__DNN_EXPERIMENTAL_NS_END
195+
}} // namespace

0 commit comments

Comments
 (0)