Skip to content

Commit 5cc8da7

Browse files
committed
Merge branch 'feature/resize_4d_conversion' of https://github.com/klassen9/qonnx into klassen9-feature/resize_4d_conversion
2 parents 6d27a18 + 7b00753 commit 5cc8da7

File tree

2 files changed

+98
-1
lines changed

2 files changed

+98
-1
lines changed

src/qonnx/transformation/change_3d_tensors_to_4d.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def _find_invalid_nodes(model):
6464
"Reshape",
6565
"MaxPool",
6666
"Upsample",
67+
"Resize",
6768
]
6869
invalid_nodes = []
6970
for n in model.graph.node:
@@ -194,6 +195,41 @@ def apply(self, model):
194195
assert list(scales.shape) == [3]
195196
scales = np.append(scales, np.asarray(1.0, dtype=np.float32))
196197
model.set_initializer(n.input[1], scales)
198+
elif node_op_type == "Resize":
199+
assert ("axes" not in [x.name for x in n.attribute]), (
200+
"%s: Axes attribute is not supported." % n.name
201+
)
202+
assert (not (len(n.input) in (3, 4) and model.get_initializer(n.input[1]) is not None)), (
203+
"%s: ROI input is not supported." % n.name
204+
)
205+
if len(n.input) == 2:
206+
# Resize version 10
207+
scales = model.get_initializer(n.input[1])
208+
scales = np.append(scales, np.asarray(1.0, dtype=np.float32))
209+
model.set_initializer(n.input[1], scales)
210+
elif len(n.input) == 3:
211+
# Resize version 11 and up (no size input)
212+
scales = model.get_initializer(n.input[2])
213+
scales = np.append(scales, np.asarray(1.0, dtype=np.float32))
214+
model.set_initializer(n.input[2], scales)
215+
elif len(n.input) == 4:
216+
scales_exists = (model.get_initializer(n.input[2]) is not None) and (len(model.get_initializer(n.input[2])) != 0)
217+
sizes_exists = (model.get_initializer(n.input[3]) is not None) and (len(model.get_initializer(n.input[3])) != 0)
218+
assert (scales_exists ^ sizes_exists), (
219+
"%s: Either scales or the target output size must "
220+
"be specified. Specifying both is prohibited." % n.name
221+
)
222+
if (scales_exists):
223+
# Scales parameter is a 1d list of upsampling factors along each axis
224+
scales = model.get_initializer(n.input[2])
225+
scales = np.append(scales, np.asarray(1.0, dtype=np.float32))
226+
model.set_initializer(n.input[2], scales)
227+
else:
228+
# Size parameter is a 1d list of the target size along each axis
229+
sizes = model.get_initializer(n.input[3])
230+
sizes = np.append(sizes, np.asarray(1.0, dtype=np.int64))
231+
model.set_initializer(n.input[3], sizes)
232+
input_shape.append(1)
197233

198234
# Change format of each input/value_info/output tensor
199235
for k, v in all_tensors.items():

tests/transformation/test_4d_conversion.py

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,67 @@ def create_conv_upsample():
327327
model.set_initializer(tensor_name, gen_finn_dt_tensor(DataType["FLOAT32"], init_shape))
328328
return model
329329

330+
def create_resize():
331+
"""
332+
Creates an model for testing the 3D to 4D transform of the resize node.
333+
"""
334+
resize_node1 = onnx.helper.make_node(
335+
"Resize",
336+
inputs=["in_resize1", "roi_resize1", "scales_resize1", "sizes_resize1"],
337+
outputs=["out_resize1"],
338+
name="Resize1",
339+
mode="nearest",
340+
)
341+
342+
resize_node2 = onnx.helper.make_node(
343+
"Resize",
344+
inputs=["out_resize1", "roi_resize2", "scales_resize2"],
345+
outputs=["out_resize2"],
346+
name="Resize2",
347+
mode="nearest",
348+
)
349+
350+
in_resize1 = onnx.helper.make_tensor_value_info("in_resize1", onnx.TensorProto.FLOAT, [1, 32, 4])
351+
out_resize1 = onnx.helper.make_tensor_value_info("out_resize1", onnx.TensorProto.FLOAT, [1, 32, 8])
352+
out_resize2 = onnx.helper.make_tensor_value_info("out_resize2", onnx.TensorProto.FLOAT, [1, 32, 16])
353+
354+
roi_resize1 = onnx.helper.make_tensor_value_info("roi_resize1", onnx.TensorProto.FLOAT, [4])
355+
scales_resize1 = onnx.helper.make_tensor_value_info("scales_resize1", onnx.TensorProto.FLOAT, [])
356+
sizes_resize1 = onnx.helper.make_tensor_value_info("sizes_resize1", onnx.TensorProto.INT64, [3])
357+
358+
roi_resize2 = onnx.helper.make_tensor_value_info("roi_resize2", onnx.TensorProto.FLOAT, [4])
359+
scales_resize2 = onnx.helper.make_tensor_value_info("scales_resize2", onnx.TensorProto.FLOAT, [3])
360+
361+
list_of_nodes = [
362+
resize_node1,
363+
resize_node2,
364+
]
365+
list_of_value_infos = [
366+
out_resize1,
367+
roi_resize1,
368+
sizes_resize1,
369+
scales_resize1,
370+
roi_resize2,
371+
scales_resize2,
372+
]
373+
374+
graph = onnx.helper.make_graph(
375+
nodes=list_of_nodes,
376+
name="4d_conversion_resize_test_graph",
377+
inputs=[in_resize1],
378+
outputs=[out_resize2],
379+
value_info=list_of_value_infos,
380+
)
381+
382+
onnx_model = qonnx_make_model(graph, producer_name="4d_conversion_resize_test-model")
383+
model = ModelWrapper(onnx_model)
384+
model = model.transform(InferShapes())
385+
model.set_initializer("sizes_resize1", np.array([1, 32, 8], dtype=np.int64))
386+
model.set_initializer("scales_resize1", np.array([], dtype=np.float32))
387+
model.set_initializer("scales_resize2", np.array([1., 1., 2.], dtype=np.float32))
388+
return model
330389

331-
@pytest.mark.parametrize("test_model", ["Quartz", "VGG", "ConvUpsample"])
390+
@pytest.mark.parametrize("test_model", ["Quartz", "VGG", "ConvUpsample", "Resize"])
332391
def test_4d_conversion(test_model):
333392
"""
334393
Test for the 3D to 4D transformation with a valid graph.
@@ -340,6 +399,8 @@ def test_4d_conversion(test_model):
340399
model = create_arbitrary_model_vgg()
341400
elif test_model == "ConvUpsample":
342401
model = create_conv_upsample()
402+
elif test_model == "Resize":
403+
model = create_resize()
343404
else:
344405
raise Exception("Unknown test_model in test_4d_conversion")
345406

0 commit comments

Comments
 (0)