Skip to content

Commit

Permalink
Conversion from primitive types to newer types
Browse files Browse the repository at this point in the history
  • Loading branch information
ayush1999 committed Feb 17, 2018
1 parent 897b0d7 commit 213005c
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 6 deletions.
83 changes: 83 additions & 0 deletions src/convert.jl
Expand Up @@ -19,6 +19,39 @@ function convert_model(model::Proto.AttributeProto)
return dict
end

"""
Convert an Array of ValueInfoProto to Array of Dicts.
"""
function convert_model(model::Array{ONNX.Proto.ValueInfoProto,1})
a = Array{Any, 1}()
for ele in model
push!(a, convert_model(ele))
end
return a
end

"""
Convert an Array of OperatorSetIdProto to Array of Dicts.
"""
function convert_model(model::Array{ONNX.Proto.OperatorSetIdProto,1})
a = Array{Any, 1}()
for ele in model
push!(a, convert_model(ele))
end
return a
end

"""
Convert an Array of StringStringEntryProto to Array of Dicts.
"""
function convert_model(model::Array{ONNX.Proto.StringStringEntryProto,1})
a = Array{Any, 1}()
for ele in model
push!(a, convert_model(ele))
end
return a
end

"""
Get the array from a TensorProto object.
"""
Expand All @@ -27,3 +60,53 @@ function get_array(x::Proto.TensorProto)
x = reshape(reinterpret(Float32, x.raw_data), x.dims...)
return permutedims(x, reverse(1:ndims(x)))
end

"""
Convert a ModelProto object to a Model type.
"""
function convert(model::Proto.ModelProto)
m = Types.Model(model.ir_version,
convert_model(model.opset_import),
model.producer_name,
model.producer_version,
model.domain, model.model_version,
model.doc_string, convert(model.graph),
convert_model(model.metadata_props))
return m
end

"""
Convert a GraphProto object to Graph type.
"""
function convert(model::Proto.GraphProto)
temp = model.initializer
d = Dict()
for ele in temp
d[ele.name] = get_array(ele)
end
a = Array{Any, 1}()
for ele in model.node
push!(a, convert(ele))
end
m = Types.Graph(a,
model.name,
d, model.doc_string,
convert_model(model.input),
convert_model(model.output),
convert_model(model.value_info))
return m
end

"""
Convert a Proto.NodeProto to Node type.
"""
function convert(model::Proto.NodeProto)
m = Types.Node(model.input,
model.output,
model.name,
model.op_type,
model.domain,
convert_model(model.attribute),
model.doc_string)
return m
end
17 changes: 11 additions & 6 deletions src/new_types.jl
Expand Up @@ -7,6 +7,8 @@
The new types will consist of Julian attributes.
=#

module Types

mutable struct Node
input::Vector{AbstractString}
output::Vector{AbstractString}
Expand All @@ -20,21 +22,24 @@ end
mutable struct Graph
node::Vector{Node}
name::AbstractString
initializer::Dict{Any, Array{Any, 1}} #Storing the array data instead of the tensorproto vector.
initializer::Dict{Any, Any} #Storing the array data instead of the tensorproto vector.
doc_string::AbstractString #in Dict format.
input::Vector{Dict} #
output::Vector{Dict} # ValueInfoProto to Dict type.
value_info::Vector{Dict} #
input::Array{Any, 1} #
output::Array{Any, 1} # ValueInfoProto to Dict type.
value_info::Array{Any, 1} #
end

mutable struct Model
ir_version::Int64
opset_import::Vector{Dict} #OperatorSetIdProto to Dict
opset_import::Array{Any, 1} #OperatorSetIdProto to Dict
producer_name::AbstractString
producer_version::AbstractString
domain::AbstractString
model_version::Int64
doc_string::AbstractString
graph::Graph
metadata_props::Vector{Dict} #StringStringEntryProto to Dict
metadata_props::Array{Any, 1} #StringStringEntryProto to Dict
end

export Model, Graph, Node
end

0 comments on commit 213005c

Please sign in to comment.