I made my object recognition model by following this example:
https://www.tensorflow.org/tutorials/images/classification
I create this model using Colab, and now I have, from Colab, a model in .py and .ipynb
Using this istruction I save my model in .h5:
model.compile(loss='mean_squared_error', optimizer='adam', metrics=['binary_accuracy'])
model.save('./modelname.h5')
Now, I can try to save this model in another format, I use this code and save it in .json and with group1-shard°of°.bin files:
!pip install tensorflowjs
!mkdir model
!tensorflowjs_converter --input_format keras modelname.h5 model/
!zip -r modelname.zip model
Now my goal is to be able to load this model into my webapp, in Javascript, and use it to recognize images
The problem is being able to load the model
Any solution?
UPDATE
I'm using a screenshot of my page view as an image to recognize
This is my fundamental part of the code:
async function LoadModel() {
Model = await tf.loadLayersModel('http://localhost/..../model.json'); //caricamento mio modello
console.log('conferma caricamento modello ' + Model);
try {
maxPredictions = Model.getTotalClasses();
console.log("durante");
}
catch (e){}
if (Model) {
//controllo caricamento modello
console.log(Model);
}
console.log("dopo e modello " + Model);
}
Then
OriginImage.onload = function (event) {
try {
document.createEvent("TouchEvent");
var width = document.body.clientWidth;
}
catch(e) {
var width = ResizeImageWidth;
}
if (OriginImage.height<OriginImage.width) {
var height = width*OriginImage.height/OriginImage.width;
}
else {
var height = width;
width = height*OriginImage.width/OriginImage.height;
}
ResizeImage.width = width;
ResizeImage.height = height;
ResizeImage.src = OriginImage.src;
}
This is resize
ResizeImage.onload = function (event) {
if (Model) recognizeImage(ResizeImage);
}
And this is recognizeImage
async function recognizeImage(Image) {
var cont;
var data = "";
var maxClassName = "";
var maxProbability = "";
const prediction = await Model.predict(Image);
for (let i = 0; i < maxPredictions; i++) {
if (i==0) {
maxClassName = prediction[i].className;
maxProbability = prediction[i].probability;
}
else if (prediction[i].probability>maxProbability) {
maxClassName = prediction[i].className;
maxProbability = prediction[i].probability;
}
}
if(maxProbability > 0.90 ) {
console.log(maxProbability + ' than' + maxClassName);
return;
}
else {
console.log(maxProbability + maxClassName + "Nothing" );
}
}