0

I am new to tensorflow, and here is my situation: I have lots of folders and each contains several images. I need my training input to be folders(each time 2 folders), and each time 4 images inside a folder be selected for training. I have tried Dataset api, and tried to use the map or flat_map function, but I failed to read images inside a folder. Here is part of my codes:

def parse_function(filename):
    print(filename)
    batch_data = []
    batch_label = []
    dir_path = os.path.join(data_path, str(filename))
    imgs_list = os.listdir(dir_path)
    random.shuffle(imgs_list)
    imgs_list = imgs_list * 4 #each time select 4 images
    for i in range(img_num):
        img_path = os.path.join(dir_path, imgs_list[i])
        image_string = tf.read_file(img_path)  
        image_decoded = tf.image.decode_image(image_string)  
        image_resized = tf.image.resize_images(image_decoded, [224, 224])
        batch_data.append(image_resized)
        batch_label.append(label)
    return batch_data, batch_label
dataset = tf.data.Dataset.from_tensor_slices((filenames, labels)) 
dataset = dataset.map(_parse_function) 

where filename is a list of folder name like '123456', labels is list of label like 0 or 1.

E_net4
  • 27,810
  • 13
  • 101
  • 139
Ra.Bear
  • 1
  • 1
  • [This](https://stackoverflow.com/questions/50356677/how-to-create-tf-data-dataset-from-directories-of-tfrecords) seems to be very similar to your question. – Mohan Radhakrishnan Oct 01 '18 at 09:46
  • The code is a bit confusing (what is `img_num`? why `img_list = img_list * 4`? is `parse_function` and `_parse_function` the same?), but anyway it cannot work as you intend, `filename` is a tensor, so `os` functions will not work. Have a look at [`list_files`](https://www.tensorflow.org/api_docs/python/tf/data/Dataset#list_files). You can have a dataset with all the file names in a directory, [`shuffle`](https://www.tensorflow.org/api_docs/python/tf/data/Dataset#shuffle) it, [`take`](https://www.tensorflow.org/api_docs/python/tf/data/Dataset#take) the top four and then do the decoding. – jdehesa Oct 01 '18 at 10:36
  • @MohanRadhakrishnan thanks! I will check it out – Ra.Bear Oct 01 '18 at 11:20
  • @jdehesa oh sorry. `img_num` is equal to 4 here meaning selecting 4 `images.img_list = img_list * 4` is ensuring the directory has 4 images at least. – Ra.Bear Oct 01 '18 at 11:23
  • @jdehesa But i still don't figure it out how to decode my diretory name since I can't use `os` function – Ra.Bear Oct 01 '18 at 11:57

0 Answers0