sezaru

sezaru

Can't run ONNX model via Ortex, input name cannot be empty

I have a ONNX model that I want to load in Elixir.

To do that, I’m using the Ortex library. I can load the model with no problem using Ortex.load function and it will give me the following model:

#Ortex.Model<
  inputs: [
    {"float_input",
     "Tensor {\n    ty: Float32,\n    dimensions: [\n        2341,\n        92,\n    ],\n}",
     [2341, 92]}
  ]
  outputs: [
    {"output_label",
     "Tensor {\n    ty: Int64,\n    dimensions: [\n        2341,\n    ],\n}",
     [2341]},
    {"output_probability",
     "Sequence(\n    Map {\n        key: Int64,\n        value: Float32,\n    },\n)",
     nil}
  ]>

The issue I’m having is when trying to run this model. it expects a "float_input" as input name, but I don’t know how to pass it in the run function.

If I try to just pass the tensor data I get the following error:

iex(19)> Ortex.run(model, x_test_scaled[0])
** (RuntimeError) Failed to run inference on model: Invalid rank for input: float_input Got: 1 Expected: 2 Please fix either the inputs/outputs or the model.
    (ortex 0.1.9) lib/ortex/model.ex:51: Ortex.Model.run/2

If I try to send something as the float_input I get the following error:

iex(19)> Ortex.run(model, {Nx.tensor(0), x_test_scaled[0]})
** (RuntimeError) Failed to run inference on model: input name cannot be empty
    (ortex 0.1.9) lib/ortex/model.ex:51: Ortex.Model.run/2

I also tried creating a Nx.Container and passing that to run:

defmodule MyData do
  @derive {Nx.Container, containers: [:float_input]}

  defstruct [:float_input]
end

iex(11)> Ortex.run(model, %MyData{float_input: x_test_scaled})
** (FunctionClauseError) no function clause matching in anonymous fn/1 in Ortex.Model.run/2    
    
    The following arguments were given to anonymous fn/1 in Ortex.Model.run/2:
    
        # 1
        %MyData{
          float_input: #Nx.Tensor<
            f32[2341][92]
            Ortex.Backend
            [
              [0.19482958316802979, 0.08041504770517349, -1.9489672183990479, 2.886521100997925, -0.4316384792327881, -0.3034050166606903, -0.2510624825954437, -0.08099287003278732, -0.010336779989302158, -0.2686353623867035, -0.2567836046218872, -0.25082194805145264, -0.23275044560432434, -0.24425934255123138, -0.31930288672447205, -0.3787024915218353, -0.27744582295417786, -0.029247770085930824, 0.7984414100646973, -0.47261980175971985, -0.2207702398300171, -0.4064401388168335, -0.06632958352565765, -0.031023602932691574, 0.0, -0.0206768736243248, -0.12976393103599548, -0.0206768736243248, -0.15836477279663086, -0.07027661055326462, -1.154808521270752, -0.034301552921533585, -0.1615263819694519, 1.3659762144088745, -0.2785608470439911, 1.2954195737838745, -0.27967265248298645, 0.35717639327049255, 1.4155744314193726, -0.48122650384902954, -0.6723564863204956, -0.402685284614563, -0.5743894577026367, -0.42710113525390625, -0.08297397941350937, -0.08165843784809113, -0.07546283304691315, -0.09343120455741882, -0.07104019820690155, ...],
              ...
            ]
          >
        }
    
    (ortex 0.1.9) lib/ortex/model.ex:49: anonymous fn/1 in Ortex.Model.run/2
    (elixir 1.16.2) lib/enum.ex:1700: Enum."-map/2-lists^map/1-1-"/2
    (ortex 0.1.9) lib/ortex/model.ex:49: Ortex.Model.run/2

In python, I can easily run the model using the following code:

onnx_session = onnxruntime.InferenceSession("logistic_regression_model.onnx")

output = onnx_session.run(None, {"float_input": input_data})

What I’m doing wrong?

Marked As Solved

gregszumel

gregszumel

Got it - looks like the call on x_test_scaled runs, but Ortex fails to parse the output.

The problem is in the output of your model, we’ve got: {"output_probability", "Sequence(Map {key: Int64,value: Float32,},)",nil}. However, Ortex does not currently support Sequences / Maps. Instead, it expects the output to be a list of tensors (that’s why there’s a panic, it’s expecting a tensor, but it’s finding a sequence/map structure).

Is there an easy way to convert your model’s output to a list of tensors?

Also Liked

sezaru

sezaru

I was able to change the output and now the model works fine, thanks @gregszumel !

Where Next?

Popular in Questions Top

albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement