1 // This file is part of OpenCV project.
2 // It is subject to the license terms in the LICENSE file found in the top-level directory
3 // of this distribution and at http://opencv.org/license.html.
5 // Copyright (C) 2018, Intel Corporation, all rights reserved.
6 // Third party copyrights are property of their respective owners.
8 #include "../precomp.hpp"
9 #include <opencv2/dnn/shape_utils.hpp>
20 #if defined(__GNUC__) && __GNUC__ >= 5
21 #pragma GCC diagnostic push
22 #pragma GCC diagnostic ignored "-Wsuggest-override"
24 #include "opencv-onnx.pb.h"
25 #if defined(__GNUC__) && __GNUC__ >= 5
26 #pragma GCC diagnostic pop
29 #include "onnx_graph_simplifier.hpp"
33 CV__DNN_INLINE_NS_BEGIN
38 opencv_onnx::ModelProto model_proto;
42 LayerInfo(int _layerId, int _outputId) : layerId(_layerId), outputId(_outputId) {}
45 std::map<std::string, Mat> getGraphTensors(
46 const opencv_onnx::GraphProto& graph_proto);
47 Mat getBlob(const opencv_onnx::NodeProto& node_proto, const std::map<std::string, Mat>& constBlobs, int index);
49 LayerParams getLayerParams(const opencv_onnx::NodeProto& node_proto);
50 bool isCeilMode(const LayerParams& layerParams);
54 ONNXImporter(const char *onnxFile)
56 std::fstream input(onnxFile, std::ios::in | std::ios::binary);
58 if (!model_proto.ParseFromIstream(&input))
59 CV_Error(Error::StsUnsupportedFormat, "Failed to parse onnx model");
62 ONNXImporter(const char* buffer, size_t sizeBuffer)
64 struct _Buf : public std::streambuf
66 _Buf(const char* buffer, size_t sizeBuffer)
68 char* p = const_cast<char*>(buffer);
69 setg(p, p, p + sizeBuffer);
73 _Buf buf(buffer, sizeBuffer);
74 std::istream input(&buf);
76 if (!model_proto.ParseFromIstream(&input))
77 CV_Error(Error::StsUnsupportedFormat, "Failed to parse onnx model from in-memory byte array.");
80 void populateNet(Net dstNet);
83 inline void replaceLayerParam(LayerParams& layerParams, const String& oldKey, const String& newKey)
85 if (layerParams.has(oldKey)) {
86 layerParams.set(newKey, layerParams.get(oldKey));
87 layerParams.erase(oldKey);
91 void releaseONNXTensor(opencv_onnx::TensorProto& tensor_proto)
93 if (!tensor_proto.raw_data().empty()) {
94 delete tensor_proto.release_raw_data();
98 template<typename T1, typename T2>
99 void convertInt64ToInt32(const T1& src, T2& dst, int size)
101 for (int i = 0; i < size; i++) {
102 if (src[i] < std::numeric_limits<int32_t>::min() || src[i] > std::numeric_limits<int32_t>::max()) {
103 CV_Error(Error::StsOutOfRange, "Input is out of OpenCV 32S range");
105 dst[i] = saturate_cast<int32_t>(src[i]);
109 Mat getMatFromTensor(opencv_onnx::TensorProto& tensor_proto)
111 CV_Assert(!tensor_proto.raw_data().empty() || !tensor_proto.float_data().empty()
112 || !tensor_proto.double_data().empty() || !tensor_proto.int64_data().empty());
114 opencv_onnx::TensorProto_DataType datatype = tensor_proto.data_type();
116 std::vector<int> sizes;
117 for (int i = 0; i < tensor_proto.dims_size(); i++) {
118 sizes.push_back(tensor_proto.dims(i));
122 if (datatype == opencv_onnx::TensorProto_DataType_FLOAT) {
124 if (!tensor_proto.float_data().empty()) {
125 const ::google::protobuf::RepeatedField<float> field = tensor_proto.float_data();
126 Mat(sizes, CV_32FC1, (void*)field.data()).copyTo(blob);
129 char* val = const_cast<char*>(tensor_proto.raw_data().c_str());
130 Mat(sizes, CV_32FC1, val).copyTo(blob);
133 else if (datatype == opencv_onnx::TensorProto_DataType_DOUBLE)
135 const ::google::protobuf::RepeatedField<double> field = tensor_proto.double_data();
136 CV_Assert(!field.empty());
137 Mat(sizes, CV_64FC1, (void*)field.data()).convertTo(blob, CV_32FC1);
139 else if (datatype == opencv_onnx::TensorProto_DataType_INT64)
141 blob.create(sizes, CV_32SC1);
142 int32_t* dst = reinterpret_cast<int32_t*>(blob.data);
144 if (!tensor_proto.int64_data().empty()) {
145 ::google::protobuf::RepeatedField< ::google::protobuf::int64> src = tensor_proto.int64_data();
146 convertInt64ToInt32(src, dst, blob.total());
150 const char* val = tensor_proto.raw_data().c_str();
151 #if CV_STRONG_ALIGNMENT
152 // Aligned pointer is required: https://github.com/opencv/opencv/issues/16373
153 // this doesn't work: typedef int64_t CV_DECL_ALIGNED(1) unaligned_int64_t;
154 AutoBuffer<int64_t, 16> aligned_val;
155 if (!isAligned<sizeof(int64_t)>(val))
157 size_t sz = tensor_proto.raw_data().size();
158 aligned_val.allocate(divUp(sz, sizeof(int64_t)));
159 memcpy(aligned_val.data(), val, sz);
160 val = (const char*)aligned_val.data();
163 const int64_t* src = reinterpret_cast<const int64_t*>(val);
164 convertInt64ToInt32(src, dst, blob.total());
168 CV_Error(Error::StsUnsupportedFormat, "Unsupported data type: " +
169 opencv_onnx::TensorProto_DataType_Name(datatype));
170 if (tensor_proto.dims_size() == 0)
171 blob.dims = 1; // To force 1-dimensional cv::Mat for scalars.
175 void runLayer(LayerParams& params, const std::vector<Mat>& inputs,
176 std::vector<Mat>& outputs)
178 Ptr<Layer> layer = LayerFactory::createLayerInstance(params.type, params);
179 CV_Assert((bool)layer);
181 std::vector<MatShape> inpShapes(inputs.size());
183 for (size_t i = 0; i < inputs.size(); ++i)
185 inpShapes[i] = shape(inputs[i]);
186 if (i > 0 && ddepth != inputs[i].depth())
187 CV_Error(Error::StsNotImplemented, "Mixed input data types.");
188 ddepth = inputs[i].depth();
191 std::vector<MatShape> outShapes, internalShapes;
192 layer->getMemoryShapes(inpShapes, 0, outShapes, internalShapes);
194 std::vector<Mat> internals(internalShapes.size());
195 outputs.resize(outShapes.size());
196 for (size_t i = 0; i < outShapes.size(); ++i)
197 outputs[i].create(outShapes[i], ddepth);
198 for (size_t i = 0; i < internalShapes.size(); ++i)
199 internals[i].create(internalShapes[i], ddepth);
201 layer->finalize(inputs, outputs);
202 layer->forward(inputs, outputs, internals);
205 std::map<std::string, Mat> ONNXImporter::getGraphTensors(
206 const opencv_onnx::GraphProto& graph_proto)
208 opencv_onnx::TensorProto tensor_proto;
209 std::map<std::string, Mat> layers_weights;
211 for (int i = 0; i < graph_proto.initializer_size(); i++)
213 tensor_proto = graph_proto.initializer(i);
214 Mat mat = getMatFromTensor(tensor_proto);
215 releaseONNXTensor(tensor_proto);
216 layers_weights.insert(std::make_pair(tensor_proto.name(), mat));
218 return layers_weights;
221 static DictValue parse(const ::google::protobuf::RepeatedField< ::google::protobuf::int64>& src) {
222 std::vector<int32_t> dst(src.size());
223 convertInt64ToInt32(src, dst, src.size());
224 return DictValue::arrayInt(&dst[0], src.size());
227 LayerParams ONNXImporter::getLayerParams(const opencv_onnx::NodeProto& node_proto)
230 for(int i = 0; i < node_proto.attribute_size(); i++)
232 opencv_onnx::AttributeProto attribute_proto = node_proto.attribute(i);
233 std::string attribute_name = attribute_proto.name();
235 if(attribute_name == "kernel_shape")
237 CV_Assert(attribute_proto.ints_size() == 2 || attribute_proto.ints_size() == 3);
238 lp.set("kernel_size", parse(attribute_proto.ints()));
240 else if(attribute_name == "strides")
242 CV_Assert(attribute_proto.ints_size() == 2 || attribute_proto.ints_size() == 3);
243 lp.set("stride", parse(attribute_proto.ints()));
245 else if(attribute_name == "pads")
247 if (node_proto.op_type() == "Pad")
250 // Paddings are in order begin0, begin1, .. beginN, end0, end1, ..., endN.
251 // We need to shuffle it to begin0, end0, begin1, end1, ...
252 CV_Assert(attribute_proto.ints_size() % 2 == 0);
253 const int dims = attribute_proto.ints_size() / 2;
254 std::vector<int32_t> paddings;
255 paddings.reserve(attribute_proto.ints_size());
256 for (int i = 0; i < dims; ++i)
258 paddings.push_back(attribute_proto.ints(i));
259 paddings.push_back(attribute_proto.ints(dims + i));
261 lp.set("paddings", DictValue::arrayInt(&paddings[0], paddings.size()));
265 // Convolution or pooling.
266 CV_Assert(attribute_proto.ints_size() == 4 || attribute_proto.ints_size() == 6);
267 lp.set("pad", parse(attribute_proto.ints()));
270 else if(attribute_name == "auto_pad")
272 if (attribute_proto.s() == "SAME_UPPER" || attribute_proto.s() == "SAME_LOWER") {
273 lp.set("pad_mode", "SAME");
275 else if (attribute_proto.s() == "VALID") {
276 lp.set("pad_mode", "VALID");
279 else if(attribute_name == "dilations")
281 CV_Assert(attribute_proto.ints_size() == 2 || attribute_proto.ints_size() == 3);
282 lp.set("dilation", parse(attribute_proto.ints()));
284 else if (attribute_proto.has_i())
286 ::google::protobuf::int64 src = attribute_proto.i();
287 if (src < std::numeric_limits<int32_t>::min() || src > std::numeric_limits<int32_t>::max())
288 CV_Error(Error::StsOutOfRange, "Input is out of OpenCV 32S range");
290 lp.set(attribute_name, saturate_cast<int32_t>(src));
292 else if (attribute_proto.has_f())
294 lp.set(attribute_name, attribute_proto.f());
296 else if (attribute_proto.has_s())
298 lp.set(attribute_name, attribute_proto.s());
300 else if (attribute_proto.floats_size() > 0)
302 lp.set(attribute_name, DictValue::arrayReal(
303 attribute_proto.floats().data(), attribute_proto.floats_size()));
305 else if (attribute_proto.ints_size() > 0)
307 lp.set(attribute_proto.name(), parse(attribute_proto.ints()));
309 else if (attribute_proto.has_t())
311 opencv_onnx::TensorProto tensor = attribute_proto.t();
312 Mat blob = getMatFromTensor(tensor);
313 lp.blobs.push_back(blob);
315 else if (attribute_proto.has_g() || attribute_proto.strings_size() > 0 ||
316 attribute_proto.tensors_size() > 0 || attribute_proto.graphs_size() > 0)
318 CV_Error(Error::StsNotImplemented, "Unexpected attribute type");
321 CV_Error(Error::StsNotImplemented, "Unsupported attribute type");
326 Mat ONNXImporter::getBlob(const opencv_onnx::NodeProto& node_proto,
327 const std::map<std::string, Mat>& constBlobs, int index)
329 CV_Assert(index < node_proto.input_size());
330 std::map<std::string, Mat>::const_iterator constBlob;
331 constBlob = constBlobs.find(node_proto.input(index));
332 if (constBlob == constBlobs.end()) {
333 CV_Error(Error::StsObjectNotFound,
334 "Blob " + node_proto.input(index) + " not found in const blobs");
336 return constBlob->second;
339 void ONNXImporter::populateNet(Net dstNet)
341 CV_Assert(model_proto.has_graph());
342 opencv_onnx::GraphProto graph_proto = model_proto.graph();
344 simplifySubgraphs(graph_proto);
346 std::map<std::string, Mat> constBlobs = getGraphTensors(graph_proto);
347 // List of internal blobs shapes.
348 std::map<std::string, MatShape> outShapes;
349 // Add all the inputs shapes. It includes as constant blobs as network's inputs shapes.
350 for (int i = 0; i < graph_proto.input_size(); ++i)
352 opencv_onnx::ValueInfoProto valueInfoProto = graph_proto.input(i);
353 CV_Assert(valueInfoProto.has_type());
354 opencv_onnx::TypeProto typeProto = valueInfoProto.type();
355 CV_Assert(typeProto.has_tensor_type());
356 opencv_onnx::TypeProto::Tensor tensor = typeProto.tensor_type();
357 CV_Assert(tensor.has_shape());
358 opencv_onnx::TensorShapeProto tensorShape = tensor.shape();
360 MatShape inpShape(tensorShape.dim_size());
361 for (int j = 0; j < inpShape.size(); ++j)
363 inpShape[j] = tensorShape.dim(j).dim_value();
365 outShapes[valueInfoProto.name()] = inpShape;
368 std::string framework_name;
369 if (model_proto.has_producer_name()) {
370 framework_name = model_proto.producer_name();
373 // create map with network inputs (without const blobs)
374 std::map<std::string, LayerInfo> layer_id;
375 std::map<std::string, LayerInfo>::iterator layerId;
376 std::map<std::string, MatShape>::iterator shapeIt;
377 // fill map: push layer name, layer id and output id
378 std::vector<String> netInputs;
379 for (int j = 0; j < graph_proto.input_size(); j++)
381 const std::string& name = graph_proto.input(j).name();
382 if (constBlobs.find(name) == constBlobs.end()) {
383 netInputs.push_back(name);
384 layer_id.insert(std::make_pair(name, LayerInfo(0, netInputs.size() - 1)));
387 dstNet.setInputsNames(netInputs);
389 int layersSize = graph_proto.node_size();
390 LayerParams layerParams;
391 opencv_onnx::NodeProto node_proto;
393 for(int li = 0; li < layersSize; li++)
395 node_proto = graph_proto.node(li);
396 layerParams = getLayerParams(node_proto);
397 CV_Assert(node_proto.output_size() >= 1);
398 layerParams.name = node_proto.output(0);
400 std::string layer_type = node_proto.op_type();
401 layerParams.type = layer_type;
404 if (layer_type == "MaxPool")
406 layerParams.type = "Pooling";
407 layerParams.set("pool", "MAX");
408 layerParams.set("ceil_mode", layerParams.has("pad_mode"));
410 else if (layer_type == "AveragePool")
412 layerParams.type = "Pooling";
413 layerParams.set("pool", "AVE");
414 layerParams.set("ceil_mode", layerParams.has("pad_mode"));
415 layerParams.set("ave_pool_padded_area", framework_name == "pytorch");
417 else if (layer_type == "GlobalAveragePool" || layer_type == "GlobalMaxPool" || layer_type == "ReduceMean")
419 CV_Assert(node_proto.input_size() == 1);
420 layerParams.type = "Pooling";
421 layerParams.set("pool", layer_type == "GlobalMaxPool"? "MAX" : "AVE");
422 layerParams.set("global_pooling", layer_type == "GlobalAveragePool" || layer_type == "GlobalMaxPool");
424 if (layer_type == "ReduceMean")
426 if (layerParams.get<int>("keepdims") == 0 || !layerParams.has("axes"))
427 CV_Error(Error::StsNotImplemented, "Unsupported mode of ReduceMean operation.");
429 MatShape inpShape = outShapes[node_proto.input(0)];
430 if (inpShape.size() != 4 && inpShape.size() != 5)
431 CV_Error(Error::StsNotImplemented, "Unsupported input shape of reduce_mean operation.");
433 DictValue axes = layerParams.get("axes");
434 CV_Assert(axes.size() <= inpShape.size() - 2);
435 std::vector<int> kernel_size(inpShape.size() - 2, 1);
436 for (int i = 0; i < axes.size(); i++) {
437 int axis = axes.get<int>(i);
438 CV_Assert_N(axis >= 2 + i, axis < inpShape.size());
439 kernel_size[axis - 2] = inpShape[axis];
442 layerParams.set("kernel_size", DictValue::arrayInt(&kernel_size[0], kernel_size.size()));
445 else if (layer_type == "Slice")
447 if (layerParams.has("steps")) {
448 DictValue steps = layerParams.get("steps");
449 for (int i = 0; i < steps.size(); ++i) {
450 if (steps.get<int>(i) != 1)
451 CV_Error(Error::StsNotImplemented,
452 "Slice layer only supports steps = 1");
457 if (layerParams.has("axes")) {
458 DictValue axes = layerParams.get("axes");
459 for (int i = 1; i < axes.size(); ++i) {
460 CV_Assert(axes.get<int>(i - 1) == axes.get<int>(i) - 1);
462 axis = axes.get<int>(0);
464 layerParams.set("axis", axis);
466 DictValue starts = layerParams.get("starts");
467 DictValue ends = layerParams.get("ends");
468 CV_Assert(starts.size() == ends.size());
470 std::vector<int> begin;
471 std::vector<int> end;
473 begin.resize(axis, 0);
474 end.resize(axis, -1);
477 for (int i = 0; i < starts.size(); ++i)
479 begin.push_back(starts.get<int>(i));
480 int finish = ends.get<int>(i);
481 end.push_back((finish < 0) ? --finish : finish); // numpy doesn't include last dim
483 layerParams.set("begin", DictValue::arrayInt(&begin[0], begin.size()));
484 layerParams.set("end", DictValue::arrayInt(&end[0], end.size()));
486 else if (layer_type == "Split")
488 DictValue splits = layerParams.get("split");
489 const int numSplits = splits.size();
490 CV_Assert(numSplits > 1);
492 std::vector<int> slicePoints(numSplits - 1, splits.get<int>(0));
493 for (int i = 1; i < splits.size() - 1; ++i)
495 slicePoints[i] = slicePoints[i - 1] + splits.get<int>(i - 1);
497 layerParams.set("slice_point", DictValue::arrayInt(&slicePoints[0], slicePoints.size()));
498 layerParams.type = "Slice";
500 else if (layer_type == "Add" || layer_type == "Sum")
502 if (layer_id.find(node_proto.input(1)) == layer_id.end())
504 Mat blob = getBlob(node_proto, constBlobs, 1);
505 blob = blob.reshape(1, 1);
506 if (blob.total() == 1) {
507 layerParams.type = "Power";
508 layerParams.set("shift", blob.at<float>(0));
511 layerParams.type = "Scale";
512 layerParams.set("bias_term", true);
513 layerParams.blobs.push_back(blob);
517 layerParams.type = "Eltwise";
520 else if (layer_type == "Max")
522 layerParams.type = "Eltwise";
523 layerParams.set("operation", "max");
525 else if (layer_type == "Sub")
527 Mat blob = getBlob(node_proto, constBlobs, 1);
528 if (blob.total() == 1) {
529 layerParams.type = "Power";
530 layerParams.set("shift", -blob.at<float>(0));
533 layerParams.type = "Scale";
534 layerParams.set("has_bias", true);
535 layerParams.blobs.push_back(-1.0f * blob.reshape(1, 1));
538 else if (layer_type == "Div")
540 if (constBlobs.find(node_proto.input(1)) == constBlobs.end())
542 layerParams.type = "Eltwise";
543 layerParams.set("operation", "div");
547 Mat blob = getBlob(node_proto, constBlobs, 1);
548 CV_Assert_N(blob.type() == CV_32F, blob.total());
549 if (blob.total() == 1)
551 layerParams.set("scale", 1.0f / blob.at<float>(0));
552 layerParams.type = "Power";
556 layerParams.type = "Scale";
557 divide(1.0, blob, blob);
558 layerParams.blobs.push_back(blob);
559 layerParams.set("bias_term", false);
563 else if (layer_type == "Neg")
565 layerParams.type = "Power";
566 layerParams.set("scale", -1);
568 else if (layer_type == "Constant")
570 CV_Assert(node_proto.input_size() == 0);
571 CV_Assert(layerParams.blobs.size() == 1);
572 constBlobs.insert(std::make_pair(layerParams.name, layerParams.blobs[0]));
575 else if (layer_type == "ImageScaler")
577 const float scale = layerParams.has("scale") ? layerParams.get<float>("scale") : 1.0f;
578 layerParams.erase("scale");
580 if (layerParams.has("bias"))
582 layerParams.type = "Scale";
583 layerParams.blobs.push_back(
584 Mat(Size(1, layerParams.get("bias").size()), CV_32FC1, scale));
586 layerParams.set("bias_term", true);
587 Mat bias(1, layerParams.get("bias").size(), CV_32FC1);
588 for (int j = 0; j < bias.total(); j++) {
589 bias.at<float>(0, j) = layerParams.get("bias").getRealValue(j);
591 layerParams.blobs.push_back(bias);
592 layerParams.erase("bias");
595 layerParams.set("scale", scale);
596 layerParams.type = "Power";
599 else if (layer_type == "Clip")
601 layerParams.type = "ReLU6";
602 replaceLayerParam(layerParams, "min", "min_value");
603 replaceLayerParam(layerParams, "max", "max_value");
606 else if (layer_type == "LeakyRelu")
608 layerParams.type = "ReLU";
609 replaceLayerParam(layerParams, "alpha", "negative_slope");
611 else if (layer_type == "LRN")
613 replaceLayerParam(layerParams, "size", "local_size");
615 else if (layer_type == "InstanceNormalization")
617 if (node_proto.input_size() != 3)
618 CV_Error(Error::StsNotImplemented,
619 "Expected input, scale, bias");
621 layerParams.blobs.resize(4);
622 layerParams.blobs[2] = getBlob(node_proto, constBlobs, 1); // weightData
623 layerParams.blobs[3] = getBlob(node_proto, constBlobs, 2); // biasData
624 layerParams.set("has_bias", true);
625 layerParams.set("has_weight", true);
627 // Get number of channels in input
628 int size = layerParams.blobs[2].total();
629 layerParams.blobs[0] = Mat::zeros(size, 1, CV_32F); // mean
630 layerParams.blobs[1] = Mat::ones(size, 1, CV_32F); // std
632 LayerParams mvnParams;
633 mvnParams.name = layerParams.name + "/MVN";
634 mvnParams.type = "MVN";
635 mvnParams.set("eps", layerParams.get<float>("epsilon"));
636 layerParams.erase("epsilon");
639 int id = dstNet.addLayer(mvnParams.name, mvnParams.type, mvnParams);
641 layerId = layer_id.find(node_proto.input(0));
642 CV_Assert(layerId != layer_id.end());
643 dstNet.connect(layerId->second.layerId, layerId->second.outputId, id, 0);
645 layer_id.insert(std::make_pair(mvnParams.name, LayerInfo(id, 0)));
646 outShapes[mvnParams.name] = outShapes[node_proto.input(0)];
648 //Replace Batch Norm's input to MVN
649 node_proto.set_input(0, mvnParams.name);
650 layerParams.type = "BatchNorm";
652 else if (layer_type == "BatchNormalization")
654 if (node_proto.input_size() != 5)
655 CV_Error(Error::StsNotImplemented,
656 "Expected input, scale, bias, mean and var");
658 layerParams.type = "BatchNorm";
659 replaceLayerParam(layerParams, "epsilon", "eps");
660 replaceLayerParam(layerParams, "spatial", "use_global_stats");
662 Mat meanData = getBlob(node_proto, constBlobs, 3);
663 Mat stdData = getBlob(node_proto, constBlobs, 4);
665 layerParams.blobs.push_back(meanData);
666 layerParams.blobs.push_back(stdData);
668 if (!node_proto.input(1).empty()) {
669 layerParams.set("has_weight", true);
670 layerParams.blobs.push_back(getBlob(node_proto, constBlobs, 1)); // weightData
672 layerParams.set("has_weight", false);
675 if (!node_proto.input(2).empty()) {
676 layerParams.set("has_bias", true);
677 layerParams.blobs.push_back(getBlob(node_proto, constBlobs, 2)); // biasData
679 layerParams.set("has_bias", false);
682 else if (layer_type == "Gemm")
684 CV_Assert(node_proto.input_size() >= 2);
685 layerParams.type = "InnerProduct";
686 Mat weights = getBlob(node_proto, constBlobs, 1);
688 if (layerParams.has("transB") && !layerParams.get<int>("transB")) {
689 transpose(weights, weights);
692 layerParams.blobs.push_back(weights);
694 if (node_proto.input_size() == 3) {
695 Mat bias = getBlob(node_proto, constBlobs, 2);
696 layerParams.blobs.push_back(bias);
699 layerParams.set("num_output", layerParams.blobs[0].size[ind_num_out]);
700 layerParams.set("bias_term", node_proto.input_size() == 3);
702 else if (layer_type == "MatMul")
704 CV_Assert(node_proto.input_size() == 2);
705 layerParams.type = "InnerProduct";
706 Mat blob = getBlob(node_proto, constBlobs, 1);
707 layerParams.blobs.push_back(blob.t());
708 layerParams.set("bias_term", false);
709 layerParams.set("num_output", layerParams.blobs[0].size[0]);
711 else if (layer_type == "Mul")
713 CV_Assert(node_proto.input_size() == 2);
714 if (layer_id.find(node_proto.input(1)) == layer_id.end()) {
715 Mat blob = getBlob(node_proto, constBlobs, 1);
716 blob = blob.reshape(1, 1);
717 if (blob.total() == 1) {
718 layerParams.set("scale", blob.at<float>(0));
719 layerParams.type = "Power";
722 layerParams.blobs.push_back(blob);
723 layerParams.type = "Scale";
727 layerParams.type = "Eltwise";
728 layerParams.set("operation", "prod");
731 else if (layer_type == "Conv")
733 CV_Assert(node_proto.input_size() >= 2);
734 layerParams.type = "Convolution";
735 for (int j = 1; j < node_proto.input_size(); j++) {
736 layerParams.blobs.push_back(getBlob(node_proto, constBlobs, j));
738 layerParams.set("num_output", layerParams.blobs[0].size[0]);
739 layerParams.set("bias_term", node_proto.input_size() == 3);
741 else if (layer_type == "ConvTranspose")
743 CV_Assert(node_proto.input_size() >= 2);
744 layerParams.type = "Deconvolution";
745 for (int j = 1; j < node_proto.input_size(); j++) {
746 layerParams.blobs.push_back(getBlob(node_proto, constBlobs, j));
748 layerParams.set("num_output", layerParams.blobs[0].size[1] * layerParams.get<int>("group", 1));
749 layerParams.set("bias_term", node_proto.input_size() == 3);
751 if (!layerParams.has("kernel_size"))
752 CV_Error(Error::StsNotImplemented,
753 "Required attribute 'kernel_size' is not present.");
755 if (layerParams.has("output_shape"))
757 const DictValue& outShape = layerParams.get("output_shape");
758 DictValue strides = layerParams.get("stride");
759 DictValue kernel = layerParams.get("kernel_size");
762 std::vector<int> adjust_pads;
763 if (layerParams.has("pad_mode"))
765 padMode = toUpperCase(layerParams.get<String>("pad_mode"));
766 if (padMode != "SAME" && padMode != "VALID")
767 CV_Error(Error::StsError, "Unsupported padding mode " + padMode);
769 for (int i = 0; i < strides.size(); i++)
771 int sz = outShape.get<int>(2 + i);
772 int stride = strides.get<int>(i);
773 adjust_pads.push_back(padMode == "SAME"? (sz - 1) % stride :
774 (sz - kernel.get<int>(i)) % stride);
776 layerParams.set("adj", DictValue::arrayInt(&adjust_pads[0], adjust_pads.size()));
779 else if (layerParams.has("output_padding"))
781 replaceLayerParam(layerParams, "output_padding", "adj");
784 else if (layer_type == "Transpose")
786 layerParams.type = "Permute";
787 replaceLayerParam(layerParams, "perm", "order");
789 CV_Assert(node_proto.input_size() == 1);
790 if (constBlobs.find(node_proto.input(0)) != constBlobs.end())
792 std::vector<Mat> inputs(1, getBlob(node_proto, constBlobs, 0)), transposed;
793 runLayer(layerParams, inputs, transposed);
794 CV_Assert(transposed.size() == 1);
795 constBlobs.insert(std::make_pair(layerParams.name, transposed[0]));
799 else if (layer_type == "ReduceL2")
801 CV_Assert_N(node_proto.input_size() == 1, layerParams.has("axes"));
802 CV_Assert(graph_proto.node_size() > li + 1 && graph_proto.node(li + 1).op_type() == "Div");
804 node_proto = graph_proto.node(li);
805 layerParams.name = node_proto.output(0);
806 layerParams.type = "Normalize";
808 DictValue axes_dict = layerParams.get("axes");
809 if (axes_dict.size() != 1)
810 CV_Error(Error::StsNotImplemented, "Multidimensional reduceL2");
811 int axis = axes_dict.getIntValue(0);
812 layerParams.set("axis",axis);
813 layerParams.set("end_axis", axis);
815 else if (layer_type == "Squeeze")
817 CV_Assert_N(node_proto.input_size() == 1, layerParams.has("axes"));
818 DictValue axes_dict = layerParams.get("axes");
819 if (axes_dict.size() != 1)
820 CV_Error(Error::StsNotImplemented, "Multidimensional squeeze");
822 int axis = axes_dict.getIntValue(0);
823 layerParams.set("axis", axis - 1);
824 layerParams.set("end_axis", axis);
825 layerParams.type = "Flatten";
827 else if (layer_type == "Unsqueeze")
829 CV_Assert(node_proto.input_size() == 1);
830 DictValue axes = layerParams.get("axes");
831 if (constBlobs.find(node_proto.input(0)) != constBlobs.end())
834 Mat input = getBlob(node_proto, constBlobs, 0);
836 std::vector<int> dims;
837 for (int j = 0; j < input.dims; j++) {
838 dims.push_back(input.size[j]);
840 CV_Assert(axes.getIntValue(axes.size()-1) <= dims.size());
841 for (int j = 0; j < axes.size(); j++) {
842 dims.insert(dims.begin() + axes.getIntValue(j), 1);
845 Mat out = input.reshape(0, dims);
846 constBlobs.insert(std::make_pair(layerParams.name, out));
851 if (axes.size() != 1)
852 CV_Error(Error::StsNotImplemented, "Multidimensional unsqueeze");
854 MatShape inpShape = outShapes[node_proto.input(0)];
855 int axis = axes.getIntValue(0);
856 CV_Assert(0 <= axis && axis <= inpShape.size());
857 std::vector<int> outShape = inpShape;
858 outShape.insert(outShape.begin() + axis, 1);
859 layerParams.type = "Reshape";
860 layerParams.set("dim", DictValue::arrayInt(&outShape[0], outShape.size()));
862 else if (layer_type == "Reshape")
864 CV_Assert(node_proto.input_size() == 2 || layerParams.has("shape"));
866 if (node_proto.input_size() == 2) {
867 Mat blob = getBlob(node_proto, constBlobs, 1);
868 CV_Assert(blob.type() == CV_32SC1);
870 layerParams.set("dim", DictValue::arrayInt<int*>(
871 blob.ptr<int>(), blob.total() ));
873 if (layer_id.find(node_proto.input(0)) == layer_id.end()) {
874 std::vector<Mat> inputs(1, getBlob(node_proto, constBlobs, 0)), outputs;
875 runLayer(layerParams, inputs, outputs);
876 constBlobs.insert(std::make_pair(layerParams.name, outputs[0]));
881 DictValue shape = layerParams.get("shape");
882 std::vector<int> dim;
883 for (int j = 0; j < shape.size(); j++) {
884 dim.push_back(shape.getIntValue(j));
887 if (layer_id.find(node_proto.input(0)) == layer_id.end()) {
888 Mat input = getBlob(node_proto, constBlobs, 0);
889 Mat out = input.reshape(0, dim);
890 constBlobs.insert(std::make_pair(layerParams.name, out));
893 replaceLayerParam(layerParams, "shape", "dim");
896 else if (layer_type == "Pad")
898 layerParams.type = "Padding";
900 else if (layer_type == "Shape")
902 CV_Assert(node_proto.input_size() == 1);
903 shapeIt = outShapes.find(node_proto.input(0));
904 CV_Assert(shapeIt != outShapes.end());
905 MatShape inpShape = shapeIt->second;
907 Mat shapeMat(inpShape.size(), 1, CV_32S);
908 for (int j = 0; j < inpShape.size(); ++j)
909 shapeMat.at<int>(j) = inpShape[j];
912 constBlobs.insert(std::make_pair(layerParams.name, shapeMat));
915 else if (layer_type == "Gather")
917 CV_Assert(node_proto.input_size() == 2);
918 CV_Assert(layerParams.has("axis"));
919 Mat input = getBlob(node_proto, constBlobs, 0);
920 Mat indexMat = getBlob(node_proto, constBlobs, 1);
921 CV_Assert_N(indexMat.type() == CV_32S, indexMat.total() == 1);
922 int index = indexMat.at<int>(0);
923 int axis = layerParams.get<int>("axis");
925 std::vector<cv::Range> ranges(input.dims, Range::all());
926 ranges[axis] = Range(index, index + 1);
928 Mat out = input(ranges);
929 constBlobs.insert(std::make_pair(layerParams.name, out));
932 else if (layer_type == "Concat")
934 bool hasVariableInps = false;
935 for (int i = 0; i < node_proto.input_size(); ++i)
937 if (layer_id.find(node_proto.input(i)) != layer_id.end())
939 hasVariableInps = true;
944 if (!hasVariableInps)
946 std::vector<Mat> inputs(node_proto.input_size()), concatenated;
947 for (size_t i = 0; i < inputs.size(); ++i)
949 inputs[i] = getBlob(node_proto, constBlobs, i);
951 runLayer(layerParams, inputs, concatenated);
953 CV_Assert(concatenated.size() == 1);
954 constBlobs.insert(std::make_pair(layerParams.name, concatenated[0]));
958 else if (layer_type == "Upsample")
960 layerParams.type = "Resize";
961 if (layerParams.has("scales"))
964 DictValue scales = layerParams.get("scales");
965 CV_Assert(scales.size() == 4);
966 layerParams.set("zoom_factor_y", scales.getIntValue(2));
967 layerParams.set("zoom_factor_x", scales.getIntValue(3));
972 replaceLayerParam(layerParams, "height_scale", "zoom_factor_y");
973 replaceLayerParam(layerParams, "width_scale", "zoom_factor_x");
975 replaceLayerParam(layerParams, "mode", "interpolation");
977 else if (layer_type == "LogSoftmax")
979 layerParams.type = "Softmax";
980 layerParams.set("log_softmax", true);
984 for (int j = 0; j < node_proto.input_size(); j++) {
985 if (layer_id.find(node_proto.input(j)) == layer_id.end())
986 layerParams.blobs.push_back(getBlob(node_proto, constBlobs, j));
990 int id = dstNet.addLayer(layerParams.name, layerParams.type, layerParams);
991 for (int i = 0; i < node_proto.output_size(); ++i)
993 layer_id.insert(std::make_pair(node_proto.output(i), LayerInfo(id, i)));
996 std::vector<MatShape> layerInpShapes, layerOutShapes, layerInternalShapes;
997 for (int j = 0; j < node_proto.input_size(); j++) {
998 layerId = layer_id.find(node_proto.input(j));
999 if (layerId != layer_id.end()) {
1000 dstNet.connect(layerId->second.layerId, layerId->second.outputId, id, j);
1001 // Collect input shapes.
1002 shapeIt = outShapes.find(node_proto.input(j));
1003 CV_Assert(shapeIt != outShapes.end());
1004 layerInpShapes.push_back(shapeIt->second);
1008 // Compute shape of output blob for this layer.
1009 Ptr<Layer> layer = dstNet.getLayer(id);
1010 layer->getMemoryShapes(layerInpShapes, 0, layerOutShapes, layerInternalShapes);
1011 for (int i = 0; i < node_proto.output_size() && i < (int)layerOutShapes.size(); ++i)
1013 outShapes[node_proto.output(i)] = layerOutShapes[i];
1018 Net readNetFromONNX(const String& onnxFile)
1020 ONNXImporter onnxImporter(onnxFile.c_str());
1022 onnxImporter.populateNet(net);
1026 Net readNetFromONNX(const char* buffer, size_t sizeBuffer)
1028 ONNXImporter onnxImporter(buffer, sizeBuffer);
1030 onnxImporter.populateNet(net);
1034 Net readNetFromONNX(const std::vector<uchar>& buffer)
1036 return readNetFromONNX(reinterpret_cast<const char*>(buffer.data()), buffer.size());
1039 Mat readTensorFromONNX(const String& path)
1041 opencv_onnx::TensorProto tensor_proto = opencv_onnx::TensorProto();
1042 std::fstream input(path.c_str(), std::ios::in | std::ios::binary);
1043 if (!tensor_proto.ParseFromIstream(&input)) {
1044 CV_Error(Error::StsUnsupportedFormat, "Failed to parse data");
1046 Mat mat = getMatFromTensor(tensor_proto);
1047 releaseONNXTensor(tensor_proto);
1051 CV__DNN_INLINE_NS_END