tfjs
tfjs copied to clipboard
customObject from loadLayersModel() is passed as undefined to loadLayersModelFromIOHandler()
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
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.
Thank you for your reply and code suggestion. I was able to load the custom layer. However, I am stuck with a different error, would you mind assisting me with that?
Your any help would be greatly appreciated.
I will explain my query, you can assist me at your convenience.
I have created a Transformer for text classification. In that I am using MultiHeadAttention, which I had to created manually in node.js project as it is not provided from tensorflow API. My model summary looks like this in tensorflow.
[image: image.png]
I have following classes defined in my node project and registered using tf.serialization.registerClass()
- ModelEmbedding
- MultiHeadAttention(used inside transformer)
- Transformer
Now when I try loading my loading using tf.loadLayersModel() I get issue where it states that Input 0 is incompatible with layer global_average_pooling1d: expected ndim=3, found ndim=2
Hence I am not able to identify why this issue occur? I am appending link to my project and a link to model.json feel free to respond.
- model.json https://raw.githubusercontent.com/Gruhit13/Paraphrase-Classifier/main/tfjsmodel/model.json*
Thank you, Gruhit Patel
On Wed, 21 Feb 2024 at 09:45, gaikwadrahul8 @.***> wrote:
Hi, @Gruhit13 https://github.com/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 @.***/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.
— Reply to this email directly, view it on GitHub https://github.com/tensorflow/tfjs/issues/8179#issuecomment-1956810568, or unsubscribe https://github.com/notifications/unsubscribe-auth/APJEH43DOWDLZWUSQR5XAI3YUYCB5AVCNFSM6AAAAABDRTK66CVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSNJWHAYTANJWHA . You are receiving this because you were mentioned.Message ID: @.***>