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

Bring constant layers unconnected to the model into the model #628

Merged
merged 1 commit into from
May 13, 2024

Conversation

PINTO0309
Copy link
Owner

@PINTO0309 PINTO0309 commented May 13, 2024

1. Content and background

  • Constant
    • Bring Constant layers unconnected to the model into the model.

    • It is assumed that the -nuo option is specified because running onnxsim will remove constants from the ONNX file.

    • Wrap constants in a Lambda layer and force them into the model.

    • toy_with_constant.onnx.zip

    • Convert test

      onnx2tf -i toy_with_constant.onnx -nuo -cotof
      ONNX TFLite
      image image

      image

    • Inference test

      import tensorflow as tf
      import numpy as np
      from pprint import pprint
      
      interpreter = tf.lite.Interpreter(model_path="saved_model/toy_with_constant_float32.tflite")
      interpreter.allocate_tensors()
      
      input_details = interpreter.get_input_details()
      output_details = interpreter.get_output_details()
      
      interpreter.set_tensor(
          tensor_index=input_details[0]['index'],
          value=np.ones(tuple(input_details[0]['shape']), dtype=np.float32)
      )
      interpreter.invoke()
      
      variable_output = interpreter.get_tensor(output_details[0]['index'])
      constant_output = interpreter.get_tensor(output_details[1]['index'])
      
      print("=================")
      print("Variable Output:")
      pprint(variable_output)
      print("=================")
      print("Constant Output:")
      pprint(constant_output)
      =================
      Variable Output:
      array([[-0.02787317, -0.05505124,  0.05421712,  0.03526559, -0.14131774,
               0.0019211 ,  0.08399964,  0.00433664, -0.00984338, -0.03370604]],
            dtype=float32)
      =================
      Constant Output:
      array([1., 2., 3., 4., 5.], dtype=float32)
      

2. Summary of corrections

3. Before/After (If there is an operating log that can be used as a reference)

4. Issue number (only if there is a related issue)

@PINTO0309 PINTO0309 merged commit 3807991 into main May 13, 2024
3 checks passed
@PINTO0309 PINTO0309 deleted the out_const branch May 13, 2024 10:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant