I am trying to use Tensorflow JS with the Cifar 10 dataset. But I am constantly getting the following error.
tfjs:17 Uncaught (in promise) Error: Argument ‘images’ passed to ‘resizeBilinear’ must be a Tensor or TensorLike, but got ‘e’
Here is my code
async function getModel() {
const NUM_OUTPUT_CLASSES = 10;
// Load the MobileNetV2 model from TensorFlow Hub
const mobilenetModel = await tf.loadGraphModel(
"https://tfhub.dev/google/tfjs-model/imagenet/mobilenet_v2_035_224/classification/3/default/1",
{ fromTFHub: true }
);
console.log(mobilenetModel); // Check the loaded model
console.log('Inputs:', mobilenetModel.inputs); // Log the model inputs
console.log('Outputs:', mobilenetModel.outputs); // Log the model outputs
// Assuming inputs and outputs are valid, proceed with the model construction
if (!mobilenetModel.inputs || mobilenetModel.inputs.length === 0) {
throw new Error('Model inputs are undefined or empty.');
}
const input = mobilenetModel.inputs[0]; // Get the first input tensor
const mobilenetOutput = mobilenetModel.outputs[0]; // Get the output tensor from MobileNetV2
// Flatten the output from MobileNetV2
const flatten = tf.layers.flatten().apply(mobilenetOutput);
// Add a dense layer for further processing
const dense1 = tf.layers.dense({
units: 128,
activation: 'relu',
kernelInitializer: 'varianceScaling'
}).apply(flatten);
// Add the final dense layer with softmax activation for classification
const output = tf.layers.dense({
units: NUM_OUTPUT_CLASSES,
activation: 'softmax'
}).apply(dense1);
// Create the final model
const finalModel = tf.model({
inputs: input,
outputs: output
});
// Compile the model
finalModel.compile({
optimizer: tf.train.adam(),
loss: 'categoricalCrossentropy',
metrics: ['accuracy'],
});
return finalModel;
}