I am trying to predict the class of single image on trained model, but I am getting a strange output, so this is my code:
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image
import matplotlib.pyplot as plt
import numpy as np
import os
def load_image(img_path, show=False):
img = image.load_img(img_path, target_size=(300, 300))
img_tensor = image.img_to_array(img)
img_tensor = np.expand_dims(img_tensor, axis=0)
img_tensor /= 255.
if show:
plt.imshow(img_tensor[0])
plt.axis('off')
plt.show()
return img_tensor
if __name__ == "__main__":
# load model
model = load_model("model1.h5")
# image path
img_path = 'dog.jpg' # dog
# load a single image
new_image = load_image(img_path, True)
# check prediction
pred = model.predict(new_image)
print(pred)
But I am getting [[0.8189566 0.18104333]]
as output. But I have classes 0 and 1.
Could this be because the batch size is not specified anywhere?
with model.predict(new_image)
you obtain the probability of each test image to belong to a particular class
to get the output class, you need to select the class with the max probability predicted:
np.argmax(pred, axis=1)