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.
<TensorSliceDataset shapes: (32, 32, 3), types: tf.float64>
## 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:
<matplotlib.image.AxesImage at 0x7fe5ba968a20>
<matplotlib.image.AxesImage at 0x7fe5ba8cab00>
Augmenting the color spaces
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
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
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.
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.
Now you can also stack functions on top of each other to get super- augmentations:
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.
As you can see images are looking a bit more recognisable now.