# How can Tensorflow be used to create a pair using a file path for the flower dataset?

To create an (image, label) pair, the path is first converted into list of path components. Then, the second to last value is added to the directory. Then, label is encoded into an integer format. The compressed string is converted to a tensor, and is then reshaped to the required size.

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.

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")
def get_label(file_path):
parts = tf.strings.split(file_path, os.path.sep)
one_hot = parts[-2] == class_names
return tf.argmax(one_hot)

print("The compressed string is converted to a 3 dimensional int tensor")
print("The image is resized to the required size")
def decode_img(img):
img = tf.image.decode_jpeg(img, channels=3)
return tf.image.resize(img, [img_height, img_width])

print("The raw data is loaded from the file as a string value")
def process_path(file_path):
label = get_label(file_path)
img = decode_img(img)
return img, label

## 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

## Explanation

• A 'get_label' function is defined, that converts the file path to an (image,label) pair.
• The file path is converted into a list of path components.
• The second to last value is added to the class directory.
• Next, the label is encoded as an integer.
• Another function named 'decode_img' is used to resize the imag and return it.
• First the compressed string is converted into a three dimensional integer tensor, and then resized.
• Another function named 'process_path'is defined, that loads the raw data from the file as a string value.