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

customObject from loadLayersModel() is passed as undefined to loadLayersModelFromIOHandler() #8179

Open
Gruhit13 opened this issue Feb 20, 2024 · 2 comments
Assignees
Labels
comp:core type:bug Something isn't working

Comments

@Gruhit13
Copy link

Gruhit13 commented Feb 20, 2024

I have created a TensorFlow model consisting of a custom layer saved in tensorflowjs format. Now when I try loading the model using tf.loadLayersModel() it does not load stating that the custom layer is not serialized but is serialized using the tf.serialization.registerClass() method.

To overcome this I passed a custom object to the loadLayersModel() method but in git, I found out that the customObject is by default passed as undefined irrespective of being passed to loadLayersModelFromIOHandler() method which does further handling of model loading or not which seems to be a bug as it would never allow loading a custom object.

It would be better if it gets resolved allowing the user the flexibility to pass custom objects or if there is an alternative to this then mention it along with the right tutorial or steps for using the alternative solution.

Error Details:
loadLayersModel() is at line 248 in https://github.com/tensorflow/tfjs/blob/tfjs-v4.17.0/tfjs-layers/src/models.ts#L248-L270
loadLayersModelFromIOHandler() is at line 282 https://github.com/tensorflow/tfjs/blob/tfjs-v4.17.0/tfjs-layers/src/models.ts#L248-L270

@Gruhit13 Gruhit13 added the type:bug Something isn't working label Feb 20, 2024
@gaikwadrahul8 gaikwadrahul8 self-assigned this Feb 21, 2024
@gaikwadrahul8
Copy link
Contributor

Hi, @Gruhit13

Thank you for bringing this issue to our attention and to confirm, did you try to re-register the custom layer class before attempting to load the model using tf.loadLayersModel(), please give it try explicitly re-registering your custom layer class using tf.serialization.registerClass(). This might ensure the layer is recognized during deserialization so you'll have to do something like below :

import * as tf from '@tensorflow/tfjs';

// Assuming your custom layer class is named 'MyCustomLayer'
tf.serialization.registerClass(MyCustomLayer);

const model = await tf.loadLayersModel('your_model.json');

Could you please help us with your Github repo with complete steps or code snippet along with your model file to replicate the same behavior from our end to investigate this issue further ?

Thank you for your cooperation and patience.

@Gruhit13
Copy link
Author

Gruhit13 commented Feb 21, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp:core type:bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants