Tensorflow Flower Classifier consistent predictions with varied input

Issue

I am following this tensorflow tutorial notebook to classify images of flowers:
https://colab.research.google.com/github/tensorflow/docs/blob/master/site/en/tutorials/images/classification.ipynb#scrollTo=U-e-XzMeyH2O

Everything seems OK until the final cell in the notebook where the trained model is used to predict the class of a new image. I am getting identical predictions for all inputs.

I tried adding:

print(predictions)
print(score)

Then predicting on the sample image (of a sunflower):

sunflower_url = "https://storage.googleapis.com/download.tensorflow.org/example_images/592px-Red_sunflower.jpg"

outputs:

[[-2.1131027  -1.3355725   0.29224062  3.8924832   1.3749899 ]]
tf.Tensor([0.00220911 0.00480723 0.02448191 0.896212   0.07228985], shape=(5,), dtype=float32)
This image most likely belongs to sunflowers with a 89.62 percent confidence.

But if I just change the input to a picture of a rose, like:

sunflower_url = "https://images.photowall.com/products/64377/rose-flower.jpg"

outputs:

[[-2.1131027  -1.3355725   0.29224062  3.8924832   1.3749899 ]]
tf.Tensor([0.00220911 0.00480723 0.02448191 0.896212   0.07228985], shape=(5,), dtype=float32)
This image most likely belongs to sunflowers with a 89.62 percent confidence.

I have seen that there can be many model related issues (scaling / overfitting etc) which can cause identical outputs, however it seems strange that a tutorial example would fail in this way. So I suspect there is something more obvious that I am missing.

Solution

The issue here was related to the line

sunflower_path = tf.keras.utils.get_file('Red_sunflower', origin=sunflower_url)

When downloading a new image, it was NOT overwriting the stored image. So the model was making a prediction against the same input every time.

I manually defined the save location and made sure to get the input from that, then it worked.

Note that you do not need to rescale the image, this is handled within the predict() function from keras as already defined in the model.

Answered By – LenLenLen

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published