Data augmentation: new views of an example
Read these first if they are new to you
- Rotating points with a matrix Basic
- Random draws: chance you can repeat Basic
- Shapes as point clouds Intermediate
- The bell curve (normal distribution) Basic
The idea in one paragraph
Data augmentation makes new training examples from old ones by changing them in ways that do not change the right answer. For a point cloud, the usual changes are a random rotation of the whole cloud and a small random nudge, called jitter, for each point. A model trained on these copies sees the same shape facing many ways and slightly blurred. It learns that direction and tiny noise do not matter for the answer. Choose only changes that truly leave the answer the same.
A model learns whatever its examples have in common
A machine-learning model is a program whose settings are learned from training examples: inputs paired with the right answers. It picks up any pattern that helps it give those answers, including patterns you did not intend.
Suppose you train a model to recognize chairs, and every chair in your examples faces the same way. The model may learn "a chair has its back on the left." Shown a chair turned around, it fails. It never saw evidence that the facing direction does not matter.
Data augmentation supplies that evidence. Each time the model sees an example, you first change the example at random, in a way that keeps the answer the same. A chair turned around is still a chair. Over many passes, the model sees each chair facing many ways and learns to ignore the direction.
For point clouds: rotate the whole cloud, then jitter each point
Two changes are common for a point cloud.
- A random rotation. Draw one random angle (see random draws) and turn every point by it, using the same rotation matrix. A rotation keeps every distance between points, so the shape itself does not change.
- Jitter. Add a small random offset to each point separately. Each offset is drawn from a bell curve centered on zero, with a small spread such as 0.01 of the shape's size. Programs often clip each offset, for example to at most 0.05, so that no point moves far.
Take the point p = (2, 0, 1). The random angle is 90° about the z axis.
- A rotation by θ about z maps (x, y, z) to (x cos θ − y sin θ, x sin θ + y cos θ, z).
- With cos 90° = 0 and sin 90° = 1, the point becomes (2 × 0 − 0 × 1, 2 × 1 + 0 × 0, 1) = (0, 2, 1).
- Its distance from the origin was √(2² + 0² + 1²) = √5 ≈ 2.236. It still is: √(0² + 2² + 1²) = √5.
- The jitter draws (0.01, −0.02, 0.005). The final point is (0.01, 1.98, 1.005).
Every other point of the cloud turns by the same 90°, but gets its own jitter.
Before you use the figure, predict: how large can the jitter get before you can no longer read the letter?
The strokes are 0.12 wide. Once the spread is a large share of that width, the strokes blur into each other. A spread of 0.01 changes almost nothing you can see, and that is the point: the model learns not to depend on exact positions. The figure leaves out clipping, so that large spreads show their full effect.
Each change teaches the model to ignore one thing
Choose each change by asking what the answer should not depend on.
| Change | The model learns to ignore | Leave it out when |
|---|---|---|
| Rotate about the vertical axis | which way the shape faces | facing matters, such as "is this arrow pointing north?" |
| Rotate in every direction | all of the shape's orientation | up and down matter, such as "is this chair standing or fallen?" |
| Jitter each point | tiny errors in position | the spread nears the size of the thinnest part that matters |
| Shuffle or resample the points | which points were sampled, and in what order | rarely; see order doesn't matter |
One more rule matters as much as the choice of change. If the answer is itself a shape, apply the same rotation to the input and to the answer. For example, if the model must predict the missing part of a shape, rotate the part you give it and the part it must predict with the same matrix. Otherwise the copies no longer match.
In NumPy, rotation is one matrix product and jitter is one random draw
Each row of cloud is a point. A rotation matrix R turns a column vector p
into Rp. For points stored as rows, the same rotation is the product
cloud @ R.T, with R transposed.
augment.py
import numpy as np
rng = np.random.default_rng(7)
cloud = np.array([[2.0, 0.0, 1.0],
[0.0, 1.0, 0.0],
[1.0, 1.0, 3.0]])
theta = rng.uniform(0, 2 * np.pi) # a random angle about the z axis
c, s = np.cos(theta), np.sin(theta)
R = np.array([[c, -s, 0],
[s, c, 0],
[0, 0, 1]])
rotated = cloud @ R.T # each row is a point, so multiply by R transposed
# np.clip keeps each jitter value between -0.05 and 0.05.
jitter = np.clip(rng.normal(0, 0.01, cloud.shape), -0.05, 0.05)
augmented = rotated + jitter
def pairwise(p): # every distance between two points of p
return np.linalg.norm(p[:, None] - p[None, :], axis=-1)
print(round(np.degrees(theta), 1))
print(np.allclose(pairwise(cloud), pairwise(rotated))) # all equal, up to rounding?
print(np.abs(pairwise(augmented) - pairwise(cloud)).max().round(3))
225.0
True
0.012
The random angle is 225°. The rotation keeps every distance between points exactly, and the jitter changes
them by at most 0.012. Inside pairwise, p[:, None] - p[None, :] subtracts every point
from every other point at once (see broadcasting).
Drawing a random rotation in every direction is harder than it looks. Three random angles, one per axis, do not make all orientations equally likely. Libraries offer a function for a uniformly random 3D rotation; use it. Also, augmentation only encourages a model to ignore a change. It does not guarantee it, so test the trained model on rotated inputs.
Check yourself
-
Rotate the point (1, 2, 5) by 90° about the z axis. Where does it go?
Answer
(1 × 0 − 2 × 1, 1 × 1 + 2 × 0, 5) = (−2, 1, 5). Its distance from the origin stays √30.
-
A model must decide whether a scanned chair is standing up or has fallen over. Should you augment with rotations in every direction?
Answer
No. A rotation in every direction can turn a standing chair into a fallen one, so it changes the right answer. Rotation about the vertical axis alone is safe.
-
A model gets part of a shape and must predict the rest. You rotate the input part by a random angle but leave the part to predict as it was. What goes wrong?
Answer
The two parts no longer fit together, so the model is trained on wrong answers. Rotate both with the same matrix.
-
The thinnest part of your shapes is 0.02 units wide. Is a jitter spread of 0.05 a good choice?
Answer
No. Points would move more than the part is wide, so the thin part would blur away and the model could not learn it. Keep the spread well below 0.02.
Go further
Optional background. This unit covers what you need here.
-
Image Augmentation: Common Image Augmentation Methods Dive into Deep Learning Book chapter Intermediate
The same idea for photos, with runnable code and pictures of flips, crops and color changes.
-
A before-and-after picture for every image augmentation in a widely used library.
Where this shows up
No unit or explanation builds on this one yet.