Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for op_type "Resize" #35

Draft
wants to merge 15 commits into
base: master
Choose a base branch
from
56 changes: 56 additions & 0 deletions lib/axon_onnx/deserialize.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1591,6 +1591,62 @@ defmodule AxonOnnx.Deserialize do
{updated_axon, params, used_params}
end

defp recur_nodes(
%Node{
op_type: "Resize",
input: input,
attribute: attrs,
output: output
},
{axon, params, used_params}
) do

# op_type spec: https://github.com/onnx/onnx/blob/main/docs/Operators.md#resize

IO.inspect([input, output], label: "IO")
IO.inspect(options!(attrs), label: "attributes")

_roi = Map.get(params, "Resize_roi") |> IO.inspect(label: "roi") # [0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0]
_sizes = Map.get(params, "Resize_sizes") |> IO.inspect(label: "sizes") # nil
scales = Map.get(params, "Resize_scales") |> IO.inspect(label: "scales") # [1.0, 1.0, 2.0, 2.0]
jnnks marked this conversation as resolved.
Show resolved Hide resolved

%{
"coordinate_transformation_mode" => _transformation,
"mode" => mode,
"nearest_mode" => "floor"
} = options!(attrs)

[in_layer_name | _ ] = input
jnnks marked this conversation as resolved.
Show resolved Hide resolved
[out_layer_name] = output
inp = input!(in_layer_name, axon, params)
input_shape = get_shape(inp)
method = case mode do
"nearest" -> :nearest
"linear" -> :linear
"bilinear" -> :biliear
end
jnnks marked this conversation as resolved.
Show resolved Hide resolved

IO.inspect(input_shape, label: "input_shape")
IO.inspect(method, label: "method")

# this is terrible...
# element-wise multiply input shape and scale
output_shape = input_shape |> Tuple.to_list()
|> Enum.zip(Nx.to_flat_list(scales))
# multiply
|> Enum.map(fn {nil, _} -> nil
{is, sc}-> trunc(is * sc) end)
# drop non-spatial dimensions
|> Enum.drop(2)
# build tuple
|> Enum.reduce({}, fn e, acc -> Tuple.append(acc, e) end)
|> IO.inspect(label: "output_shape")
jnnks marked this conversation as resolved.
Show resolved Hide resolved

jnnks marked this conversation as resolved.
Show resolved Hide resolved
layer = Axon.resize(inp, output_shape, method: method)
updated_axon = Map.put(axon, out_layer_name, layer)
{updated_axon, params, used_params}
end

defp recur_nodes(%Node{op_type: unsupported}, _) do
raise ArgumentError, "unsupported #{inspect(unsupported)}"
end
Expand Down