Initial tensors(Initializer in Graph) reading by C++ are empty
See original GitHub issueAsk a Question
Question
When I use C + + to parse the *.onnx file by compiling the onnx.proto file in the repository, I can’t read the initial value though this value can be read in netron.app.
The code showing this problem is provided later. I use graph.initialzier() to read the initial tensors and print their names, types and some values. For example, in the model addition lstm, there is a none empty value W but I can’t read it(size() = 0).
#include <fstream>
#include <cassert>
#include <vector>
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include "onnx.pb.h"
std::string onnxPath = "../model/addition-lstm/addition_lstm.onnx";
int main(int argc, char** argv)
{
std::ifstream readStream(onnxPath, std::ios::ate | std::ios::in | std::ios::binary); // open file and move current position in file to the end
if (readStream.is_open()) {
printf("open successfully!\n");
}
std::streamsize size = readStream.tellg(); // get current position in file
readStream.seekg(0, std::ios::beg);
std::vector<char> buffer(size);
readStream.read(buffer.data(), size);
onnx::ModelProto model;
model.ParseFromArray(buffer.data(), size);
auto graph = model.graph();
auto nodes = graph.node();
std::cout << graph.input().size() << std::endl;
std::cout << "initializer" << std::endl;
for (auto init : graph.initializer()) {
std::cout << init.name()
<< ", type: " << init.data_type()
<< ", INT32_SIZE:" << init.int32_data().size()
<< ", INT64_SIZE:" << init.int64_data().size()
<< ", FLOAT32_SIZE:" << init.float_data().size();
std::cout << std::endl;
}
return 0;
}
The model can be downloaded from [addition_lstm_onnx](https://drive.google.com/file/d/1skYS4NRNHSJklS50jN5kjCWl3WlrDhiT/view?usp=sharing). In fact, The same problem occurs in all the models I have tried.
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Creating a tensorflow Variable without setting the initial value
When we create a Tensor using tf.Variable, the first argument is "initial_value". So, even if we don't have specific values as initial values...
Read more >TensorFlow Basics: Tensor, Shape, Type, Sessions & Operators
Variables are empty by default, even after you create a tensor. You need to initialize the variable if you want to use the...
Read more >tf.Variable | TensorFlow v2.11.0
A Tensor , or Python object convertible to a Tensor , which is the initial value for the Variable. The initial value must...
Read more >2- Tensor Types - Easy TensorFlow
This is the easiest way to initialize variables all variables at once. The following toy example shows how we can add an op...
Read more >ONNX Concepts — onnxcustom
The first scenario is to make it easier to deploy a machine learning model in production. ... x = onnx.input(0) a = initializer...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Sure. It looks like:
The function ParseData is the one mentioned above.
@jcwchen Thank you! Your suggestion succeeded in solving my problem. Wish you all the best!