Search code examples
pythontensorflowdatasettensorflow-datasetsdata-augmentation

Changes to Dataset in for loop don't work


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


Solution

  • 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.