python-kerasHow can I use Python and Keras to create an image dataset from a directory?
Creating an image dataset from a directory with Python and Keras is a relatively straightforward process. The following example code block can be used to achieve this:
from keras.preprocessing.image import ImageDataGenerator
# Set the directory where the images are stored
data_dir = 'data/'
# Create the data generator
datagen = ImageDataGenerator(rescale=1./255)
# Load the images from the directory
data_generator = datagen.flow_from_directory(
data_dir,
target_size=(150, 150),
batch_size=32,
class_mode='categorical')
The code above will load the images from the directory specified by data_dir
. The images will be resized to 150x150 pixels. The images will also be rescaled to a range of 0-1. The data_generator
variable will contain the dataset.
Code explanation
from keras.preprocessing.image import ImageDataGenerator
- imports theImageDataGenerator
class from thekeras.preprocessing.image
module.data_dir = 'data/'
- sets the directory where the images are stored.datagen = ImageDataGenerator(rescale=1./255)
- creates the data generator, with the images rescaled to a range of 0-1.data_generator = datagen.flow_from_directory(data_dir, target_size=(150, 150), batch_size=32, class_mode='categorical')
- loads the images from the directory, resizing them to 150x150 pixels, and returns the dataset in thedata_generator
variable.
Helpful links
More of Python Keras
- How can I use word2vec and Keras to develop a machine learning model in Python?
- How do I use validation_data when creating a Keras model in Python?
- How can I use batch normalization in Python Keras?
- How do I use Python Keras to zip a file?
- How can I resolve the issue of Python module Tensorflow.keras not being found?
- How do I check which version of Keras I am using in Python?
- How can I use Python and Keras together?
- How do I use zero padding in Python Keras?
- How can I use YOLO with Python and Keras?
- How do I use TensorFlow, Python, Keras, and utils to_categorical?
See more codes...