# Augmentation

# Augmenting your input in TensorFlow

One good way to improve the performance of your neural network is by using augmentations. There are several ways to augment images, so I will discuss some of them in this article. First of all it would be nice to have some images we can apply our augmentations on. For this article I will use the cifar10 dataset, as it is included with Keras, is small to download, and has three image channels.

**In [10]:**

**In [11]:**

```
<TensorSliceDataset shapes: (32, 32, 3), types: tf.float64>
```

**In [12]:**

## How to augment There are multiple ways which are normally beneficial to your training algorithm. Think about rotating and flipping the images, or changing the colours. Luckily TensorFlow already has many of these algorithms included:

**In [13]:**

```
<matplotlib.image.AxesImage at 0x7fe5ba968a20>
```

**In [14]:**

```
<matplotlib.image.AxesImage at 0x7fe5ba8cab00>
```

### Augmenting the color spaces

**In [18]:**

**In [22]:**

```
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
```

Hopefully you can still recognize the image, although the colours are jumbled a
bit.
One thing you can already see is that it’s possible that your image pixel values
are outside of the normal range. To fix this you can call the function
`tf.clip_by_value(tensor, 0, 1)`

.

## Applying the augmentations on your `tf.data.Dataset`

Now that you know what functions are available the next step is to map these
functions to your dataset. The Dataset object itself supports this with it’s map
function. However, you need to wrap the function you map inside a new function
which contains all your parameters.

If you get a message like `InvalidArgumentError: Length for attr 'output_shapes' of 0 must be at least minimum 1`

it means that you
forgot to return the tensor after applying an operation.

**In [8]:**

You can even make the code a bit shorter and use the function directly, but then you have to map it into a lambda function.

**In [9]:**

Now you can also stack functions on top of each other to get super- augmentations:

**In [23]:**

**In [24]:**

Images are looking great now, but the amount of augmentations is maybe a bit too much now.

If you get the error `TypeError: 'Tensor' object is not callable`

you did not
wrap the functions in a lambda/callable function.

**In [37]:**

**In [41]:**

As you can see images are looking a bit more recognisable now.

**In [None]:**