0

I'm trying to augment my dataset by randomly changing the hue of its images within a for loop but the changes do not persist outside of the loop. I imported the dataset with tf.keras.utils.image_dataset_from_directory. The rest of the code looks as follows:


def augment(image, label, counter):
  randNr = tf.random.uniform(shape=(), minval=-1, maxval=1, dtype=tf.dtypes.float32)
  image = tf.image.adjust_hue(image, delta=randNr)
  #desplay some values
  if(counter<1):
    print(randNr)
    plt.figure()
    plt.imshow(image[0].numpy().astype("uint8"))
    plt.show()
  return image, label

temp1 = 0
for image, label in v_dataset:
  image, label = augment(image, label, temp1)
  #desplay some values
  if(temp1<1):
    plt.figure()
    plt.imshow(image[0].numpy().astype("uint8"))
    plt.show()
  temp1 += 1

#display some values
plt.figure(figsize=(10, 10))
for images, labels in v_dataset.take(1):
    print("images shape: ", np.shape(images))
    for i in range(9):
        ax = plt.subplot(3, 3, i + 1)
        plt.imshow(images[i].numpy().astype("uint8"))
        plt.title(int(labels[i]))
        plt.axis("off")
plt.show()


When I print an image the first two times, the hue has changed as intended. When I print out more images later, however, none of them have a variation in hue. Any Ideas on why this occurs and how to fix it? First Plot Second Plot Third Plot

  • You aren't changing anything in `v_dataset`, so when you start the next loop, it's going to use the original, unmodified data. – Djinn Sep 20 '22 at 19:15
  • Ok that makes sense. How would I change it though? Do I need to iterate over the dataset differently somehow? Or is there any way to access specific elements of v_dataset, e.g. "v_dataset.getElement(0)" or "v_dataset[0]"? I tried v_dataset.map(augment) but that applies the same random value everywhere, which would be pointless. – Rumpel Stilzchen Sep 20 '22 at 19:24
  • Usually if you want to change a `tf.Dataset`, you apply whatever operation to the entire dataset because it's not just a data structure holding arrays, but a computational representation of operations too. If you want to do random index augmentations, or specific ones, you should try to do that as you're creating (really before) the dataset. Maybe this could help: https://stackoverflow.com/questions/48176348/tensorflow-how-to-change-dataset – Djinn Sep 20 '22 at 23:18
  • But yes, you can access elements from a dataset using `dataset.take()`. – Djinn Sep 20 '22 at 23:21

3 Answers3

0

You can augment the image inside the model:

inputs = Input(shape=(...))
x = augment_hue(inputs)
...

If you use a Sequential model, use a Lambda layer:

Lambda(lambda x: augment_hue(x))
AndrzejO
  • 1,502
  • 1
  • 9
  • 12
0

I did not find a working solution for this problem. In the end I just modified the data before importing it, using the keras ImageDataGenerator.

0

You could modify dataset by calling dataset.map(...). However, everything inside .map(...) will be compiled, so using random number generator from python will just yield the same result every time (the one which was traced). Your options are:

Goofy_Goof
  • 43
  • 4