1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11 // For Open Source Computer Vision Library
12 // (3-clause BSD License)
14 // Copyright (C) 2017, Intel Corporation, all rights reserved.
15 // Third party copyrights are property of their respective owners.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistributions of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
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.
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.
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.
44 /*M///////////////////////////////////////////////////////////////////////////////////////
47 //Copyright (c) 2017 Joseph Redmon
49 //Permission is hereby granted, free of charge, to any person obtaining a copy
50 //of this software and associated documentation files (the "Software"), to deal
51 //in the Software without restriction, including without limitation the rights
52 //to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
53 //copies of the Software, and to permit persons to whom the Software is
54 //furnished to do so, subject to the following conditions:
56 //The above copyright notice and this permission notice shall be included in all
57 //copies or substantial portions of the Software.
59 //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
60 //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
61 //FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
62 //AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
63 //LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
64 //OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
69 #include "../precomp.hpp"
75 #include "darknet_io.hpp"
82 T getParam(const std::map<std::string, std::string> ¶ms, const std::string param_name, T init_val)
84 std::map<std::string, std::string>::const_iterator it = params.find(param_name);
85 if (it != params.end()) {
86 std::stringstream ss(it->second);
92 static const std::string kFirstLayerName = "data";
94 class setLayersParams {
98 std::string last_layer;
99 std::vector<std::string> fused_layer_names;
102 setLayersParams(NetParameter *_net) :
103 net(_net), layer_id(0), last_layer(kFirstLayerName)
106 void setLayerBlobs(int i, std::vector<cv::Mat> blobs)
108 cv::dnn::LayerParams ¶ms = net->layers[i].layerParams;
109 params.blobs = blobs;
112 cv::dnn::LayerParams getParamConvolution(int kernel, int pad,
113 int stride, int filters_num)
115 cv::dnn::LayerParams params;
116 params.name = "Convolution-name";
117 params.type = "Convolution";
119 params.set<int>("kernel_size", kernel);
120 params.set<int>("pad", pad);
121 params.set<int>("stride", stride);
123 params.set<bool>("bias_term", false); // true only if(BatchNorm == false)
124 params.set<int>("num_output", filters_num);
130 void setConvolution(int kernel, int pad, int stride,
131 int filters_num, int channels_num, int use_batch_normalize)
133 cv::dnn::LayerParams conv_param =
134 getParamConvolution(kernel, pad, stride, filters_num);
136 darknet::LayerParameter lp;
137 std::string layer_name = cv::format("conv_%d", layer_id);
139 // use BIAS in any case
140 if (!use_batch_normalize) {
141 conv_param.set<bool>("bias_term", true);
144 lp.layer_name = layer_name;
145 lp.layer_type = conv_param.type;
146 lp.layerParams = conv_param;
147 lp.bottom_indexes.push_back(last_layer);
148 last_layer = layer_name;
149 net->layers.push_back(lp);
151 if (use_batch_normalize)
153 cv::dnn::LayerParams bn_param;
155 bn_param.name = "BatchNorm-name";
156 bn_param.type = "BatchNorm";
157 bn_param.set<bool>("has_weight", true);
158 bn_param.set<bool>("has_bias", true);
159 bn_param.set<float>("eps", 1E-6); // .000001f in Darknet Yolo
161 darknet::LayerParameter lp;
162 std::string layer_name = cv::format("bn_%d", layer_id);
163 lp.layer_name = layer_name;
164 lp.layer_type = bn_param.type;
165 lp.layerParams = bn_param;
166 lp.bottom_indexes.push_back(last_layer);
167 last_layer = layer_name;
168 net->layers.push_back(lp);
172 fused_layer_names.push_back(last_layer);
177 cv::dnn::LayerParams activation_param;
178 activation_param.set<float>("negative_slope", 0.1f);
179 activation_param.name = "ReLU-name";
180 activation_param.type = "ReLU";
182 darknet::LayerParameter lp;
183 std::string layer_name = cv::format("relu_%d", layer_id);
184 lp.layer_name = layer_name;
185 lp.layer_type = activation_param.type;
186 lp.layerParams = activation_param;
187 lp.bottom_indexes.push_back(last_layer);
188 last_layer = layer_name;
189 net->layers.push_back(lp);
191 fused_layer_names.back() = last_layer;
194 void setMaxpool(size_t kernel, size_t pad, size_t stride)
196 cv::dnn::LayerParams maxpool_param;
197 maxpool_param.set<cv::String>("pool", "max");
198 maxpool_param.set<int>("kernel_size", kernel);
199 maxpool_param.set<int>("pad", pad);
200 maxpool_param.set<int>("stride", stride);
201 maxpool_param.set<cv::String>("pad_mode", "SAME");
202 maxpool_param.name = "Pooling-name";
203 maxpool_param.type = "Pooling";
204 darknet::LayerParameter lp;
206 std::string layer_name = cv::format("pool_%d", layer_id);
207 lp.layer_name = layer_name;
208 lp.layer_type = maxpool_param.type;
209 lp.layerParams = maxpool_param;
210 lp.bottom_indexes.push_back(last_layer);
211 last_layer = layer_name;
212 net->layers.push_back(lp);
214 fused_layer_names.push_back(last_layer);
219 cv::dnn::LayerParams avgpool_param;
220 avgpool_param.set<cv::String>("pool", "ave");
221 avgpool_param.set<bool>("global_pooling", true);
222 avgpool_param.name = "Pooling-name";
223 avgpool_param.type = "Pooling";
224 darknet::LayerParameter lp;
226 std::string layer_name = cv::format("avgpool_%d", layer_id);
227 lp.layer_name = layer_name;
228 lp.layer_type = avgpool_param.type;
229 lp.layerParams = avgpool_param;
230 lp.bottom_indexes.push_back(last_layer);
231 last_layer = layer_name;
232 net->layers.push_back(lp);
234 fused_layer_names.push_back(last_layer);
239 cv::dnn::LayerParams softmax_param;
240 softmax_param.name = "Softmax-name";
241 softmax_param.type = "Softmax";
242 darknet::LayerParameter lp;
244 std::string layer_name = cv::format("softmax_%d", layer_id);
245 lp.layer_name = layer_name;
246 lp.layer_type = softmax_param.type;
247 lp.layerParams = softmax_param;
248 lp.bottom_indexes.push_back(last_layer);
249 last_layer = layer_name;
250 net->layers.push_back(lp);
252 fused_layer_names.push_back(last_layer);
255 void setConcat(int number_of_inputs, int *input_indexes)
257 cv::dnn::LayerParams concat_param;
258 concat_param.name = "Concat-name";
259 concat_param.type = "Concat";
260 concat_param.set<int>("axis", 1); // channels are in axis = 1
262 darknet::LayerParameter lp;
264 std::string layer_name = cv::format("concat_%d", layer_id);
265 lp.layer_name = layer_name;
266 lp.layer_type = concat_param.type;
267 lp.layerParams = concat_param;
268 for (int i = 0; i < number_of_inputs; ++i)
269 lp.bottom_indexes.push_back(fused_layer_names.at(input_indexes[i]));
271 last_layer = layer_name;
272 net->layers.push_back(lp);
275 fused_layer_names.push_back(last_layer);
278 void setIdentity(int bottom_index)
280 cv::dnn::LayerParams identity_param;
281 identity_param.name = "Identity-name";
282 identity_param.type = "Identity";
284 darknet::LayerParameter lp;
286 std::string layer_name = cv::format("identity_%d", layer_id);
287 lp.layer_name = layer_name;
288 lp.layer_type = identity_param.type;
289 lp.layerParams = identity_param;
290 lp.bottom_indexes.push_back(fused_layer_names.at(bottom_index));
292 last_layer = layer_name;
293 net->layers.push_back(lp);
296 fused_layer_names.push_back(last_layer);
299 void setReorg(int stride)
301 cv::dnn::LayerParams reorg_params;
302 reorg_params.name = "Reorg-name";
303 reorg_params.type = "Reorg";
304 reorg_params.set<int>("reorg_stride", stride);
306 darknet::LayerParameter lp;
307 std::string layer_name = cv::format("reorg_%d", layer_id);
308 lp.layer_name = layer_name;
309 lp.layer_type = reorg_params.type;
310 lp.layerParams = reorg_params;
311 lp.bottom_indexes.push_back(last_layer);
312 last_layer = layer_name;
314 net->layers.push_back(lp);
317 fused_layer_names.push_back(last_layer);
320 void setPermute(bool isDarknetLayer = true)
322 cv::dnn::LayerParams permute_params;
323 permute_params.name = "Permute-name";
324 permute_params.type = "Permute";
325 int permute[] = { 0, 2, 3, 1 };
326 cv::dnn::DictValue paramOrder = cv::dnn::DictValue::arrayInt(permute, 4);
328 permute_params.set("order", paramOrder);
330 darknet::LayerParameter lp;
331 std::string layer_name = cv::format("permute_%d", layer_id);
332 lp.layer_name = layer_name;
333 lp.layer_type = permute_params.type;
334 lp.layerParams = permute_params;
335 lp.bottom_indexes.push_back(last_layer);
336 last_layer = layer_name;
337 net->layers.push_back(lp);
342 fused_layer_names.push_back(last_layer);
346 void setRegion(float thresh, int coords, int classes, int anchors, int classfix, int softmax, int softmax_tree, float *biasData)
348 cv::dnn::LayerParams region_param;
349 region_param.name = "Region-name";
350 region_param.type = "Region";
352 region_param.set<float>("thresh", thresh);
353 region_param.set<int>("coords", coords);
354 region_param.set<int>("classes", classes);
355 region_param.set<int>("anchors", anchors);
356 region_param.set<int>("classfix", classfix);
357 region_param.set<bool>("softmax_tree", softmax_tree);
358 region_param.set<bool>("softmax", softmax);
360 cv::Mat biasData_mat = cv::Mat(1, anchors * 2, CV_32F, biasData).clone();
361 region_param.blobs.push_back(biasData_mat);
363 darknet::LayerParameter lp;
364 std::string layer_name = "detection_out";
365 lp.layer_name = layer_name;
366 lp.layer_type = region_param.type;
367 lp.layerParams = region_param;
368 lp.bottom_indexes.push_back(last_layer);
369 last_layer = layer_name;
370 net->layers.push_back(lp);
373 fused_layer_names.push_back(last_layer);
376 void setYolo(int classes, const std::vector<int>& mask, const std::vector<float>& anchors, float thresh, float nms_threshold)
378 cv::dnn::LayerParams region_param;
379 region_param.name = "Region-name";
380 region_param.type = "Region";
382 const int numAnchors = mask.size();
384 region_param.set<int>("classes", classes);
385 region_param.set<int>("anchors", numAnchors);
386 region_param.set<bool>("logistic", true);
387 region_param.set<float>("thresh", thresh);
388 region_param.set<float>("nms_threshold", nms_threshold);
390 std::vector<float> usedAnchors(numAnchors * 2);
391 for (int i = 0; i < numAnchors; ++i)
393 usedAnchors[i * 2] = anchors[mask[i] * 2];
394 usedAnchors[i * 2 + 1] = anchors[mask[i] * 2 + 1];
397 cv::Mat biasData_mat = cv::Mat(1, numAnchors * 2, CV_32F, &usedAnchors[0]).clone();
398 region_param.blobs.push_back(biasData_mat);
400 darknet::LayerParameter lp;
401 std::string layer_name = cv::format("yolo_%d", layer_id);
402 lp.layer_name = layer_name;
403 lp.layer_type = region_param.type;
404 lp.layerParams = region_param;
405 lp.bottom_indexes.push_back(last_layer);
406 lp.bottom_indexes.push_back(kFirstLayerName);
407 last_layer = layer_name;
408 net->layers.push_back(lp);
411 fused_layer_names.push_back(last_layer);
414 void setShortcut(int from, float alpha)
416 cv::dnn::LayerParams shortcut_param;
417 shortcut_param.name = "Shortcut-name";
418 shortcut_param.type = "Eltwise";
422 std::vector<float> coeffs(2, 1);
424 shortcut_param.set("coeff", DictValue::arrayReal<float*>(&coeffs[0], coeffs.size()));
427 shortcut_param.set<std::string>("op", "sum");
429 darknet::LayerParameter lp;
430 std::string layer_name = cv::format("shortcut_%d", layer_id);
431 lp.layer_name = layer_name;
432 lp.layer_type = shortcut_param.type;
433 lp.layerParams = shortcut_param;
434 lp.bottom_indexes.push_back(last_layer);
435 lp.bottom_indexes.push_back(fused_layer_names.at(from));
436 last_layer = layer_name;
437 net->layers.push_back(lp);
440 fused_layer_names.push_back(last_layer);
443 void setUpsample(int scaleFactor)
445 cv::dnn::LayerParams param;
446 param.name = "Upsample-name";
447 param.type = "Resize";
449 param.set<int>("zoom_factor", scaleFactor);
450 param.set<String>("interpolation", "nearest");
452 darknet::LayerParameter lp;
453 std::string layer_name = cv::format("upsample_%d", layer_id);
454 lp.layer_name = layer_name;
455 lp.layer_type = param.type;
456 lp.layerParams = param;
457 lp.bottom_indexes.push_back(last_layer);
458 last_layer = layer_name;
459 net->layers.push_back(lp);
462 fused_layer_names.push_back(last_layer);
466 std::string escapeString(const std::string &src)
469 for (size_t i = 0; i < src.size(); ++i)
470 if (src[i] > ' ' && src[i] <= 'z')
476 std::vector<T> getNumbers(const std::string &src)
479 std::stringstream ss(src);
481 for (std::string str; std::getline(ss, str, ',');) {
482 std::stringstream line(str);
490 bool ReadDarknetFromCfgStream(std::istream &ifile, NetParameter *net)
492 bool read_net = false;
493 int layers_counter = -1;
494 for (std::string line; std::getline(ifile, line);) {
495 line = escapeString(line);
496 if (line.empty()) continue;
502 if (line == "[net]") {
509 const size_t layer_type_size = line.find("]") - 1;
510 CV_Assert(layer_type_size < line.size());
511 std::string layer_type = line.substr(1, layer_type_size);
512 net->layers_cfg[layers_counter]["type"] = layer_type;
517 const size_t separator_index = line.find('=');
518 CV_Assert(separator_index < line.size());
519 if (separator_index != std::string::npos) {
520 std::string name = line.substr(0, separator_index);
521 std::string value = line.substr(separator_index + 1, line.size() - (separator_index + 1));
522 name = escapeString(name);
523 value = escapeString(value);
524 if (name.empty() || value.empty()) continue;
526 net->net_cfg[name] = value;
528 net->layers_cfg[layers_counter][name] = value;
533 std::string anchors = net->layers_cfg[net->layers_cfg.size() - 1]["anchors"];
534 std::vector<float> vec = getNumbers<float>(anchors);
535 std::map<std::string, std::string> &net_params = net->net_cfg;
536 net->width = getParam(net_params, "width", 416);
537 net->height = getParam(net_params, "height", 416);
538 net->channels = getParam(net_params, "channels", 3);
539 CV_Assert(net->width > 0 && net->height > 0 && net->channels > 0);
541 int current_channels = net->channels;
542 net->out_channels_vec.resize(net->layers_cfg.size());
546 setLayersParams setParams(net);
548 typedef std::map<int, std::map<std::string, std::string> >::iterator it_type;
549 for (it_type i = net->layers_cfg.begin(); i != net->layers_cfg.end(); ++i) {
551 std::map<std::string, std::string> &layer_params = i->second;
552 std::string layer_type = layer_params["type"];
554 if (layer_type == "convolutional")
556 int kernel_size = getParam<int>(layer_params, "size", -1);
557 int pad = getParam<int>(layer_params, "pad", 0);
558 int stride = getParam<int>(layer_params, "stride", 1);
559 int filters = getParam<int>(layer_params, "filters", -1);
560 bool batch_normalize = getParam<int>(layer_params, "batch_normalize", 0) == 1;
561 int flipped = getParam<int>(layer_params, "flipped", 0);
563 CV_Error(cv::Error::StsNotImplemented, "Transpose the convolutional weights is not implemented");
565 // correct the strange value of pad=1 for kernel_size=1 in the Darknet cfg-file
566 if (kernel_size < 3) pad = 0;
568 CV_Assert(kernel_size > 0 && filters > 0);
569 CV_Assert(current_channels > 0);
571 setParams.setConvolution(kernel_size, pad, stride, filters, current_channels,
574 current_channels = filters;
576 else if (layer_type == "maxpool")
578 int kernel_size = getParam<int>(layer_params, "size", 2);
579 int stride = getParam<int>(layer_params, "stride", 2);
580 int pad = getParam<int>(layer_params, "pad", 0);
581 setParams.setMaxpool(kernel_size, pad, stride);
583 else if (layer_type == "avgpool")
585 setParams.setAvgpool();
587 else if (layer_type == "softmax")
589 int groups = getParam<int>(layer_params, "groups", 1);
591 CV_Error(Error::StsNotImplemented, "Softmax from Darknet with groups != 1");
592 setParams.setSoftmax();
594 else if (layer_type == "route")
596 std::string bottom_layers = getParam<std::string>(layer_params, "layers", "");
597 CV_Assert(!bottom_layers.empty());
598 std::vector<int> layers_vec = getNumbers<int>(bottom_layers);
600 current_channels = 0;
601 for (size_t k = 0; k < layers_vec.size(); ++k) {
602 layers_vec[k] = layers_vec[k] >= 0 ? layers_vec[k] : (layers_vec[k] + layers_counter);
603 current_channels += net->out_channels_vec[layers_vec[k]];
606 if (layers_vec.size() == 1)
607 setParams.setIdentity(layers_vec.at(0));
609 setParams.setConcat(layers_vec.size(), layers_vec.data());
611 else if (layer_type == "reorg")
613 int stride = getParam<int>(layer_params, "stride", 2);
614 current_channels = current_channels * (stride*stride);
616 setParams.setReorg(stride);
618 else if (layer_type == "region")
620 float thresh = getParam<float>(layer_params, "thresh", 0.001);
621 int coords = getParam<int>(layer_params, "coords", 4);
622 int classes = getParam<int>(layer_params, "classes", -1);
623 int num_of_anchors = getParam<int>(layer_params, "num", -1);
624 int classfix = getParam<int>(layer_params, "classfix", 0);
625 bool softmax = (getParam<int>(layer_params, "softmax", 0) == 1);
626 bool softmax_tree = (getParam<std::string>(layer_params, "tree", "").size() > 0);
628 std::string anchors_values = getParam<std::string>(layer_params, "anchors", std::string());
629 CV_Assert(!anchors_values.empty());
630 std::vector<float> anchors_vec = getNumbers<float>(anchors_values);
632 CV_Assert(classes > 0 && num_of_anchors > 0 && (num_of_anchors * 2) == anchors_vec.size());
634 setParams.setPermute(false);
635 setParams.setRegion(thresh, coords, classes, num_of_anchors, classfix, softmax, softmax_tree, anchors_vec.data());
637 else if (layer_type == "shortcut")
639 std::string bottom_layer = getParam<std::string>(layer_params, "from", "");
640 float alpha = getParam<float>(layer_params, "alpha", 1);
641 float beta = getParam<float>(layer_params, "beta", 0);
643 CV_Error(Error::StsNotImplemented, "Non-zero beta");
644 CV_Assert(!bottom_layer.empty());
645 int from = std::atoi(bottom_layer.c_str());
647 from = from < 0 ? from + layers_counter : from;
648 setParams.setShortcut(from, alpha);
650 else if (layer_type == "upsample")
652 int scaleFactor = getParam<int>(layer_params, "stride", 1);
653 setParams.setUpsample(scaleFactor);
655 else if (layer_type == "yolo")
657 int classes = getParam<int>(layer_params, "classes", -1);
658 int num_of_anchors = getParam<int>(layer_params, "num", -1);
659 float thresh = getParam<float>(layer_params, "thresh", 0.2);
660 float nms_threshold = getParam<float>(layer_params, "nms_threshold", 0.4);
662 std::string anchors_values = getParam<std::string>(layer_params, "anchors", std::string());
663 CV_Assert(!anchors_values.empty());
664 std::vector<float> anchors_vec = getNumbers<float>(anchors_values);
666 std::string mask_values = getParam<std::string>(layer_params, "mask", std::string());
667 CV_Assert(!mask_values.empty());
668 std::vector<int> mask_vec = getNumbers<int>(mask_values);
670 CV_Assert(classes > 0 && num_of_anchors > 0 && (num_of_anchors * 2) == anchors_vec.size());
672 setParams.setPermute(false);
673 setParams.setYolo(classes, mask_vec, anchors_vec, thresh, nms_threshold);
676 CV_Error(cv::Error::StsParseError, "Unknown layer type: " + layer_type);
679 std::string activation = getParam<std::string>(layer_params, "activation", "linear");
680 if (activation == "leaky")
684 else if (activation != "linear")
685 CV_Error(cv::Error::StsParseError, "Unsupported activation: " + activation);
687 net->out_channels_vec[layers_counter] = current_channels;
693 bool ReadDarknetFromWeightsStream(std::istream &ifile, NetParameter *net)
695 int32_t major_ver, minor_ver, revision;
696 ifile.read(reinterpret_cast<char *>(&major_ver), sizeof(int32_t));
697 ifile.read(reinterpret_cast<char *>(&minor_ver), sizeof(int32_t));
698 ifile.read(reinterpret_cast<char *>(&revision), sizeof(int32_t));
701 if ((major_ver * 10 + minor_ver) >= 2) {
702 ifile.read(reinterpret_cast<char *>(&seen), sizeof(uint64_t));
706 ifile.read(reinterpret_cast<char *>(&iseen), sizeof(int32_t));
709 bool transpose = (major_ver > 1000) || (minor_ver > 1000);
711 CV_Error(cv::Error::StsNotImplemented, "Transpose the weights (except for convolutional) is not implemented");
713 int current_channels = net->channels;
714 int cv_layers_counter = -1;
715 int darknet_layers_counter = -1;
717 setLayersParams setParams(net);
719 typedef std::map<int, std::map<std::string, std::string> >::iterator it_type;
720 for (it_type i = net->layers_cfg.begin(); i != net->layers_cfg.end(); ++i) {
721 ++darknet_layers_counter;
723 std::map<std::string, std::string> &layer_params = i->second;
724 std::string layer_type = layer_params["type"];
726 if (layer_type == "convolutional")
728 int kernel_size = getParam<int>(layer_params, "size", -1);
729 int filters = getParam<int>(layer_params, "filters", -1);
730 bool use_batch_normalize = getParam<int>(layer_params, "batch_normalize", 0) == 1;
732 CV_Assert(kernel_size > 0 && filters > 0);
733 CV_Assert(current_channels > 0);
735 size_t const weights_size = filters * current_channels * kernel_size * kernel_size;
736 int sizes_weights[] = { filters, current_channels, kernel_size, kernel_size };
738 weightsBlob.create(4, sizes_weights, CV_32F);
739 CV_Assert(weightsBlob.isContinuous());
741 cv::Mat meanData_mat(1, filters, CV_32F); // mean
742 cv::Mat stdData_mat(1, filters, CV_32F); // variance
743 cv::Mat weightsData_mat(1, filters, CV_32F);// scale
744 cv::Mat biasData_mat(1, filters, CV_32F); // bias
746 ifile.read(reinterpret_cast<char *>(biasData_mat.ptr<float>()), sizeof(float)*filters);
747 if (use_batch_normalize) {
748 ifile.read(reinterpret_cast<char *>(weightsData_mat.ptr<float>()), sizeof(float)*filters);
749 ifile.read(reinterpret_cast<char *>(meanData_mat.ptr<float>()), sizeof(float)*filters);
750 ifile.read(reinterpret_cast<char *>(stdData_mat.ptr<float>()), sizeof(float)*filters);
752 ifile.read(reinterpret_cast<char *>(weightsBlob.ptr<float>()), sizeof(float)*weights_size);
754 // set convolutional weights
755 std::vector<cv::Mat> conv_blobs;
756 conv_blobs.push_back(weightsBlob);
757 if (!use_batch_normalize) {
758 // use BIAS in any case
759 conv_blobs.push_back(biasData_mat);
761 setParams.setLayerBlobs(cv_layers_counter, conv_blobs);
763 // set batch normalize (mean, variance, scale, bias)
764 if (use_batch_normalize) {
766 std::vector<cv::Mat> bn_blobs;
767 bn_blobs.push_back(meanData_mat);
768 bn_blobs.push_back(stdData_mat);
769 bn_blobs.push_back(weightsData_mat);
770 bn_blobs.push_back(biasData_mat);
771 setParams.setLayerBlobs(cv_layers_counter, bn_blobs);
774 if (layer_type == "region" || layer_type == "yolo")
776 ++cv_layers_counter; // For permute.
779 std::string activation = getParam<std::string>(layer_params, "activation", "linear");
780 if(activation == "leaky")
781 ++cv_layers_counter; // For ReLU
783 current_channels = net->out_channels_vec[darknet_layers_counter];
791 void ReadNetParamsFromCfgStreamOrDie(std::istream &ifile, darknet::NetParameter *net)
793 if (!darknet::ReadDarknetFromCfgStream(ifile, net)) {
794 CV_Error(cv::Error::StsParseError, "Failed to parse NetParameter stream");
798 void ReadNetParamsFromBinaryStreamOrDie(std::istream &ifile, darknet::NetParameter *net)
800 if (!darknet::ReadDarknetFromWeightsStream(ifile, net)) {
801 CV_Error(cv::Error::StsParseError, "Failed to parse NetParameter stream");