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

[TFLite] add support for half_pixel_centers in resize #8689

Merged
merged 1 commit into from
Aug 17, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions python/tvm/relay/frontend/tflite.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,9 +642,12 @@ def _convert_resize(self, method, op):
op_options = op.BuiltinOptions()
resize_options.Init(op_options.Bytes, op_options.Pos)
align_corners = resize_options.AlignCorners()
half_pixel_centers = resize_options.HalfPixelCenters()

# Use layout NHWC
coord_trans = "align_corners" if align_corners else "asymmetric"
coord_trans = "half_pixel" if half_pixel_centers else coord_trans

if bilinear_method and input_tensor.qnn_params:
in_expr = self.dequantize(in_expr, input_tensor)
out = _op.image.resize2d(
Expand Down
46 changes: 40 additions & 6 deletions tests/python/frontend/tflite/test_forward.py
Original file line number Diff line number Diff line change
Expand Up @@ -1516,7 +1516,9 @@ def test_forward_reshape():
# ------


def _test_resize(tf_resize_op, images_data, size_data, align_corners, quantized=False):
def _test_resize(
tf_resize_op, images_data, size_data, align_corners, half_pixel_centers, quantized=False
):
"""One iteration of Resize"""
# Test with tensor and constant
with tf.Graph().as_default():
Expand All @@ -1529,7 +1531,10 @@ def _test_resize(tf_resize_op, images_data, size_data, align_corners, quantized=
)
input_range = {"in": (-3, 2)}
out_tensor = tf_resize_op(
images=images_tensor_q, size=size, align_corners=align_corners
images=images_tensor_q,
size=size,
align_corners=align_corners,
half_pixel_centers=half_pixel_centers,
)
out_tensor = tf.quantization.fake_quant_with_min_max_args(
out_tensor, min=-3, max=2, name="out_tensor"
Expand All @@ -1544,7 +1549,12 @@ def _test_resize(tf_resize_op, images_data, size_data, align_corners, quantized=
input_range=input_range,
)
else:
out_tensor = tf_resize_op(images=images_tensor, size=size, align_corners=align_corners)
out_tensor = tf_resize_op(
images=images_tensor,
size=size,
align_corners=align_corners,
half_pixel_centers=half_pixel_centers,
)
compare_tflite_with_tvm([images_data], ["in:0"], [images_tensor], [out_tensor])


Expand All @@ -1560,20 +1570,40 @@ def test_all_resize():
images_data_float32,
size_data,
align_corners=False,
half_pixel_centers=False,
quantized=False,
)
_test_resize(
tf.image.resize_bilinear,
images_data_float32,
size_data,
align_corners=True,
half_pixel_centers=False,
quantized=False,
)
_test_resize(
tf.image.resize_bilinear, images_data_uint8, size_data, align_corners=False, quantized=True
tf.image.resize_bilinear,
images_data_uint8,
size_data,
align_corners=False,
half_pixel_centers=False,
quantized=True,
)
_test_resize(
tf.image.resize_bilinear, images_data_uint8, size_data, align_corners=True, quantized=True
tf.image.resize_bilinear,
images_data_uint8,
size_data,
align_corners=True,
half_pixel_centers=False,
quantized=True,
)
_test_resize(
tf.image.resize_bilinear,
images_data_uint8,
size_data,
align_corners=False,
half_pixel_centers=True,
quantized=True,
)
### RESIZE_NEAREST_NEIGHBOR (was added in v1.13)
# According to topi resize.h
Expand All @@ -1582,7 +1612,11 @@ def test_all_resize():

if "RESIZE_NEAREST_NEIGHBOR" in dir(BuiltinOperator()):
_test_resize(
tf.image.resize_nearest_neighbor, images_data_float32, size_data, align_corners=False
tf.image.resize_nearest_neighbor,
images_data_float32,
size_data,
align_corners=False,
half_pixel_centers=False,
)


Expand Down