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?
v_dataset
, so when you start the next loop, it's going to use the original, unmodified data.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: stackoverflow.com/questions/48176348/…dataset.take()
.