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

Deserialization error with Lambda layers when loading saved model in new environment #146

Open
bourcierj opened this issue Jun 28, 2022 · 0 comments

Comments

@bourcierj
Copy link

bourcierj commented Jun 28, 2022

When one saves a converted Keras model with Lambda layers to disk and loads it in a completely separate environment, one will get errors like TypeError: Exception encountered when calling layer "LAYER_173_CHW" (type Lambda). This is because Lambda layers are fundamentally non-portable across environments (cf. Keras docs).

The root cause is given in warnings right before the exception is raised:

[...]UserWarning: onnx2keras.pooling_layers is not loaded, but a Lambda layer uses it. It may cause errors.
To solve this: replace all `Lambda` layers with native or custom layers.
[...]UserWarning: onnx2keras.reshape_layers is not loaded, but a Lambda layer uses it. It may cause errors.

To solve this, we need to replace all Lambda layers with native or custom layers (or at least in this example where the model is ResNet18, those related to pooling and reshape layers).
However Lambda layers are everywhere in this library, so this could require a great amount of work to cover all cases.

Steps to reproduce:

  1. convert ResNet18 from PyTorch to Keras (similarly to what's done in test.models.test_resnet18.test_resnet18)
  2. save the keras model to a SavedModel format with tf.keras.Model.save(..., save_format="tf")
  3. load the saved model with tf.keras.models.load_model in a completely separate environment
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

No branches or pull requests

1 participant