简体   繁体   English

根据 Keras 中的图像和子文件夹名称设置 X 和 y 用于训练 CNN

[英]Setting X and y for training CNN based on images and subfolder names in Keras

The following snippet of code gives me 150 images belonging to three classes (there are three subfolders with 50 images in each - the folders are named after the classes of Iris that I'm trying to write a CNN to classify).下面的代码片段为我提供了属于三个类别的 150 个图像(有三个子文件夹,每个子文件夹有 50 个图像 - 这些文件夹以 Iris 的类别命名,我正在尝试编写一个 CNN 进行分类)。

The question I have is how do I set np arrays of the images as my X and the subfolder names as my y for training my CNN?我的问题是如何将图像的 np arrays 设置为我的 X 并将子文件夹名称设置为我的 y 来训练我的 CNN?

import keras
from keras.preprocessing.image import ImageDataGenerator

train_gen = ImageDataGenerator(rescale = 1./255,
shear_range = 0.2, zoom_range = 0.2, horizontal_flip = True)
test_gen = ImageDataGenerator(rescale = 1./255)
training_set = train_gen.flow_from_directory(r"Iris_Imgs",
target_size = (5, 5), shuffle=True, batch_size = 15, class_mode = 'binary')
train_imgs, train_labels = next(training_set)
test_set = test_gen.flow_from_directory(r"Iris_Imgs",
target_size = (5, 5), shuffle=True, class_mode = 'binary')
test_imgs, test_labels = next(test_set)

You are using flow_from_directory incorrectly.您错误地使用了 flow_from_directory。 Unless your data is already grouped in subfolders, flow_from_directory will not work properly.除非您的数据已经分组到子文件夹中,否则 flow_from_directory 将无法正常工作。

Use flow_from_dataframe.使用 flow_from_dataframe。 Store path to images in one column and class_labels in another column.将图像的路径存储在一列中,将 class_labels 存储在另一列中。 Then use flow_from_dataframe.然后使用 flow_from_dataframe。 This is what you should be using since your data is not classified in subfolders.这是您应该使用的,因为您的数据未分类在子文件夹中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM