Prediction for single image file using tensor flow transfer learning model

174 views Asked by At

I am new to image classification and sorry if this problem seems too naive. I am using tf transfer learning model for my recent work. Ref: https://www.tensorflow.org/tutorials/images/transfer_learning.

Here there is clear mention of how to use this model to prediction on batch prediction for images. But i am having a hard time figuring out how to do prediction for single image using this.

I tried with this:

np_image = Image.open(image_path)
np_image = np.array(np_image).astype('float32')/255
np_image = transform.resize(np_image, (800, 700, 3))
np_image = np.expand_dims(np_image, axis=0)
probability_model = tf.keras.Sequential([model, tf.keras.layers.Softmax()])
predictions = probability_model.predict_proba(np_image)

But this is giving 1 as result for all images. I want probability prediction at an image level using this model.

1

There are 1 answers

0
Heisenberg On

Got a solution for this problem.

from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
image = load_img(image_path, target_size=(800, 700))


image = img_to_array(image)
image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
predictions = tf.nn.sigmoid(new_model.predict(image))
predictions=np.array(predictions)[0][0]