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

[Relay] Support resize in the ONNX conversion #8455

Merged
merged 5 commits into from
Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions python/tvm/contrib/target/onnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,86 @@ def convert_attributes(cls, attrs):
return {"to": getattr(TensorProto, attrs.dtype.upper())}


class Resize(OpConverter):
"""Operator converter for Resize."""

@classmethod
def convert_attributes(cls, attrs):
method = attrs.get_str("method")
if method == "nearest_neighbor":
mode = b"nearest"
elif "linear" in method: # linear / bilinear
mode = b"linear"
elif "cubic" in method: # cubic / bicubic
mode = b"cubic"
comaniac marked this conversation as resolved.
Show resolved Hide resolved

coord_trans = attrs.get_str("coordinate_transformation_mode")
if coord_trans == "half_pixel":
coord_trans = b"half_pixel"
elif coord_trans == "align_corners":
coord_trans = b"align_corners"
elif coord_trans == "asymmetric":
coord_trans = b"asymmetric"

rounding_method = attrs.get_str("rounding_method")
if rounding_method == "round":
rounding_method = b"round_prefer_floor"
elif rounding_method == "floor":
rounding_method = b"floor"
elif rounding_method == "ceil":
rounding_method = b"ceil"

size = attrs.get_int_tuple("size")

return {
"mode": mode,
"coord_trans": coord_trans,
"size": size,
"nearest_mode": rounding_method,
}

@classmethod
def convert(cls, node_entry, model_container, node_dict):
attrs = cls.convert_attributes(node_entry["relay_node"].attrs)

name = node_entry["name"]
input_node = node_dict[node_entry["inputs"][0]]
assert len(input_node) == 1, "input node can not be a Tuple"
input_node = input_node[0]
input_shape = input_node["types"][0].shape

# (TBD) needed in opset 11
roi = [0] * len(input_shape) + [1] * len(input_shape)
roi_array = numpy.asarray(roi).astype(numpy.float64)
roi_node = add_input(roi_array, name, "roi", model_container)

out_size = attrs["size"]

# (onnx) rank of scale / size must match rank of X
# relay size node contains only spatial dimensions
# pad with 1s to match rank
match_rank_pad = len(input_shape) - len(out_size)
out_size_full_rank = input_shape[:match_rank_pad] + list(out_size)
out_size_array = numpy.asarray(out_size_full_rank).astype(numpy.int64)

input_size_array = numpy.asarray(list(input_shape)).astype(numpy.int64)

scale_array = numpy.divide(out_size_array, input_size_array).astype(numpy.float32)
scale_node = add_input(scale_array, name, "scales", model_container)

input_names = [node_entry["input_names"][0], roi_node, scale_node]

resize_node = onnx.helper.make_node(
cls.__name__,
input_names,
node_entry["output_names"],
mode=attrs["mode"],
coordinate_transformation_mode=attrs["coord_trans"],
nearest_mode=attrs["nearest_mode"],
)
model_container.add_nodes([resize_node])


relay_to_onnx_op_mapping = {
"reshape": Reshape,
"nn.conv2d": Conv,
Expand Down Expand Up @@ -701,6 +781,7 @@ def convert_attributes(cls, attrs):
"copy": rename("Identity"),
"round": rename("Round"),
"cast": Cast,
"image.resize2d": Resize,
}


Expand Down
21 changes: 21 additions & 0 deletions tests/python/contrib/test_onnx.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,26 @@ def verify_cast(dshape, dtype):
verify_cast(i, o_dtype)


def test_resize():
"""Resize unit test."""

def verify_resize(dshape, outsize, method=None, dtype="float32"):
x = relay.var("x", relay.ty.TensorType(dshape, dtype))
y = relay.image.resize2d(x, outsize, layout="NCHW", method=method)
func = relay.Function([x], y)
x_data = np.random.uniform(size=dshape).astype(dtype)
verify_results(func, [x_data], "test_resize", rtol=1e-4, atol=1e-4)

isize = [(1, 3, 480, 640)]
osize = [(240, 320), (960, 1280)]
method = ["nearest_neighbor", "linear", "cubic"]
comaniac marked this conversation as resolved.
Show resolved Hide resolved

for i in isize:
for j in osize:
for k in method:
verify_resize(i, j, k)


if __name__ == "__main__":
test_add()
test_bias_add()
Expand Down Expand Up @@ -684,3 +704,4 @@ def verify_cast(dshape, dtype):
test_copy()
test_round()
test_cast()
test_resize()