Batching image data based on timestamp contained in filename

I have a dataset of images in keras-style:

main_directory/
...class_a/
......a_image_1.jpg
......a_image_2.jpg
...class_b/
......b_image_1.jpg
......b_image_2.jpg

I create a dataset object by calling

tf.keras.preprocessing.image_dataset_from_directory(train_dir,
                                             shuffle=False,
                                             label_mode='categorical',
                                             batch_size=hyperparameters["BATCH_SIZE"],
                                             image_size=IMG_SIZE)

Then i unbatch it either by applying tf.data.Dataset.unbatch() or tf.data.Dataset.flat_map(lambda x,y: tf.data.Dataset.from_tensor_slices(x))

I need to batch this data in windows of “n” temporally-consecutive elements (for ease, let’s say 3 elements, although the exact number isn’t decided yet). The images come from a set of videos, so i want batches of 3 images, each image 1 second apart. If there’s more than 1 second between at least 2 consecutive frames in the window, i want to drop that whole window
I’ve been trying to figure out how to do it, and have tried a number of approaches, but i have been unable to do what i need
I tried:
use the tf.data.Dataset API and use the .window() method. This creates the dataset of windows, but resulting dataset loses the .file_paths property, meaning there is no way for me to check whether the elements in a window belong together or not
use the .map() function, didn’t manage to make it work with the dataset i have

I have also converted my dataset to TFRecord format, using this script, but i don’t really understand how to load the data and batch it

any help is greatly appreciated

Hi @ghylander

Welcome to the TensorFlow Forum!

Please share minimal reproducible code to replicate and understand the issue better? Thank you.

Hi,

Frankly, it’s been 2 years since I posted this issue, it’d be very hard to trace back at what exactly I was attempting back then.

Iirc correctly, I had a directory with images and wanted to group them together based on information stored in the filename.

I created these groups using the Dataset() object built in method window(), but this method removes (or used to remove, back then) the file_paths property from the images (which I needed).

I think I finally managed to solve this using a complex data pipeline with the tf.data API.

1 Like