Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/dnn/src/onnx/onnx_importer.cpp
16339 views
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.
4
5
// Copyright (C) 2018, Intel Corporation, all rights reserved.
6
// Third party copyrights are property of their respective owners.
7
8
#include "../precomp.hpp"
9
10
#ifdef HAVE_PROTOBUF
11
12
#include <iostream>
13
#include <fstream>
14
#include <string>
15
#include <limits>
16
#include <algorithm>
17
18
19
#if defined(__GNUC__) && __GNUC__ >= 5
20
#pragma GCC diagnostic push
21
#pragma GCC diagnostic ignored "-Wsuggest-override"
22
#endif
23
#include "opencv-onnx.pb.h"
24
#if defined(__GNUC__) && __GNUC__ >= 5
25
#pragma GCC diagnostic pop
26
#endif
27
28
namespace cv {
29
namespace dnn {
30
CV__DNN_INLINE_NS_BEGIN
31
32
33
class ONNXImporter
34
{
35
opencv_onnx::ModelProto model_proto;
36
struct LayerInfo {
37
int layerId;
38
int outputId;
39
LayerInfo(int _layerId, int _outputId) : layerId(_layerId), outputId(_outputId) {}
40
};
41
42
std::map<std::string, Mat> getGraphTensors(
43
const opencv_onnx::GraphProto& graph_proto);
44
Mat getBlob(const opencv_onnx::NodeProto& node_proto, const std::map<std::string, Mat>& constBlobs, int index);
45
46
LayerParams getLayerParams(const opencv_onnx::NodeProto& node_proto);
47
bool isCeilMode(const LayerParams& layerParams);
48
49
public:
50
51
ONNXImporter(const char *onnxFile)
52
{
53
std::fstream input(onnxFile, std::ios::in | std::ios::binary);
54
55
if (!model_proto.ParseFromIstream(&input))
56
CV_Error(Error::StsUnsupportedFormat, "Failed to parse onnx model");
57
}
58
59
void populateNet(Net dstNet);
60
};
61
62
inline void replaceLayerParam(LayerParams& layerParams, const String& oldKey, const String& newKey)
63
{
64
if (layerParams.has(oldKey)) {
65
layerParams.set(newKey, layerParams.get(oldKey));
66
layerParams.erase(oldKey);
67
}
68
}
69
70
void releaseONNXTensor(opencv_onnx::TensorProto& tensor_proto)
71
{
72
if (!tensor_proto.raw_data().empty()) {
73
delete tensor_proto.release_raw_data();
74
}
75
}
76
77
template<typename T1, typename T2>
78
void convertInt64ToInt32(const T1& src, T2& dst, int size)
79
{
80
for (int i = 0; i < size; i++) {
81
if (src[i] < std::numeric_limits<int32_t>::min() || src[i] > std::numeric_limits<int32_t>::max()) {
82
CV_Error(Error::StsOutOfRange, "Input is out of OpenCV 32S range");
83
}
84
dst[i] = saturate_cast<int32_t>(src[i]);
85
}
86
}
87
88
Mat getMatFromTensor(opencv_onnx::TensorProto& tensor_proto)
89
{
90
CV_Assert(!tensor_proto.raw_data().empty() || !tensor_proto.float_data().empty()
91
|| !tensor_proto.double_data().empty() || !tensor_proto.int64_data().empty());
92
93
opencv_onnx::TensorProto_DataType datatype = tensor_proto.data_type();
94
Mat blob;
95
std::vector<int> sizes;
96
for (int i = 0; i < tensor_proto.dims_size(); i++) {
97
sizes.push_back(tensor_proto.dims(i));
98
}
99
if (datatype == opencv_onnx::TensorProto_DataType_FLOAT) {
100
101
if (!tensor_proto.float_data().empty()) {
102
const ::google::protobuf::RepeatedField<float> field = tensor_proto.float_data();
103
Mat(sizes, CV_32FC1, (void*)field.data()).copyTo(blob);
104
}
105
else {
106
char* val = const_cast<char*>(tensor_proto.raw_data().c_str());
107
Mat(sizes, CV_32FC1, val).copyTo(blob);
108
}
109
}
110
else if (datatype == opencv_onnx::TensorProto_DataType_DOUBLE)
111
{
112
const ::google::protobuf::RepeatedField<double> field = tensor_proto.double_data();
113
CV_Assert(!field.empty());
114
Mat(sizes, CV_64FC1, (void*)field.data()).convertTo(blob, CV_32FC1);
115
}
116
else if (datatype == opencv_onnx::TensorProto_DataType_INT64)
117
{
118
blob.create(sizes, CV_32SC1);
119
int32_t* dst = reinterpret_cast<int32_t*>(blob.data);
120
121
if (!tensor_proto.int64_data().empty()) {
122
::google::protobuf::RepeatedField< ::google::protobuf::int64> src = tensor_proto.int64_data();
123
convertInt64ToInt32(src, dst, blob.total());
124
}
125
else
126
{
127
char* val = const_cast<char*>(tensor_proto.raw_data().c_str());
128
int64_t* src = reinterpret_cast<int64_t*>(val);
129
convertInt64ToInt32(src, dst, blob.total());
130
}
131
}
132
else
133
CV_Error(Error::StsUnsupportedFormat, "Unsupported data type: " +
134
opencv_onnx::TensorProto_DataType_Name(datatype));
135
return blob;
136
}
137
138
std::map<std::string, Mat> ONNXImporter::getGraphTensors(
139
const opencv_onnx::GraphProto& graph_proto)
140
{
141
opencv_onnx::TensorProto tensor_proto;
142
std::map<std::string, Mat> layers_weights;
143
144
for (int i = 0; i < graph_proto.initializer_size(); i++)
145
{
146
tensor_proto = graph_proto.initializer(i);
147
Mat mat = getMatFromTensor(tensor_proto);
148
releaseONNXTensor(tensor_proto);
149
layers_weights.insert(std::make_pair(tensor_proto.name(), mat));
150
}
151
return layers_weights;
152
}
153
154
LayerParams ONNXImporter::getLayerParams(const opencv_onnx::NodeProto& node_proto)
155
{
156
LayerParams lp;
157
for(int i = 0; i < node_proto.attribute_size(); i++)
158
{
159
opencv_onnx::AttributeProto attribute_proto = node_proto.attribute(i);
160
std::string attribute_name = attribute_proto.name();
161
162
if(attribute_name == "kernel_shape")
163
{
164
CV_Assert(attribute_proto.ints_size() == 2);
165
lp.set("kernel_h", saturate_cast<int32_t>(attribute_proto.ints(0)));
166
lp.set("kernel_w", saturate_cast<int32_t>(attribute_proto.ints(1)));
167
}
168
else if(attribute_name == "strides")
169
{
170
CV_Assert(attribute_proto.ints_size() == 2);
171
lp.set("stride_h", saturate_cast<int32_t>(attribute_proto.ints(0)));
172
lp.set("stride_w", saturate_cast<int32_t>(attribute_proto.ints(1)));
173
}
174
else if(attribute_name == "pads")
175
{
176
CV_Assert(attribute_proto.ints_size() == 4);
177
lp.set("pad_t", saturate_cast<int32_t>(attribute_proto.ints(0)));
178
lp.set("pad_l", saturate_cast<int32_t>(attribute_proto.ints(1)));
179
lp.set("pad_b", saturate_cast<int32_t>(attribute_proto.ints(2)));
180
lp.set("pad_r", saturate_cast<int32_t>(attribute_proto.ints(3)));
181
}
182
else if(attribute_name == "auto_pad")
183
{
184
if (attribute_proto.s() == "SAME_UPPER" || attribute_proto.s() == "SAME_LOWER") {
185
lp.set("pad_mode", "SAME");
186
}
187
else if (attribute_proto.s() == "VALID") {
188
lp.set("pad_mode", "VALID");
189
}
190
}
191
else if(attribute_name == "dilations")
192
{
193
CV_Assert(attribute_proto.ints_size() == 2);
194
lp.set("dilation_h", saturate_cast<int32_t>(attribute_proto.ints(0)));
195
lp.set("dilation_w", saturate_cast<int32_t>(attribute_proto.ints(1)));
196
}
197
else if (attribute_proto.has_i())
198
{
199
::google::protobuf::int64 src = attribute_proto.i();
200
if (src < std::numeric_limits<int32_t>::min() || src > std::numeric_limits<int32_t>::max())
201
CV_Error(Error::StsOutOfRange, "Input is out of OpenCV 32S range");
202
else
203
lp.set(attribute_name, saturate_cast<int32_t>(src));
204
}
205
else if (attribute_proto.has_f())
206
{
207
lp.set(attribute_name, attribute_proto.f());
208
}
209
else if (attribute_proto.has_s())
210
{
211
lp.set(attribute_name, attribute_proto.s());
212
}
213
else if (attribute_proto.floats_size() > 0)
214
{
215
lp.set(attribute_name, DictValue::arrayReal(
216
attribute_proto.floats().data(), attribute_proto.floats_size()));
217
}
218
else if (attribute_proto.ints_size() > 0)
219
{
220
const ::google::protobuf::RepeatedField< ::google::protobuf::int64> src = attribute_proto.ints();
221
std::vector<int32_t> dst(attribute_proto.ints_size());
222
convertInt64ToInt32(src, dst, attribute_proto.ints_size());
223
lp.set(attribute_proto.name(), DictValue::arrayInt(&dst[0], attribute_proto.ints_size()));
224
}
225
else if (attribute_proto.has_t())
226
{
227
opencv_onnx::TensorProto tensor = attribute_proto.t();
228
Mat blob = getMatFromTensor(tensor);
229
lp.blobs.push_back(blob);
230
}
231
else if (attribute_proto.has_g() || attribute_proto.strings_size() > 0 ||
232
attribute_proto.tensors_size() > 0 || attribute_proto.graphs_size() > 0)
233
{
234
CV_Error(Error::StsNotImplemented, "Unexpected attribute type");
235
}
236
else
237
CV_Error(Error::StsNotImplemented, "Unsupported attribute type");
238
}
239
return lp;
240
}
241
242
Mat ONNXImporter::getBlob(const opencv_onnx::NodeProto& node_proto,
243
const std::map<std::string, Mat>& constBlobs, int index)
244
{
245
CV_Assert(index < node_proto.input_size());
246
std::map<std::string, Mat>::const_iterator constBlob;
247
constBlob = constBlobs.find(node_proto.input(index));
248
if (constBlob == constBlobs.end()) {
249
CV_Error(Error::StsObjectNotFound,
250
"Blob " + node_proto.input(index) + " not found in const blobs");
251
}
252
return constBlob->second;
253
}
254
255
256
bool ONNXImporter::isCeilMode(const LayerParams& layerParams) {
257
if (!layerParams.has("pad_mode")) {
258
if (layerParams.has("pad_h")) {
259
return layerParams.get<int>("pad_h") != layerParams.get<int>("pad_b") ||
260
layerParams.get<int>("pad_w") != layerParams.get<int>("pad_r");
261
}
262
else
263
return false; // all pads == 0
264
}
265
return true;
266
}
267
268
void ONNXImporter::populateNet(Net dstNet)
269
{
270
CV_Assert(model_proto.has_graph());
271
opencv_onnx::GraphProto graph_proto = model_proto.graph();
272
std::map<std::string, Mat> constBlobs = getGraphTensors(graph_proto);
273
274
std::string framework_name;
275
if (model_proto.has_producer_name()) {
276
framework_name = model_proto.producer_name();
277
}
278
279
// create map with network inputs (without const blobs)
280
std::map<std::string, LayerInfo> layer_id;
281
std::map<std::string, LayerInfo>::iterator layerId;
282
// fill map: push layer name, layer id and output id
283
std::vector<String> netInputs;
284
for (int j = 0; j < graph_proto.input_size(); j++)
285
{
286
const std::string& name = graph_proto.input(j).name();
287
if (constBlobs.find(name) == constBlobs.end()) {
288
netInputs.push_back(name);
289
layer_id.insert(std::make_pair(name, LayerInfo(0, netInputs.size() - 1)));
290
}
291
}
292
dstNet.setInputsNames(netInputs);
293
294
int layersSize = graph_proto.node_size();
295
LayerParams layerParams;
296
opencv_onnx::NodeProto node_proto;
297
298
for(int i = 0; i < layersSize; i++)
299
{
300
node_proto = graph_proto.node(i);
301
layerParams = getLayerParams(node_proto);
302
CV_Assert(node_proto.output_size() >= 1);
303
layerParams.name = node_proto.output(0);
304
305
std::string layer_type = node_proto.op_type();
306
layerParams.type = layer_type;
307
308
309
if (layer_type == "MaxPool")
310
{
311
layerParams.type = "Pooling";
312
layerParams.set("pool", "MAX");
313
layerParams.set("ceil_mode", isCeilMode(layerParams));
314
}
315
else if (layer_type == "AveragePool")
316
{
317
layerParams.type = "Pooling";
318
layerParams.set("pool", "AVE");
319
layerParams.set("ceil_mode", isCeilMode(layerParams));
320
layerParams.set("ave_pool_padded_area", framework_name == "pytorch");
321
}
322
else if (layer_type == "GlobalAveragePool")
323
{
324
layerParams.type = "Pooling";
325
layerParams.set("pool", "AVE");
326
layerParams.set("global_pooling", true);
327
}
328
else if (layer_type == "Add" || layer_type == "Sum")
329
{
330
if (layer_id.find(node_proto.input(1)) == layer_id.end())
331
{
332
Mat blob = getBlob(node_proto, constBlobs, 1);
333
blob = blob.reshape(1, 1);
334
if (blob.total() == 1) {
335
layerParams.type = "Power";
336
layerParams.set("shift", blob.at<float>(0));
337
}
338
else {
339
layerParams.type = "Shift";
340
layerParams.blobs.push_back(blob);
341
}
342
}
343
else {
344
layerParams.type = "Eltwise";
345
}
346
}
347
else if (layer_type == "Sub")
348
{
349
Mat blob = (-1.0f) * getBlob(node_proto, constBlobs, 1);
350
blob = blob.reshape(1, 1);
351
if (blob.total() == 1) {
352
layerParams.type = "Power";
353
layerParams.set("shift", blob.at<float>(0));
354
}
355
else {
356
layerParams.type = "Shift";
357
layerParams.blobs.push_back(blob);
358
}
359
}
360
else if (layer_type == "Constant")
361
{
362
CV_Assert(node_proto.input_size() == 0);
363
CV_Assert(layerParams.blobs.size() == 1);
364
constBlobs.insert(std::make_pair(layerParams.name, layerParams.blobs[0]));
365
continue;
366
}
367
else if (layer_type == "ImageScaler")
368
{
369
const float scale = layerParams.has("scale") ? layerParams.get<float>("scale") : 1.0f;
370
layerParams.erase("scale");
371
372
if (layerParams.has("bias"))
373
{
374
layerParams.type = "Scale";
375
layerParams.blobs.push_back(
376
Mat(Size(1, layerParams.get("bias").size()), CV_32FC1, scale));
377
378
layerParams.set("bias_term", true);
379
Mat bias(1, layerParams.get("bias").size(), CV_32FC1);
380
for (int j = 0; j < bias.total(); j++) {
381
bias.at<float>(0, j) = layerParams.get("bias").getRealValue(j);
382
}
383
layerParams.blobs.push_back(bias);
384
layerParams.erase("bias");
385
}
386
else {
387
layerParams.set("scale", scale);
388
layerParams.type = "Power";
389
}
390
}
391
else if (layer_type == "LeakyRelu")
392
{
393
layerParams.type = "ReLU";
394
replaceLayerParam(layerParams, "alpha", "negative_slope");
395
}
396
else if (layer_type == "LRN")
397
{
398
replaceLayerParam(layerParams, "size", "local_size");
399
}
400
else if (layer_type == "BatchNormalization")
401
{
402
if (node_proto.input_size() != 5)
403
CV_Error(Error::StsNotImplemented,
404
"Expected input, scale, bias, mean and var");
405
406
layerParams.type = "BatchNorm";
407
replaceLayerParam(layerParams, "epsilon", "eps");
408
replaceLayerParam(layerParams, "spatial", "use_global_stats");
409
410
Mat meanData = getBlob(node_proto, constBlobs, 3);
411
Mat stdData = getBlob(node_proto, constBlobs, 4);
412
413
layerParams.blobs.push_back(meanData);
414
layerParams.blobs.push_back(stdData);
415
416
if (!node_proto.input(1).empty()) {
417
layerParams.set("has_weight", true);
418
layerParams.blobs.push_back(getBlob(node_proto, constBlobs, 1)); // weightData
419
} else {
420
layerParams.set("has_weight", false);
421
}
422
423
if (!node_proto.input(2).empty()) {
424
layerParams.set("has_bias", true);
425
layerParams.blobs.push_back(getBlob(node_proto, constBlobs, 2)); // biasData
426
} else {
427
layerParams.set("has_bias", false);
428
}
429
}
430
else if (layer_type == "Gemm")
431
{
432
CV_Assert(node_proto.input_size() >= 2);
433
layerParams.type = "InnerProduct";
434
Mat weights = getBlob(node_proto, constBlobs, 1);
435
int ind_num_out = 0;
436
if (layerParams.has("transB") && !layerParams.get<int>("transB")) {
437
transpose(weights, weights);
438
ind_num_out = 1;
439
}
440
layerParams.blobs.push_back(weights);
441
442
if (node_proto.input_size() == 3) {
443
Mat bias = getBlob(node_proto, constBlobs, 2);
444
layerParams.blobs.push_back(bias);
445
}
446
447
layerParams.set("num_output", layerParams.blobs[0].size[ind_num_out]);
448
layerParams.set("bias_term", node_proto.input_size() == 3);
449
}
450
else if (layer_type == "MatMul")
451
{
452
CV_Assert(node_proto.input_size() == 2);
453
layerParams.type = "InnerProduct";
454
Mat blob = getBlob(node_proto, constBlobs, 1);
455
layerParams.blobs.push_back(blob.t());
456
layerParams.set("bias_term", false);
457
layerParams.set("num_output", layerParams.blobs[0].size[0]);
458
}
459
else if (layer_type == "Mul")
460
{
461
CV_Assert(node_proto.input_size() == 2);
462
if (layer_id.find(node_proto.input(1)) == layer_id.end()) {
463
Mat blob = getBlob(node_proto, constBlobs, 1);
464
blob = blob.reshape(1, 1);
465
if (blob.total() == 1) {
466
layerParams.set("scale", blob.at<float>(0));
467
layerParams.type = "Power";
468
}
469
else {
470
layerParams.blobs.push_back(blob);
471
layerParams.type = "Scale";
472
}
473
}
474
else {
475
layerParams.type = "Eltwise";
476
layerParams.set("operation", "prod");
477
}
478
}
479
else if (layer_type == "Conv")
480
{
481
CV_Assert(node_proto.input_size() >= 2);
482
layerParams.type = "Convolution";
483
for (int j = 1; j < node_proto.input_size(); j++) {
484
layerParams.blobs.push_back(getBlob(node_proto, constBlobs, j));
485
}
486
layerParams.set("num_output", layerParams.blobs[0].size[0]);
487
layerParams.set("bias_term", node_proto.input_size() == 3);
488
}
489
else if (layer_type == "Transpose")
490
{
491
layerParams.type = "Permute";
492
replaceLayerParam(layerParams, "perm", "order");
493
}
494
else if (layer_type == "Unsqueeze")
495
{
496
CV_Assert(node_proto.input_size() == 1);
497
Mat input = getBlob(node_proto, constBlobs, 0);
498
499
DictValue axes = layerParams.get("axes");
500
std::vector<int> dims;
501
for (int j = 0; j < input.dims; j++) {
502
dims.push_back(input.size[j]);
503
}
504
CV_Assert(axes.getIntValue(axes.size()-1) <= dims.size());
505
for (int j = 0; j < axes.size(); j++) {
506
dims.insert(dims.begin() + axes.getIntValue(j), 1);
507
}
508
509
Mat out = input.reshape(0, dims);
510
constBlobs.insert(std::make_pair(layerParams.name, out));
511
continue;
512
}
513
else if (layer_type == "Reshape")
514
{
515
CV_Assert(node_proto.input_size() == 2 || layerParams.has("shape"));
516
517
if (node_proto.input_size() == 2) {
518
Mat blob = getBlob(node_proto, constBlobs, 1);
519
CV_Assert(blob.type() == CV_32SC1);
520
521
if (layer_id.find(node_proto.input(0)) == layer_id.end()) {
522
Mat input = getBlob(node_proto, constBlobs, 0);
523
Mat out = input.reshape(0, static_cast<std::vector<int> >(blob));
524
constBlobs.insert(std::make_pair(layerParams.name, out));
525
continue;
526
}
527
layerParams.set("dim", DictValue::arrayInt<int*>(
528
blob.ptr<int>(), blob.total() ));
529
}
530
else {
531
DictValue shape = layerParams.get("shape");
532
std::vector<int> dim;
533
for (int j = 0; j < shape.size(); j++) {
534
dim.push_back(shape.getIntValue(j));
535
}
536
537
if (layer_id.find(node_proto.input(0)) == layer_id.end()) {
538
Mat input = getBlob(node_proto, constBlobs, 0);
539
Mat out = input.reshape(0, dim);
540
constBlobs.insert(std::make_pair(layerParams.name, out));
541
continue;
542
}
543
replaceLayerParam(layerParams, "shape", "dim");
544
}
545
}
546
else
547
{
548
for (int j = 0; j < node_proto.input_size(); j++) {
549
if (layer_id.find(node_proto.input(j)) == layer_id.end())
550
layerParams.blobs.push_back(getBlob(node_proto, constBlobs, j));
551
}
552
}
553
554
int id = dstNet.addLayer(layerParams.name, layerParams.type, layerParams);
555
layer_id.insert(std::make_pair(layerParams.name, LayerInfo(id, 0)));
556
557
for (int j = 0; j < node_proto.input_size(); j++) {
558
layerId = layer_id.find(node_proto.input(j));
559
if (layerId != layer_id.end()) {
560
dstNet.connect(layerId->second.layerId, layerId->second.outputId, id, j);
561
}
562
}
563
}
564
}
565
566
Net readNetFromONNX(const String& onnxFile)
567
{
568
ONNXImporter onnxImporter(onnxFile.c_str());
569
Net net;
570
onnxImporter.populateNet(net);
571
return net;
572
}
573
574
Mat readTensorFromONNX(const String& path)
575
{
576
opencv_onnx::TensorProto tensor_proto = opencv_onnx::TensorProto();
577
std::fstream input(path.c_str(), std::ios::in | std::ios::binary);
578
if (!tensor_proto.ParseFromIstream(&input)) {
579
CV_Error(Error::StsUnsupportedFormat, "Failed to parse data");
580
}
581
Mat mat = getMatFromTensor(tensor_proto);
582
releaseONNXTensor(tensor_proto);
583
return mat;
584
}
585
586
CV__DNN_INLINE_NS_END
587
}} // namespace
588
589
#endif
590
591