Search code examples
kerasconv-neural-networkdata-augmentation

Keras RandomRotation layer produces black lines?


I'm trying to augment my image dataset with Keras RandomRotation. Here's the code:

  data_augmentation = tf.keras.Sequential([
      keras.layers.experimental.preprocessing.RandomRotation(
         0.5,
         fill_mode='reflect',
         interpolation='bilinear')
      ])
  im = data_augmentation(valid_images[0:1])[0]
  plt.imshow(im)
  plt.show()

enter image description here

Unfortunately, the image produced contains black lines. I do like the reflect fill mode, so I want to keep that. Could you suggest what I can get rid of the black lines and produce smoother image? Could the same be done with Numpy? I tried setting interpolation='nearest', but that didn't help.


Solution

  • Using scipy.ndimage.rotate:

    import tensorflow as tf
    from PIL import Image
    import numpy as np
    import matplotlib.pyplot as plt
    import scipy
    
    img = np.array(Image.open("s.jpg"))
    im = scipy.ndimage.rotate(img, 30, mode='reflect', reshape=False)
    plt.imshow(im)
    plt.show()
    

    Outputs:

    enter image description here