I am trying to use a trained model with learning transfer using the @tensorflow/tfjs-node
library and I run into a problem not being able to use the exported model as it asks me for the layer of the pre-trained model but it is not possible to properly load the model here I leave the code.
modelo = keras.Sequential([
hub.KerasLayer(url, input_shape=(224,224,3), trainable=False),
keras.layers.Dense(100, activation="relu"),
keras.layers.Dense(2, activation="softmax"),
])
...
modelo.save(('./modelos/'+modelName+'/keras/modelo.h5'))
score = modelo.evaluate(data_test)
tfjs.converters.save_keras_model(modelo, ('./modelos/'+modelName+'/tfjs'))```
y tengo este error
[ERROR] 00:48:28 Error: Unknown layer: KerasLayer. This may be due to one of the following reasons:
1. The layer is defined in Python, in which case it needs to be ported to TensorFlow.js or your JavaScript code.
2. The custom layer is defined in JavaScript, but is not registered properly with tf.serialization.registerClass().
que debo de hacer para que funcione, lo estoy haciendo asÃ.
`model = await tf.loadLayersModel(`file://./src/MLModels/tfjs/model.json`);`
y con
`model = await tf.loadGraphModel(`file://./src/MLModels/tfjs/model.json`);`
obtengo el error
[ERROR] 01:03:17 TypeError: Cannot read properties of undefined (reading 'producer')
I can't find how to fix this on the internet.
this is the code
import { LoadOptions } from '@tensorflow/tfjs-core/dist/io/types';
import * as tf from '@tensorflow/tfjs-node';
import { LayersModel,Shape, loadGraphModel, Tensor, Tensor4D } from '@tensorflow/tfjs-node';
class KerasLayer extends tf.layers.Layer {
constructor(config?: any) {
super(config);
}
build(inputShape: Shape | Shape[]): void {
}
call(inputs: Tensor | Tensor[], kwargs?: any): Tensor | Tensor[] | Tensor4D {
return super.call(inputs, kwargs) as Tensor4D;
}
static get className(): string {
return 'KerasLayer';
}
}
tf.serialization.registerClass(KerasLayer);
let model: any | null = null;
export const loadModel = async (): Promise<LayersModel > => {
if (!model) {
try {
const customLayer = new KerasLayer({});
const modelUrl = 'https://www.kaggle.com/models/google/mobilenet-v2/frameworks/TensorFlow2/variations/tf2-preview-feature-vector/versions/4';
model = await loadGraphModel(modelUrl, { customObjects: { 'KerasLayer': customLayer } } as LoadOptions) ;
console.log("Modelo cargado exitosamente");
model = model as LayersModel;
return model;
} catch (error: any) {
console.error("Error al cargar el modelo:", error.message);
return model;
}
} else {
return model;
}
};
loadModel();
export default {
loadModel,
};
Thanks for the help, in this issue you will find a more specific explanation of my problem.
Server running on port 3000
Error: Unknown layer: KerasLayer. This may be due to one of the following reasons:
1. The layer is defined in Python, in which case it needs to be ported to TensorFlow.js or your JavaScript code.
2. The custom layer is defined in JavaScript, but is not registered properly with tf.serialization.registerClass().
at new ValueError (/git/backendTDG/tfjs-layers/src/errors.ts:48:5)
at deserializeKerasObject (/git/backendTDG/tfjs-layers/src/utils/generic_utils.ts:243:13)
at deserialize (/git/backendTDG/tfjs-layers/src/layers/serialization.ts:31:10)
at Sequential.fromConfig (/git/backendTDG/tfjs-layers/src/models.ts:1037:21)
at deserializeKerasObject (/git/backendTDG/tfjs-layers/src/utils/generic_utils.ts:278:11)
at deserialize (/git/backendTDG/tfjs-layers/src/layers/serialization.ts:31:10)
at /git/backendTDG/tfjs-layers/src/models.ts:308:7
at step (/git/backendTDG/node_modules/tslib/tslib.es6.js:102:23)
at Object.next (/git/backendTDG/node_modules/tslib/tslib.es6.js:83:53)
at fulfilled (/git/backendTDG/node_modules/tslib/tslib.es6.js:73:58)
[ERROR] 01:04:48 Error: Unknown layer: KerasLayer. This may be due to one of the following reasons:
1. The layer is defined in Python, in which case it needs to be ported to TensorFlow.js or your JavaScript code.
2. The custom layer is defined in JavaScript, but is not registered properly with tf.serialization.registerClass().
Waiting for the debugger to disconnect...