diff options
author | Tim Hall <tim.hall@arm.com> | 2022-07-18 21:26:47 +0100 |
---|---|---|
committer | tim.hall <tim.hall@arm.com> | 2022-07-23 16:56:07 +0000 |
commit | 47c7636586be265eed9e352e6ad4c090a02fb31f (patch) | |
tree | 9ab0472d909b75c8718fd4c476adb94f095946a7 /ethosu/vela/tflite_graph_optimiser.py | |
parent | e178f387c56705e7c46ccf57d3676086fb22f05a (diff) | |
download | ethos-u-vela-47c7636586be265eed9e352e6ad4c090a02fb31f.tar.gz |
MLBEDSW-6616: ResizeBilinear align corners is incorrect
- Fixed align corners support when converting in to upscale and average
pool. The problem was due to the wrong ratio ifm to ofm size, causing an
scaling factor that was not 2x/4x/8x. Works for uint8, int8 and int16.
- Fixed checking of align corners in supported operators check
- Added additional supported operators check for the size tensor
- Updated and added more supported operators unit tests
Signed-off-by: Tim Hall <tim.hall@arm.com>
Change-Id: Idb78fa9e76ede2c37e8ac6cb1c322154bd156898
Diffstat (limited to 'ethosu/vela/tflite_graph_optimiser.py')
-rw-r--r-- | ethosu/vela/tflite_graph_optimiser.py | 25 |
1 files changed, 12 insertions, 13 deletions
diff --git a/ethosu/vela/tflite_graph_optimiser.py b/ethosu/vela/tflite_graph_optimiser.py index b1a56605..d2899c4c 100644 --- a/ethosu/vela/tflite_graph_optimiser.py +++ b/ethosu/vela/tflite_graph_optimiser.py @@ -303,26 +303,22 @@ def convert_resizebilinear_1x1_to_add(op): # Convert ResizeBilinear to a number of 2x2 nearest neighbor upscaling and one avgpool op with kernel size dependent # on the upscaling factor. Avgpool kernel limit of 8x8 when padding is applied limits upscaling to 8x8. -def convert_resizebilinear_to_nearest_neighbor_upscaling_and_pool(op): +def convert_resizebilinear_to_upscale_and_average_pool(op): pre_op = op outputs = op.outputs dtype = op.ifm.dtype op.attrs.update({"strides": (1, 1, 1, 1), "ksize": (1, 1, 1, 1)}) - if op.attrs["align_corners"]: - shape_modifier = 1 - op.attrs["padding"] = Padding.VALID - else: - shape_modifier = 0 - op.attrs["padding"] = Padding.SAME + op.attrs["padding"] = Padding.SAME # doesn't really matter as the kernel is 1x1 op.ifm_resampling_mode = resampling_mode.NEAREST upscaled_shape = np.array(op.ifm_shapes[0].get_hw_as_list()) - out_shape = np.array(op.ofm_shapes[0].get_hw_as_list()) + + # Get upscale factor that was calculated in the supported operators check + upscale_factor = op.attrs["upscale_factor"] # Calculate how many times 2x2 upscaling needs to be performed # Force the result of round to be an integer. This is because the behaviour of rounding numpy.float64 values changed # between different versions of numpy. This consistency ensures that the kernel dimensions are kept integral - upscale_factor = int(round(out_shape[1] / upscaled_shape[1])) n = int(np.log2(upscale_factor)) # Perform 2x2 upscaling n-1 times @@ -333,7 +329,7 @@ def convert_resizebilinear_to_nearest_neighbor_upscaling_and_pool(op): scaled_op.inputs[0] = pre_op.outputs[0] # Nearest neighbor 2x2 upscaling - upscaled_shape = upscaled_shape * 2 - shape_modifier + upscaled_shape = upscaled_shape * 2 shape = op.ofm_shapes[0].as_list() shape[1:3] = upscaled_shape out_tens = Tensor(shape, dtype, f"{op.outputs[0].name}_{count}") @@ -348,8 +344,11 @@ def convert_resizebilinear_to_nearest_neighbor_upscaling_and_pool(op): if n > 1: scaled_op = op.clone(f"_{n-1}") scaled_op.inputs[0] = pre_op.outputs[0] - scaled_op.attrs["padding"] = Padding.EXPLICIT - scaled_op.attrs["explicit_padding"] = [0, 0, upscale_factor - 1, upscale_factor - 1] + if op.attrs["align_corners"]: + scaled_op.attrs["padding"] = Padding.VALID + else: + scaled_op.attrs["padding"] = Padding.EXPLICIT + scaled_op.attrs["explicit_padding"] = [0, 0, upscale_factor - 1, upscale_factor - 1] scaled_op.attrs.update({"ksize": (1, upscale_factor, upscale_factor, 1)}) scaled_op.outputs = outputs scaled_op.outputs[0].ops = [scaled_op] @@ -367,7 +366,7 @@ def fixup_resizebilinear(op, arch, nng): elif op.ifm_shapes[0].height == 1 and op.ifm_shapes[0].width == 1: convert_resizebilinear_1x1_to_add(op) else: - convert_resizebilinear_to_nearest_neighbor_upscaling_and_pool(op) + convert_resizebilinear_to_upscale_and_average_pool(op) return op |