# Reading image shapes before doing CNN arithmetic

Subject: Deep Learning

Exam: TEE

Reader: https://deep-learning-study-room.vercel.app/#tee/read/7/tee-c7-s0

Canonical: https://deep-learning-study-room.vercel.app/read/dl/tee/section/tee-c7-s0/

**Definition.** An image or intermediate CNN representation is a tensor indexed by spatial position and channel. This course writes its shape as height × width × channels, excluding the batch dimension.

### Explanation and working

### Height, width and channels

The shape 4 × 5 × 3 means 4 rows, 5 columns and 3 channels. It contains 4 × 5 × 3 = 60 numbers. A grayscale image commonly has one intensity channel; an RGB image has red, green and blue channels. Intermediate CNN channels are learned feature maps, not necessarily colors.

![Count height, width and channels separately. Three channels are stacked at the same spatial positions.](https://deep-learning-study-room.vercel.app/assets/figures/v_image_shape.svg)

Diagram: Count height, width and channels separately. Three channels are stacked at the same spatial positions.. Labels: Read height, width and channels separately | Read height, width and channels separately | The text uses 4 × 5 × 3: four rows, five columns and three channels. | 1 | 2 | 3 | 4 | 5 | 1 | 2 | 3 | 4 | 5 | 1 | 2 | 3 | 4 | 5 | 1 | 2 | 3 | 4 | 5 | 1 | 2 | 3 | 4 | 5 | 1 | 2 | 3 | 4 | 5 | 1 | 2 | 3 | 4 | 5 | 1 | 2 | 3 | 4 | 5 | 1 | 2 | 3 | 4 | 5 | 1 | 2 | 3 | 4 | 5 | 1 | 2 | 3 | 4 | 5 | 1 | 2 | 3 | 4 | 5 | 3 stacked channels | H × W × C | H = 4 rows | W = 5 columns | C = 3 channels | Total entries = 60 | A filter spans the incoming channels. Its spatial width is a different quantity.

Count height, width and channels separately. Three channels are stacked at the same spatial positions. Tap the diagram to enlarge.

### One position versus the whole map

A filter placed at one location computes one output value. Sliding that same filter over all valid locations creates one feature map. A second filter creates a second feature map. Therefore the number of filters equals the number of output channels.

### Why filter depth matches input channels

A 3 × 3 filter applied to a 3-channel input has 3 × 3 × 3 = 27 weights. At one location, it combines 27 input values into one sum. A single bias is then added for that filter. It does not produce three separate output channels just because the input has three channels.

### Reading a complete shape calculation

The spatial-size formula computes height and width. It does not compute the channel count.



$$
N_{\mathrm{out}}=\left\lfloor\frac{N+2P-K}{S}\right\rfloor+1
$$



N = current input size; P = padding on one side; K = filter size; S = stride. The floor symbol means keep only complete placements. Add one because the initial placement is also an output position.

### Shape, parameters and computation are three different counts

**Shape** counts the arrangement of output values. **Parameters** count learned weights and biases. **Computation** counts how many arithmetic operations are performed. Reusing the same filter at 100 positions creates 100 outputs but does not create 100 new sets of weights.

In a multi-layer question, write the output shape of a layer first. That becomes the next layer's input shape. This is why the second convolution uses the first convolution's channel count, not the original image's channel count.
