liveBook · Manning

Rotation

As explained in Deep Learning with PyTorch, Second Edition

A data augmentation transform that turns an image or volume around an axis, here constrained to the X-Y plane for CT data.

Rotation augments CT training data by turning an image or volume around the head-foot axis. The transform changes orientation while keeping the sample representative, helping create useful variation without inventing unrealistic anatomy. section 14.6.1

Listing 14.16 Rotating an image
if 'rotate' in augmentation_dict:
  angle_rad = random.random() * math.pi * 2
  s = math.sin(angle_rad)
  c = math.cos(angle_rad)

  rotation_t = torch.tensor([
    [c, -s, 0, 0],
    [s, c, 0, 0],
    [0, 0, 1, 0],
    [0, 0, 0, 1],
  ])

  transform_t @= rotation_t

The rotation branch samples an angle from 0 to 2π, builds an X-Y rotation matrix with sine and cosine, and multiplies it into the affine transform before resampling. section 14.6.1

How Deep Learning with PyTorch, Second Edition covers it

How deep it goes
Implements random 0–2π X-Y rotation with a 4×4 affine matrix, then resamples through PyTorch grid functions.
Examples
Uses a positive nodule sample and a runnable rotation branch that constructs a sine-cosine matrix.
What it assumes
Assumes CT chunks and tensors, while demonstrating the affine matrix construction needed for rotation.
What you can do after
Apply it via PyTorch affine_grid and grid_sample to rotate CT candidates during augmentation.
How it connects
Connects data augmentation with affine transformations, CT tensors, affine_grid, and grid_sample.

Also explained in

Compare how 4 books explain rotation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with PyTorch, Second Edition. The section links point to the parts of the book it draws on.