How can Tensorflow be used to visualize the flower dataset using Python?

The flower dataset can be visualized with the help of the matplotlib library. The imshow method is used to display the image on the console. The entire dataset is iterated over, and only the first few images are displayed.

Read More: What is TensorFlow and how Keras work with TensorFlow to create Neural Networks?

We will be using the flowers dataset, which contains images of several thousands of flowers. It contains 5 sub-directories, and there is one sub-directory for every class.

We are using the Google Colaboratory to run the below code. Google Colab or Colaboratory helps run Python code over the browser and requires zero configuration and free access to GPUs (Graphical Processing Units). Colaboratory has been built on top of Jupyter Notebook.

Loading the Dataset

First, let's load and prepare the flower dataset using TensorFlow ?

import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np

# Load the flowers 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)

# Create dataset
batch_size = 32
img_height = 180
img_width = 180

train_ds = tf.keras.utils.image_dataset_from_directory(
    data_dir,
    validation_split=0.2,
    subset="training",
    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)
Found 3670 files belonging to 5 classes.
Using 2936 files for training.
Class names: ['daisy', 'dandelion', 'roses', 'sunflowers', 'tulips']

Visualizing the Flower Dataset

Now let's visualize a sample of the flower images from the dataset ?

import matplotlib.pyplot as plt

print("Visualizing the flower dataset")
plt.figure(figsize=(10, 10))
for images, labels in train_ds.take(1):
    for i in range(6):
        ax = plt.subplot(3, 3, i + 1)
        plt.imshow(images[i].numpy().astype("uint8"))
        plt.title(class_names[labels[i]])
        plt.axis("off")

plt.tight_layout()
plt.show()

Dataset Information

Let's examine the structure and dimensions of our dataset ?

print("Iterating over dataset")
print("Retrieving batches of images")
for image_batch, labels_batch in train_ds:
    print("Image batch shape:", image_batch.shape)
    print("Labels batch shape:", labels_batch.shape)
    print("Number of classes:", len(class_names))
    break
Visualizing the flower dataset
Iterating over dataset
Retrieving batches of images
Image batch shape: (32, 180, 180, 3)
Labels batch shape: (32,)
Number of classes: 5

Understanding the Dataset Structure

Flower Dataset Structure Batch: 32 Height: 180 Width: 180 Channels: 3 Classes (5): ? Daisy ? Dandelion ? Roses ? Sunflowers ? Tulips Shape: (batch_size, height, width, RGB_channels)

Key Points

  • The flower dataset contains 5 classes: daisy, dandelion, roses, sunflowers, and tulips
  • Each image is resized to 180×180 pixels with 3 RGB channels
  • The dataset is processed in batches of 32 images for efficient training
  • The matplotlib.pyplot.imshow() method displays images on the console
  • Images are converted to uint8 format for proper visualization

Conclusion

TensorFlow makes it easy to load and visualize the flower dataset using image_dataset_from_directory(). The matplotlib library provides excellent visualization capabilities for displaying sample images and understanding the dataset structure before training machine learning models.

Updated on: 2026-03-25T15:58:15+05:30

302 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements