如何使用Keras ImageDataGenerator预测单个图像? [英] How to predict a single image with Keras ImageDataGenerator?

查看:341
本文介绍了如何使用Keras ImageDataGenerator预测单个图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经训练了CNN,可以将图像分类为3类. 在训练模型时,我使用了来自keras的ImageDataGenerator类将预处理功能应用于图像并对其进行缩放. 现在,我的网络已经在测试集上得到了很好的培训,但是我不知道如何在单个图像预测上应用预处理功能.如果我使用ImageDataGenerator,它将查找目录. 向我建议一些替代方法,以对单个图像执行预处理功能和重新缩放. 在下面查看我的代码

I have trained the CNN to classify images on 3 class. while training the model i have used ImageDataGenerator class from keras to apply preprocessing function on image and rescale it. Now my network is trained with a good accuracy on test set, but i don't know how to apply preprocessing function on single image prediction. If i use ImageDataGenerator it looks for directory. Suggest me some alternatives to do preprocessing function and rescaling on single image. see my code below

培训设置:

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')

测试设置:

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') 

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

Now,im unable to apply preprocessing function and rescaling on single image before prediction. SINGLE PREDICTION:

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') 

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

ERROR: NotADirectoryError: [Errno 20] Not a directory: './single_prediction/cc.jpg'

推荐答案

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

The image data generator looks at the directory you specify and searches for sub directories within that directory that specify the classes. So create a directory called './single_prediction. Within that directory create a single sub directory call it test. Within that sub directory named test place the images that you want to test. Alternatively you can write some python code to produce the pre-processed images. Create a directory called test and place your images in it. I have not tested it but the code below should work.

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

    

这篇关于如何使用Keras ImageDataGenerator预测单个图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆