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

Update README.md #66

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

johndpope
Copy link

No description provided.

@happyTonakai
Copy link

I think nobuco.pytorch_to_keras already convert the pytorch model with weights to a keras model. I use the following code to convert a pretrained pytorch model to keras and finally tflite, and the results are close.

    # Convert to Keras model
    keras_model = nobuco.pytorch_to_keras(
        torch_model,
        args=[dummy_input],
        kwargs=None,
        trace_shape=True,
        inputs_channel_order=ChannelOrder.PYTORCH,
        outputs_channel_order=ChannelOrder.PYTORCH,
    )

    # Convert to TFLite model
    converter = tf.lite.TFLiteConverter.from_keras_model(keras_model)
    tflite_model = converter.convert()
    interpreter = tf.lite.Interpreter(model_content=tflite_model)

    # Inference and compare the results
    torch_output = torch_model(dummy_input).detach().numpy()

    keras_input = tf.convert_to_tensor(dummy_input.numpy())
    keras_output = keras_model(keras_input)

    interpreter.allocate_tensors()
    input_details = interpreter.get_input_details()
    output_details = interpreter.get_output_details()
    interpreter.set_tensor(input_details[0]["index"], input_tensor)
    interpreter.invoke()
    tflite_output = interpreter.get_tensor(output_details[0]["index"])    

    diff_torch_keras = np.mean(np.abs(torch_output - keras_output))
    diff_torch_tflite = np.mean(np.abs(torch_output - tflite_output))

    print(f"Difference between torch and keras: {diff_torch_keras}")
    print(f"Difference between torch and tflite: {diff_torch_tflite}")

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.

2 participants