After compiling and training my custom model, I saved it and got two files such as .bin and .json. Further, I loaded that custom model on another page where I'm giving images as input which I used for training of that model and getting the prediction for those images based on the loaded custom model.
Since it works fine for some of the images but returning the wrong prediction for other images.
This is my code:
$("#predict-button").click(async function(){
let image= $('#selected-image').get(0);
let image1 = $('#selected-image1').get(0);
console.log('image:::',image);
console.log('image1:::',image1);
let tensorarr = [];
let tensor1 = preprocessImage(image,$("#model-selector").val());
tensorarr.push(tensor1);
let tensor2 = preprocessImage(image1,$("#model-selector").val());
tensorarr.push(tensor2);
let resize_image = [];
let resize;
for(var i=0; i<tensorarr.length; i++)
{
resize = tf.reshape(tensorarr[i], [1, 224, 224, 3],'resize');
console.log('resize:::',resize);
resize_image.push(resize);
}
// Labels
const label = ['Shelf','Rack'];
const setLabel = Array.from(new Set(label));
let ysarr =[];
const ys = tf.oneHot(tf.tensor1d(label.map((a) => setLabel.findIndex(e => e === a)), 'int32'), 10)
console.log('ys:::'+ys);
const y = tf.reshape(ys, [-1]);
y.print();
const d = y.slice([0], [10]);
d.print();
ysarr.push(d);
const e = y.slice([10], [10]);
e.print();
ysarr.push(e);
console.log('ysarr',ysarr);
model.add(tf.layers.conv2d({
inputShape: [224, 224 , 3],
kernelSize: 5,
filters: 8,
strides: 1,
activation: 'relu',
kernelInitializer: 'VarianceScaling'
}));
model.add(tf.layers.maxPooling2d({poolSize: 2, strides: 2}));
model.add(tf.layers.maxPooling2d({poolSize: 2, strides: 2}));
model.add(tf.layers.flatten({}));
model.add(tf.layers.dense({units: 64, activation: 'relu'}));
model.add(tf.layers.dense({units: 10, activation: 'softmax'}));
model.compile({
loss: 'meanSquaredError',
optimizer : 'sgd'
})
console.log('model:::'+model);
// Train the model using the data.
let tesnor_dim =[];
let tensr;
for(var j=0; j<2; j++){
console.log('resize_image',resize_image);
tensr = tf.expandDims(ysarr[j], 0);
tesnor_dim.push(tensr);
console.log('tesnor_dim',tesnor_dim);
console.log('before resize_image[j]',resize_image[j]);
console.log('before tesnor_dim[j]',tesnor_dim[j]);
await model.fit(resize_image[j], tesnor_dim[j], {epochs: 100}).then((loss) => {
console.log('resize_image.get[j]',resize_image[j]);
console.log('tesnor_dim[j]',tesnor_dim[j]);
console.log('loss',loss);
const t = model.predict(resize_image[j]);
console.log('Prediction:::'+t);
pred = t.argMax(1).dataSync(); // get the class of highest probability
const labelsPred = Array.from(pred).map(e => setLabel[e]);
console.log('labelsPred:::'+labelsPred);
}).catch((e) => {
console.log(e.message);
})
}
const saveResults = model.save('downloads://my-model-1');
console.log(saveResults);
});
The model is giving wrong prediction. What to do ?
check the accuracy of the model. A very low accuracy of the model will indicate that the model is either not the right one for the problem solved or that some parameters needs to be changed.
even if the accuracy is good the model can be wrong in predicting a particular class. In that case, the confusion matrix will be of a great help identify the classes incorrectly predicted. When those classes are identified, one can use more training data for those classes to improve their accuracy after the training
Looking at the model of the question it is clearly obvious that it is a classification model ie given an image, the model will predict the class the image belongs to.
'meanSquaredError'
loss is not the best loss function for classification problem.categoricalCrossEntropy
will achieve best accuracy. Even after changing the loss function, the accuracy might still not be what is expected. Then one needs to add more layers, change other parameters of the model. Then one will train and compare the accuracy, and the cycle goes on...