如何在新图像上使用.predict_generator()-Keras [英] How to use .predict_generator() on new Images - Keras
问题描述
我已经使用ImageDataGenerator
和flow_from_directory
进行培训和验证.
I've used ImageDataGenerator
and flow_from_directory
for training and validation.
这些是我的目录:
train_dir = Path('D:/Datasets/Trell/images/new_images/training')
test_dir = Path('D:/Datasets/Trell/images/new_images/validation')
pred_dir = Path('D:/Datasets/Trell/images/new_images/testing')
ImageGenerator代码:
ImageGenerator Code:
img_width, img_height = 28, 28
batch_size=32
train_datagen = ImageDataGenerator(
rescale=1. / 255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
test_datagen = ImageDataGenerator(rescale=1. / 255)
train_generator = train_datagen.flow_from_directory(
train_dir,
target_size=(img_height, img_width),
batch_size=batch_size,
class_mode='categorical')
validation_generator = test_datagen.flow_from_directory(
test_dir,
target_size=(img_height, img_width),
batch_size=batch_size,
class_mode='categorical')
找到了1852个属于4类的图像
Found 1852 images belonging to 4 classes
找到了115个属于4类的图像
Found 115 images belonging to 4 classes
这是我的模型训练代码:
This is my model training code:
history = cnn.fit_generator(
train_generator,
steps_per_epoch=1852 // batch_size,
epochs=20,
validation_data=validation_generator,
validation_steps=115 // batch_size)
现在,我要在测试文件夹中有一些新图像(所有图像仅在同一文件夹内).但是当我使用.predict_generator
时,我得到:
Now I have some new images in a test folder (all images are inside the same folder only), on which I want to predict. But when I use .predict_generator
I get:
找到0个属于0类的图像
Found 0 images belonging to 0 class
所以我尝试了以下解决方案:
So I tried these solutions:
1) Keras:如何将predict_generator与ImageDataGenerator一起使用?无法解决,因为它仅尝试验证集.
1) Keras: How to use predict_generator with ImageDataGenerator? This didn't work out, because its trying on validation set only.
2)如何使用model.predict预测新图像? module image not found
3)如何使用predict_generator对Keras中的流式测试数据进行预测?这也没有奏效.
我的火车数据基本上存储在4个单独的文件夹中,即4个特定的类,验证也以相同的方式存储,并且效果很好.
My train data is basically stored in 4 separate folders, i.e. 4 specific classes, validation also stored in same way and works out pretty well.
因此,在我的测试文件夹中,我大约有300张图像,我要在这些图像上进行预测并制作一个数据框,如下所示:
So in my test folder I have around 300 images, on which I want to predict and make a dataframe, like this:
image_name class
gghh.jpg 1
rrtq.png 2
1113.jpg 1
44rf.jpg 4
tyug.png 1
ssgh.jpg 3
我还使用了以下代码:
img = image.load_img(pred_dir, target_size=(28, 28))
img_tensor = image.img_to_array(img)
img_tensor = np.expand_dims(img_tensor, axis=0)
img_tensor /= 255.
cnn.predict(img_tensor)
但我收到此错误:[Errno 13] Permission denied: 'D:\\Datasets\\Trell\\images\\new_images\\testing'
但是我无法在测试图像上显示predict_generator
.因此,如何使用Keras对新图像进行预测.我在Google上搜索了很多,也在Kaggle Kernels上进行了搜索,但仍未找到解决方案.
But I haven't been able to predict_generator
on my test images. So how can I predict on my new images using Keras. I have googled a lot, searched on Kaggle Kernels also but haven't been able to get a solution.
推荐答案
因此,首先,应将测试图像放置在测试文件夹内的单独文件夹中.因此,在我的情况下,我在test
文件夹中创建了另一个文件夹,并将其命名为all_classes
.
然后运行以下代码:
So first of all the test images should be placed inside a separate folder inside the test folder. So in my case I made another folder inside test
folder and named it all_classes
.
Then ran the following code:
test_generator = test_datagen.flow_from_directory(
directory=pred_dir,
target_size=(28, 28),
color_mode="rgb",
batch_size=32,
class_mode=None,
shuffle=False
)
上面的代码给了我一个输出:
The above code gives me an output:
找到306个属于1类的图像
Found 306 images belonging to 1 class
最重要的是,您必须编写以下代码:
And most importantly you've to write the following code:
test_generator.reset()
其他奇怪的输出将会出现.
然后使用.predict_generator()
函数:
else weird outputs will come.
Then using the .predict_generator()
function:
pred=cnn.predict_generator(test_generator,verbose=1,steps=306/batch_size)
运行上面的代码将给出概率输出,因此首先我需要将其转换为类编号.在我的情况下,这是4个班级,所以班级编号分别是0、1、2和3.
Running the above code will give output in probabilities so at first I need to convert them to class number. In my case it was 4 classes, so class numbers were 0,1,2 and 3.
编写代码:
predicted_class_indices=np.argmax(pred,axis=1)
下一步是我想要类的名称:
Next step is I want the name of the classes:
labels = (train_generator.class_indices)
labels = dict((v,k) for k,v in labels.items())
predictions = [labels[k] for k in predicted_class_indices]
在这里,由班级编号将替换为班级名称.最后一步,如果要将其保存到csv文件中,请将其布置在数据帧中,图像名称后附有预测的类.
Where by class numbers will be replaced by the class names. One final step if you want to save it to a csv file, arrange it in a dataframe with the image names appended with the class predicted.
filenames=test_generator.filenames
results=pd.DataFrame({"Filename":filenames,
"Predictions":predictions})
显示您的数据框.现在一切都完成了.您将获得图像的所有预测类别.
Display your dataframe. Everything is done now. You get all the predicted class for your images.
这篇关于如何在新图像上使用.predict_generator()-Keras的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!