How to rescaling when using `image_dataset_from_directory`

The module tf.keras.preprocessing is deprecated so I’m using tf.utils instead, in particular tf.utils.image_dataset_from_directory. But I don’t see a convenient way to rescale the pixels to [0, 1]. In tf.keras.preprocessing.image.ImageDataGenerator , this is easily done by train_datagen = ImageDataGenerator(rescale=1/255) .

Hi, Roger.

Welcome to the DLS Courses, although it sounds like you’ve been working your way through them already. You posted this in the DLS Course 1 category, but TF doesn’t get introduced until C2. But this question has more of a “C4” flavor to it …

The output of image_dataset_from_directory is a, which is basically an “iterator”. You can then apply further functions to that dataset. One of the links on the second link you gave above is to this tutorial that shows how to do that and also mentions tf.keras.layers.Rescaling.

1 Like

There is also an implementation of essentially the same idea you want to implement in the Transfer Learning with MobilNet assignment (C4 W2 A2). Watch how they use image_dataset_from_directory to create the datasets and then apply the normalize function to them.

Of course one caveat is that the courses are using TF version 2.3.0, since they were published over a year ago. So you may find that things have mutated a bit in TF since then. Or you can install the old version and then everything you see in the notebook will be applicable. Note that it’s also possible to find version specific documentation for TF with a little care.

Yes! That does the trick. Thank you.