Changes to Dataset in for loop don't work
Question:
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?
Answers:
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))
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.
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:
- use
tf.py_function
- wrap your function in
tf.autograph.experimental.do_not_convert
- use keras preprocessing layer e.g. https://www.tensorflow.org/api_docs/python/tf/keras/preprocessing/image/random_channel_shift
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?
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))
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.
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:
- use
tf.py_function
- wrap your function in
tf.autograph.experimental.do_not_convert
- use keras preprocessing layer e.g. https://www.tensorflow.org/api_docs/python/tf/keras/preprocessing/image/random_channel_shift