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 create a pair using a file path for the flower dataset?
TensorFlow can process image datasets by creating (image, label) pairs from file paths. The flowers dataset contains thousands of flower images organized in subdirectories, where each subdirectory represents a different flower class.
Read More: What is TensorFlow and how Keras work with TensorFlow to create Neural Networks?
We are using Google Colaboratory to run the below code. Google Colab helps run Python code over the browser and requires zero configuration with free access to GPUs.
Setting Up the Dataset
First, let's set up the basic variables and import required libraries ?
import tensorflow as tf import os # Dataset configuration img_height = 180 img_width = 180 class_names = ['daisy', 'dandelion', 'roses', 'sunflowers', 'tulips']
Creating Label Extraction Function
The get_label function extracts the class label from the file path ?
def get_label(file_path):
# Split the path into components
parts = tf.strings.split(file_path, os.path.sep)
# Get the second-to-last part (class directory name)
one_hot = parts[-2] == class_names
# Return the index of the matching class
return tf.argmax(one_hot)
Image Decoding Function
The decode_img function converts the compressed image string to a tensor and resizes it ?
def decode_img(img):
# Convert JPEG to 3D int tensor
img = tf.image.decode_jpeg(img, channels=3)
# Resize to the desired size
return tf.image.resize(img, [img_height, img_width])
Complete Processing Function
The process_path function combines everything to create (image, label) pairs ?
def process_path(file_path):
# Extract label from file path
label = get_label(file_path)
# Read the raw file as string
img = tf.io.read_file(file_path)
# Decode and resize the image
img = decode_img(img)
return img, label
print("Function to convert file path to (image,label) pair")
print("First, path is converted to list of path components")
print("Then, the second to last value is added to class directory")
print("The label is integer encoded")
print("The compressed string is converted to a 3 dimensional int tensor")
print("The image is resized to the required size")
print("The raw data is loaded from the file as a string value")
Code credit: https://www.tensorflow.org/tutorials/load_data/images
Output
Function to convert file path to (image,label) pair First, path is converted to list of path components Then, the second to last value is added to class directory The label is integer encoded The compressed string is converted to a 3 dimensional int tensor The image is resized to the required size The raw data is loaded from the file as a string value
How It Works
| Function | Purpose | Input | Output |
|---|---|---|---|
get_label() |
Extract class label | File path | Integer label |
decode_img() |
Process image data | Raw image string | Resized tensor |
process_path() |
Create image-label pair | File path | (image, label) tuple |
Key Points
- The file path structure is used to automatically extract class labels
-
tf.strings.split()breaks the path into components - The second−to−last path component contains the class name
- Images are decoded from JPEG format and resized to consistent dimensions
- The process creates ready−to−use (image, label) pairs for training
Conclusion
This approach efficiently converts file paths to (image, label) pairs by leveraging the directory structure. The functions work together to extract labels, decode images, and create properly formatted data for TensorFlow model training.
