Keras: How to predict the new image by using model.predict?

Created on 15 Jun 2017  路  7Comments  路  Source: keras-team/keras

I have built a model and saved its weights as 'first_try.h5' but I am not able to load the model & run it on any random image. My problem is how to use model.predict_generator or model.predict or model.predict_on_batch methods. I have read the documentation, but I need one small example to do so. Sorry, I am new to this. It is a binary classifier.

I am using CNTK as my backend. I am following this tutorial.

Need little help.

EDIT: I am trying the following way, but the output is just [[0.]]

'model.load_weights('first_try.h5')
img = Image.open("data/predict/T160305M-0222c_5.jpg")
a = array(img).reshape(1,512,512,3)
score = model.predict(a, batch_size=32, verbose=0)
print(score)'

Most helpful comment

  1. When you load weights, you have to have something to load them into. So first you have to load the model, before your weights. Exactly the same model you used to get your weights. Guide on how to save your model to JSON/YAML: https://machinelearningmastery.com/save-load-keras-deep-learning-models/
  2. Load your weight (you got that)
  3. Preprocess the image in the same way as you did with the training (everything have to end up in range between 0 and 1. In your case img/255)
  4. Now you can do the prediction. Remember to include a fictive ID as an extra dimension, because you most likely had that in your training, to separate the trainingdata. For a single image you can solve it this way:
import numpy as np
model.predict(np.expand_dims(img, axis=0))

Let me know if you need a complete example on training, saving model+weight and prediction.

All 7 comments

I had same problem.
I was not saving the weights properly but now everything seem fine.
Please add the save_weights code so that we can know where the problem is.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 30 days if no further activity occurs, but feel free to re-open a closed issue if needed.

Probably you have to normalise the numpy array by 255.

  1. When you load weights, you have to have something to load them into. So first you have to load the model, before your weights. Exactly the same model you used to get your weights. Guide on how to save your model to JSON/YAML: https://machinelearningmastery.com/save-load-keras-deep-learning-models/
  2. Load your weight (you got that)
  3. Preprocess the image in the same way as you did with the training (everything have to end up in range between 0 and 1. In your case img/255)
  4. Now you can do the prediction. Remember to include a fictive ID as an extra dimension, because you most likely had that in your training, to separate the trainingdata. For a single image you can solve it this way:
import numpy as np
model.predict(np.expand_dims(img, axis=0))

Let me know if you need a complete example on training, saving model+weight and prediction.

@punnerud where can I get the complete example??

I also encounter the preprocess problem, i am not sure what shall i do in preprocess

Sorry, I have moved most of my work to PyTorch now. Don鈥檛 think I still have the complete example in Keras.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

LuCeHe picture LuCeHe  路  3Comments

braingineer picture braingineer  路  3Comments

fredtcaroli picture fredtcaroli  路  3Comments

Imorton-zd picture Imorton-zd  路  3Comments

nryant picture nryant  路  3Comments