简体   繁体   中英

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

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?

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. Unless your data is already grouped in subfolders, flow_from_directory will not work properly.

Use flow_from_dataframe. Store path to images in one column and class_labels in another column. Then use flow_from_dataframe. This is what you should be using since your data is not classified in subfolders.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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