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 @@ -2028,6 +2028,62 @@ defmodule AxonOnnx.Deserialize do
{updated_axon, params, used_params}
end

defp recur_nodes(
%Node{
op_type: "Resize",
input: [in_layer_name | rest_of_inputs],
attribute: attrs,
output: [out_layer_name]
},
{axon, params, used_params}
) do
# op_type spec: https://github.com/onnx/onnx/blob/main/docs/Operators.md#resize

inp = input!(in_layer_name, axon, params, used_params)

%{"mode" => mode} = options!(attrs)
method =
case mode do
"linear" -> :bilinear
"cubic" -> :bicubic
"nearest" -> :nearest
_mode -> raise ArgumentError, "only linear is implemented"
end

[roi, scales, sizes] =
case rest_of_inputs do
# ["", scales] -> input!(scales, axon, params, used_params)
["", scales] -> [nil, constant!(scales, axon, params, used_params), nil]
[roi, scales] -> [constant!(roi, axon, params, used_params), constant!(scales, axon, params, used_params), nil]

["", "", sizes] -> [nil, nil, constant!(sizes, axon, params, used_params)]
[roi, "", sizes] -> [constant!(roi, axon, params, used_params), nil, constant!(sizes, axon, params, used_params)]
_ -> raise ArgumentError, "only scales is implemented"
end

layer_inputs = inp
|> Axon.get_inputs()
|> Map.new(fn {name, shape} ->
{name, Nx.broadcast(0.0, shape)}
end)

output_shape =
Axon.get_output_shape(inp, layer_inputs)
|> Tuple.to_list()
|> Nx.tensor()
|> Nx.multiply(scales)
|> Nx.to_flat_list()
|> Enum.map(&Kernel.trunc/1)

output_shape = output_shape
|> Enum.drop(length(output_shape) - 2)
|> List.to_tuple()

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: "Dropout",
Expand Down
47 changes: 24 additions & 23 deletions test/axon_onnx/deserialize_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -683,29 +683,30 @@ defmodule DeserializeTest do
# check_onnx_test_case!("node", "test_reshape_zero_dim")
# end

# test "Resize" do
# check_onnx_test_case!("node", "test_resize_downsample_scales_cubic")
# check_onnx_test_case!("node", "test_resize_downsample_scales_cubic_A_n0p5_exclude_outside")
# check_onnx_test_case!("node", "test_resize_downsample_scales_cubic_align_corners")
# check_onnx_test_case!("node", "test_resize_downsample_scales_linear")
# check_onnx_test_case!("node", "test_resize_downsample_scales_linear_align_corners")
# check_onnx_test_case!("node", "test_resize_downsample_scales_nearest")
# check_onnx_test_case!("node", "test_resize_downsample_sizes_cubic")
# check_onnx_test_case!("node", "test_resize_downsample_sizes_linear_pytorch_half_pixel")
# check_onnx_test_case!("node", "test_resize_downsample_sizes_nearest")
# check_onnx_test_case!("node", "test_resize_downsample_sizes_nearest_tf_half_pixel_for_nn")
# check_onnx_test_case!("node", "test_resize_tf_crop_and_resize")
# check_onnx_test_case!("node", "test_resize_upsample_scales_cubic")
# check_onnx_test_case!("node", "test_resize_upsample_scales_cubic_A_n0p5_exclude_outside")
# check_onnx_test_case!("node", "test_resize_upsample_scales_cubic_align_corners")
# check_onnx_test_case!("node", "test_resize_upsample_scales_cubic_asymmetric")
# check_onnx_test_case!("node", "test_resize_upsample_scales_linear")
# check_onnx_test_case!("node", "test_resize_upsample_scales_linear_align_corners")
# check_onnx_test_case!("node", "test_resize_upsample_scales_nearest")
# check_onnx_test_case!("node", "test_resize_upsample_scales_nearest_ceil_half_pixel")
# check_onnx_test_case!("node", "test_resize_upsample_scales_nearest_floor_align_corners")
# check_onnx_test_case!("node", "test_resize_upsample_scales_nearest_round_prefer_ceil_asymmetric")
# end
@tag :resize
test "Resize" do
# check_onnx_test_case!("node", "test_resize_downsample_scales_cubic")
# check_onnx_test_case!("node", "test_resize_downsample_scales_cubic_A_n0p5_exclude_outside")
# check_onnx_test_case!("node", "test_resize_downsample_scales_cubic_align_corners")
# check_onnx_test_case!("node", "test_resize_downsample_scales_linear")
# check_onnx_test_case!("node", "test_resize_downsample_scales_linear_align_corners")
check_onnx_test_case!("..", "test_resize_downsample_scales_nearest")
# check_onnx_test_case!("node", "test_resize_downsample_sizes_cubic")
# check_onnx_test_case!("node", "test_resize_downsample_sizes_linear_pytorch_half_pixel")
# check_onnx_test_case!("node", "test_resize_downsample_sizes_nearest")
# check_onnx_test_case!("node", "test_resize_downsample_sizes_nearest_tf_half_pixel_for_nn")
# check_onnx_test_case!("node", "test_resize_tf_crop_and_resize")
# check_onnx_test_case!("node", "test_resize_upsample_scales_cubic")
# check_onnx_test_case!("node", "test_resize_upsample_scales_cubic_A_n0p5_exclude_outside")
# check_onnx_test_case!("node", "test_resize_upsample_scales_cubic_align_corners")
# check_onnx_test_case!("node", "test_resize_upsample_scales_cubic_asymmetric")
# check_onnx_test_case!("node", "test_resize_upsample_scales_linear")
# check_onnx_test_case!("node", "test_resize_upsample_scales_linear_align_corners")
# check_onnx_test_case!("node", "test_resize_upsample_scales_nearest")
# check_onnx_test_case!("node", "test_resize_upsample_scales_nearest_ceil_half_pixel")
# check_onnx_test_case!("node", "test_resize_upsample_scales_nearest_floor_align_corners")
# check_onnx_test_case!("node", "test_resize_upsample_scales_nearest_round_prefer_ceil_asymmetric")
end

# test "Round" do
# check_onnx_test_case!("node", "test_round")
Expand Down
10 changes: 10 additions & 0 deletions test/test_resize_downsample_scales_nearest/decoded/input_0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#Nx.Tensor<
f32[1][1][2][4]
[
[
[
[1.0, 2.0, 3.0, 4.0],
[5.0, 6.0, 7.0, 8.0]
]
]

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#Nx.Tensor<
f32[4]
[1.0, 1.0, 0.6000000238418579, 0.6000000238418579]
>
73 changes: 73 additions & 0 deletions test/test_resize_downsample_scales_nearest/decoded/model.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
ir_version: 7
producer_name: "backend-test"
graph {
node {
input: "X"
input: ""
input: "scales"
output: "Y"
op_type: "Resize"
attribute {
name: "mode"
s: "nearest"
type: STRING
}
}
name: "test_resize_downsample_scales_nearest"
initializer {
dims: 4
data_type: 1
float_data: 1
float_data: 1
float_data: 0.6
float_data: 0.6
name: "scales"
}
input {
name: "X"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 1
}
dim {
dim_value: 1
}
dim {
dim_value: 2
}
dim {
dim_value: 4
}
}
}
}
}
output {
name: "Y"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_value: 1
}
dim {
dim_value: 1
}
dim {
dim_value: 1
}
dim {
dim_value: 2
}
}
}
}
}
}
opset_import {
version: 13
}
10 changes: 10 additions & 0 deletions test/test_resize_downsample_scales_nearest/decoded/output_0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#Nx.Tensor<
f32[1][1][1][2]
[
[
[
[1.0, 3.0]
]
]
]
>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.