简体   繁体   中英

Classify images with tab containing path

I am looking to train a network with images whose paths are contained in a table.

I have searched on the TensorFlow website and I find the following instruction:

train_data_gen = train_image_generator.flow_from_directory(batch_size=batch_size,
                                                           directory=train_dir,
                                                           shuffle=True,
                                                           target_size=(IMG_HEIGHT, IMG_WIDTH),
                                                           class_mode='binary')

The problem is that I don't have a separate folder for my test and validation data. Simply one table contains the paths to the test images and another containing the paths to the validation images.

However, my images are in different folders depending on their class. How do I load these PNG test images whose paths are in one table and verify them with the other images whose paths are in another table?

You can pass the list of paths to tf.data.Dataset.list_files() and later pass them to map() function to read these images and do all the preprocessing you would like to do. You can find more about tf.data.Dataset and supported methods available here .

Here is an example where I am having bird and dog images in 3 different folders. I am passing these paths to tf.data.Dataset.list_files() and in map function I am doing crop_central to crop the image and later displaying them. Have added print statements to display the paths of files.

Code -

%tensorflow_version 2.x
import tensorflow as tf
from tensorflow.keras.preprocessing.image import load_img
from tensorflow.keras.preprocessing.image import img_to_array, array_to_img
from matplotlib import pyplot as plt
import numpy as np

file_path = ['/content/bird.jpg','/content/sample_data/dog.jpg','/usr/bird1.jpg']

def load_file_and_process(path):
    print("Loading image from path :",bytes.decode(path.numpy()))
    image = load_img(bytes.decode(path.numpy()), target_size=(224, 224))
    image = img_to_array(image)
    image = tf.image.central_crop(image, np.random.uniform(0.50, 1.00))
    return image

train_dataset = tf.data.Dataset.list_files(file_path)

train_dataset = train_dataset.map(lambda x: tf.py_function(load_file_and_process, [x], [tf.float32]))

for f in train_dataset:
  for l in f:
    image = np.array(array_to_img(l))
    print("Crop Image is of shape : ", image.shape)
    plt.figure()
    plt.imshow(image)

Output -

Loading image from path : /content/bird.jpg
Crop Image is of shape :  (124, 124, 3)
Loading image from path : /content/sample_data/dog.jpg
Crop Image is of shape :  (220, 220, 3)
Loading image from path : /usr/bird1.jpg
Crop Image is of shape :  (158, 158, 3)

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

Hope this answers your question. Happy Learning.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM