Create New Training Data with Rotations

Explore how to create new training data by rotating the images.

Extend the training dataset

If we think about the MNIST training data, we realize that it is quite a rich set of examples for how people write numbers.

The neural network has to learn as many of these variations as possible. It does help that there are many forms of the number 44 in there. Some are squished, some are wide, some are rotated, some have an open top and some are closed.

Wouldn’t it be useful if we could create more such variations as examples? How would we do that? We can’t easily collect thousands of additional examples of human handwriting. We could, but it would be very laborious.

One idea is to take the existing examples, and create new ones from them by rotating the images clockwise and anticlockwise—for example, by 10 degrees. For each training example, we could have two additional examples. We could create many more examples with different rotation angles, but for now, let’s just try +10+10 and 10–10 degrees to see if the idea works.

Python’s many extensions and libraries come to the rescue again. The ndimage.interpolation.rotate() function can rotate an array by a given angle, which is exactly what we need. Remember that our inputs are a one-dimensional long list of 784, because we’ve designed our neural networks to take a long list of input signals. We’ll need to reshape that long list into a 28×2828\times28 array so we can rotate it, and then unroll the result back into a 784 long list of input signals before we feed it to our neural network.

The following code shows how we use the ndimage.interpolation.rotate() function, assuming we have the scaled_input array from before:

Get hands-on with 1200+ tech skills courses.