From 3628fae2ed488c1ee41b51d1041887dea6890b84 Mon Sep 17 00:00:00 2001 From: EunTaik Lee Date: Mon, 9 Aug 2021 22:46:49 +0900 Subject: [PATCH] add support for half_pixel_centers in resize --- python/tvm/relay/frontend/tflite.py | 3 ++ tests/python/frontend/tflite/test_forward.py | 46 +++++++++++++++++--- 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/python/tvm/relay/frontend/tflite.py b/python/tvm/relay/frontend/tflite.py index 5501185f7985..db6e053628bf 100644 --- a/python/tvm/relay/frontend/tflite.py +++ b/python/tvm/relay/frontend/tflite.py @@ -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( diff --git a/tests/python/frontend/tflite/test_forward.py b/tests/python/frontend/tflite/test_forward.py index 5b5c7fda1de9..ef03906db884 100644 --- a/tests/python/frontend/tflite/test_forward.py +++ b/tests/python/frontend/tflite/test_forward.py @@ -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(): @@ -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" @@ -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]) @@ -1560,6 +1570,7 @@ def test_all_resize(): images_data_float32, size_data, align_corners=False, + half_pixel_centers=False, quantized=False, ) _test_resize( @@ -1567,13 +1578,32 @@ def test_all_resize(): 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 @@ -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, )