tensorflow-onnx icon indicating copy to clipboard operation
tensorflow-onnx copied to clipboard

Update resize node default value of nearest_mode attribute

Open thoomi opened this issue 1 year ago • 5 comments

Describe the bug

Converting tf.keras.backend.resize_images with interpolation mode nearest to ONNX and then to TensorRT produces mismatching results. The created Resize op uses rounding method floor as default. Although round_prefer_floor is supported in TensorRT by now and is the actual default as stated in the onnx docs. :)

See:

  • onnx op default: https://github.com/onnx/onnx/blob/main/docs/Operators.md#resize

nearest_mode : string (default is round_prefer_floor)

  • onnx_tensorrt ops: https://github.com/onnx/onnx-tensorrt/blob/main/docs/operators.md

Supported nearest modes: floor, ceil, round_prefer_floor, round_prefer_ceil

  • onnx_tensorrt default value for nearest_mode: https://github.com/onnx/onnx-tensorrt/commit/8fea430fc1ad452085add61b04045a9efbf2b981#diff-b23eabdc4ee3ebabdbb607535b2ed80c471093b0fef41a5b53bce876ca9ec96dL3013

Is anything preventing us from using round_prefer_floor as the default here? https://github.com/onnx/tensorflow-onnx/blob/26bdcff67b1d408d2df816a8ba96fe24b99ff508/tf2onnx/onnx_opset/nn.py#L1427

System information

  • OS Platform and Distribution: Linux Ubuntu 20.04
  • TensorFlow Version: 2.9
  • Python version: 3.8
  • ONNX version: 1.12.0
  • ONNX opset: 13
  • TensorRT version: 8.5

To Reproduce

Screenshots

Additional context

thoomi avatar May 03 '23 13:05 thoomi

+1

sbudenkov avatar Jul 11 '23 07:07 sbudenkov

In tf2onnx, the default value you mentioned doesn't mean the default value of onnx resize op. The default value in tf2onnx is designed to match what tf op 'ResizeNearestNeighbor' means. The function tf.keras.backend.resize_images will be transformed to this 'ResizeNearestNeighbor' op during loading the tensorflow graph. Setting nearest_mode to 'floor' could represent the meaning of this op. I will add a test for tf.keras.backend.resize_images to check this in CI.

Regarding of the error you met after conversion, it'd better to share more details so we can discuss further.

fatcat-z avatar Jul 27 '23 15:07 fatcat-z

I have a related problem: i use tf.keras.layers.UpSampling2D with 'nearest', convert to ONNX. When i try to convert to TensorRT i get the error: Assertion failed: (transformationMode != "tf_half_pixel_for_nn" || nearest_mode == "round_prefer_floor") && "This version of TensorRT only support round_prefer_floor nearest mode in tf_half_pixel_for_nn!" Tried Opset=13 and 18

(onnx = 1.14.1, tf2onnx=1.16.1, tf=2.15, tensorrt=8.6.1.6)

This bug is also described in the issue here: https://github.com/onnx/onnx-tensorrt/issues/913 There, @george-synx describes a solution that worked for me: i just commented out lines 1436-1439 of tf2onnx/onnx_opset/nn.py

The issue seems also related to https://github.com/onnx/onnx/issues/3924

dkroeni avatar Apr 19 '24 08:04 dkroeni

I'm just facing the same issue.

kadu-v avatar May 22 '24 09:05 kadu-v

It seems commenting out is not the solution here, i solved the problem for me by doing:

        if "half_pixel_centers" in node.attr and node.attr["half_pixel_centers"].i:
            if node.type == "ResizeNearestNeighbor" and not ctx.is_target(constants.TARGET_TENSORRT):
                transformation_mode = "half_pixel"
                nearest_mode = "round_prefer_floor"
            else:
                transformation_mode = "half_pixel"

This is essentially the code from v1.13 that i knew still worked for me. It is not clear to me if this is a tf2onnx bug or a onnx-tensorrt bug. There is a special treatment for tensorflow next neighbor ("tf_half_pixel_for_nn") in onnx-tensorrt that seems to do the wrong thing if used with the current tf2onnx version.

dkroeni avatar Jun 03 '24 11:06 dkroeni