2

The script that builds and trains the model looks like this:

const model = tf.sequential();

model.add(tf.layers.conv2d({
  inputShape: [160, 200, 3],
  filters: 32,
  kernelSize: 3,
  activation: 'relu',
}));

model.add(tf.layers.flatten());

model.add(tf.layers.dense({units: labels.length, activation: 'softmax'}));  

model.compile({
  optimizer: 'sgd',
  loss: 'categoricalCrossentropy',
  metrics: ['accuracy']
});    

const info = await model.fitDataset(ds, {epochs: 5});
console.log('Accuracy:', info.history.acc);

console.log('Saving...');
await model.save('file://' + MODEL_PATH);
console.log('Saved model');

ds consists of images and labels. For 100 images I get these results:

4478ms 135692us/step - acc=0.109 loss=14.37

and it produced a 20 MB weights.bin file...

Frankly, I have no idea if that's good or not because I don't know how to use it classify new images.

I know how to load the model:

const model = await tf.loadLayersModel('file://' + MODEL_PATH + '/model.json');

but that's it.

mobilenet has a .classify method to which I can just pass an image and it outputs the predicted laabel. But this is not available on the model object.. So how do I proceeed?

1 Answer 1

1

After training your model, to classify a new image, the predict method will be used. It will return the probability of each label given your input image(s).

output = model.predict(image) // It can be a tensor of one image or a batch of many 
// output is a tensor that contain the probability for each label
images.argMax([-1]) // contain the label of high probability for each input
Sign up to request clarification or add additional context in comments.

8 Comments

I get this error: Error when checking : expected conv2d_Conv2D1_input to have 4 dimension(s), but got array with shape [100,200,3]
It is because your image is a tensor3d. You will need to do image_reshaped = tf.expandDims(image) and predict from the reshaped image
thanks. the result I get is a label tensor, but it doesn't match at all with the corresponding label. And I provided an image that I used in training
It is not because the image was used during the training that the model will predict its label correctly. Some predictions will be correct whereas other might be wrong. It is related to the accuracy of the model
ah ok. do you know if I can also get the probability percentage, like in mobilenet.classify() ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.