title: TF2 Data Augmentation Randomness
body: <p>I've been trying to add custom augmentation to a model. However, while library functions such as <code>tf.image.random_flip_left_right</code> or <code>tf.image.random_brightness</code> actually work and create random variations for every image in every sample, my custom functions always have the same effect for ALL images in ALL batches.</p>
  
  <p>For instance, these are my functions for "random" gaussian and motion blurs:</p>
  
  <pre><code>@tf.function
  def gaussian(image, n_channels, dtype):
    size = np.random.randint(2, 6)
    sigma = np.random.uniform(0.5, 4.0)
    x = tf.range(-size // 2 + 1, size // 2 + 1, dtype=dtype)
    g = tf.math.exp(-(tf.pow(x, 2) / (2 * tf.pow(tf.cast(sigma, dtype), 2))))
    g_norm2d = tf.pow(tf.reduce_sum(g), 2)
    k = tf.tensordot(g, g, axes=0) / g_norm2d
    k = tf.expand_dims(k, axis=-1)
    k = tf.expand_dims(tf.tile(k, (1, 1, n_channels)), axis=-1)
    return tf.nn.depthwise_conv2d(image[None], k, [1,1,1,1], 'SAME')[0] 
  
  @tf.function
  def motion(image, n_channels, dtype):
      size = np.random.randint(2, 11)
      k = np.zeros((size, size))
      k[int((size-1)/2), :] = np.ones(size)
      k = k / size
      k = tf.convert_to_tensor(k, dtype=dtype)
      k = tf.expand_dims(k, axis=-1)
      k = tf.expand_dims(tf.tile(k, (1, 1, n_channels)), axis=-1)
      return tf.nn.depthwise_conv2d(image[None], k, [1,1,1,1], 'SAME')[0] 
  </code></pre>
  
  <p>As I said, my images get random brightness, flip, etc. But they ALL get the same motion and Gaussian blur, which is not what I wanted.</p>
  
  <p>EDIT: To clarify, I call all augmentation functions sequentially in an <code>augment(image, label)</code> function, which is called via <code>dataset.map(augment)</code>. This works just fine for the other augmentations.</p>