liveBook · Manning

Noise

As explained in Deep Learning with JAX

Unstructured pseudo-random perturbation added to training examples, here used as an image augmentation.

Random noise is an unstructured pseudo-random perturbation added to an image to create training variations. In image classification, this augmentation increases dataset diversity and can act as implicit regularization against overfitting. section 9.1

A JAX implementation follows a small, practical pipeline:

  • Create a PRNG key from a seed, then generate Gaussian values with jax.random.normal() using the image shape. section 9.1.2
  • Scale and shift the standard Gaussian tensor, add it to the image, and normalize the result back to [0.0, 1.0]. section 9.1.2
  • Split keys when selecting an augmentation and generating its noise; reusing one key can produce correlated results. section 9.1.3

Noisy image

For an image shaped (200, 200, 3), generate noise with jax.random.normal(key, image.shape), compute noise = 0.5 + 0.1*std_noise, add it to the image, and normalize the result. The same idea can be demonstrated with scikit-image's random_noise(img, mode='gaussian'). section 9.1.2 section 3.1.3

Listing 9.8 Performing a random augmentation
def add_noise_func(image, rng_key):                             #1
  noise = 0.5 + 0.1*jax.random.normal(rng_key, image.shape)
  new_image = image + noise
  new_image = (new_image - new_image.min())/(
    new_image.max() - new_image.min())
  return new_image
 
def horizontal_flip_func(image, rng_key):                       #2
  return jnp.fliplr(image)
 
augmentations = [                                               #3
   add_noise_func,
   horizontal_flip_func
]
 
def random_augmentation(image, augmentations, rng_key):         #4
   key1, key2 = jax.random.split(rng_key)                       #5
   augmentation_index = jax.random.randint(
       key=key1, minval=0, maxval=len(augmentations), shape=()) #6
   augmented_image = jax.lax.switch(
       augmentation_index, augmentations, image, key2)          #7
   return augmented_image
 
key = jax.random.PRNGKey(4242)                                  #8
 
img = random_augmentation(image, augmentations, key)            #9
 
plt.imshow(img)
#1 Function for generating a noisy image
#2 Function for performing a horizontal flip
#3 A list of augmentations
#4 Function for doing a random augmentation
#5 Splits a key into two keys
#6 Randomly chooses one of the augmentation functions using the first key
#7 Applies the chosen function to an image and the second key
#8 Initializes a key with some seed
#9 Calls the whole pipeline for random augmentation

Shows noise generation, normalization, and random selection between noise and horizontal flipping. section 9.1.3

How Deep Learning with JAX covers it

How deep it goes
Explains Gaussian generation, scaling, addition, normalization, PRNG keys, and key splitting, but defers deeper PRNG internals.
Examples
Demonstrates noisy photographs, image-shaped Gaussian tensors, normalized noisy images, and random choice between noise and horizontal flipping.
What it assumes
Assumes an image array and training dataset, while teaching PRNG keys, Gaussian sampling, image-shaped tensors, and key splitting.
What you can do after
Apply it via JAX or scikit-image to generate normalized noisy images and compose noise with other augmentations.
How it connects
Connects data augmentation with image classification, Gaussian distribution, PRNGs, normalization, horizontal flipping, and implicit regularization.

Also explained in

Compare how 11 books explain noise →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with JAX. The section links point to the parts of the book it draws on.