Best way to load images from directory if numpy and cv2 preprocessing is required

Viewed 93

I'm currently trying to use tf.keras.preprocessing.image_dataset_from_directory to create a dataset but I'm running into a couple of problems

  • I can't insert a numpy/cv2 preprocessing step before conversion to tensors. I can use tf.data.Dataset.map but that means I have to unbatch, convert to numpy array, do the preprocessing ... and so on. I tried this but I keep running into little bugs that are fixable, but I'm getting the feeling it wasn't designed to be used this way.
  • I don't have much control over the way the train/val split is done. I want to do do a particular KFold splitting.

FYI I'm used to doing this in PyTorch via custom Datasets and DataLoaders.

1 Answers

You may use preprocessing_function argument, in tf.keras.preprocessing.image.ImageDataGenerator to pass your preprocessing function to the generator in order to apply on each sample.

Reference: Image Data Generator

Related