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

PythonServer Side ProgrammingProgrammingTensorflow

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.
{"ft_lang_label":"__label__en","ft_lang_prob":0.79292613,"math_prob":0.8113944,"size":2044,"snap":"2021-43-2021-49","text_gpt3_token_len":446,"char_repetition_ratio":0.11715686,"word_repetition_ratio":0.18666667,"special_character_ratio":0.22015655,"punctuation_ratio":0.122395836,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.97216064,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-11-28T14:18:51Z\",\"WARC-Record-ID\":\"<urn:uuid:a150b57f-c9d7-4a18-9eba-c39807c619eb>\",\"Content-Length\":\"33871\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:b151d00a-baaf-42ae-a0ca-abc2244001a5>\",\"WARC-Concurrent-To\":\"<urn:uuid:2f24b9c4-75dc-48e3-a2d9-2631e8227054>\",\"WARC-IP-Address\":\"72.21.91.42\",\"WARC-Target-URI\":\"https://www.tutorialspoint.com/how-can-tensorflow-be-used-to-create-a-pair-using-a-file-path-for-the-flower-dataset\",\"WARC-Payload-Digest\":\"sha1:ODNG6SOA7P3LCD4XKDS7F4M3OUE2W6RB\",\"WARC-Block-Digest\":\"sha1:2I6MJ2G6TEKDCHPNUTL4Q3YXQLZRDSGG\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-49/CC-MAIN-2021-49_segments_1637964358560.75_warc_CC-MAIN-20211128134516-20211128164516-00130.warc.gz\"}"}