简体   繁体   中英

How to give file or image to model.predict as a parameter in a Keras model?

I've watched a tutorial about image recognition in Python, and used written code for training a.network. It compiles and learning fine, but how to use it for prediction on new images? Maybe something like: model.predict(y) ?

Here is the code:

import numpy
from keras.datasets import cifar10
from keras.models import Sequential
from keras.layers import Dense, Flatten, Activation
from keras.layers import Dropout
from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.utils import np_utils
from keras.optimizers import SGD

numpy.random.seed(42)

#Loading data
(X_train, y_train), (X_test, y_test) = cifar10.load_data()

batch_size = 32

nb_classes = 10
#Number of epochs
epochNumber = 25
#Image size
img_rows, img_cols = 32, 32
#RGB
img_channels = 3


X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255

#To catogories
Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)

#Creating a model
model = Sequential()

#Adding layers
model.add(Conv2D(32, (3, 3), padding='same',
                        input_shape=(32, 32, 3), activation='relu'))
model.add(Conv2D(32, (3, 3), activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Conv2D(64, (3, 3), padding='same', activation='relu'))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.5))

model.add(Dense(nb_classes, activation='softmax'))

#Optimization
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy',
              optimizer=sgd,
              metrics=['accuracy'])
#Training model
model.fit(X_train, Y_train,
              batch_size=batch_size,
              epochs=epochNumber,
              validation_split=0.1,
              shuffle=True,
              verbose=2)


scores = model.evaluate(X_test, Y_test, verbose=0)
print("Accuracy on test data: %.2f%%" % (scores[1]*100))

Then, what to do to predict?

target = "C://Users//Target.png"
print(model.predict(target))

How to correctly use model.predict and how to convert result to user-friendly output?

Note: if you are using keras package instead of tf.keras , replace tf.keras with keras in all the following code snippets.


To load a single image, you can use tf.keras.preprocessing.image.load_img :

image = tf.keras.preprocessing.image.load_img(image_path, target_size=(img_rows, img_cols))

This would load the image into PIL format; therefore, we need to convert it to numpy array before feeding it to our model:

import numpy as np

input_arr = tf.keras.preprocessing.image.img_to_array(image)
input_arr = np.array([input_arr])  # Convert single image to a batch.

Now, you might make a mistake by rushing into using predict method on input_arr . However, you should first perform the same preprocessing steps of training phase in prediction phase as well:

input_arr = input_arr.astype('float32') / 255.  # This is VERY important

Now, it's ready to be given to the model for prediction:

predictions = model.predict(input_arr)

Bonus: Since your model is a classifier and it's using Softmax activation at the top, the predictions variable would contain the probabilities for each class. To find out the predicted class, we use argmax from Numpy to find the index of the class with the highest probability:

predicted_class = np.argmax(predictions, axis=-1)

you can use cv2 to read in the image. You want to make sure that what ever processing you did on the input image in training you also do on the image you read in with CV2. Be careful CV2 reads images in BGR format. If you trained your model on rgb images you need to convert the cv2 image to rgb as shown in the code below.Then you want to make the image 32 X 32 X3 so if it is not that size use cv2 to resize the image. I assume you rescaled your training images so you need to rescale the cv2 image as well. Code is below

import cv2
img=cv2.imread(f_path)  # where f_path is the path to the image file
img=cv2.resize(img, (32,32), interpolation = cv2.INTER_AREA)  
img=img/255
# CV2 inputs images in BGR format in general when you train a model you may have
#trained it with images in rgb format. If so you need to convert the cv2 image.
#uncomment the line below if that is the case.
#img=img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
predictions=model.predict(img)
pre_class=predictions.argmax()
# this will give you an integer value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM