read_onnx.cpp 2.47 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
4
5
6

#include <google/protobuf/text_format.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>
#include <onnx.pb.h>
#include <iostream>
#include <fstream>
Paul's avatar
Paul committed
7
#include <unordered_map>
Paul's avatar
Paul committed
8

Paul's avatar
Paul committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <rtg/program.hpp>

struct unknown
{
    rtg::shape s;
    std::string op;
    std::string name() const
    {
        return "unknown:"+op;
    }
    rtg::shape compute_shape(std::vector<rtg::shape> input) const
    {
        return s;
    }
    rtg::argument compute(std::vector<rtg::argument> input) const
    {
        throw "not computable";
    }
};
Paul's avatar
Paul committed
28

Paul's avatar
Paul committed
29
30
31
32
33
34
35
36
37
38
std::unordered_map<std::string, onnx::AttributeProto> get_attributes(const onnx::NodeProto& node)
{
    std::unordered_map<std::string, onnx::AttributeProto> result;
    for(auto&& attr:node.attribute())
    {
        result[attr.name()] = attr;
    }
    return result;
}

Paul's avatar
Paul committed
39
40
41
42
43
44
45
46
47
48
std::unordered_map<std::string, onnx::NodeProto> get_nodes(const onnx::GraphProto& graph)
{
    std::unordered_map<std::string, onnx::NodeProto> result;
    for(auto&& node:graph.node())
    {
        result[node.name()] = node;
    }
    return result;
}

Paul's avatar
Paul committed
49
50
51
52
53
54
55
56
57
58
void parse_graph(onnx::GraphProto graph)
{
    std::cout << "Graph name: " << graph.name() << std::endl;
    for(onnx::NodeProto node:graph.node()) {
        std::cout << "Layer: " << node.op_type() << std::endl;
        std::cout << "    Name: " << node.name() << std::endl;
        if(node.input_size() > 0)
            std::cout << "    Input: " << node.input(0) << std::endl;
        if(node.output_size() > 0)
            std::cout << "    Output: " << node.output(0) << std::endl;
Paul's avatar
Paul committed
59
60
61
62
63
64
        
        std::cout << "    Attributes: " << std::endl;
        for(auto&& attr:node.attribute())
        {
            std::cout << "        " << attr.name() << std::endl;
        }
Paul's avatar
Paul committed
65
66
67
    }
}

Paul's avatar
Paul committed
68
69
70
71
72
73
74
75
76
77
78
79
80
int main(int argc, char const *argv[])
{
    if(argc > 1)
    {
        std::string file = argv[1];
        std::fstream input(file.c_str(), std::ios::in | std::ios::binary);
        onnx::ModelProto model;
        if(model.ParseFromIstream(&input)) {
            std::cout << "Model version: " << model.model_version() << std::endl;
            std::cout << "Producer name: " << model.producer_name() << std::endl;
            std::cout << "Producer version: " << model.release_producer_version() << std::endl;
            if(model.has_graph()) {
                std::cout << "Model has graph" << std::endl;
Paul's avatar
Paul committed
81
                parse_graph(model.graph());
Paul's avatar
Paul committed
82
83
84
85
86
87
88
            }
        } else {
            std::cout << "Failed reading: " << file << std::endl;
        }

    }
}