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.