Kornia Tensors for Vision
Classical vision ops rebuilt as differentiable tensor functions, so geometry and color steps train inside the model.
Why Does This Exist?
Augmentation and geometry usually live on the CPU outside the model, which splits the pipeline and stops gradients at the border. Kornia moves those ops onto tensors: flips, warps, color shifts and filters as batch functions on device, all differentiable. PyTorch models then train through their own preprocessing, and augmentation becomes part of the graph instead of a pre-step. Geometric ops keep batch shape throughout.
This page covers the tensor-op contract, one flip fixture, and appropriate use.
Think of It Like This
A kitchen built into the dining table
Guests used to walk to a separate kitchen for seasoning. Now the table cooks: burners, spices and scales sit in the tabletop, and every dish seasons where it is served.
Kornia is the cooking table. Tensors never leave the device, seasoning is the augmentation, and gradients taste every step. The analogy stops at fire: tables cannot hold every kitchen tool, and ops needing CPU libraries still belong outside.
How It Actually Works
Input is a channels-first batch on device. Output is the same-shaped batch transformed, with gradients flowing when requested. Purpose is on-device, differentiable preprocessing and augmentation inside training. Appropriate use is GPU training loops that need synced, seeded, batched geometry and color. Match the tensor layout exactly: channels-first in, channels-first out, with no silent permutation.
Flip fixture
Take row in a one-row batch. A horizontal flip returns , the same mirror as CPU code, but the tensor stays on device and the backward pass stays connected. The diagram draws that row with its gradient arrow intact.
Code
Flip in plain Python, the rule the tensor op applies per row:
def hflip(row: list[int]) -> list[int]: return row[::-1] # mirror left to right
print(hflip([1, 2, 3])) # -> [3, 2, 1]print(hflip([3, 2, 1])) # -> [1, 2, 3]Mirror twice returns home, which is the invertibility check to run on device too.
Watch Out For
Feeding channels-last images into channels-first ops
Kornia expects the tensor layout its docs state, usually channels-first. The symptom is square images that run fine with scrambled colors and stretched nonsquare ones. Fix it by asserting the four-dim shape at the boundary and permuting once on entry.
The Quick Version
- Kornia maps device batches to transformed batches with gradients intact.
- Row 1, 2, 3 flips to 3, 2, 1 without leaving the device.
- Use it for on-device augmentation and geometry inside training.
- Keep channels-first layout at every boundary.
- Double-flip home as the wiring check.