Path: blob/master/modules/dnn/src/onnx/onnx_importer.cpp
16339 views
// This file is part of OpenCV project.1// It is subject to the license terms in the LICENSE file found in the top-level directory2// of this distribution and at http://opencv.org/license.html.34// Copyright (C) 2018, Intel Corporation, all rights reserved.5// Third party copyrights are property of their respective owners.67#include "../precomp.hpp"89#ifdef HAVE_PROTOBUF1011#include <iostream>12#include <fstream>13#include <string>14#include <limits>15#include <algorithm>161718#if defined(__GNUC__) && __GNUC__ >= 519#pragma GCC diagnostic push20#pragma GCC diagnostic ignored "-Wsuggest-override"21#endif22#include "opencv-onnx.pb.h"23#if defined(__GNUC__) && __GNUC__ >= 524#pragma GCC diagnostic pop25#endif2627namespace cv {28namespace dnn {29CV__DNN_INLINE_NS_BEGIN303132class ONNXImporter33{34opencv_onnx::ModelProto model_proto;35struct LayerInfo {36int layerId;37int outputId;38LayerInfo(int _layerId, int _outputId) : layerId(_layerId), outputId(_outputId) {}39};4041std::map<std::string, Mat> getGraphTensors(42const opencv_onnx::GraphProto& graph_proto);43Mat getBlob(const opencv_onnx::NodeProto& node_proto, const std::map<std::string, Mat>& constBlobs, int index);4445LayerParams getLayerParams(const opencv_onnx::NodeProto& node_proto);46bool isCeilMode(const LayerParams& layerParams);4748public:4950ONNXImporter(const char *onnxFile)51{52std::fstream input(onnxFile, std::ios::in | std::ios::binary);5354if (!model_proto.ParseFromIstream(&input))55CV_Error(Error::StsUnsupportedFormat, "Failed to parse onnx model");56}5758void populateNet(Net dstNet);59};6061inline void replaceLayerParam(LayerParams& layerParams, const String& oldKey, const String& newKey)62{63if (layerParams.has(oldKey)) {64layerParams.set(newKey, layerParams.get(oldKey));65layerParams.erase(oldKey);66}67}6869void releaseONNXTensor(opencv_onnx::TensorProto& tensor_proto)70{71if (!tensor_proto.raw_data().empty()) {72delete tensor_proto.release_raw_data();73}74}7576template<typename T1, typename T2>77void convertInt64ToInt32(const T1& src, T2& dst, int size)78{79for (int i = 0; i < size; i++) {80if (src[i] < std::numeric_limits<int32_t>::min() || src[i] > std::numeric_limits<int32_t>::max()) {81CV_Error(Error::StsOutOfRange, "Input is out of OpenCV 32S range");82}83dst[i] = saturate_cast<int32_t>(src[i]);84}85}8687Mat getMatFromTensor(opencv_onnx::TensorProto& tensor_proto)88{89CV_Assert(!tensor_proto.raw_data().empty() || !tensor_proto.float_data().empty()90|| !tensor_proto.double_data().empty() || !tensor_proto.int64_data().empty());9192opencv_onnx::TensorProto_DataType datatype = tensor_proto.data_type();93Mat blob;94std::vector<int> sizes;95for (int i = 0; i < tensor_proto.dims_size(); i++) {96sizes.push_back(tensor_proto.dims(i));97}98if (datatype == opencv_onnx::TensorProto_DataType_FLOAT) {99100if (!tensor_proto.float_data().empty()) {101const ::google::protobuf::RepeatedField<float> field = tensor_proto.float_data();102Mat(sizes, CV_32FC1, (void*)field.data()).copyTo(blob);103}104else {105char* val = const_cast<char*>(tensor_proto.raw_data().c_str());106Mat(sizes, CV_32FC1, val).copyTo(blob);107}108}109else if (datatype == opencv_onnx::TensorProto_DataType_DOUBLE)110{111const ::google::protobuf::RepeatedField<double> field = tensor_proto.double_data();112CV_Assert(!field.empty());113Mat(sizes, CV_64FC1, (void*)field.data()).convertTo(blob, CV_32FC1);114}115else if (datatype == opencv_onnx::TensorProto_DataType_INT64)116{117blob.create(sizes, CV_32SC1);118int32_t* dst = reinterpret_cast<int32_t*>(blob.data);119120if (!tensor_proto.int64_data().empty()) {121::google::protobuf::RepeatedField< ::google::protobuf::int64> src = tensor_proto.int64_data();122convertInt64ToInt32(src, dst, blob.total());123}124else125{126char* val = const_cast<char*>(tensor_proto.raw_data().c_str());127int64_t* src = reinterpret_cast<int64_t*>(val);128convertInt64ToInt32(src, dst, blob.total());129}130}131else132CV_Error(Error::StsUnsupportedFormat, "Unsupported data type: " +133opencv_onnx::TensorProto_DataType_Name(datatype));134return blob;135}136137std::map<std::string, Mat> ONNXImporter::getGraphTensors(138const opencv_onnx::GraphProto& graph_proto)139{140opencv_onnx::TensorProto tensor_proto;141std::map<std::string, Mat> layers_weights;142143for (int i = 0; i < graph_proto.initializer_size(); i++)144{145tensor_proto = graph_proto.initializer(i);146Mat mat = getMatFromTensor(tensor_proto);147releaseONNXTensor(tensor_proto);148layers_weights.insert(std::make_pair(tensor_proto.name(), mat));149}150return layers_weights;151}152153LayerParams ONNXImporter::getLayerParams(const opencv_onnx::NodeProto& node_proto)154{155LayerParams lp;156for(int i = 0; i < node_proto.attribute_size(); i++)157{158opencv_onnx::AttributeProto attribute_proto = node_proto.attribute(i);159std::string attribute_name = attribute_proto.name();160161if(attribute_name == "kernel_shape")162{163CV_Assert(attribute_proto.ints_size() == 2);164lp.set("kernel_h", saturate_cast<int32_t>(attribute_proto.ints(0)));165lp.set("kernel_w", saturate_cast<int32_t>(attribute_proto.ints(1)));166}167else if(attribute_name == "strides")168{169CV_Assert(attribute_proto.ints_size() == 2);170lp.set("stride_h", saturate_cast<int32_t>(attribute_proto.ints(0)));171lp.set("stride_w", saturate_cast<int32_t>(attribute_proto.ints(1)));172}173else if(attribute_name == "pads")174{175CV_Assert(attribute_proto.ints_size() == 4);176lp.set("pad_t", saturate_cast<int32_t>(attribute_proto.ints(0)));177lp.set("pad_l", saturate_cast<int32_t>(attribute_proto.ints(1)));178lp.set("pad_b", saturate_cast<int32_t>(attribute_proto.ints(2)));179lp.set("pad_r", saturate_cast<int32_t>(attribute_proto.ints(3)));180}181else if(attribute_name == "auto_pad")182{183if (attribute_proto.s() == "SAME_UPPER" || attribute_proto.s() == "SAME_LOWER") {184lp.set("pad_mode", "SAME");185}186else if (attribute_proto.s() == "VALID") {187lp.set("pad_mode", "VALID");188}189}190else if(attribute_name == "dilations")191{192CV_Assert(attribute_proto.ints_size() == 2);193lp.set("dilation_h", saturate_cast<int32_t>(attribute_proto.ints(0)));194lp.set("dilation_w", saturate_cast<int32_t>(attribute_proto.ints(1)));195}196else if (attribute_proto.has_i())197{198::google::protobuf::int64 src = attribute_proto.i();199if (src < std::numeric_limits<int32_t>::min() || src > std::numeric_limits<int32_t>::max())200CV_Error(Error::StsOutOfRange, "Input is out of OpenCV 32S range");201else202lp.set(attribute_name, saturate_cast<int32_t>(src));203}204else if (attribute_proto.has_f())205{206lp.set(attribute_name, attribute_proto.f());207}208else if (attribute_proto.has_s())209{210lp.set(attribute_name, attribute_proto.s());211}212else if (attribute_proto.floats_size() > 0)213{214lp.set(attribute_name, DictValue::arrayReal(215attribute_proto.floats().data(), attribute_proto.floats_size()));216}217else if (attribute_proto.ints_size() > 0)218{219const ::google::protobuf::RepeatedField< ::google::protobuf::int64> src = attribute_proto.ints();220std::vector<int32_t> dst(attribute_proto.ints_size());221convertInt64ToInt32(src, dst, attribute_proto.ints_size());222lp.set(attribute_proto.name(), DictValue::arrayInt(&dst[0], attribute_proto.ints_size()));223}224else if (attribute_proto.has_t())225{226opencv_onnx::TensorProto tensor = attribute_proto.t();227Mat blob = getMatFromTensor(tensor);228lp.blobs.push_back(blob);229}230else if (attribute_proto.has_g() || attribute_proto.strings_size() > 0 ||231attribute_proto.tensors_size() > 0 || attribute_proto.graphs_size() > 0)232{233CV_Error(Error::StsNotImplemented, "Unexpected attribute type");234}235else236CV_Error(Error::StsNotImplemented, "Unsupported attribute type");237}238return lp;239}240241Mat ONNXImporter::getBlob(const opencv_onnx::NodeProto& node_proto,242const std::map<std::string, Mat>& constBlobs, int index)243{244CV_Assert(index < node_proto.input_size());245std::map<std::string, Mat>::const_iterator constBlob;246constBlob = constBlobs.find(node_proto.input(index));247if (constBlob == constBlobs.end()) {248CV_Error(Error::StsObjectNotFound,249"Blob " + node_proto.input(index) + " not found in const blobs");250}251return constBlob->second;252}253254255bool ONNXImporter::isCeilMode(const LayerParams& layerParams) {256if (!layerParams.has("pad_mode")) {257if (layerParams.has("pad_h")) {258return layerParams.get<int>("pad_h") != layerParams.get<int>("pad_b") ||259layerParams.get<int>("pad_w") != layerParams.get<int>("pad_r");260}261else262return false; // all pads == 0263}264return true;265}266267void ONNXImporter::populateNet(Net dstNet)268{269CV_Assert(model_proto.has_graph());270opencv_onnx::GraphProto graph_proto = model_proto.graph();271std::map<std::string, Mat> constBlobs = getGraphTensors(graph_proto);272273std::string framework_name;274if (model_proto.has_producer_name()) {275framework_name = model_proto.producer_name();276}277278// create map with network inputs (without const blobs)279std::map<std::string, LayerInfo> layer_id;280std::map<std::string, LayerInfo>::iterator layerId;281// fill map: push layer name, layer id and output id282std::vector<String> netInputs;283for (int j = 0; j < graph_proto.input_size(); j++)284{285const std::string& name = graph_proto.input(j).name();286if (constBlobs.find(name) == constBlobs.end()) {287netInputs.push_back(name);288layer_id.insert(std::make_pair(name, LayerInfo(0, netInputs.size() - 1)));289}290}291dstNet.setInputsNames(netInputs);292293int layersSize = graph_proto.node_size();294LayerParams layerParams;295opencv_onnx::NodeProto node_proto;296297for(int i = 0; i < layersSize; i++)298{299node_proto = graph_proto.node(i);300layerParams = getLayerParams(node_proto);301CV_Assert(node_proto.output_size() >= 1);302layerParams.name = node_proto.output(0);303304std::string layer_type = node_proto.op_type();305layerParams.type = layer_type;306307308if (layer_type == "MaxPool")309{310layerParams.type = "Pooling";311layerParams.set("pool", "MAX");312layerParams.set("ceil_mode", isCeilMode(layerParams));313}314else if (layer_type == "AveragePool")315{316layerParams.type = "Pooling";317layerParams.set("pool", "AVE");318layerParams.set("ceil_mode", isCeilMode(layerParams));319layerParams.set("ave_pool_padded_area", framework_name == "pytorch");320}321else if (layer_type == "GlobalAveragePool")322{323layerParams.type = "Pooling";324layerParams.set("pool", "AVE");325layerParams.set("global_pooling", true);326}327else if (layer_type == "Add" || layer_type == "Sum")328{329if (layer_id.find(node_proto.input(1)) == layer_id.end())330{331Mat blob = getBlob(node_proto, constBlobs, 1);332blob = blob.reshape(1, 1);333if (blob.total() == 1) {334layerParams.type = "Power";335layerParams.set("shift", blob.at<float>(0));336}337else {338layerParams.type = "Shift";339layerParams.blobs.push_back(blob);340}341}342else {343layerParams.type = "Eltwise";344}345}346else if (layer_type == "Sub")347{348Mat blob = (-1.0f) * getBlob(node_proto, constBlobs, 1);349blob = blob.reshape(1, 1);350if (blob.total() == 1) {351layerParams.type = "Power";352layerParams.set("shift", blob.at<float>(0));353}354else {355layerParams.type = "Shift";356layerParams.blobs.push_back(blob);357}358}359else if (layer_type == "Constant")360{361CV_Assert(node_proto.input_size() == 0);362CV_Assert(layerParams.blobs.size() == 1);363constBlobs.insert(std::make_pair(layerParams.name, layerParams.blobs[0]));364continue;365}366else if (layer_type == "ImageScaler")367{368const float scale = layerParams.has("scale") ? layerParams.get<float>("scale") : 1.0f;369layerParams.erase("scale");370371if (layerParams.has("bias"))372{373layerParams.type = "Scale";374layerParams.blobs.push_back(375Mat(Size(1, layerParams.get("bias").size()), CV_32FC1, scale));376377layerParams.set("bias_term", true);378Mat bias(1, layerParams.get("bias").size(), CV_32FC1);379for (int j = 0; j < bias.total(); j++) {380bias.at<float>(0, j) = layerParams.get("bias").getRealValue(j);381}382layerParams.blobs.push_back(bias);383layerParams.erase("bias");384}385else {386layerParams.set("scale", scale);387layerParams.type = "Power";388}389}390else if (layer_type == "LeakyRelu")391{392layerParams.type = "ReLU";393replaceLayerParam(layerParams, "alpha", "negative_slope");394}395else if (layer_type == "LRN")396{397replaceLayerParam(layerParams, "size", "local_size");398}399else if (layer_type == "BatchNormalization")400{401if (node_proto.input_size() != 5)402CV_Error(Error::StsNotImplemented,403"Expected input, scale, bias, mean and var");404405layerParams.type = "BatchNorm";406replaceLayerParam(layerParams, "epsilon", "eps");407replaceLayerParam(layerParams, "spatial", "use_global_stats");408409Mat meanData = getBlob(node_proto, constBlobs, 3);410Mat stdData = getBlob(node_proto, constBlobs, 4);411412layerParams.blobs.push_back(meanData);413layerParams.blobs.push_back(stdData);414415if (!node_proto.input(1).empty()) {416layerParams.set("has_weight", true);417layerParams.blobs.push_back(getBlob(node_proto, constBlobs, 1)); // weightData418} else {419layerParams.set("has_weight", false);420}421422if (!node_proto.input(2).empty()) {423layerParams.set("has_bias", true);424layerParams.blobs.push_back(getBlob(node_proto, constBlobs, 2)); // biasData425} else {426layerParams.set("has_bias", false);427}428}429else if (layer_type == "Gemm")430{431CV_Assert(node_proto.input_size() >= 2);432layerParams.type = "InnerProduct";433Mat weights = getBlob(node_proto, constBlobs, 1);434int ind_num_out = 0;435if (layerParams.has("transB") && !layerParams.get<int>("transB")) {436transpose(weights, weights);437ind_num_out = 1;438}439layerParams.blobs.push_back(weights);440441if (node_proto.input_size() == 3) {442Mat bias = getBlob(node_proto, constBlobs, 2);443layerParams.blobs.push_back(bias);444}445446layerParams.set("num_output", layerParams.blobs[0].size[ind_num_out]);447layerParams.set("bias_term", node_proto.input_size() == 3);448}449else if (layer_type == "MatMul")450{451CV_Assert(node_proto.input_size() == 2);452layerParams.type = "InnerProduct";453Mat blob = getBlob(node_proto, constBlobs, 1);454layerParams.blobs.push_back(blob.t());455layerParams.set("bias_term", false);456layerParams.set("num_output", layerParams.blobs[0].size[0]);457}458else if (layer_type == "Mul")459{460CV_Assert(node_proto.input_size() == 2);461if (layer_id.find(node_proto.input(1)) == layer_id.end()) {462Mat blob = getBlob(node_proto, constBlobs, 1);463blob = blob.reshape(1, 1);464if (blob.total() == 1) {465layerParams.set("scale", blob.at<float>(0));466layerParams.type = "Power";467}468else {469layerParams.blobs.push_back(blob);470layerParams.type = "Scale";471}472}473else {474layerParams.type = "Eltwise";475layerParams.set("operation", "prod");476}477}478else if (layer_type == "Conv")479{480CV_Assert(node_proto.input_size() >= 2);481layerParams.type = "Convolution";482for (int j = 1; j < node_proto.input_size(); j++) {483layerParams.blobs.push_back(getBlob(node_proto, constBlobs, j));484}485layerParams.set("num_output", layerParams.blobs[0].size[0]);486layerParams.set("bias_term", node_proto.input_size() == 3);487}488else if (layer_type == "Transpose")489{490layerParams.type = "Permute";491replaceLayerParam(layerParams, "perm", "order");492}493else if (layer_type == "Unsqueeze")494{495CV_Assert(node_proto.input_size() == 1);496Mat input = getBlob(node_proto, constBlobs, 0);497498DictValue axes = layerParams.get("axes");499std::vector<int> dims;500for (int j = 0; j < input.dims; j++) {501dims.push_back(input.size[j]);502}503CV_Assert(axes.getIntValue(axes.size()-1) <= dims.size());504for (int j = 0; j < axes.size(); j++) {505dims.insert(dims.begin() + axes.getIntValue(j), 1);506}507508Mat out = input.reshape(0, dims);509constBlobs.insert(std::make_pair(layerParams.name, out));510continue;511}512else if (layer_type == "Reshape")513{514CV_Assert(node_proto.input_size() == 2 || layerParams.has("shape"));515516if (node_proto.input_size() == 2) {517Mat blob = getBlob(node_proto, constBlobs, 1);518CV_Assert(blob.type() == CV_32SC1);519520if (layer_id.find(node_proto.input(0)) == layer_id.end()) {521Mat input = getBlob(node_proto, constBlobs, 0);522Mat out = input.reshape(0, static_cast<std::vector<int> >(blob));523constBlobs.insert(std::make_pair(layerParams.name, out));524continue;525}526layerParams.set("dim", DictValue::arrayInt<int*>(527blob.ptr<int>(), blob.total() ));528}529else {530DictValue shape = layerParams.get("shape");531std::vector<int> dim;532for (int j = 0; j < shape.size(); j++) {533dim.push_back(shape.getIntValue(j));534}535536if (layer_id.find(node_proto.input(0)) == layer_id.end()) {537Mat input = getBlob(node_proto, constBlobs, 0);538Mat out = input.reshape(0, dim);539constBlobs.insert(std::make_pair(layerParams.name, out));540continue;541}542replaceLayerParam(layerParams, "shape", "dim");543}544}545else546{547for (int j = 0; j < node_proto.input_size(); j++) {548if (layer_id.find(node_proto.input(j)) == layer_id.end())549layerParams.blobs.push_back(getBlob(node_proto, constBlobs, j));550}551}552553int id = dstNet.addLayer(layerParams.name, layerParams.type, layerParams);554layer_id.insert(std::make_pair(layerParams.name, LayerInfo(id, 0)));555556for (int j = 0; j < node_proto.input_size(); j++) {557layerId = layer_id.find(node_proto.input(j));558if (layerId != layer_id.end()) {559dstNet.connect(layerId->second.layerId, layerId->second.outputId, id, j);560}561}562}563}564565Net readNetFromONNX(const String& onnxFile)566{567ONNXImporter onnxImporter(onnxFile.c_str());568Net net;569onnxImporter.populateNet(net);570return net;571}572573Mat readTensorFromONNX(const String& path)574{575opencv_onnx::TensorProto tensor_proto = opencv_onnx::TensorProto();576std::fstream input(path.c_str(), std::ios::in | std::ios::binary);577if (!tensor_proto.ParseFromIstream(&input)) {578CV_Error(Error::StsUnsupportedFormat, "Failed to parse data");579}580Mat mat = getMatFromTensor(tensor_proto);581releaseONNXTensor(tensor_proto);582return mat;583}584585CV__DNN_INLINE_NS_END586}} // namespace587588#endif589590591