Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How can Tensorflow be used to explore the flower dataset using keras sequential API?
The flower dataset can be explored using TensorFlow's Keras Sequential API with the help of the PIL package for image processing. This dataset contains 3,670 images organized into 5 subdirectories representing different flower types: daisy, dandelion, roses, sunflowers, and tulips.
Read More: What is TensorFlow and how Keras work with TensorFlow to create Neural Networks?
We will use the Keras Sequential API to build an image classifier. The Sequential model works with a plain stack of layers where every layer has exactly one input tensor and one output tensor. Data is loaded efficiently using preprocessing.image_dataset_from_directory.
Prerequisites
First, we need to import the required libraries and set up the dataset path ?
import tensorflow as tf
from PIL import Image
import pathlib
# Download and extract the dataset
dataset_url = "https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file('flower_photos', origin=dataset_url, untar=True)
data_dir = pathlib.Path(data_dir)
Exploring the Dataset
Let's examine the structure and contents of the flower dataset ?
import tensorflow as tf
from PIL import Image
import pathlib
# For demonstration, we'll simulate the data directory structure
data_dir = pathlib.Path("flower_photos")
# Count total images (simulated)
image_count = 3670
print("The number of images in the dataset is:")
print(image_count)
# Display dataset structure
print("\nDataset structure:")
flower_types = ['daisy', 'dandelion', 'roses', 'sunflowers', 'tulips']
for flower in flower_types:
print(f"- {flower}/")
print("\nSample of available flower types:")
for flower in flower_types[:3]:
print(f"? {flower.title()}")
The number of images in the dataset is: 3670 Dataset structure: - daisy/ - dandelion/ - roses/ - sunflowers/ - tulips/ Sample of available flower types: ? Daisy ? Dandelion ? Roses
Loading and Creating the Dataset
We can create training and validation datasets using TensorFlow's built-in functions ?
import tensorflow as tf
# Dataset parameters
batch_size = 32
img_height = 180
img_width = 180
# Create training dataset (80% of data)
train_ds = tf.keras.utils.image_dataset_from_directory(
"flower_photos",
validation_split=0.2,
subset="training",
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size
)
# Create validation dataset (20% of data)
val_ds = tf.keras.utils.image_dataset_from_directory(
"flower_photos",
validation_split=0.2,
subset="validation",
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size
)
# Get class names
class_names = train_ds.class_names
print("Class names:", class_names)
print(f"Number of classes: {len(class_names)}")
Dataset Performance Optimization
To improve performance during training, we can configure the dataset for better efficiency ?
# Configure dataset for performance
AUTOTUNE = tf.data.AUTOTUNE
train_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)
val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)
print("Dataset optimized for training performance")
print(f"Training batches: {tf.data.experimental.cardinality(train_ds).numpy()}")
print(f"Validation batches: {tf.data.experimental.cardinality(val_ds).numpy()}")
Dataset optimized for training performance Training batches: 92 Validation batches: 23
Key Features
| Feature | Description | Benefits |
|---|---|---|
| Sequential API | Linear stack of layers | Simple and intuitive |
| image_dataset_from_directory | Direct loading from folders | Efficient data pipeline |
| Built-in preprocessing | Automatic resizing and batching | Reduced manual work |
Conclusion
The flower dataset provides an excellent foundation for building image classification models using TensorFlow's Keras Sequential API. The dataset's organized structure with 5 flower classes and built-in preprocessing functions make it ideal for learning computer vision techniques.
