Python 如何使用 Keras 中的训练模型预测输入图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43469281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:06:08  来源:igfitidea点击:

How to predict input image using trained model in Keras?

pythonmachine-learningkerascomputer-vision

提问by ritiek

I'm only beginning with keras and machine learning in general.

我只是从 keras 和机器学习开始。

I trained a model to classify images from 2 classes and saved it using model.save(). Here is the code I used:

我训练了一个模型来对 2 个类别的图像进行分类,并使用model.save(). 这是我使用的代码:

from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras import backend as K


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

train_data_dir = 'data/train'
validation_data_dir = 'data/validation'
nb_train_samples = 200  #total
nb_validation_samples = 10  # total
epochs = 6
batch_size = 10

if K.image_data_format() == 'channels_first':
    input_shape = (3, img_width, img_height)
else:
    input_shape = (img_width, img_height, 3)

model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=input_shape))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('sigmoid'))

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

# this is the augmentation configuration we will use for training
train_datagen = ImageDataGenerator(
    rescale=1. / 255,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True)

# this is the augmentation configuration we will use for testing:
# only rescaling
test_datagen = ImageDataGenerator(rescale=1. / 255)

train_generator = train_datagen.flow_from_directory(
    train_data_dir,
    target_size=(img_width, img_height),
    batch_size=batch_size,
    class_mode='binary')

validation_generator = test_datagen.flow_from_directory(
    validation_data_dir,
    target_size=(img_width, img_height),
    batch_size=batch_size,
    class_mode='binary')

model.fit_generator(
    train_generator,
    steps_per_epoch=nb_train_samples // batch_size,
    epochs=epochs,
    validation_data=validation_generator,
    validation_steps=5)

model.save('model.h5')

It successfully trained with 0.98 accuracy which is pretty good. To load and test this model on new images, I used the below code:

它以 0.98 的准确率成功训练,非常好。为了在新图像上加载和测试这个模型,我使用了以下代码:

from keras.models import load_model
import cv2
import numpy as np

model = load_model('model.h5')

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

img = cv2.imread('test.jpg')
img = cv2.resize(img,(320,240))
img = np.reshape(img,[1,320,240,3])

classes = model.predict_classes(img)

print classes

It outputs:

它输出:

[[0]]

[[0]]

Why wouldn't it give out the actual name of the class and why [[0]]?

为什么它不给出类的实际名称,为什么[[0]]

Thanks in advance.

提前致谢。

采纳答案by DNK

keras predict_classes (docs) outputs A numpy array of class predictions. Which in your model case, the index of neuron of highest activation from your last(softmax) layer. [[0]]means that your model predicted that your test data is class 0. (usually you will be passing multiple image, and the result will look like [[0], [1], [1], [0]])

keras predict_classes ( docs) 输出类预测的 numpy 数组。在您的模型案例中,您的最后一个(softmax)层的最高激活神经元的索引。[[0]]意味着你的模型预测你的测试数据是 0 类。(通常你会传递多个图像,结果看起来像[[0], [1], [1], [0]]

You must convert your actual label (e.g. 'cancer', 'not cancer') into binary encoding (0for 'cancer', 1for 'not cancer') for binary classification. Then you will interpret your sequence output of [[0]]as having class label 'cancer'

您必须将实际标签(例如'cancer', 'not cancer')转换为二进制编码(0对于“癌症”,1对于“非癌症”)进行二进制分类。然后您将您的序列输出解释[[0]]为具有类标签'cancer'

回答by ritiek

If someone is still struggling to make predictions on images, here is the optimized code to load the saved model and make predictions:

如果有人仍在努力对图像进行预测,这里是加载保存的模型并进行预测的优化代码:

# 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('model.h5')
model.compile(loss='binary_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])

# predicting images
img = image.load_img('test1.jpg', 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('test2.jpg', 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]

回答by auraham

You can use model.predict()to predict the class of a single image as follows [doc]:

您可以使用model.predict()以下[doc]来预测单个图像的类别:

# load_model_sample.py
from keras.models import load_model
from 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=(150, 150))
    img_tensor = image.img_to_array(img)                    # (height, width, channels)
    img_tensor = np.expand_dims(img_tensor, axis=0)         # (1, height, width, channels), add a dimension because the model expects this shape: (batch_size, height, width, channels)
    img_tensor /= 255.                                      # imshow expects values in the range [0, 1]

    if show:
        plt.imshow(img_tensor[0])                           
        plt.axis('off')
        plt.show()

    return img_tensor


if __name__ == "__main__":

    # load model
    model = load_model("model_aug.h5")

    # image path
    img_path = '/media/data/dogscats/test1/3867.jpg'    # dog
    #img_path = '/media/data/dogscats/test1/19.jpg'      # cat

    # load a single image
    new_image = load_image(img_path)

    # check prediction
    pred = model.predict(new_image)

In this example, a image is loaded as a numpyarray with shape (1, height, width, channels). Then, we load it into the model and predict its class, returned as a real value in the range [0, 1] (binary classification in this example).

在此示例中,图像作为numpyshape的数组加载(1, height, width, channels)。然后,我们将其加载到模型中并预测它的类别,以 [0, 1] 范围内的真实值形式返回(本例中为二元分类)。

回答by Javapocalypse

That's because you're getting the numeric value associated with the class. For example if you have two classes cats and dogs, Keras will associate them numeric values 0 and 1. To get the mapping between your classes and their associated numeric value, you can use

那是因为您正在获取与该类相关联的数值。例如,如果您有两个类猫和狗,Keras 会将它们关联到数值 0 和 1。要获取您的类与其关联数值之间的映射,您可以使用

>>> classes = train_generator.class_indices    
>>> print(classes)
    {'cats': 0, 'dogs': 1}

Now you know the mapping between your classes and indices. So now what you can do is

现在您知道类和索引之间的映射。所以现在你能做的是

if classes[0][0] == 1: prediction = 'dog' else: prediction = 'cat'

if classes[0][0] == 1: prediction = 'dog' else: prediction = 'cat'

回答by Vineeth Sai

Forwarding the example by @ritiek, I'm a beginner in ML too, maybe this kind of formatting will help see the name instead of just class number.

转发@ritiek 的示例,我也是 ML 的初学者,也许这种格式将有助于查看名称而不仅仅是班级编号。

images = np.vstack([x, y])

prediction = model.predict(images)

print(prediction)

i = 1

for things in prediction:  
    if(things == 0):
        print('%d.It is cancer'%(i))
    else:
        print('%d.Not cancer'%(i))
    i = i + 1