Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to predict from saved model in Keras ?

I have trained an image classifier using keras and it gave a very good accuracy. I've saved the model using the save()and saved it using the h5 format. How can I make a prediction using the model?

The code is:

from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense

classifier = Sequential()

classifier.add(Conv2D(32, (3, 3), input_shape = (64, 64, 3), activation =   'relu'))

classifier.add(MaxPooling2D(pool_size = (2, 2)))

classifier.add(Conv2D(32, (3, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))
classifier.add(Flatten())

classifier.add(Dense(units = 128, activation = 'relu'))
classifier.add(Dense(units = 1, activation = 'sigmoid'))
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])

from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
test_datagen = ImageDataGenerator(rescale = 1./255)
training_set = train_datagen.flow_from_directory('training_set',
target_size = (64, 64),
batch_size = 32,
class_mode = 'binary')
test_set = test_datagen.flow_from_directory('test_set',
target_size = (64, 64),
batch_size = 32,
class_mode = 'binary')
classifier.fit_generator(training_set,
steps_per_epoch = 8000,
epochs = 5,
validation_data = test_set,
validation_steps = 2000)
classifier.save('classifier.h5')

Thanks in Advance..!!

like image 878
Arjun Avatar asked May 08 '18 07:05

Arjun


People also ask

How do you predict a saved model in python?

The first step is to import your model using load_model method. Then you have to compile the model in order to make predictions. Now you can predict results for a new entry image. You do not need to compile anymore.

How do you save a Keras model for prediction?

Keras provides the ability to describe any model using JSON format with a to_json() function. This can be saved to a file and later loaded via the model_from_json() function that will create a new model from the JSON specification.

How do you predict accuracy from a model?

Accuracy is a metric used in classification problems used to tell the percentage of accurate predictions. We calculate it by dividing the number of correct predictions by the total number of predictions.

How do you connect model input data with predictions?

To give inputs to a machine learning model, you have to create a NumPy array, where you have to input the values of the features you used to train your machine learning model. Then we can use that array in the model. predict() method, and at the end, it will give the predicted value as an output based on the inputs.


1 Answers

The first step is to import your model using load_model method.

from keras.models import load_model
model = load_model('my_model.h5')

Then you have to compile the model in order to make predictions.

model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])

Now you can predict results for a new entry image.

from keras.preprocessing import image

test_image = image.load_img(imagePath, target_size = (64, 64)) 
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis = 0)

#predict the result
result = model.predict(test_image)
like image 114
Mihai Alexandru-Ionut Avatar answered Oct 12 '22 16:10

Mihai Alexandru-Ionut