Timo_v:

train_ds = train_ds.shuffle(20000)


Okay.
The main thing to remember here is that shuffle runs in memory. So this loads all 20000 images into memory.



 Timo_v:

melanoma_ds.skip(tmp_start).take(batch_each_class)
...
both_portions_ds.shuffle(5000)



Remember that .skip still loads all the data it just throws the first N on the floor.
And that it still has to load at least 5k images before it returns the first batch.



 RZK:

If so, you can use ImageDataGenerator to load and shuffle the data for you.


Yes, if you have loose image files. But prefer tf.keras.utils.image_dataset_from_directory 3.
If you don’t have directories of image files … what do you have? Where are these melanoma_ds and no_melanoma_ds coming from?