question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Initial tensors(Initializer in Graph) reading by C++ are empty

See original GitHub issue

Ask 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;
}

from_netron.app.jpg

my_output.jpg

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:closed
  • Created a year ago
  • Reactions:1
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
SeHuibixiaoxingcommented, Oct 27, 2022

@jcwchen Thank you! Your suggestion succeeded in solving my problem. Wish you all the best!

Hi, would you please share the code snippet for reading initializer values?

Sure. It looks like:

auto initializer = graph->initializer();
for (auto tmp : initializer)
{
		if (tmp.data_type() == onnx::TensorProto::DataType::TensorProto_DataType_INT32) {
			auto intVec = ParseData<int32_t>(&tmp);
		}
		else if (tmp.data_type() == onnx::TensorProto::DataType::TensorProto_DataType_INT64) {
		
			auto llVec = ParseData<int64_t>(&tmp);
		}
               ... ...
}

The function ParseData is the one mentioned above.

1reaction
SeHuibixiaoxingcommented, Sep 8, 2022

@jcwchen Thank you! Your suggestion succeeded in solving my problem. Wish you all the best!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found