0

I have been using the function mentioned here to add different types of noise (Gauss, salt and pepper, etc) to an image.

However, I am trying to build an input pipeline using tf.data.Dataset. I think I have figured out how to add Gaussian and Poisson noise:

@tf.function
def load_images(imagePath):   
   label = tf.io.read_file(base_path + "/clean/" + imagePath)   
   label = tf.image.decode_jpeg(label, channels=3)   
   label = tf.image.convert_image_dtype(label, dtype=tf.float32)

   image=label+tf.random.normal(shape=tf.shape(label),mean=0,stddev=0.1**0.5) #Add Gauss noise
   #image=label+tf.random.poisson(shape=tf.shape(label),lam=0.3) #Add Poisson noise

   return (image, label)

How could I add salt & pepper and speckle noise, as well?

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
aDav
  • 41
  • 8

1 Answers1

1

Here's a way to do the random salt and pepper augmentation:

from sklearn.datasets import load_sample_image
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np

china = load_sample_image('china.jpg')[None, ...].astype(np.float32) / 255

def salt_and_pepper(image, prob_salt=0.1, prob_pepper=0.1):
    random_values = tf.random.uniform(shape=image[0, ..., -1:].shape)
    image = tf.where(random_values < prob_salt, 1., image)
    image = tf.where(1 - random_values < prob_pepper, 0., image)
    return image

ds = tf.data.Dataset.from_tensor_slices(china).batch(1).map(salt_and_pepper)

result_image = next(iter(ds))

plt.imshow(result_image[0])
plt.show()

enter image description here

Nicolas Gervais
  • 33,817
  • 13
  • 115
  • 143
  • I get an error at tf.random.uniform(shape=image[0, ..., -1:].shape). Could you please explain the idea behind it? It works when using shape=tf.shape(image), but I think this is not correct. – aDav Feb 06 '22 at 18:07
  • What error do you get? I'd like to reproduce your error so I can fix it. The reasoning is that an image has three channels, and so I need to make an array of random values of 1 channel so every pixel can be turned to 0 or 1. Otherwise the [R, G, B] values will be turned to 0 or 1 independently and that's just going to distort, not make the salt and pepper effect. – Nicolas Gervais Feb 06 '22 at 18:33
  • Actually, I just ran it for the china.jpg sample it works like a charm! So it must be something wrong with my dataset shape. Thank you! Could you please also give me a hint what are the 3 dots "..."? I've never seen them before and I can't find any documentation about it. – aDav Feb 06 '22 at 20:50
  • Ok, I think I got it. shape=image[0, ..., -1:] is equivalent to shape=image[0, :, :, -1:] I wasn't aware of this. – aDav Feb 07 '22 at 07:55
  • Is it working now? – Nicolas Gervais Feb 08 '22 at 13:25
  • 1
    Yes, it works perfectly, thank you! I just had to modify the code accordingly to work for my batch size. – aDav Feb 09 '22 at 14:54