如何将文件或图像作为 Keras 模型中的参数提供给 model.predict?
How to give file or image to model.predict as a parameter in a Keras model?
我看过 Python 中关于图像识别的教程,并使用编写的代码来训练网络。它编译和学习都很好,但是如何使用它来预测新图像呢?也许是这样的:model.predict(y)
?
代码如下:
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))
那么,要预测什么?
target = "C://Users//Target.png"
print(model.predict(target))
如何正确使用model.predict
以及如何将结果转换为用户友好的输出?
注意:如果您使用 keras
包而不是 tf.keras
,请将以下所有代码片段中的 tf.keras
替换为 keras
。
加载单张图片,可以使用tf.keras.preprocessing.image.load_img
:
image = tf.keras.preprocessing.image.load_img(image_path, target_size=(img_rows, img_cols))
这会将图像加载为 PIL 格式;因此,我们需要在将其提供给我们的模型之前将其转换为 numpy 数组:
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.
现在,您可能会因为急于在 input_arr
上使用 predict
方法而犯错误。但是,您也应该首先在预测阶段执行与训练阶段相同的预处理步骤:
input_arr = input_arr.astype('float32') / 255. # This is VERY important
现在,可以将其提供给模型进行预测:
predictions = model.predict(input_arr)
奖励: 因为你的模型是一个 classifier 并且它在顶部使用 Softmax 激活,predictions
变量将包含每个的概率class。为了找出预测的 class,我们使用 Numpy 中的 argmax
来找到概率最高的 class 的索引:
predicted_class = np.argmax(predictions, axis=-1)
你可以使用cv2读入图像。你想确保你在训练中对输入图像所做的任何处理你也对你使用 CV2 读取的图像进行了处理。注意 CV2 读取 BGR 格式的图像。如果您在 rgb 图像上训练您的模型,您需要将 cv2 图像转换为 rgb,如代码 below.Then 所示,您希望将图像设为 32 X 32 X3,因此如果不是该大小,请使用 cv2 调整图像大小.我假设您重新缩放了训练图像,因此您也需要重新缩放 cv2 图像。代码如下
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
我看过 Python 中关于图像识别的教程,并使用编写的代码来训练网络。它编译和学习都很好,但是如何使用它来预测新图像呢?也许是这样的:model.predict(y)
?
代码如下:
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))
那么,要预测什么?
target = "C://Users//Target.png"
print(model.predict(target))
如何正确使用model.predict
以及如何将结果转换为用户友好的输出?
注意:如果您使用 keras
包而不是 tf.keras
,请将以下所有代码片段中的 tf.keras
替换为 keras
。
加载单张图片,可以使用tf.keras.preprocessing.image.load_img
:
image = tf.keras.preprocessing.image.load_img(image_path, target_size=(img_rows, img_cols))
这会将图像加载为 PIL 格式;因此,我们需要在将其提供给我们的模型之前将其转换为 numpy 数组:
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.
现在,您可能会因为急于在 input_arr
上使用 predict
方法而犯错误。但是,您也应该首先在预测阶段执行与训练阶段相同的预处理步骤:
input_arr = input_arr.astype('float32') / 255. # This is VERY important
现在,可以将其提供给模型进行预测:
predictions = model.predict(input_arr)
奖励: 因为你的模型是一个 classifier 并且它在顶部使用 Softmax 激活,predictions
变量将包含每个的概率class。为了找出预测的 class,我们使用 Numpy 中的 argmax
来找到概率最高的 class 的索引:
predicted_class = np.argmax(predictions, axis=-1)
你可以使用cv2读入图像。你想确保你在训练中对输入图像所做的任何处理你也对你使用 CV2 读取的图像进行了处理。注意 CV2 读取 BGR 格式的图像。如果您在 rgb 图像上训练您的模型,您需要将 cv2 图像转换为 rgb,如代码 below.Then 所示,您希望将图像设为 32 X 32 X3,因此如果不是该大小,请使用 cv2 调整图像大小.我假设您重新缩放了训练图像,因此您也需要重新缩放 cv2 图像。代码如下
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