如何使用 Keras ImageDataGenerator 预测单个图像?

Raj*_*bhu 7 python image conv-neural-network keras tensorflow

我已经训练 CNN 对图像进行 3 类分类。在训练模型时,我使用 keras 的 ImageDataGenerator 类对图像应用预处理功能并重新缩放它。现在我的网络在测试集上训练得非常准确,但我不知道如何在单图像预测上应用预处理功能。如果我使用 ImageDataGenerator 它会查找目录。建议我一些替代方法来对单个图像进行预处理功能和重新缩放。请参阅下面的我的代码

训练集:

train_datagen = ImageDataGenerator(preprocessing_function = tf.keras.applications.vgg16.preprocess_input,
                                   rescale = 1./255,
                                   shear_range = 0.2,
                                   zoom_range = 0.2,
                                   horizontal_flip = True)
training_set = train_datagen.flow_from_directory('./training_set',
                                                 target_size = (224, 224),
                                                 batch_size = 10,
                                                 class_mode = 'categorical')
Run Code Online (Sandbox Code Playgroud)

测试装置:

test_datagen =ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input,                                                            
                                                         rescale = 1./255)
test_set = test_datagen.flow_from_directory('./test_set',
                                            target_size = (224, 224),
                                            batch_size = 10,
                                            shuffle=False,
                                            class_mode = 'categorical') 
Run Code Online (Sandbox Code Playgroud)

现在,我无法在预测之前对单个图像应用预处理功能和重新缩放。单次预测:

single_datagen = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input,
                                   rescale = 1./255)
single_test = single_datagen.flow_from_directory('./single_prediction/cc.jpg',
                                            target_size = (224, 224),
                                            batch_size = 1,
                                            class_mode = 'categorical') 
Run Code Online (Sandbox Code Playgroud)

错误:NotADirectoryError:[Errno 20] 不是目录:'./single_prediction/cc.jpg'

Ger*_*y P 0

图像数据生成器查看您指定的目录并搜索该目录中指定类的子目录。因此,创建一个名为“./single_prediction”的目录。在该目录中创建一个子目录,并将其命名为 test。在名为 test 的子目录中放置要测试的图像。或者,您可以编写一些 python 代码来生成预处理图像。创建一个名为 test 的目录并将图像放入其中。我还没有测试过,但下面的代码应该可以工作。

import cv2
import numpy as np
import os
data_list=[]
dir=r'c:\test'
test_list=os.listdir(dir) # create a list of the files in the directory
batch_size=len(test_list) # determine number of files to process
for f in test_list:  # iterate through the files
    fpath=os.path.join (dir, f) # create path to the image file
    img=cv2.imread(fpath) # read image using cv2
    img=cv2.resize(img, (224,224)) # resize the image
    img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) # cv2 creates bgr images, convert to rgb images
    img=tf.keras.applications.vgg16.preprocess_input(img)   # apply the Vgg16 preprocess function
    data_list.append(img)  # append processed image to the list
data=np.array(data_list)/255 # convert to an np array and rescale images
print (data.shape, batch_size)
predictions=model.predict(data,batch_size=batch_size, verbose=0 )
trials=len (predictions)
for i in range(0,trials):
    predicted_class=predictions[i].argmax() # get index of highest probability
    print (test_list[i], predicted_class) # print file name and class prediction

    
Run Code Online (Sandbox Code Playgroud)