mirror of
https://github.com/opencv/opencv.git
synced 2025-06-07 17:44:04 +08:00
Merge pull request #12071 from l-bat/l-bat:onnx_parser
* Add Squeezenet support in ONNX * Add AlexNet support in ONNX * Add Googlenet support in ONNX * Add CaffeNet and RCNN support in ONNX * Add VGG16 and VGG16 with batch normalization support in ONNX * Add RCNN, ZFNet, ResNet18v1 and ResNet50v1 support in ONNX * Add ResNet101_DUC_HDC * Add Tiny Yolov2 * Add CNN_MNIST, MobileNetv2 and LResNet100 support in ONNX * Add ONNX models for emotion recognition * Add DenseNet121 support in ONNX * Add Inception v1 support in ONNX * Refactoring * Fix tests * Fix tests * Skip unstable test * Modify Reshape operation
This commit is contained in:
parent
c331a214d0
commit
0c8590027f
@ -67,13 +67,13 @@ ocv_warnings_disable(CMAKE_CXX_FLAGS
|
||||
)
|
||||
|
||||
if(PROTOBUF_UPDATE_FILES)
|
||||
file(GLOB proto_files "${CMAKE_CURRENT_LIST_DIR}/src/tensorflow/*.proto" "${CMAKE_CURRENT_LIST_DIR}/src/caffe/opencv-caffe.proto")
|
||||
file(GLOB proto_files "${CMAKE_CURRENT_LIST_DIR}/src/tensorflow/*.proto" "${CMAKE_CURRENT_LIST_DIR}/src/caffe/opencv-caffe.proto" "${CMAKE_CURRENT_LIST_DIR}/src/onnx/opencv-onnx.proto")
|
||||
set(PROTOBUF_GENERATE_CPP_APPEND_PATH ON) # required for tensorflow
|
||||
protobuf_generate_cpp(fw_srcs fw_hdrs ${proto_files})
|
||||
else()
|
||||
file(GLOB fw_srcs "${CMAKE_CURRENT_LIST_DIR}/misc/tensorflow/*.cc" "${CMAKE_CURRENT_LIST_DIR}/misc/caffe/opencv-caffe.pb.cc")
|
||||
file(GLOB fw_hdrs "${CMAKE_CURRENT_LIST_DIR}/misc/tensorflow/*.h" "${CMAKE_CURRENT_LIST_DIR}/misc/caffe/opencv-caffe.pb.h")
|
||||
set(fw_inc "${CMAKE_CURRENT_LIST_DIR}/misc/caffe" "${CMAKE_CURRENT_LIST_DIR}/misc/tensorflow")
|
||||
file(GLOB fw_srcs "${CMAKE_CURRENT_LIST_DIR}/misc/tensorflow/*.cc" "${CMAKE_CURRENT_LIST_DIR}/misc/caffe/opencv-caffe.pb.cc" "${CMAKE_CURRENT_LIST_DIR}/misc/onnx/opencv-onnx.pb.cc")
|
||||
file(GLOB fw_hdrs "${CMAKE_CURRENT_LIST_DIR}/misc/tensorflow/*.h" "${CMAKE_CURRENT_LIST_DIR}/misc/caffe/opencv-caffe.pb.h" "${CMAKE_CURRENT_LIST_DIR}/misc/onnx/opencv-onnx.pb.h")
|
||||
set(fw_inc "${CMAKE_CURRENT_LIST_DIR}/misc/caffe" "${CMAKE_CURRENT_LIST_DIR}/misc/tensorflow" "${CMAKE_CURRENT_LIST_DIR}/misc/onnx")
|
||||
endif()
|
||||
|
||||
set(include_dirs ${fw_inc})
|
||||
|
@ -141,6 +141,9 @@ public:
|
||||
template<typename T>
|
||||
const T &set(const String &key, const T &value);
|
||||
|
||||
//! Erase @p key from the dictionary.
|
||||
void erase(const String &key);
|
||||
|
||||
friend std::ostream &operator<<(std::ostream &stream, const Dict &dict);
|
||||
|
||||
std::map<String, DictValue>::const_iterator begin() const;
|
||||
|
@ -814,6 +814,18 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN
|
||||
*/
|
||||
CV_EXPORTS_W Net readNetFromModelOptimizer(const String &xml, const String &bin);
|
||||
|
||||
/** @brief Reads a network model <a href="https://onnx.ai/">ONNX</a>.
|
||||
* @param onnxFile path to the .onnx file with text description of the network architecture.
|
||||
* @returns Network object that ready to do forward, throw an exception in failure cases.
|
||||
*/
|
||||
CV_EXPORTS_W Net readNetFromONNX(const String &onnxFile);
|
||||
|
||||
/** @brief Creates blob from .pb file.
|
||||
* @param path to the .pb file with input tensor.
|
||||
* @returns Mat.
|
||||
*/
|
||||
CV_EXPORTS_W Mat readTensorFromONNX(const String& path);
|
||||
|
||||
/** @brief Creates 4-dimensional blob from image. Optionally resizes and crops @p image from center,
|
||||
* subtract @p mean values, scales values by @p scalefactor, swap Blue and Red channels.
|
||||
* @param image input image (with 1-, 3- or 4-channels).
|
||||
|
@ -364,6 +364,11 @@ inline const T &Dict::set(const String &key, const T &value)
|
||||
return value;
|
||||
}
|
||||
|
||||
inline void Dict::erase(const String &key)
|
||||
{
|
||||
dict.erase(key);
|
||||
}
|
||||
|
||||
inline std::ostream &operator<<(std::ostream &stream, const Dict &dict)
|
||||
{
|
||||
Dict::_Dict::const_iterator it;
|
||||
|
6977
modules/dnn/misc/onnx/opencv-onnx.pb.cc
Normal file
6977
modules/dnn/misc/onnx/opencv-onnx.pb.cc
Normal file
File diff suppressed because it is too large
Load Diff
5849
modules/dnn/misc/onnx/opencv-onnx.pb.h
Normal file
5849
modules/dnn/misc/onnx/opencv-onnx.pb.h
Normal file
File diff suppressed because it is too large
Load Diff
@ -3462,6 +3462,10 @@ Net readNet(const String& _model, const String& _config, const String& _framewor
|
||||
std::swap(model, config);
|
||||
return readNetFromModelOptimizer(config, model);
|
||||
}
|
||||
if (framework == "onnx" || modelExt == "onnx")
|
||||
{
|
||||
return readNetFromONNX(model);
|
||||
}
|
||||
CV_Error(Error::StsError, "Cannot determine an origin framework of files: " +
|
||||
model + (config.empty() ? "" : ", " + config));
|
||||
}
|
||||
|
585
modules/dnn/src/onnx/onnx_importer.cpp
Normal file
585
modules/dnn/src/onnx/onnx_importer.cpp
Normal file
@ -0,0 +1,585 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
|
||||
// Copyright (C) 2018, Intel Corporation, all rights reserved.
|
||||
// Third party copyrights are property of their respective owners.
|
||||
|
||||
#include "../precomp.hpp"
|
||||
|
||||
#ifdef HAVE_PROTOBUF
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <limits>
|
||||
#include <algorithm>
|
||||
|
||||
|
||||
#if defined(__GNUC__) && __GNUC__ >= 5
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wsuggest-override"
|
||||
#endif
|
||||
#include "opencv-onnx.pb.h"
|
||||
#if defined(__GNUC__) && __GNUC__ >= 5
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
|
||||
namespace cv {
|
||||
namespace dnn {
|
||||
CV__DNN_EXPERIMENTAL_NS_BEGIN
|
||||
|
||||
|
||||
class ONNXImporter
|
||||
{
|
||||
opencv_onnx::ModelProto model_proto;
|
||||
struct LayerInfo {
|
||||
int layerId;
|
||||
int outputId;
|
||||
LayerInfo(int _layerId, int _outputId) : layerId(_layerId), outputId(_outputId) {}
|
||||
};
|
||||
|
||||
std::map<std::string, Mat> getGraphTensors(
|
||||
const opencv_onnx::GraphProto& graph_proto);
|
||||
Mat getBlob(const opencv_onnx::NodeProto& node_proto, const std::map<std::string, Mat>& constBlobs, int index);
|
||||
|
||||
LayerParams getLayerParams(const opencv_onnx::NodeProto& node_proto);
|
||||
bool isCeilMode(const LayerParams& layerParams);
|
||||
|
||||
public:
|
||||
|
||||
ONNXImporter(const char *onnxFile)
|
||||
{
|
||||
std::fstream input(onnxFile, std::ios::in | std::ios::binary);
|
||||
|
||||
if (!model_proto.ParseFromIstream(&input))
|
||||
CV_Error(Error::StsUnsupportedFormat, "Failed to parse onnx model");
|
||||
}
|
||||
|
||||
void populateNet(Net dstNet);
|
||||
};
|
||||
|
||||
inline void replaceLayerParam(LayerParams& layerParams, const String& oldKey, const String& newKey)
|
||||
{
|
||||
if (layerParams.has(oldKey)) {
|
||||
layerParams.set(newKey, layerParams.get(oldKey));
|
||||
layerParams.erase(oldKey);
|
||||
}
|
||||
}
|
||||
|
||||
void releaseONNXTensor(opencv_onnx::TensorProto& tensor_proto)
|
||||
{
|
||||
if (!tensor_proto.raw_data().empty()) {
|
||||
delete tensor_proto.release_raw_data();
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T1, typename T2>
|
||||
void convertInt64ToInt32(const T1& src, T2& dst, int size)
|
||||
{
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (src[i] < std::numeric_limits<int32_t>::min() || src[i] > std::numeric_limits<int32_t>::max()) {
|
||||
CV_Error(Error::StsOutOfRange, "Input is out of OpenCV 32S range");
|
||||
}
|
||||
dst[i] = saturate_cast<int32_t>(src[i]);
|
||||
}
|
||||
}
|
||||
|
||||
Mat getMatFromTensor(opencv_onnx::TensorProto& tensor_proto)
|
||||
{
|
||||
CV_Assert(!tensor_proto.raw_data().empty() || !tensor_proto.float_data().empty()
|
||||
|| !tensor_proto.double_data().empty() || !tensor_proto.int64_data().empty());
|
||||
|
||||
opencv_onnx::TensorProto_DataType datatype = tensor_proto.data_type();
|
||||
Mat blob;
|
||||
std::vector<int> sizes;
|
||||
for (int i = 0; i < tensor_proto.dims_size(); i++) {
|
||||
sizes.push_back(tensor_proto.dims(i));
|
||||
}
|
||||
if (datatype == opencv_onnx::TensorProto_DataType_FLOAT) {
|
||||
|
||||
if (!tensor_proto.float_data().empty()) {
|
||||
const ::google::protobuf::RepeatedField<float> field = tensor_proto.float_data();
|
||||
Mat(sizes, CV_32FC1, (void*)field.data()).copyTo(blob);
|
||||
}
|
||||
else {
|
||||
char* val = const_cast<char*>(tensor_proto.raw_data().c_str());
|
||||
Mat(sizes, CV_32FC1, val).copyTo(blob);
|
||||
}
|
||||
}
|
||||
else if (datatype == opencv_onnx::TensorProto_DataType_DOUBLE)
|
||||
{
|
||||
const ::google::protobuf::RepeatedField<double> field = tensor_proto.double_data();
|
||||
CV_Assert(!field.empty());
|
||||
Mat(sizes, CV_64FC1, (void*)field.data()).convertTo(blob, CV_32FC1);
|
||||
}
|
||||
else if (datatype == opencv_onnx::TensorProto_DataType_INT64)
|
||||
{
|
||||
blob.create(sizes, CV_32SC1);
|
||||
int32_t* dst = reinterpret_cast<int32_t*>(blob.data);
|
||||
|
||||
if (!tensor_proto.int64_data().empty()) {
|
||||
::google::protobuf::RepeatedField< ::google::protobuf::int64> src = tensor_proto.int64_data();
|
||||
convertInt64ToInt32(src, dst, blob.total());
|
||||
}
|
||||
else
|
||||
{
|
||||
char* val = const_cast<char*>(tensor_proto.raw_data().c_str());
|
||||
int64_t* src = reinterpret_cast<int64_t*>(val);
|
||||
convertInt64ToInt32(src, dst, blob.total());
|
||||
}
|
||||
}
|
||||
else
|
||||
CV_Error(Error::StsUnsupportedFormat, "Unsupported data type: " +
|
||||
opencv_onnx::TensorProto_DataType_Name(datatype));
|
||||
return blob;
|
||||
}
|
||||
|
||||
std::map<std::string, Mat> ONNXImporter::getGraphTensors(
|
||||
const opencv_onnx::GraphProto& graph_proto)
|
||||
{
|
||||
opencv_onnx::TensorProto tensor_proto;
|
||||
std::map<std::string, Mat> layers_weights;
|
||||
|
||||
for (int i = 0; i < graph_proto.initializer_size(); i++)
|
||||
{
|
||||
tensor_proto = graph_proto.initializer(i);
|
||||
Mat mat = getMatFromTensor(tensor_proto);
|
||||
releaseONNXTensor(tensor_proto);
|
||||
layers_weights.insert(std::make_pair(tensor_proto.name(), mat));
|
||||
}
|
||||
return layers_weights;
|
||||
}
|
||||
|
||||
LayerParams ONNXImporter::getLayerParams(const opencv_onnx::NodeProto& node_proto)
|
||||
{
|
||||
LayerParams lp;
|
||||
for(int i = 0; i < node_proto.attribute_size(); i++)
|
||||
{
|
||||
opencv_onnx::AttributeProto attribute_proto = node_proto.attribute(i);
|
||||
std::string attribute_name = attribute_proto.name();
|
||||
|
||||
if(attribute_name == "kernel_shape")
|
||||
{
|
||||
CV_Assert(attribute_proto.ints_size() == 2);
|
||||
lp.set("kernel_h", saturate_cast<int32_t>(attribute_proto.ints(0)));
|
||||
lp.set("kernel_w", saturate_cast<int32_t>(attribute_proto.ints(1)));
|
||||
}
|
||||
else if(attribute_name == "strides")
|
||||
{
|
||||
CV_Assert(attribute_proto.ints_size() == 2);
|
||||
lp.set("stride_h", saturate_cast<int32_t>(attribute_proto.ints(0)));
|
||||
lp.set("stride_w", saturate_cast<int32_t>(attribute_proto.ints(1)));
|
||||
}
|
||||
else if(attribute_name == "pads")
|
||||
{
|
||||
CV_Assert(attribute_proto.ints_size() == 4);
|
||||
lp.set("pad_h", saturate_cast<int32_t>(attribute_proto.ints(0)));
|
||||
lp.set("pad_w", saturate_cast<int32_t>(attribute_proto.ints(1)));
|
||||
// push pad_b and pad_r for compute ceil_mode
|
||||
lp.set("pad_b", saturate_cast<int32_t>(attribute_proto.ints(2)));
|
||||
lp.set("pad_r", saturate_cast<int32_t>(attribute_proto.ints(3)));
|
||||
}
|
||||
else if(attribute_name == "auto_pad")
|
||||
{
|
||||
if (attribute_proto.s() == "SAME_UPPER" || attribute_proto.s() == "SAME_LOWER") {
|
||||
lp.set("pad_mode", "SAME");
|
||||
}
|
||||
else if (attribute_proto.s() == "VALID") {
|
||||
lp.set("pad_mode", "VALID");
|
||||
}
|
||||
}
|
||||
else if(attribute_name == "dilations")
|
||||
{
|
||||
CV_Assert(attribute_proto.ints_size() == 2);
|
||||
lp.set("dilation_h", saturate_cast<int32_t>(attribute_proto.ints(0)));
|
||||
lp.set("dilation_w", saturate_cast<int32_t>(attribute_proto.ints(1)));
|
||||
}
|
||||
else if (attribute_proto.has_i())
|
||||
{
|
||||
::google::protobuf::int64 src = attribute_proto.i();
|
||||
if (src < std::numeric_limits<int32_t>::min() || src > std::numeric_limits<int32_t>::max())
|
||||
CV_Error(Error::StsOutOfRange, "Input is out of OpenCV 32S range");
|
||||
else
|
||||
lp.set(attribute_name, saturate_cast<int32_t>(src));
|
||||
}
|
||||
else if (attribute_proto.has_f())
|
||||
{
|
||||
lp.set(attribute_name, attribute_proto.f());
|
||||
}
|
||||
else if (attribute_proto.has_s())
|
||||
{
|
||||
lp.set(attribute_name, attribute_proto.s());
|
||||
}
|
||||
else if (attribute_proto.floats_size() > 0)
|
||||
{
|
||||
lp.set(attribute_name, DictValue::arrayReal(
|
||||
(float*)attribute_proto.mutable_floats(), attribute_proto.floats_size()));
|
||||
}
|
||||
else if (attribute_proto.ints_size() > 0)
|
||||
{
|
||||
const ::google::protobuf::RepeatedField< ::google::protobuf::int64> src = attribute_proto.ints();
|
||||
std::vector<int32_t> dst(attribute_proto.ints_size());
|
||||
convertInt64ToInt32(src, dst, attribute_proto.ints_size());
|
||||
lp.set(attribute_proto.name(), DictValue::arrayInt(&dst[0], attribute_proto.ints_size()));
|
||||
}
|
||||
else if (attribute_proto.has_t())
|
||||
{
|
||||
opencv_onnx::TensorProto tensor = attribute_proto.t();
|
||||
Mat blob = getMatFromTensor(tensor);
|
||||
lp.blobs.push_back(blob);
|
||||
}
|
||||
else if (attribute_proto.has_g() || attribute_proto.strings_size() > 0 ||
|
||||
attribute_proto.tensors_size() > 0 || attribute_proto.graphs_size() > 0)
|
||||
{
|
||||
CV_Error(Error::StsNotImplemented, "Unexpected attribute type");
|
||||
}
|
||||
else
|
||||
CV_Error(Error::StsNotImplemented, "Unsupported attribute type");
|
||||
}
|
||||
return lp;
|
||||
}
|
||||
|
||||
Mat ONNXImporter::getBlob(const opencv_onnx::NodeProto& node_proto,
|
||||
const std::map<std::string, Mat>& constBlobs, int index)
|
||||
{
|
||||
CV_Assert(index < node_proto.input_size());
|
||||
std::map<std::string, Mat>::const_iterator constBlob;
|
||||
constBlob = constBlobs.find(node_proto.input(index));
|
||||
if (constBlob == constBlobs.end()) {
|
||||
CV_Error(Error::StsObjectNotFound,
|
||||
"Blob " + node_proto.input(index) + " not found in const blobs");
|
||||
}
|
||||
return constBlob->second;
|
||||
}
|
||||
|
||||
|
||||
bool ONNXImporter::isCeilMode(const LayerParams& layerParams) {
|
||||
if (!layerParams.has("pad_mode")) {
|
||||
if (layerParams.has("pad_h")) {
|
||||
return layerParams.get<int>("pad_h") != layerParams.get<int>("pad_b") ||
|
||||
layerParams.get<int>("pad_w") != layerParams.get<int>("pad_r");
|
||||
}
|
||||
else
|
||||
return false; // all pads == 0
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void ONNXImporter::populateNet(Net dstNet)
|
||||
{
|
||||
CV_Assert(model_proto.has_graph());
|
||||
opencv_onnx::GraphProto graph_proto = model_proto.graph();
|
||||
std::map<std::string, Mat> constBlobs = getGraphTensors(graph_proto);
|
||||
|
||||
std::string framework_name;
|
||||
if (model_proto.has_producer_name()) {
|
||||
framework_name = model_proto.producer_name();
|
||||
}
|
||||
|
||||
// create map with network inputs (without const blobs)
|
||||
std::map<std::string, LayerInfo> layer_id;
|
||||
std::map<std::string, LayerInfo>::iterator layerId;
|
||||
// fill map: push layer name, layer id and output id
|
||||
std::vector<String> netInputs;
|
||||
for (int j = 0; j < graph_proto.input_size(); j++)
|
||||
{
|
||||
const std::string& name = graph_proto.input(j).name();
|
||||
if (constBlobs.find(name) == constBlobs.end()) {
|
||||
netInputs.push_back(name);
|
||||
layer_id.insert(std::make_pair(name, LayerInfo(0, netInputs.size() - 1)));
|
||||
}
|
||||
}
|
||||
dstNet.setInputsNames(netInputs);
|
||||
|
||||
int layersSize = graph_proto.node_size();
|
||||
LayerParams layerParams;
|
||||
opencv_onnx::NodeProto node_proto;
|
||||
|
||||
for(int i = 0; i < layersSize; i++)
|
||||
{
|
||||
node_proto = graph_proto.node(i);
|
||||
layerParams = getLayerParams(node_proto);
|
||||
CV_Assert(node_proto.output_size() >= 1);
|
||||
layerParams.name = node_proto.output(0);
|
||||
|
||||
std::string layer_type = node_proto.op_type();
|
||||
layerParams.type = layer_type;
|
||||
|
||||
if (layer_type == "MaxPool")
|
||||
{
|
||||
layerParams.type = "Pooling";
|
||||
layerParams.set("pool", "MAX");
|
||||
layerParams.set("ceil_mode", isCeilMode(layerParams));
|
||||
}
|
||||
else if (layer_type == "AveragePool")
|
||||
{
|
||||
layerParams.type = "Pooling";
|
||||
layerParams.set("pool", "AVE");
|
||||
layerParams.set("ceil_mode", isCeilMode(layerParams));
|
||||
layerParams.set("ave_pool_padded_area", framework_name == "pytorch");
|
||||
}
|
||||
else if (layer_type == "GlobalAveragePool")
|
||||
{
|
||||
layerParams.type = "Pooling";
|
||||
layerParams.set("pool", "AVE");
|
||||
layerParams.set("global_pooling", true);
|
||||
}
|
||||
else if (layer_type == "Add" || layer_type == "Sum")
|
||||
{
|
||||
if (layer_id.find(node_proto.input(1)) == layer_id.end())
|
||||
{
|
||||
Mat blob = getBlob(node_proto, constBlobs, 1);
|
||||
blob = blob.reshape(1, 1);
|
||||
if (blob.total() == 1) {
|
||||
layerParams.type = "Power";
|
||||
layerParams.set("shift", blob.at<float>(0));
|
||||
}
|
||||
else {
|
||||
layerParams.type = "Shift";
|
||||
layerParams.blobs.push_back(blob);
|
||||
}
|
||||
}
|
||||
else {
|
||||
layerParams.type = "Eltwise";
|
||||
}
|
||||
}
|
||||
else if (layer_type == "Sub")
|
||||
{
|
||||
Mat blob = (-1.0f) * getBlob(node_proto, constBlobs, 1);
|
||||
blob = blob.reshape(1, 1);
|
||||
if (blob.total() == 1) {
|
||||
layerParams.type = "Power";
|
||||
layerParams.set("shift", blob.at<float>(0));
|
||||
}
|
||||
else {
|
||||
layerParams.type = "Shift";
|
||||
layerParams.blobs.push_back(blob);
|
||||
}
|
||||
}
|
||||
else if (layer_type == "Constant")
|
||||
{
|
||||
CV_Assert(node_proto.input_size() == 0);
|
||||
CV_Assert(layerParams.blobs.size() == 1);
|
||||
constBlobs.insert(std::make_pair(layerParams.name, layerParams.blobs[0]));
|
||||
continue;
|
||||
}
|
||||
else if (layer_type == "ImageScaler")
|
||||
{
|
||||
const float scale = layerParams.has("scale") ? layerParams.get<float>("scale") : 1.0f;
|
||||
layerParams.erase("scale");
|
||||
|
||||
if (layerParams.has("bias"))
|
||||
{
|
||||
layerParams.type = "Scale";
|
||||
layerParams.blobs.push_back(
|
||||
Mat(Size(1, layerParams.get("bias").size()), CV_32FC1, scale));
|
||||
|
||||
layerParams.set("bias_term", true);
|
||||
Mat bias(1, layerParams.get("bias").size(), CV_32FC1);
|
||||
for (int j = 0; j < bias.total(); j++) {
|
||||
bias.at<float>(0, j) = layerParams.get("bias").getRealValue(j);
|
||||
}
|
||||
layerParams.blobs.push_back(bias);
|
||||
layerParams.erase("bias");
|
||||
}
|
||||
else {
|
||||
layerParams.set("scale", scale);
|
||||
layerParams.type = "Power";
|
||||
}
|
||||
}
|
||||
else if (layer_type == "LeakyRelu")
|
||||
{
|
||||
layerParams.type = "ReLU";
|
||||
replaceLayerParam(layerParams, "alpha", "negative_slope");
|
||||
}
|
||||
else if (layer_type == "LRN")
|
||||
{
|
||||
replaceLayerParam(layerParams, "size", "local_size");
|
||||
}
|
||||
else if (layer_type == "BatchNormalization")
|
||||
{
|
||||
if (node_proto.input_size() != 5)
|
||||
CV_Error(Error::StsNotImplemented,
|
||||
"Expected input, scale, bias, mean and var");
|
||||
|
||||
layerParams.type = "BatchNorm";
|
||||
replaceLayerParam(layerParams, "epsilon", "eps");
|
||||
replaceLayerParam(layerParams, "spatial", "use_global_stats");
|
||||
|
||||
Mat meanData = getBlob(node_proto, constBlobs, 3);
|
||||
Mat stdData = getBlob(node_proto, constBlobs, 4);
|
||||
|
||||
layerParams.blobs.push_back(meanData);
|
||||
layerParams.blobs.push_back(stdData);
|
||||
|
||||
if (!node_proto.input(1).empty()) {
|
||||
layerParams.set("has_weight", true);
|
||||
layerParams.blobs.push_back(getBlob(node_proto, constBlobs, 1)); // weightData
|
||||
} else {
|
||||
layerParams.set("has_weight", false);
|
||||
}
|
||||
|
||||
if (!node_proto.input(2).empty()) {
|
||||
layerParams.set("has_bias", true);
|
||||
layerParams.blobs.push_back(getBlob(node_proto, constBlobs, 2)); // biasData
|
||||
} else {
|
||||
layerParams.set("has_bias", false);
|
||||
}
|
||||
}
|
||||
else if (layer_type == "Gemm")
|
||||
{
|
||||
CV_Assert(node_proto.input_size() >= 2);
|
||||
layerParams.type = "InnerProduct";
|
||||
Mat weights = getBlob(node_proto, constBlobs, 1);
|
||||
int ind_num_out = 0;
|
||||
if (layerParams.has("transB") && !layerParams.get<int>("transB")) {
|
||||
transpose(weights, weights);
|
||||
ind_num_out = 1;
|
||||
}
|
||||
layerParams.blobs.push_back(weights);
|
||||
|
||||
if (node_proto.input_size() == 3) {
|
||||
Mat bias = getBlob(node_proto, constBlobs, 2);
|
||||
layerParams.blobs.push_back(bias);
|
||||
}
|
||||
|
||||
layerParams.set("num_output", layerParams.blobs[0].size[ind_num_out]);
|
||||
layerParams.set("bias_term", node_proto.input_size() == 3);
|
||||
}
|
||||
else if (layer_type == "MatMul")
|
||||
{
|
||||
CV_Assert(node_proto.input_size() == 2);
|
||||
layerParams.type = "InnerProduct";
|
||||
Mat blob = getBlob(node_proto, constBlobs, 1);
|
||||
layerParams.blobs.push_back(blob.t());
|
||||
layerParams.set("bias_term", false);
|
||||
layerParams.set("num_output", layerParams.blobs[0].size[0]);
|
||||
}
|
||||
else if (layer_type == "Mul")
|
||||
{
|
||||
CV_Assert(node_proto.input_size() == 2);
|
||||
if (layer_id.find(node_proto.input(1)) == layer_id.end()) {
|
||||
Mat blob = getBlob(node_proto, constBlobs, 1);
|
||||
blob = blob.reshape(1, 1);
|
||||
if (blob.total() == 1) {
|
||||
layerParams.set("scale", blob.at<float>(0));
|
||||
layerParams.type = "Power";
|
||||
}
|
||||
else {
|
||||
layerParams.blobs.push_back(blob);
|
||||
layerParams.type = "Scale";
|
||||
}
|
||||
}
|
||||
else {
|
||||
layerParams.type = "Eltwise";
|
||||
layerParams.set("operation", "prod");
|
||||
}
|
||||
}
|
||||
else if (layer_type == "Conv")
|
||||
{
|
||||
CV_Assert(node_proto.input_size() >= 2);
|
||||
layerParams.type = "Convolution";
|
||||
for (int j = 1; j < node_proto.input_size(); j++) {
|
||||
layerParams.blobs.push_back(getBlob(node_proto, constBlobs, j));
|
||||
}
|
||||
layerParams.set("num_output", layerParams.blobs[0].size[0]);
|
||||
layerParams.set("bias_term", node_proto.input_size() == 3);
|
||||
}
|
||||
else if (layer_type == "Unsqueeze")
|
||||
{
|
||||
CV_Assert(node_proto.input_size() == 1);
|
||||
Mat input = getBlob(node_proto, constBlobs, 0);
|
||||
|
||||
DictValue axes = layerParams.get("axes");
|
||||
std::vector<int> dims;
|
||||
for (int j = 0; j < input.dims; j++) {
|
||||
dims.push_back(input.size[j]);
|
||||
}
|
||||
CV_Assert(axes.getIntValue(axes.size()-1) <= dims.size());
|
||||
for (int j = 0; j < axes.size(); j++) {
|
||||
dims.insert(dims.begin() + axes.getIntValue(j), 1);
|
||||
}
|
||||
|
||||
Mat out = input.reshape(0, dims);
|
||||
constBlobs.insert(std::make_pair(layerParams.name, out));
|
||||
continue;
|
||||
}
|
||||
else if (layer_type == "Reshape")
|
||||
{
|
||||
CV_Assert(node_proto.input_size() == 2 || layerParams.has("shape"));
|
||||
|
||||
if (node_proto.input_size() == 2) {
|
||||
Mat blob = getBlob(node_proto, constBlobs, 1);
|
||||
CV_Assert(blob.type() == CV_32SC1);
|
||||
|
||||
if (layer_id.find(node_proto.input(0)) == layer_id.end()) {
|
||||
Mat input = getBlob(node_proto, constBlobs, 0);
|
||||
Mat out = input.reshape(0, static_cast<std::vector<int> >(blob));
|
||||
constBlobs.insert(std::make_pair(layerParams.name, out));
|
||||
continue;
|
||||
}
|
||||
layerParams.set("dim", DictValue::arrayInt<int*>(
|
||||
blob.ptr<int>(), blob.total() ));
|
||||
}
|
||||
else {
|
||||
DictValue shape = layerParams.get("shape");
|
||||
std::vector<int> dim;
|
||||
for (int j = 0; j < shape.size(); j++) {
|
||||
dim.push_back(shape.getIntValue(j));
|
||||
}
|
||||
|
||||
if (layer_id.find(node_proto.input(0)) == layer_id.end()) {
|
||||
Mat input = getBlob(node_proto, constBlobs, 0);
|
||||
Mat out = input.reshape(0, dim);
|
||||
constBlobs.insert(std::make_pair(layerParams.name, out));
|
||||
continue;
|
||||
}
|
||||
replaceLayerParam(layerParams, "shape", "dim");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int j = 0; j < node_proto.input_size(); j++) {
|
||||
if (layer_id.find(node_proto.input(j)) == layer_id.end())
|
||||
layerParams.blobs.push_back(getBlob(node_proto, constBlobs, j));
|
||||
}
|
||||
}
|
||||
|
||||
int id = dstNet.addLayer(layerParams.name, layerParams.type, layerParams);
|
||||
layer_id.insert(std::make_pair(layerParams.name, LayerInfo(id, 0)));
|
||||
|
||||
for (int j = 0; j < node_proto.input_size(); j++) {
|
||||
layerId = layer_id.find(node_proto.input(j));
|
||||
|
||||
if (layerId != layer_id.end()) {
|
||||
dstNet.connect(layerId->second.layerId, layerId->second.outputId, id, j);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Net readNetFromONNX(const String& onnxFile)
|
||||
{
|
||||
ONNXImporter onnxImporter(onnxFile.c_str());
|
||||
Net net;
|
||||
onnxImporter.populateNet(net);
|
||||
return net;
|
||||
}
|
||||
|
||||
Mat readTensorFromONNX(const String& path)
|
||||
{
|
||||
opencv_onnx::TensorProto tensor_proto = opencv_onnx::TensorProto();
|
||||
std::fstream input(path.c_str(), std::ios::in | std::ios::binary);
|
||||
if (!tensor_proto.ParseFromIstream(&input)) {
|
||||
CV_Error(Error::StsUnsupportedFormat, "Failed to parse data");
|
||||
}
|
||||
Mat mat = getMatFromTensor(tensor_proto);
|
||||
releaseONNXTensor(tensor_proto);
|
||||
return mat;
|
||||
}
|
||||
|
||||
CV__DNN_EXPERIMENTAL_NS_END
|
||||
}} // namespace
|
||||
|
||||
#endif
|
446
modules/dnn/src/onnx/opencv-onnx.proto
Normal file
446
modules/dnn/src/onnx/opencv-onnx.proto
Normal file
@ -0,0 +1,446 @@
|
||||
//
|
||||
// WARNING: This file is automatically generated! Please edit onnx.in.proto.
|
||||
//
|
||||
|
||||
|
||||
// Copyright (c) Facebook Inc. and Microsoft Corporation.
|
||||
// Licensed under the MIT license.
|
||||
|
||||
syntax = "proto2";
|
||||
|
||||
package opencv_onnx;
|
||||
|
||||
// Overview
|
||||
//
|
||||
// ONNX is an open specification that is comprised of the following components:
|
||||
//
|
||||
// 1) A definition of an extensible computation graph model.
|
||||
// 2) Definitions of standard data types.
|
||||
// 3) Definitions of built-in operators.
|
||||
//
|
||||
// This document describes the syntax of models and their computation graphs,
|
||||
// as well as the standard data types. Together, they are referred to as the ONNX
|
||||
// Intermediate Representation, or 'IR' for short.
|
||||
//
|
||||
// The normative semantic specification of the ONNX IR is found in docs/IR.md.
|
||||
// Definitions of the built-in neural network operators may be found in docs/Operators.md.
|
||||
|
||||
// Notes
|
||||
//
|
||||
// Release
|
||||
//
|
||||
// We are still in the very early stage of defining ONNX. The current
|
||||
// version of ONNX is a starting point. While we are actively working
|
||||
// towards a complete spec, we would like to get the community involved
|
||||
// by sharing our working version of ONNX.
|
||||
//
|
||||
// Protobuf compatibility
|
||||
//
|
||||
// To simplify framework compatibility, ONNX is defined using the subset of protobuf
|
||||
// that is compatible with both protobuf v2 and v3. This means that we do not use any
|
||||
// protobuf features that are only available in one of the two versions.
|
||||
//
|
||||
// Here are the most notable contortions we have to carry out to work around
|
||||
// these limitations:
|
||||
//
|
||||
// - No 'map' (added protobuf 3.0). We instead represent mappings as lists
|
||||
// of key-value pairs, where order does not matter and duplicates
|
||||
// are not allowed.
|
||||
|
||||
|
||||
// Versioning
|
||||
//
|
||||
// ONNX versioning is specified in docs/IR.md and elaborated on in docs/Versioning.md
|
||||
//
|
||||
// To be compatible with both proto2 and proto3, we will use a version number
|
||||
// that is not defined by the default value but an explicit enum number.
|
||||
enum Version {
|
||||
// proto3 requires the first enum value to be zero.
|
||||
// We add this just to appease the compiler.
|
||||
_START_VERSION = 0;
|
||||
// The version field is always serialized and we will use it to store the
|
||||
// version that the graph is generated from. This helps us set up version
|
||||
// control.
|
||||
// For the IR, we are using simple numbers starting with with 0x00000001,
|
||||
// which was the version we published on Oct 10, 2017.
|
||||
IR_VERSION_2017_10_10 = 0x0000000000000001;
|
||||
|
||||
// IR_VERSION 2 published on Oct 30, 2017
|
||||
// - Added type discriminator to AttributeProto to support proto3 users
|
||||
IR_VERSION_2017_10_30 = 0x0000000000000002;
|
||||
|
||||
// IR VERSION 3 published on Nov 3, 2017
|
||||
// - For operator versioning:
|
||||
// - Added new message OperatorSetIdProto
|
||||
// - Added opset_import in ModelProto
|
||||
// - For vendor extensions, added domain in NodeProto
|
||||
IR_VERSION = 0x0000000000000003;
|
||||
}
|
||||
|
||||
// Attributes
|
||||
//
|
||||
// A named attribute containing either singular float, integer, string, graph,
|
||||
// and tensor values, or repeated float, integer, string, graph, and tensor values.
|
||||
// An AttributeProto MUST contain the name field, and *only one* of the
|
||||
// following content fields, effectively enforcing a C/C++ union equivalent.
|
||||
message AttributeProto {
|
||||
|
||||
// Note: this enum is structurally identical to the OpSchema::AttrType
|
||||
// enum defined in schema.h. If you rev one, you likely need to rev the other.
|
||||
enum AttributeType {
|
||||
UNDEFINED = 0;
|
||||
FLOAT = 1;
|
||||
INT = 2;
|
||||
STRING = 3;
|
||||
TENSOR = 4;
|
||||
GRAPH = 5;
|
||||
|
||||
FLOATS = 6;
|
||||
INTS = 7;
|
||||
STRINGS = 8;
|
||||
TENSORS = 9;
|
||||
GRAPHS = 10;
|
||||
}
|
||||
|
||||
// The name field MUST be present for this version of the IR.
|
||||
optional string name = 1; // namespace Attribute
|
||||
|
||||
// if ref_attr_name is not empty, ref_attr_name is the attribute name in parent function.
|
||||
// In this case, this AttributeProto does not contain data, and it's a reference of attribute
|
||||
// in parent scope.
|
||||
// NOTE: This should ONLY be used in function (sub-graph). It's invalid to be used in main graph.
|
||||
optional string ref_attr_name = 21;
|
||||
|
||||
// A human-readable documentation for this attribute. Markdown is allowed.
|
||||
optional string doc_string = 13;
|
||||
|
||||
// The type field MUST be present for this version of the IR.
|
||||
// For 0.0.1 versions of the IR, this field was not defined, and
|
||||
// implementations needed to use has_field hueristics to determine
|
||||
// which value field was in use. For IR_VERSION 0.0.2 or later, this
|
||||
// field MUST be set and match the f|i|s|t|... field in use. This
|
||||
// change was made to accomodate proto3 implementations.
|
||||
optional AttributeType type = 20; // discriminator that indicates which field below is in use
|
||||
|
||||
// Exactly ONE of the following fields must be present for this version of the IR
|
||||
optional float f = 2; // float
|
||||
optional int64 i = 3; // int
|
||||
optional bytes s = 4; // UTF-8 string
|
||||
optional TensorProto t = 5; // tensor value
|
||||
optional GraphProto g = 6; // graph
|
||||
// Do not use field below, it's deprecated.
|
||||
// optional ValueProto v = 12; // value - subsumes everything but graph
|
||||
|
||||
repeated float floats = 7; // list of floats
|
||||
repeated int64 ints = 8; // list of ints
|
||||
repeated bytes strings = 9; // list of UTF-8 strings
|
||||
repeated TensorProto tensors = 10; // list of tensors
|
||||
repeated GraphProto graphs = 11; // list of graph
|
||||
}
|
||||
|
||||
// Defines information on value, including the name, the type, and
|
||||
// the shape of the value.
|
||||
message ValueInfoProto {
|
||||
// This field MUST be present in this version of the IR.
|
||||
optional string name = 1; // namespace Value
|
||||
// This field MUST be present in this version of the IR.
|
||||
optional TypeProto type = 2;
|
||||
// A human-readable documentation for this value. Markdown is allowed.
|
||||
optional string doc_string = 3;
|
||||
}
|
||||
|
||||
// Nodes
|
||||
//
|
||||
// Computation graphs are made up of a DAG of nodes, which represent what is
|
||||
// commonly called a "layer" or "pipeline stage" in machine learning frameworks.
|
||||
//
|
||||
// For example, it can be a node of type "Conv" that takes in an image, a filter
|
||||
// tensor and a bias tensor, and produces the convolved output.
|
||||
message NodeProto {
|
||||
repeated string input = 1; // namespace Value
|
||||
repeated string output = 2; // namespace Value
|
||||
|
||||
// An optional identifier for this node in a graph.
|
||||
// This field MAY be absent in ths version of the IR.
|
||||
optional string name = 3; // namespace Node
|
||||
|
||||
// The symbolic identifier of the Operator to execute.
|
||||
optional string op_type = 4; // namespace Operator
|
||||
// The domain of the OperatorSet that specifies the operator named by op_type.
|
||||
optional string domain = 7; // namespace Domain
|
||||
|
||||
// Additional named attributes.
|
||||
repeated AttributeProto attribute = 5;
|
||||
|
||||
// A human-readable documentation for this node. Markdown is allowed.
|
||||
optional string doc_string = 6;
|
||||
}
|
||||
|
||||
// Models
|
||||
//
|
||||
// ModelProto is a top-level file/container format for bundling a ML model and
|
||||
// associating its computation graph with metadata.
|
||||
//
|
||||
// The semantics of the model are described by the associated GraphProto.
|
||||
message ModelProto {
|
||||
// The version of the IR this model targets. See Version enum above.
|
||||
// This field MUST be present.
|
||||
optional int64 ir_version = 1;
|
||||
|
||||
// The OperatorSets this model relies on.
|
||||
// All ModelProtos MUST have at least one entry that
|
||||
// specifies which version of the ONNX OperatorSet is
|
||||
// being imported.
|
||||
//
|
||||
// All nodes in the ModelProto's graph will bind against the operator
|
||||
// with the same-domain/same-op_type operator with the HIGHEST version
|
||||
// in the referenced operator sets.
|
||||
repeated OperatorSetIdProto opset_import = 8;
|
||||
|
||||
// The name of the framework or tool used to generate this model.
|
||||
// This field SHOULD be present to indicate which implementation/tool/framework
|
||||
// emitted the model.
|
||||
optional string producer_name = 2;
|
||||
|
||||
// The version of the framework or tool used to generate this model.
|
||||
// This field SHOULD be present to indicate which implementation/tool/framework
|
||||
// emitted the model.
|
||||
optional string producer_version = 3;
|
||||
|
||||
// Domain name of the model.
|
||||
// We use reverse domain names as name space indicators. For example:
|
||||
// `com.facebook.fair` or `com.microsoft.cognitiveservices`
|
||||
//
|
||||
// Together with `model_version` and GraphProto.name, this forms the unique identity of
|
||||
// the graph.
|
||||
optional string domain = 4;
|
||||
|
||||
// The version of the graph encoded. See Version enum below.
|
||||
optional int64 model_version = 5;
|
||||
|
||||
// A human-readable documentation for this model. Markdown is allowed.
|
||||
optional string doc_string = 6;
|
||||
|
||||
// The parameterized graph that is evaluated to execute the model.
|
||||
optional GraphProto graph = 7;
|
||||
|
||||
// Named metadata values; keys should be distinct.
|
||||
repeated StringStringEntryProto metadata_props = 14;
|
||||
};
|
||||
|
||||
// StringStringEntryProto follows the pattern for cross-proto-version maps.
|
||||
// See https://developers.google.com/protocol-buffers/docs/proto3#maps
|
||||
message StringStringEntryProto {
|
||||
optional string key = 1;
|
||||
optional string value= 2;
|
||||
};
|
||||
|
||||
// Graphs
|
||||
//
|
||||
// A graph defines the computational logic of a model and is comprised of a parameterized
|
||||
// list of nodes that form a directed acyclic graph based on their inputs and outputs.
|
||||
// This is the equivalent of the "network" or "graph" in many deep learning
|
||||
// frameworks.
|
||||
message GraphProto {
|
||||
// The nodes in the graph, sorted topologically.
|
||||
repeated NodeProto node = 1;
|
||||
|
||||
// The name of the graph.
|
||||
optional string name = 2; // namespace Graph
|
||||
|
||||
// A list of named tensor values, used to specify constant inputs of the graph.
|
||||
// Each TensorProto entry must have a distinct name (within the list) that
|
||||
// also appears in the input list.
|
||||
repeated TensorProto initializer = 5;
|
||||
|
||||
// A human-readable documentation for this graph. Markdown is allowed.
|
||||
optional string doc_string = 10;
|
||||
|
||||
// The inputs and outputs of the graph.
|
||||
repeated ValueInfoProto input = 11;
|
||||
repeated ValueInfoProto output = 12;
|
||||
|
||||
// Information for the values in the graph. The ValueInfoProto.name's
|
||||
// must be distinct. It is optional for a value to appear in value_info list.
|
||||
repeated ValueInfoProto value_info = 13;
|
||||
|
||||
// DO NOT USE the following fields, they were deprecated from earlier versions.
|
||||
// repeated string input = 3;
|
||||
// repeated string output = 4;
|
||||
// optional int64 ir_version = 6;
|
||||
// optional int64 producer_version = 7;
|
||||
// optional string producer_tag = 8;
|
||||
// optional string domain = 9;
|
||||
}
|
||||
|
||||
// Tensors
|
||||
//
|
||||
// A serialized tensor value.
|
||||
message TensorProto {
|
||||
enum DataType {
|
||||
UNDEFINED = 0;
|
||||
// Basic types.
|
||||
FLOAT = 1; // float
|
||||
UINT8 = 2; // uint8_t
|
||||
INT8 = 3; // int8_t
|
||||
UINT16 = 4; // uint16_t
|
||||
INT16 = 5; // int16_t
|
||||
INT32 = 6; // int32_t
|
||||
INT64 = 7; // int64_t
|
||||
STRING = 8; // string
|
||||
BOOL = 9; // bool
|
||||
|
||||
// Advanced types
|
||||
FLOAT16 = 10;
|
||||
DOUBLE = 11;
|
||||
UINT32 = 12;
|
||||
UINT64 = 13;
|
||||
COMPLEX64 = 14; // complex with float32 real and imaginary components
|
||||
COMPLEX128 = 15; // complex with float64 real and imaginary components
|
||||
// Future extensions go here.
|
||||
}
|
||||
|
||||
// The shape of the tensor.
|
||||
repeated int64 dims = 1;
|
||||
|
||||
// The data type of the tensor.
|
||||
optional DataType data_type = 2;
|
||||
|
||||
// For very large tensors, we may want to store them in chunks, in which
|
||||
// case the following fields will specify the segment that is stored in
|
||||
// the current TensorProto.
|
||||
message Segment {
|
||||
optional int64 begin = 1;
|
||||
optional int64 end = 2;
|
||||
}
|
||||
optional Segment segment = 3;
|
||||
|
||||
// Tensor content must be organized in row-major order.
|
||||
//
|
||||
// Depending on the data_type field, exactly one of the fields below with
|
||||
// name ending in _data is used to store the elements of the tensor.
|
||||
|
||||
// For float and complex64 values
|
||||
// Complex64 tensors are encoded as a single array of floats,
|
||||
// with the real components appearing in odd numbered positions,
|
||||
// and the corresponding imaginary component apparing in the
|
||||
// subsequent even numbered position. (e.g., [1.0 + 2.0i, 3.0 + 4.0i]
|
||||
// is encoded as [1.0, 2.0 ,3.0 ,4.0]
|
||||
// When this field is present, the data_type field MUST be FLOAT or COMPLEX64.
|
||||
repeated float float_data = 4 [packed = true];
|
||||
|
||||
// For int32, uint8, int8, uint16, int16, bool, and float16 values
|
||||
// float16 values must be bit-wise converted to an uint16_t prior
|
||||
// to writing to the buffer.
|
||||
// When this field is present, the data_type field MUST be
|
||||
// INT32, INT16, INT8, UINT16, INT8, BOOL, or FLOAT16
|
||||
repeated int32 int32_data = 5 [packed = true];
|
||||
|
||||
// For strings.
|
||||
// Each element of string_data is a UTF-8 encoded Unicode
|
||||
// string. No trailing null, no leading BOM. The protobuf "string"
|
||||
// scalar type is not used to match ML community conventions.
|
||||
// When this field is present, the data_type field MUST be STRING
|
||||
repeated bytes string_data = 6;
|
||||
|
||||
// For int64.
|
||||
// When this field is present, the data_type field MUST be INT64
|
||||
repeated int64 int64_data = 7 [packed = true];
|
||||
|
||||
// Optionally, a name for the tensor.
|
||||
optional string name = 8; // namespace Value
|
||||
|
||||
// A human-readable documentation for this tensor. Markdown is allowed.
|
||||
optional string doc_string = 12;
|
||||
|
||||
// Serializations can either use one of the fields above, or use this
|
||||
// raw bytes field. The only exception is the string case, where one is
|
||||
// required to store the content in the repeated bytes string_data field.
|
||||
//
|
||||
// When this raw_data field is used to store tensor value, elements MUST
|
||||
// be stored in as fixed-width, little-endian order.
|
||||
// Floating-point data types MUST be stored in IEEE 754 format.
|
||||
// Complex64 elements must be written as two consecutive FLOAT values, real component first.
|
||||
// Complex128 elements must be written as two consecutive DOUBLE values, real component first.
|
||||
// Boolean type MUST be written one byte per tensor element (00000001 for true, 00000000 for false).
|
||||
//
|
||||
// Note: the advantage of specific field rather than the raw_data field is
|
||||
// that in some cases (e.g. int data), protobuf does a better packing via
|
||||
// variable length storage, and may lead to smaller binary footprint.
|
||||
// When this field is present, the data_type field MUST NOT be STRING or UNDEFINED
|
||||
optional bytes raw_data = 9;
|
||||
|
||||
// For double
|
||||
// Complex64 tensors are encoded as a single array of doubles,
|
||||
// with the real components appearing in odd numbered positions,
|
||||
// and the corresponding imaginary component apparing in the
|
||||
// subsequent even numbered position. (e.g., [1.0 + 2.0i, 3.0 + 4.0i]
|
||||
// is encoded as [1.0, 2.0 ,3.0 ,4.0]
|
||||
// When this field is present, the data_type field MUST be DOUBLE or COMPLEX128
|
||||
repeated double double_data = 10 [packed = true];
|
||||
|
||||
// For uint64 and uint32 values
|
||||
// When this field is present, the data_type field MUST be
|
||||
// UINT32 or UINT64
|
||||
repeated uint64 uint64_data = 11 [packed = true];
|
||||
}
|
||||
|
||||
// Defines a tensor shape. A dimension can be either an integer value
|
||||
// or a symbolic variable. A symbolic variable represents an unknown
|
||||
// dimension.
|
||||
message TensorShapeProto {
|
||||
message Dimension {
|
||||
oneof value {
|
||||
int64 dim_value = 1;
|
||||
string dim_param = 2; // namespace Shape
|
||||
};
|
||||
// Standard denotation can optionally be used to denote tensor
|
||||
// dimensions with standard semantic descriptions to ensure
|
||||
// that operations are applied to the correct axis of a tensor.
|
||||
// Refer to https://github.com/onnx/onnx/blob/master/docs/DimensionDenotation.md#denotation-definition
|
||||
// for pre-defined dimension denotations.
|
||||
optional string denotation = 3;
|
||||
};
|
||||
repeated Dimension dim = 1;
|
||||
}
|
||||
|
||||
// Types
|
||||
//
|
||||
// The standard ONNX data types.
|
||||
message TypeProto {
|
||||
|
||||
message Tensor {
|
||||
// This field MUST NOT have the value of UNDEFINED
|
||||
// This field MUST be present for this version of the IR.
|
||||
optional TensorProto.DataType elem_type = 1;
|
||||
optional TensorShapeProto shape = 2;
|
||||
}
|
||||
|
||||
|
||||
oneof value {
|
||||
// The type of a tensor.
|
||||
Tensor tensor_type = 1;
|
||||
|
||||
}
|
||||
|
||||
// An optional denotation can be used to denote the whole
|
||||
// type with a standard semantic description as to what is
|
||||
// stored inside. Refer to https://github.com/onnx/onnx/blob/master/docs/TypeDenotation.md#type-denotation-definition
|
||||
// for pre-defined type denotations.
|
||||
optional string denotation = 6;
|
||||
}
|
||||
|
||||
// Operator Sets
|
||||
//
|
||||
// OperatorSets are uniquely identified by a (domain, opset_version) pair.
|
||||
message OperatorSetIdProto {
|
||||
// The domain of the operator set being identified.
|
||||
// The empty string ("") or absence of this field implies the operator
|
||||
// set that is defined as part of the ONNX specification.
|
||||
// This field MUST be present in this version of the IR when referring to any other operator set.
|
||||
optional string domain = 1;
|
||||
|
||||
// The version of the operator set being identified.
|
||||
// This field MUST be present in this version of the IR.
|
||||
optional int64 version = 2;
|
||||
}
|
344
modules/dnn/test/test_onnx_importer.cpp
Normal file
344
modules/dnn/test/test_onnx_importer.cpp
Normal file
@ -0,0 +1,344 @@
|
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
|
||||
// Copyright (C) 2018, Intel Corporation, all rights reserved.
|
||||
// Third party copyrights are property of their respective owners.
|
||||
|
||||
|
||||
#include "test_precomp.hpp"
|
||||
#include "npy_blob.hpp"
|
||||
#include <opencv2/dnn/shape_utils.hpp>
|
||||
|
||||
namespace opencv_test { namespace {
|
||||
|
||||
template<typename TString>
|
||||
static std::string _tf(TString filename)
|
||||
{
|
||||
String rootFolder = "dnn/onnx/";
|
||||
return findDataFile(rootFolder + filename, false);
|
||||
}
|
||||
|
||||
class Test_ONNX_layers : public DNNTestLayer
|
||||
{
|
||||
public:
|
||||
enum Extension
|
||||
{
|
||||
npy,
|
||||
pb
|
||||
};
|
||||
|
||||
void testONNXModels(const String& basename, const Extension ext = npy, const double l1 = 0, const float lInf = 0)
|
||||
{
|
||||
String onnxmodel = _tf("models/" + basename + ".onnx");
|
||||
Mat inp, ref;
|
||||
if (ext == npy) {
|
||||
inp = blobFromNPY(_tf("data/input_" + basename + ".npy"));
|
||||
ref = blobFromNPY(_tf("data/output_" + basename + ".npy"));
|
||||
}
|
||||
else if (ext == pb) {
|
||||
inp = readTensorFromONNX(_tf("data/input_" + basename + ".pb"));
|
||||
ref = readTensorFromONNX(_tf("data/output_" + basename + ".pb"));
|
||||
}
|
||||
else
|
||||
CV_Error(Error::StsUnsupportedFormat, "Unsupported extension");
|
||||
|
||||
checkBackend(&inp, &ref);
|
||||
Net net = readNetFromONNX(onnxmodel);
|
||||
ASSERT_FALSE(net.empty());
|
||||
|
||||
net.setPreferableBackend(backend);
|
||||
net.setPreferableTarget(target);
|
||||
|
||||
net.setInput(inp);
|
||||
Mat out = net.forward();
|
||||
normAssert(ref, out, "", l1 ? l1 : default_l1, lInf ? lInf : default_lInf);
|
||||
}
|
||||
};
|
||||
|
||||
TEST_P(Test_ONNX_layers, MaxPooling)
|
||||
{
|
||||
testONNXModels("maxpooling");
|
||||
testONNXModels("two_maxpooling");
|
||||
}
|
||||
|
||||
TEST_P(Test_ONNX_layers, Convolution)
|
||||
{
|
||||
testONNXModels("convolution");
|
||||
testONNXModels("two_convolution");
|
||||
}
|
||||
|
||||
TEST_P(Test_ONNX_layers, Dropout)
|
||||
{
|
||||
testONNXModels("dropout");
|
||||
}
|
||||
|
||||
TEST_P(Test_ONNX_layers, Linear)
|
||||
{
|
||||
if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16)
|
||||
throw SkipTestException("");
|
||||
testONNXModels("linear");
|
||||
}
|
||||
|
||||
TEST_P(Test_ONNX_layers, ReLU)
|
||||
{
|
||||
testONNXModels("ReLU");
|
||||
}
|
||||
|
||||
TEST_P(Test_ONNX_layers, MaxPooling_Sigmoid)
|
||||
{
|
||||
testONNXModels("maxpooling_sigmoid");
|
||||
}
|
||||
|
||||
TEST_P(Test_ONNX_layers, Concatenation)
|
||||
{
|
||||
if (backend == DNN_BACKEND_INFERENCE_ENGINE &&
|
||||
(target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_OPENCL || target == DNN_TARGET_MYRIAD))
|
||||
throw SkipTestException("");
|
||||
testONNXModels("concatenation");
|
||||
}
|
||||
|
||||
TEST_P(Test_ONNX_layers, AveragePooling)
|
||||
{
|
||||
testONNXModels("average_pooling");
|
||||
}
|
||||
|
||||
TEST_P(Test_ONNX_layers, BatchNormalization)
|
||||
{
|
||||
testONNXModels("batch_norm");
|
||||
}
|
||||
|
||||
TEST_P(Test_ONNX_layers, Multiplication)
|
||||
{
|
||||
if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16 ||
|
||||
backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_MYRIAD)
|
||||
throw SkipTestException("");
|
||||
testONNXModels("mul");
|
||||
}
|
||||
|
||||
TEST_P(Test_ONNX_layers, Constant)
|
||||
{
|
||||
testONNXModels("constant");
|
||||
}
|
||||
|
||||
TEST_P(Test_ONNX_layers, MultyInputs)
|
||||
{
|
||||
const String model = _tf("models/multy_inputs.onnx");
|
||||
|
||||
Net net = readNetFromONNX(model);
|
||||
ASSERT_FALSE(net.empty());
|
||||
|
||||
net.setPreferableBackend(backend);
|
||||
net.setPreferableTarget(target);
|
||||
|
||||
Mat inp1 = blobFromNPY(_tf("data/input_multy_inputs_0.npy"));
|
||||
Mat inp2 = blobFromNPY(_tf("data/input_multy_inputs_1.npy"));
|
||||
Mat ref = blobFromNPY(_tf("data/output_multy_inputs.npy"));
|
||||
checkBackend(&inp1, &ref);
|
||||
|
||||
net.setInput(inp1, "0");
|
||||
net.setInput(inp2, "1");
|
||||
Mat out = net.forward();
|
||||
|
||||
normAssert(ref, out, "", default_l1, default_lInf);
|
||||
}
|
||||
|
||||
|
||||
INSTANTIATE_TEST_CASE_P(/*nothing*/, Test_ONNX_layers, dnnBackendsAndTargets());
|
||||
|
||||
class Test_ONNX_nets : public Test_ONNX_layers {};
|
||||
TEST_P(Test_ONNX_nets, Alexnet)
|
||||
{
|
||||
const String model = _tf("models/alexnet.onnx");
|
||||
|
||||
Net net = readNetFromONNX(model);
|
||||
ASSERT_FALSE(net.empty());
|
||||
|
||||
net.setPreferableBackend(backend);
|
||||
net.setPreferableTarget(target);
|
||||
|
||||
Mat inp = imread(_tf("../grace_hopper_227.png"));
|
||||
Mat ref = blobFromNPY(_tf("../caffe_alexnet_prob.npy"));
|
||||
checkBackend(&inp, &ref);
|
||||
|
||||
net.setInput(blobFromImage(inp, 1.0f, Size(227, 227), Scalar(), false));
|
||||
ASSERT_FALSE(net.empty());
|
||||
Mat out = net.forward();
|
||||
|
||||
normAssert(out, ref, "", default_l1, default_lInf);
|
||||
}
|
||||
|
||||
TEST_P(Test_ONNX_nets, Squeezenet)
|
||||
{
|
||||
testONNXModels("squeezenet", pb);
|
||||
}
|
||||
|
||||
TEST_P(Test_ONNX_nets, Googlenet)
|
||||
{
|
||||
if (backend == DNN_BACKEND_INFERENCE_ENGINE)
|
||||
throw SkipTestException("");
|
||||
|
||||
const String model = _tf("models/googlenet.onnx");
|
||||
|
||||
Net net = readNetFromONNX(model);
|
||||
ASSERT_FALSE(net.empty());
|
||||
|
||||
net.setPreferableBackend(backend);
|
||||
net.setPreferableTarget(target);
|
||||
|
||||
std::vector<Mat> images;
|
||||
images.push_back( imread(_tf("../googlenet_0.png")) );
|
||||
images.push_back( imread(_tf("../googlenet_1.png")) );
|
||||
Mat inp = blobFromImages(images, 1.0f, Size(), Scalar(), false);
|
||||
Mat ref = blobFromNPY(_tf("../googlenet_prob.npy"));
|
||||
checkBackend(&inp, &ref);
|
||||
|
||||
net.setInput(inp);
|
||||
ASSERT_FALSE(net.empty());
|
||||
Mat out = net.forward();
|
||||
|
||||
normAssert(ref, out, "", default_l1, default_lInf);
|
||||
}
|
||||
|
||||
TEST_P(Test_ONNX_nets, CaffeNet)
|
||||
{
|
||||
testONNXModels("caffenet", pb);
|
||||
}
|
||||
|
||||
TEST_P(Test_ONNX_nets, RCNN_ILSVRC13)
|
||||
{
|
||||
testONNXModels("rcnn_ilsvrc13", pb);
|
||||
}
|
||||
|
||||
TEST_P(Test_ONNX_nets, VGG16)
|
||||
{
|
||||
double l1 = default_l1;
|
||||
double lInf = default_lInf;
|
||||
// output range: [-69; 72]
|
||||
if (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) {
|
||||
l1 = 0.087;
|
||||
lInf = 0.585;
|
||||
}
|
||||
else if (backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_OPENCL) {
|
||||
lInf = 1.2e-4;
|
||||
}
|
||||
testONNXModels("vgg16", pb, l1, lInf);
|
||||
}
|
||||
|
||||
TEST_P(Test_ONNX_nets, VGG16_bn)
|
||||
{
|
||||
double l1 = default_l1;
|
||||
double lInf = default_lInf;
|
||||
// output range: [-16; 27]
|
||||
if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16) {
|
||||
l1 = 0.0086;
|
||||
lInf = 0.037;
|
||||
}
|
||||
else if (backend == DNN_BACKEND_INFERENCE_ENGINE &&
|
||||
(target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD)) {
|
||||
l1 = 0.031;
|
||||
lInf = 0.2;
|
||||
}
|
||||
testONNXModels("vgg16-bn", pb, l1, lInf);
|
||||
}
|
||||
|
||||
TEST_P(Test_ONNX_nets, ZFNet)
|
||||
{
|
||||
testONNXModels("zfnet512", pb);
|
||||
}
|
||||
|
||||
TEST_P(Test_ONNX_nets, ResNet18v1)
|
||||
{
|
||||
// output range: [-16; 22]
|
||||
const double l1 = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 0.022 : default_l1;
|
||||
const double lInf = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 0.12 : default_lInf;
|
||||
testONNXModels("resnet18v1", pb, l1, lInf);
|
||||
}
|
||||
|
||||
TEST_P(Test_ONNX_nets, ResNet50v1)
|
||||
{
|
||||
// output range: [-67; 75]
|
||||
const double l1 = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 0.6 : 1.25e-5;
|
||||
const double lInf = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 0.51 : 1.2e-4;
|
||||
testONNXModels("resnet50v1", pb, l1, lInf);
|
||||
}
|
||||
|
||||
TEST_P(Test_ONNX_nets, ResNet101_DUC_HDC)
|
||||
{
|
||||
if (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_OPENCL
|
||||
|| target == DNN_TARGET_MYRIAD) {
|
||||
throw SkipTestException("");
|
||||
}
|
||||
testONNXModels("resnet101_duc_hdc", pb);
|
||||
}
|
||||
|
||||
TEST_P(Test_ONNX_nets, TinyYolov2)
|
||||
{
|
||||
if (cvtest::skipUnstableTests ||
|
||||
backend == DNN_BACKEND_INFERENCE_ENGINE && (target == DNN_TARGET_OPENCL || target == DNN_TARGET_OPENCL_FP16)) {
|
||||
throw SkipTestException("");
|
||||
}
|
||||
// output range: [-11; 8]
|
||||
const double l1 = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 0.017 : default_l1;
|
||||
const double lInf = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 0.14 : default_lInf;
|
||||
testONNXModels("tiny_yolo2", pb, l1, lInf);
|
||||
}
|
||||
|
||||
TEST_P(Test_ONNX_nets, CNN_MNIST)
|
||||
{
|
||||
// output range: [-1952; 6574]
|
||||
const double l1 = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 3.82 : 4.3e-4;
|
||||
const double lInf = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 13.5 : 1e-3;
|
||||
|
||||
testONNXModels("cnn_mnist", pb, l1, lInf);
|
||||
}
|
||||
|
||||
TEST_P(Test_ONNX_nets, MobileNet_v2)
|
||||
{
|
||||
// output range: [-166; 317]
|
||||
const double l1 = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 0.38 : 7e-5;
|
||||
const double lInf = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 2.87 : 5e-4;
|
||||
testONNXModels("mobilenetv2", pb, l1, lInf);
|
||||
}
|
||||
|
||||
TEST_P(Test_ONNX_nets, LResNet100E_IR)
|
||||
{
|
||||
if (backend == DNN_BACKEND_INFERENCE_ENGINE &&
|
||||
(target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_OPENCL || target == DNN_TARGET_MYRIAD))
|
||||
throw SkipTestException("");
|
||||
|
||||
double l1 = default_l1;
|
||||
double lInf = default_lInf;
|
||||
// output range: [-3; 3]
|
||||
if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16) {
|
||||
l1 = 0.009;
|
||||
lInf = 0.035;
|
||||
}
|
||||
testONNXModels("LResNet100E_IR", pb, l1, lInf);
|
||||
}
|
||||
|
||||
TEST_P(Test_ONNX_nets, Emotion_ferplus)
|
||||
{
|
||||
testONNXModels("emotion_ferplus", pb);
|
||||
}
|
||||
|
||||
TEST_P(Test_ONNX_nets, Inception_v2)
|
||||
{
|
||||
if (backend == DNN_BACKEND_INFERENCE_ENGINE)
|
||||
throw SkipTestException("");
|
||||
|
||||
testONNXModels("inception_v2", pb);
|
||||
}
|
||||
|
||||
TEST_P(Test_ONNX_nets, DenseNet121)
|
||||
{
|
||||
// output range: [-87; 138]
|
||||
const double l1 = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 0.12 : 1.88e-5;
|
||||
const double lInf = (target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD) ? 0.74 : 1.23e-4;
|
||||
testONNXModels("densenet121", pb, l1, lInf);
|
||||
}
|
||||
|
||||
|
||||
INSTANTIATE_TEST_CASE_P(/**/, Test_ONNX_nets, dnnBackendsAndTargets());
|
||||
|
||||
}} // namespace
|
Loading…
Reference in New Issue
Block a user