Visualize the Flower Dataset using Tensorflow - Python
Last Updated : 28 Apr, 2025
The Tensorflow flower dataset is a large dataset that consists of flower images. In this article, we are going to learn how we can visualize the flower dataset in python. For the purposes of this article, we will use tensorflow_datasets and Matplotlib library.
Prerequisites
If you don't have any of the libraries mentioned below, you can install them using the pip command, for example, to install the tensorflow_datasets library you need to write the following command:
pip install tensorflow-datasets
Matplotlib is a plotting library for Python. It allows the user with functionalities to visualize and plot complex graphs in python yet provides extensive customization capabilities. To install Matplotlib, just as before you can use the pip command:
pip install matplotlib
Importing Libraries
tensorflow_datasets is a library of public datasets ready to use with TensorFlow and MatplotLib is one of the handy tools used by machine learning practitioners to build visualization on different kinds of datasets.
Python3 import tensorflow_datasets as tfds import matplotlib.pyplot as plt
Loading Flower Dataset
Let's start by importing the dataset. We will utilize the tfds.load() function to import the flower dataset. It is used to load the specified dataset into a tf.data.Dataset, which is supplied via the name argument. The flower dataset is called tf_flowers.
Python3 dataset, info = tfds.load( 'tf_flowers', with_info=True, as_supervised=True, ) get_image_label = info.features['label'].int2str
Let's check how many images we have in the dataset.
Python3 print(len(dataset['train']))
Output:
3670
Now it's time to visualize the images. The following piece of code shows the first four images in the dataset.
Python3 num = 1 fig = plt.figure(figsize=(20, 14)) for image, label in iter(dataset['train']): if num > 4: break img = image.numpy() fig.add_subplot(2, 2, num) plt.imshow(img) plt.axis('off') plt.title(get_image_label(label)) num += 1 plt.tight_layout() plt.show()
Output:
Visualizing some images from the flower dataset We can also apply data augmentation on it using one of the very handy libraries that are Albumentation.
Python3 import albumentations as A augmenter = A.VerticalFlip(p=1) image, label = next(iter(dataset['train'])) plt.subplot(1, 2, 1) _ = plt.imshow(image) _ = plt.title(get_image_label(label)) plt.subplot(1, 2, 2) image = augmenter(image=image)['image'] _ = plt.imshow(image) _ = plt.title(get_image_label(label))
Output:
Original image and the augmented image