Skip to content
Free public beta until December 4, 2026: every course is open, no card needed. After that, the first chapter of each course stays free and full access is a one-time $39 ($19 for up to 100 founding members chosen for helpful feedback). Your progress is kept. How the beta works

Images as tensors: shape, channels, and batches

~12 min

digital-image-processing already settled what a single image is mathematically. It is a matrix, or — once color enters the picture — a rank-3 tensor. Every technique in that course worked on one image at a time. A vision model almost never does. It trains and predicts on many images at once; Chapter 11 explains why in full. This section adds the one piece of bookkeeping that changes: a new axis, tacked onto the same tensor, that counts how many images are along for the ride.

Picture a single photo as a big spreadsheet. There's a row for every pixel row and a column for every pixel column. For a color photo, stack three of these spreadsheets back to back: one records red, one records green, one records blue. digital-image-processing calls that whole stack a tensor. It calls the three numbers describing its size — height, width, three channels — its shape.

Now suppose you don't feed a vision model just one photo. You hand it a whole box of 32 photos at once, all the same size, and ask it to look at all 32 together. The natural way to store that is to line the 32 stacks up side by side. Then you add one more number out front, counting how many photos are in the box. Say each photo is 224 by 224 pixels (224\times224, the same "times" as ordinary multiplication — 224 pixels tall, 224 pixels wide) with 3 color channels. The whole box then has shape (32, 224, 224, 3). That leading 32 is the batch size. Chapter 11 explains why models train on batches rather than one photo at a time. This section only owes you the shape bookkeeping: one new number in front, counting boxes. The numbers describing one individual photo — height 224, width 224, channels 3 — don't change at all.

Here's the part that trips almost everyone up once. Two vision libraries can store the exact same box of photos with that 32 in a different spot. One writes (32, 224, 224, 3): batch, height, width, channel. This order is called channels-last, since the "3" for color sits at the end. The other writes (32, 3, 224, 224): batch, channel, height, width. This order is called channels-first. Neither library is doing different math. Neither is storing different pixel values. They just wrote the same four numbers in a different order. It's the exact same kind of convention as a spoken "1920 by 1080" versus a printed .shape reading "(1080, 1920)" — something digital-image-processing's own readiness chapter already warned about. Know which convention you're reading, and nothing about it is mysterious.

A tensor is a block of numbers indexed by some fixed number of integers. A scalar needs zero indices. A vector \mathbf v \in \mathbb R^n needs one. A matrix M \in \mathbb R^{m\times n} needs two. A rank-3 tensor needs three. digital-image-processing fixed a single color image as a rank-3 tensor of shape (H, W, C): height H, width W, and C=3 color channels (red, green, blue, in that order). Fixing the channel index and letting the other two vary recovers one ordinary H\times W matrix — one channel's worth of the image.

A vision model rarely trains or predicts on one image in isolation. Instead it processes a batch of N images at once. Chapter 11 explains the design choice behind this: a single noisy image's gradient is a poor training signal, and averaging over a batch smooths it out. The batch is built by stacking N separate (H, W, C) tensors along one new leading axis. That produces a single rank-4 tensor of shape (N, H, W, C), with N the batch size. Fixing the first index to some value n and letting the rest vary recovers exactly one image out of the batch, back to an ordinary (H, W, C) tensor. Batching stacks whole images together without touching what any single image looks like.

This axis order, (N, H, W, C), is called channels-last, since the channel axis C sits last. It is one working convention, not a mathematical law. A different library may instead store the identical numbers as (N, C, H, W), channels-first, with the channel axis moved right after the batch axis. Both tensors hold the exact same pixel values in the exact same spatial arrangement. Only the order the axes are listed in differs — a bookkeeping decision a specific software library made. digital-image-processing's own readiness chapter flagged this same kind of convention for row-major array indexing. It is not a property of the image, or of the mathematics, at all. Reading a shape tuple correctly means knowing which convention is in play, the same way knowing (H,W) vs. (W,H) mattered for a single image.

Quick review

Practice from earlier coursework, pulled in for this topic.

From Linear Algebra — Vectors, Matrices, and the Geometry of Data

In the notation a_{ij}, which index gives the column number?

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

i is the first subscript, and by convention the first subscript is always the row index, not the column.

Each subscript has a distinct, fixed job: i is the row, j is the column. They aren't combined to name one location together.

a_{ij} specifically means 'the entry in row i, column j' — it's precise location notation, not a vague reference.

Why

The convention is row-then-column: a_{ij} is the entry in row i (first subscript) and column j (second subscript).

From Digital Image Processing

Which best explains why nearly every technique in this course is developed first for a single grayscale matrix?

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

Color images are the common case in practice, not a rare exception -- that's not the reason.

The underlying arithmetic (convolution, thresholding, transforms) is identical per channel -- nothing about the math itself changes for color.

Resolution and channel count are unrelated properties of an image.

Why

A rank-3 color tensor decomposes into three ordinary matrices sharing one shape, so a grayscale-matrix technique extends to color by simply running it once per channel.

One image out of a batch, still an ordinary RGB tensor

This is ONE image -- a 3x3 RGB tensor, shape (3, 3, 3). A batch of 4 such images would stack four of these into shape (4, 3, 3, 3), with no change at all to what any single image (this one included) looks like. Click a cell and read off its (R, G, B) vector.

Loading interactive…

Worked example

Reading a batch tensor's shape

  1. 1

    A vision model loads a batch tensor of shape (16, 64, 64, 3), channels-last. Identify the batch size, height, width, and channel count. The batch axis is always first in a channels-last tensor; the other three match a single image's own (H, W, C) shape exactly.

  2. 2

    Batch size N=16 (sixteen images), height H=64, width W=64, channels C=3. That's the same (H,W,C)=(64,64,3) a single one of these images would have on its own. There's just one new leading axis, counting how many images are stacked together.

Loading interactive…

Worked example

Picking one image back out of a batch

  1. 1

    A channels-last batch tensor batch has shape (16, 64, 64, 3). What does batch[3] return, and what is its shape? Fixing only the first (batch) index to a single value leaves the other three axes untouched.

  2. 2

    batch[3] is the fourth image in the batch (index 3, counting from 0). It's an ordinary single-image tensor of shape (64, 64, 3), identical in kind to any image digital-image-processing ever processed on its own. Fixing one index out of four removes exactly that one axis. That's the same rule Chapter 0 of digital-image-processing used to recover a single color channel from an (H,W,3) tensor.

Loading interactive…

Where this goes next

Vision-model training pipelines

Chapter 11 (Neurons, training, and CNNs for images) explains why models train on batches rather than one image at a time. In brief: a single image's own training signal is noisy. Averaging it over a batch of, say, 32 images gives a more reliable direction to adjust a model's weights in. This section's shape bookkeeping is the prerequisite for that chapter. Every tensor a vision-model training loop touches carries this same added batch axis. Reading a shape correctly means knowing which number is the batch size, and which convention — channels-first or channels-last — a specific library expects. Getting this wrong is a real, constant source of bugs once code enters the picture. It is not a pedantic detail.

Check your understanding (counts toward your progress)

A single color image has shape (H, W, 3). Stacking 8 such images into one batch tensor gives a shape of...

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

The batch axis is a NEW leading axis, not appended at the end -- the channel axis stays last in a channels-last tensor.

Stacking images doesn't multiply the channel count together -- each image keeps its own separate 3 channels, it doesn't merge into 24.

Dropping the channel axis entirely would lose every pixel's own color information -- batching adds an axis, it never removes one.

Why

Batching adds one new leading axis counting how many images are stacked; every other axis (height, width, channels) is untouched.

A channels-last batch tensor has shape (24, 32, 32, 3). How many total numbers does it store?

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

Why

Multiply every axis length together -- 24 \times 32 \times 32 \times 3 = 73{,}728, the same "multiply the shape" rule a single image's own pixel count already used.

Correct answer

Also accepted:

Why

A channels-first tensor stores the identical batch of images as a channels-last tensor would. What is actually different between the two?

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

Both conventions store the exact same number of values -- one per pixel per channel per image -- just organized with the axes in a different order.

Batch size is a property of how many images are being processed, unrelated to which axis order a library chooses.

No pixel value changes at all -- this is purely a bookkeeping choice about which axis is listed where.

Why

Channels-first and channels-last describe the identical data; only the position of the channel axis in the shape tuple differs.

Before any batch axis is added, a single RGB image is, mathematically, a...

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

A scalar is one lone number -- an entire color image needs far more than one value.

A vector needs only one index to name an entry -- a color image needs three (row, column, channel).

Rank 4 only appears once a batch axis is added on top of the existing three -- a single image alone is still rank 3.

Why

A single color image needs three indices (row, column, channel) to pin down one value, which is the definition of a rank-3 tensor.

For a channels-last batch tensor batch of shape (10, 28, 28, 3), what does batch[4] return?

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

Only the batch index was fixed here -- the spatial and channel axes are still completely free, so far more than one pixel's worth of data comes back.

Fixing only one index out of four leaves three axes free to vary -- this is not the same as fixing every axis down to one pixel.

Fixing one index out of four DOES reduce the object -- the batch axis is gone, leaving an ordinary single image behind.

Why

Fixing the batch index alone removes that one axis, leaving an ordinary (H, W, C) image tensor -- exactly the shape a standalone image already has.

In a channels-last batch tensor of shape (N, H, W, C), which single number is the batch size?

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

C is the channel count (3 for RGB), a property of one image, not how many images are stacked.

H is a single image's own height -- it says nothing about how many images are in the batch.

The batch size is one plain number read directly off the shape, not a product of two axes.

Why

The batch axis is defined as the FIRST entry of a channels-last shape tuple -- N by itself.

Practice: Reading a batch tensor's shape (4 problems · doesn’t affect your progress)

In your own words, what does the "batch" axis on an image tensor actually count?

A single grayscale image has shape (H, W). What shape does a batch of 5 such images have?

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

Why

The batch axis is added as a new leading axis, ahead of the image's own existing axes.

(5, H, W)

A channels-last batch tensor has shape (12, 40, 40, 3). What is the height of each individual image?

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

Why

In a channels-last shape (N, H, W, C), the height is the second entry -- 40.

Correct answer

Also accepted:

Why

40

A channels-last batch tensor of shape (N, 50, 50, 3) stores 37{,}500 total numbers. What is N?

Correct! 🎉 Not quite.

Right on try — credit. Right — the answer had already been shown, so no credit this time. Next try is worth Answer shown after three tries — this one no longer earns credit. You chose to see the answer — this one earns no credit.

Why

50\times50\times3=7{,}500 per image; 37{,}500/7{,}500=5 images in the batch.

Correct answer

Also accepted:

Why

5

Feeling good about this section?

Take the section quiz

Every answer gets a full explanation at the end.

Graph settings

Calculator

Sign in to save highlights

Insert an equation

  • No matches.