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

[OSX] [Resnet18] Parameter replacement usage #399

Closed
QianqianF opened this issue Jul 3, 2023 · 7 comments
Closed

[OSX] [Resnet18] Parameter replacement usage #399

QianqianF opened this issue Jul 3, 2023 · 7 comments
Labels
Environment Environment third party Third-party tool issues

Comments

@QianqianF
Copy link

Issue Type

Documentation Feature Request, Others

onnx2tf version number

1.13.12

onnx version number

1.13.1

onnxruntime version number

1.15.0

onnxsim (onnx_simplifier) version number

0.4.17

tensorflow version number

2.13.0rc0

Download URL for ONNX

https://github.com/PINTO0309/onnx2tf/releases/download/0.0.2/resnet18-v1-7.onnx

Parameter Replacement JSON

{
    "format_version": 1,
    "operations": [
      {
        "op_name": "resnetv15_pool1_fwd/Mean",
        "param_target": "attributes", 
        "param_name": "axes",
        "values": [1, 2] # also tried [0, 1]
      }
    ]
}

Description

  1. Purpose: Research development
  2. What:
    I tried runningonnx2tf -i resnet18-v1-7.onnx -cotof or onnx2tf -i resnet18-v1-7.onnx -ois data:1,3,224,224 or onnx2tf -i resnet18-v1-7.onnx -ois data:1,3,224,224 -prf replace.json. Attached is the original onnx graph for resnet18 and the converted tflite-float32 graph respectively
image image

It looks like the problem is caused by the conversion from GlobalAveragePool to ReduceMean being applied to the wrong axis.

The output of running onnx2tf -i resnet18-v1-7.onnx -cotof is attached here

  1. How: So I tried resolving it with using replace.json. However, it's not very successful and I suspect that it's due to me supplying the wrong op_name. I have tried Mean__34 and resnetv15_pool1_fwd/Mean. This might be a really stupid question: where should I look to find the correct op_name especially in this case the onnx operation is converted to a few different tf operations?

  2. Why: I would really like to figure out how to use the package properly so I can convert my other pytorch models to tflite for deploying on edge devices.

  3. Resources: the example parameter replacement json in documentation

@QianqianF QianqianF changed the title [Resnet18] Confusions regarding parameter replacement [Resnet18] Parameter replacement usage Jul 3, 2023
@PINTO0309
Copy link
Owner

PINTO0309 commented Jul 3, 2023

It was not clear to me why parameter substitution was necessary. It just fixes the batch size.

onnx2tf -i resnet18-v1-7.onnx -cotof -b 1
INFO: onnx_output_name: resnetv15_stage2_relu1_fwd tf_output_name: tf.nn.relu_7/Relu:0 shape: (1, 128, 28, 28) dtype: float32 validate_result:  Matches 
INFO: onnx_output_name: resnetv15_stage2_conv4_fwd tf_output_name: tf.math.add_15/Add:0 shape: (1, 128, 28, 28) dtype: float32 validate_result:  Matches 
INFO: onnx_output_name: resnetv15_stage2__plus1 tf_output_name: tf.math.add_17/Add:0 shape: (1, 128, 28, 28) dtype: float32 validate_result:  Matches 
INFO: onnx_output_name: resnetv15_stage2_activation1 tf_output_name: tf.nn.relu_8/Relu:0 shape: (1, 128, 28, 28) dtype: float32 validate_result:  Matches 
INFO: onnx_output_name: resnetv15_stage3_conv2_fwd tf_output_name: tf.math.add_18/Add:0 shape: (1, 256, 14, 14) dtype: float32 validate_result:  Matches 
INFO: onnx_output_name: resnetv15_stage3_conv0_fwd tf_output_name: tf.math.add_19/Add:0 shape: (1, 256, 14, 14) dtype: float32 validate_result:  Matches 
INFO: onnx_output_name: resnetv15_stage3_relu0_fwd tf_output_name: tf.nn.relu_9/Relu:0 shape: (1, 256, 14, 14) dtype: float32 validate_result:  Matches 
INFO: onnx_output_name: resnetv15_stage3_conv1_fwd tf_output_name: tf.math.add_20/Add:0 shape: (1, 256, 14, 14) dtype: float32 validate_result:  Matches 
INFO: onnx_output_name: resnetv15_stage3__plus0 tf_output_name: tf.math.add_22/Add:0 shape: (1, 256, 14, 14) dtype: float32 validate_result:  Matches 
INFO: onnx_output_name: resnetv15_stage3_activation0 tf_output_name: tf.nn.relu_10/Relu:0 shape: (1, 256, 14, 14) dtype: float32 validate_result:  Matches 
INFO: onnx_output_name: resnetv15_stage3_conv3_fwd tf_output_name: tf.math.add_23/Add:0 shape: (1, 256, 14, 14) dtype: float32 validate_result:  Matches 
INFO: onnx_output_name: resnetv15_stage3_relu1_fwd tf_output_name: tf.nn.relu_11/Relu:0 shape: (1, 256, 14, 14) dtype: float32 validate_result:  Matches 
INFO: onnx_output_name: resnetv15_stage3_conv4_fwd tf_output_name: tf.math.add_24/Add:0 shape: (1, 256, 14, 14) dtype: float32 validate_result:  Matches 
INFO: onnx_output_name: resnetv15_stage3__plus1 tf_output_name: tf.math.add_26/Add:0 shape: (1, 256, 14, 14) dtype: float32 validate_result:  Matches 
INFO: onnx_output_name: resnetv15_stage3_activation1 tf_output_name: tf.nn.relu_12/Relu:0 shape: (1, 256, 14, 14) dtype: float32 validate_result:  Matches 
INFO: onnx_output_name: resnetv15_stage4_conv2_fwd tf_output_name: tf.math.add_27/Add:0 shape: (1, 512, 7, 7) dtype: float32 validate_result:  Matches 
INFO: onnx_output_name: resnetv15_stage4_conv0_fwd tf_output_name: tf.math.add_28/Add:0 shape: (1, 512, 7, 7) dtype: float32 validate_result:  Matches 
INFO: onnx_output_name: resnetv15_stage4_relu0_fwd tf_output_name: tf.nn.relu_13/Relu:0 shape: (1, 512, 7, 7) dtype: float32 validate_result:  Matches 
INFO: onnx_output_name: resnetv15_stage4_conv1_fwd tf_output_name: tf.math.add_29/Add:0 shape: (1, 512, 7, 7) dtype: float32 validate_result:  Matches 
INFO: onnx_output_name: resnetv15_stage4__plus0 tf_output_name: tf.math.add_31/Add:0 shape: (1, 512, 7, 7) dtype: float32 validate_result:  Matches 
INFO: onnx_output_name: resnetv15_stage4_activation0 tf_output_name: tf.nn.relu_14/Relu:0 shape: (1, 512, 7, 7) dtype: float32 validate_result:  Matches 
INFO: onnx_output_name: resnetv15_stage4_conv3_fwd tf_output_name: tf.math.add_32/Add:0 shape: (1, 512, 7, 7) dtype: float32 validate_result:  Matches 
INFO: onnx_output_name: resnetv15_stage4_relu1_fwd tf_output_name: tf.nn.relu_15/Relu:0 shape: (1, 512, 7, 7) dtype: float32 validate_result:  Matches 
INFO: onnx_output_name: resnetv15_stage4_conv4_fwd tf_output_name: tf.math.add_33/Add:0 shape: (1, 512, 7, 7) dtype: float32 validate_result:  Matches 
INFO: onnx_output_name: resnetv15_stage4__plus1 tf_output_name: tf.math.add_35/Add:0 shape: (1, 512, 7, 7) dtype: float32 validate_result:  Matches 
INFO: onnx_output_name: resnetv15_stage4_activation1 tf_output_name: tf.nn.relu_16/Relu:0 shape: (1, 512, 7, 7) dtype: float32 validate_result:  Matches 
INFO: onnx_output_name: resnetv15_pool1_fwd tf_output_name: tf.math.reduce_mean/Mean:0 shape: (1, 512, 1, 1) dtype: float32 validate_result:  Matches 
INFO: onnx_output_name: flatten_170 tf_output_name: tf.reshape/Reshape:0 shape: (1, 512) dtype: float32 validate_result:  Matches 
INFO: onnx_output_name: resnetv15_dense0_fwd tf_output_name: Placeholder:0 shape: (1, 1000) dtype: float32 validate_result:  Matches 

image

@QianqianF
Copy link
Author

QianqianF commented Jul 3, 2023

Thanks for the fast reply. Fixing the batch size did not fix it for me somehow.

INFO: onnx_op_type: GlobalAveragePool onnx_op_name: resnetv15_pool1_fwd
INFO:  input_name.1: resnetv15_stage4_activation1 shape: [1, 512, 7, 7] dtype: float32
INFO:  output_name.1: resnetv15_pool1_fwd shape: [1, 512, 1, 1] dtype: float32
INFO: tf_op_type: reduce_mean
INFO:  input.1.input_tensor: name: tf.nn.relu_16/Relu:0 shape: (1, 7, 7, 512) dtype: <dtype: 'float32'>
INFO:  input.2.axis: val: [1, 2]
INFO:  input.3.keepdims: val: True
INFO:  output.1.output: name: tf.math.reduce_mean/Mean:0 shape: (1, 7, 1, 1) dtype: <dtype: 'float32'>

INFO: onnx_op_type: Flatten onnx_op_name: flatten_170
INFO:  input_name.1: resnetv15_pool1_fwd shape: [1, 512, 1, 1] dtype: float32
INFO:  output_name.1: flatten_170 shape: [1, 512] dtype: float32
ERROR: The trace log is below.

...


ValueError: Exception encountered when calling layer "tf.reshape" (type TFOpLambda).

Cannot reshape a tensor with 7 elements to shape [1,512] (512 elements) for '{{node tf.reshape/Reshape}} = Reshape[T=DT_FLOAT, Tshape=DT_INT32](Placeholder, tf.reshape/Reshape/shape)' with input shapes: [1,1,7,1], [2] and with input tensors computed as partial shapes: input[1] = [1,512].

Call arguments received by layer "tf.reshape" (type TFOpLambda):
  • tensor=tf.Tensor(shape=(1, 1, 7, 1), dtype=float32)
  • shape=['1', '512']
  • name='flatten_170'

@PINTO0309
Copy link
Owner

PINTO0309 commented Jul 3, 2023

It is a problem specific to your environment.

pip show onnx onnxruntime onnxruntime-gpu onnxsim tensorflow onnx2tf
$ docker login ghcr.io
Username (xxxx): {Enter}
Password: {Personal Access Token}
Login Succeeded

$ docker run --rm -it \
-v `pwd`:/workdir \
-w /workdir \
ghcr.io/pinto0309/onnx2tf:1.13.12

To begin with, your log shows that the conversion was successful. https://n.ethz.ch/~qifeng/download/log.txt There is an error in the version of the tools you reported at the beginning of this issue. Perhaps there is a problem with onnxruntime.
image

I will not investigate environment specific issues. If you cannot solve the problem on your own, you should use Docker.

@PINTO0309 PINTO0309 added third party Third-party tool issues Environment Environment labels Jul 3, 2023
@QianqianF
Copy link
Author

Thanks a lot! I'll look into it.

@QianqianF
Copy link
Author

Update: problem resolved after moving from osx to linux system (in case anyone is having the same issue).

@PINTO0309
Copy link
Owner

Seems to be a bug in onnxruntime for OSX or TensorFlow.

@PINTO0309 PINTO0309 changed the title [Resnet18] Parameter replacement usage [OSX] [Resnet18] Parameter replacement usage Jul 3, 2023
@QianqianF
Copy link
Author

Ah I figured out what the problem was. I changed ~/.keras/keras.json to be "image_data_format": "channels_first" when trying out a different converter. After changing it back to channels_last, everything is working again on OSX.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Environment Environment third party Third-party tool issues
Projects
None yet
Development

No branches or pull requests

2 participants