0

In my project I have dataset of original images and correcponding masks. I want to perform augmentation so it is random, yet same for image and matching mask. For this I have defined keras sequential layer rotation_1 and rotation_2 with same seed. Then I am using it for augmentation using .map on dataset. But it yields different rotation for image and for amtching mask.

Could someone give me an advice why this is not working? Is there some other nice and easy way to do this task differently?

# ROTATION
  rotation_1 = keras.Sequential([
      layers.RandomRotation((-0.2,0.2), seed = 42, interpolation = "nearest" )
  ])

  rotation_2 = keras.Sequential([
      layers.RandomRotation((-0.2,0.2), seed = 42, interpolation = "nearest" )
  ])

# AUGMENTATION
train_dataset = train_dataset.map(lambda x,y: (rotation_1(x), rotation_2(y)),         num_parallel_calls= tf.data.AUTOTUNE)

For plotting and seeing the images I use this function. Is it possible that there is some issue in taking images using take on train_ds?

 def generate_plots(train_ds,n):
  j = 0
  # Plot samples
  for image, mask in train_ds.take(n):
    generated_img = gen(image, training=True)
    generated_img = tf.cast(generated_img > 0, 1, 0)

    images = [image[0], mask[0], generated_img[0]]
    titles = ['Input Image', 'Ground Truth', 'Generated Mask']

    plt.figure(figsize=(10, n*2))

    for i in range(3):
      plt.subplot(n,3,j+i+1)
      plt.imshow(images[i]*0.5 + 0.5, cmap='gray', vmin=0, vmax=1)
      plt.title(titles[i])
    j = j + 3

 generate_plots(train_ds,20)`

I have read that there was some bug in RandomFlip layer but should have been fixed since tensorflow 2.12. (https://github.com/keras-team/keras/issues/17457)

0 Answers0