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.
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.
Related
Halo there, I'm still struggling in python.
Now I'm going to use the EfficientNet model to detect the ripeness of palm oil.
I'm using 5852 training picture which is divided into 4 class (1463 per class) with 132 testing picture (33 per class).
After testing with 200 epoch, the result is far from good.
Is there any solution for me to improve the result?
Here's the result of my model accuracy and model loss.
Here's my code
https://colab.research.google.com/drive/18AtIP7aOycHPDR84PuQ7iS8aYUdclZIe?usp=sharing
your help means a lot to me.
You have rescaling in your generators, and it may be the root of the problem.
Tensorflow implementation of Efficientnets already contain rescaling layer, so you mustn't rescale images in your ImageDataGenerator. You can check this via .summary() method.
Official documentation says:
Note: each Keras Application expects a specific kind of input preprocessing. For EfficientNet, input preprocessing is included as part of the model (as a Rescaling layer), and thus tf.keras.applications.efficientnet.preprocess_input is actually a pass-through function. EfficientNet models expect their inputs to be float tensors of pixels with values in the [0-255] range
Resnets, for example, don't have this layer, and you should rescale images before feeding them to the model. It's tricky to remember those things for every single network from tf.keras.applications, so I suggest to just check them before using new models.
I have trained my CNN in Tensorflow using MNIST data set; when I tested it, it worked very well using the test data. Even, to prove my model in a better way, I made another set taking images from train and test set randomly. All the images that I took from those set, at the same time, I deleted and I didn't give them to my model. It worked very well too, but with a dowloaded image from Google, it doesn't classify well, so my question is: should I have to apply any filter to that image before I give it to the prediction part?
I resized the image and converted it to gray scale before.
MNIST is an easy dataset. Your model (CNN) structure may do quite well for MNIST, but there is no guarantee that it does well for more complex images too. You can add some more layers and check different activation functions (like Relu, Elu, etc.). Normalizing your image pixel values for small values like between -1 and 1 may help too.
I'm trying to get an Image Classifier to work. So far the model does seem to work but now every time I want to test an image to see if it is being recognized appropriately I have to do the whole training all over. I'm very new to this but I suppose there should be another way to only test the images without the training right?
I also have one further question concerning the code itself.
if result [0][0] >= 0.5:
prediction = "cogwheel"
else:
prediction = "not a cogwheel"
print(prediction)
I am trying to differentiate between images that represent cogwheels and those that don't. I understand that if the probability is > 0,5 it is a cogwheel and else it is not. But what does [0][0] here mean?
Thank you so much for your help!
Since you are a beginner, you may not know that you actually do not need to retrain the model in order to test :D. Your hunch is right, and we will see down below how you can do that.
You can save the weights of your model in a specific file format. In Keras, it is a file with the extension .hdf5.
from tensorflow.keras.models import load_model
##Do some stuff, train model
model.save(model_name)
##Do some stuff
loaded_model = load_model(model_name)
Please make sure that "model_name" includes .hdf5. For example, "my_model.hdf5".
Although it is not clear what you used to get the result (I assume result = model.predict(sample), where sample is a test sample), the first index corresponds to the class(label) and the second label corresponds to the probability of that specific class.
Test to see result[0][0] (probability of class 0), result[1][0] (probability of class 1).
I built an image classifier using CNTK. The images are grayscale. Therefore, I entered the number of channels as 1. So, the model requires (1x64x64) data (64 being the image height and width).
The problem is, when I try to predict the class of a new image, it is seen as (64x64) only. So, the code errors out due to data mismatch.
Therefore, I reshaped the image using:
image_data = image_data.reshape((1, image_data.shape[0], image_data.shape[1]))
This generated (1x64x64) - which worked. Though the predictions are coming the same class for every image I select. I wonder if it is because of this reshaping or not. Can someone chime in? Thanks!
Reshaping your input would not affect the output of the model. If it is only predicting one class for every image, it is an issue with model training. I would suggest you try predicting on your training data to see if it only predicts one class on the training data. If that is the case, it is definitely a model training issue.
I am a freshman in Keras and deep learning, I am not quite sure the right way to add the regularization, I wrote a CNN autoencoder using the API model class, right now I add the regularizer in each of the "Conv2D" Keras function,I am not sure if this is the right place to add regularization, could anyone please give me some suggestions?
(I tried to run the training and check the reconstructed test images, it is OK, but not very good, I use MNIST to test, the line of the reconstructed MNIST number is thicker than the original one.)
In my problem, the input image is an impaired one, and the original good image is used as a training label, by comparing the output image of the CNN with the training label image, I use the "mean absolute error" to define the loss , and also use it as the metric.
I defined three functions first, one downsampling function (the one below), one upsampling function, and one function to squeeze the third dimension of the matrix to get a two-dimensional matrix as the output.
My code is too long, just to help illustrate the problem, part of my code is as follow:
After having three defined functions, I defined the model as follow (not in detail, just part of it to help explain my problem)
load all necessary parameters to the model,then define the optimizer parameters, and compile the model