0

I have trained my model and saved it using model.save.

How can i use model file to predict images.

I used this article How to predict input image using trained model in Keras? and used this codes

# Modify 'test1.jpg' and 'test2.jpg' to the images you want to predict on

from keras.models import load_model
from keras.preprocessing import image
import numpy as np

# dimensions of our images
img_width, img_height = 320, 240

# load the model we saved
model = load_model('model1.h5')
model.compile(loss='binary_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])

# predicting images
img = image.load_img('yes.jpeg', target_size=(img_width, img_height))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)

images = np.vstack([x])
classes = model.predict_classes(images, batch_size=10)
print(classes)

# predicting multiple images at once
##img = image.load_img('yes.jpeg', target_size=(img_width, img_height))
##y = image.img_to_array(img)
##y = np.expand_dims(y, axis=0)

# pass the list of multiple images np.vstack()
##images = np.vstack([x, y])
##classes = model.predict_classes(images, batch_size=10)

# print the classes, the images belong to
print(classes)
print(classes[0])
print(classes[0][0])

but this result is

[[1]]
[[1]]
[1]
1

how can i convert it into class indices?

James
  • 1,124
  • 3
  • 17
  • 37
  • Those are the class indices. `model.predict()` returns class probabilities, `model.predict_classes()` returns class indices. – sdcbr Aug 07 '18 at 12:35
  • I have done model.predict_classes(). To return class indices like {0: cancer ,1: noncancerous} how to get this? – James Aug 07 '18 at 13:39

1 Answers1

1

Do not recompile your model unless you want to train it again. simply load your model then predict.

Compiling will reset the weights.

VegardKT
  • 1,226
  • 10
  • 21