Multiple Input and Multiple Output Channels

Dive into Deep Learning · §6.4

Multiple Input and Multiple Output Channels
multi-channel kernels · feature maps · compute & memory scaling · 1×1 bottlenecks

Why Vision Networks Need Multi-Channel Tensors

Motivation & The Quadratic Channel Scaling Wall

  • Multi-channel inputs (c_\textrm{i}): RGB images carry 3 channels per pixel; hidden stages carry 64\text{--}512 parallel feature maps (c_\textrm{o}).
  • The c_\textrm{i} c_\textrm{o} scaling wall: Because every output channel connects to every input channel, a single 256 \to 256 3 \times 3 layer demands 590{,}080 parameters and 1.85 \times 10^9 MACs at 56 \times 56.
  • Solution preview: 1 \times 1 bottleneck projections cut both by 8.5\times.

Cross-Correlation with Multiple Input Channels

One Spatial Filter Slice per Input Channel, Then Sum

When the input \mathsf{X} has c_\textrm{i} channels of shape (c_\textrm{i}, n_\textrm{h}, n_\textrm{w}), a single output channel’s kernel \mathsf{K} must have shape (c_\textrm{i}, k_\textrm{h}, k_\textrm{w}). We cross-correlate each channel \mathbf{X}_c with its filter slice \mathbf{K}_c and sum across all c_\textrm{i} channels:

\mathbf{Y} = \sum_{c=1}^{c_\textrm{i}} \mathbf{X}_c \star \mathbf{K}_c

  • Scalar products per output pixel: Each entry [\mathbf{Y}]_{i,j} aggregates c_\textrm{i} k_\textrm{h} k_\textrm{w} products across space and channels.
  • Shaded top-left output (0,0) (c_\textrm{i}=2):
    • Channel 0: 0{\cdot}0 + 1{\cdot}1 + 3{\cdot}2 + 4{\cdot}3 = 19.
    • Channel 1: 1{\cdot}1 + 2{\cdot}2 + 4{\cdot}3 + 5{\cdot}4 = 37.
    • Sum: 19 + 37 = 56.

How Cross-Channel Summation Detects Color Opponency

Why Spatial Filtering Alone Cannot See Equal-Luminance Edges

[\mathbf{Y}]_{i,j} = [\mathbf{X}_{\textrm{R}}]_{i,j} - [\mathbf{X}_{\textrm{G}}]_{i,j}

  • Opponent color weighting: Pairing +1 on Red with -1 on Green outputs +1 on red, -1 on green, and 0 on neutral gray of any brightness.
  • Isoluminant edges: Detects red-on-green boundaries where grayscale luminance is completely flat.

Implementing Multi-Input Cross-Correlation in Code

Summing 2D Cross-Correlations Over the Channel Axis

In Python, corr2d_multi_in zips the channel slices of X and K, calls d2l.corr2d(x, k) on each pair, and sums the resulting 2D grids:

def corr2d_multi_in(X, K):
    """Cross-correlate each channel of X with its channel of K and add."""
    return sum(d2l.corr2d(x, k) for x, k in zip(X, K))
X = d2l.tensor([[[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]],
               [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]])
K = d2l.tensor([[[0.0, 1.0], [2.0, 3.0]], [[1.0, 2.0], [3.0, 4.0]]])

corr2d_multi_in(X, K)
tensor([[ 56.,  72.],
        [104., 120.]])

Multiple Output Channels: The 4D Kernel Tensor

Extracting c_o Parallel Feature Maps

\mathbf{Y}_o = u_o + \sum_{c=1}^{c_\textrm{i}} \mathbf{X}_c \star \mathbf{K}_{o, c}, \qquad o = 1, \ldots, c_\textrm{o}

  • 4D filter bank \mathsf{K} \in \mathbb{R}^{c_\textrm{o} \times c_\textrm{i} \times k_\textrm{h} \times k_\textrm{w}}: Stacks c_\textrm{o} independent 3D kernels to produce c_\textrm{o} output feature maps.
  • Memory layouts: Depending on the framework, the channel axis comes first, (B, C, H, W), or last, (B, H, W, C); the arithmetic is identical.

Implementing Multi-Input, Multi-Output Convolution

Stacking Output Feature Maps Along Axis 0

Iterating corr2d_multi_in(X, k) over each 3D kernel k in the 4D tensor K and stacking along axis 0 produces the 3D output tensor (c_\textrm{o}, h_\textrm{o}, w_\textrm{o}):

def corr2d_multi_in_out(X, K):
    """Apply corr2d_multi_in once per output channel of K and stack."""
    return d2l.stack([corr2d_multi_in(X, k) for k in K], 0)
K = d2l.stack((K, K + 1, K + 2), 0)
K.shape
torch.Size([3, 2, 2, 2])
corr2d_multi_in_out(X, K)
tensor([[[ 56.,  72.],
         [104., 120.]],

        [[ 76., 100.],
         [148., 172.]],

        [[ 96., 128.],
         [192., 224.]]])

Mental-Math Verification of Multi-Channel Outputs

Why Adding +1 to All Kernel Weights Adds 20 to Position (0,0)

  • Linearity in the kernel: Shifting every weight in \mathsf{K} by +c adds c times the sum of all input elements inside the window.
  • Top-left 2 \times 2 window sum: (0+1+3+4) + (1+2+4+5) = 20.
  • Exact outputs at (0,0): Channel 0 gives 56, Channel 1 (\mathsf{K}+1) gives 56 + 20 = 76, and Channel 2 (\mathsf{K}+2) gives 56 + 40 = 96.

Three Distinct Resource Costs of a Convolutional Layer

Parameters, Multiply-Adds (MACs), and Activation Memory

  • 1. Parameters: c_\textrm{o} c_\textrm{i} k_\textrm{h} k_\textrm{w} + c_\textrm{o} (independent of H \times W).
  • 2. Multiply-Adds (MACs): h_\textrm{o} w_\textrm{o} \cdot c_\textrm{o} c_\textrm{i} k_\textrm{h} k_\textrm{w} (scales with output area \times weights).
  • 3. Activation RAM: B \cdot h_\textrm{o} w_\textrm{o} c_\textrm{o} cached during forward pass for backprop.

Systems Pitfall: Where Memory and Compute Actually Live

Early Stem Layers vs. Deep High-Channel Layers

  • Early stem (3 \to 64 @ 224^2): Tiny weights (1{,}792 params = 7\textrm{ KB}), huge cached activations (3.21\textrm{M} = $12.8).
  • Deep stage (256 \to 256 @ 56^2): Huge weights (590\textrm{K} params = $2.36), 4\times smaller activations (3.2\textrm{ MB/image}).
  • Training OOM rule: GPU VRAM is dominated by batch size \times early high-res feature maps.

The 1×1 Convolution: A Per-Pixel Channel MLP

Mixing Channels Without Spatial Aggregation

At k_\textrm{h} = k_\textrm{w} = 1, the spatial window covers just a single pixel (i, j)—so what does a 1 \times 1 convolution actually compute?

[\mathsf{Y}]_{o, i, j} = u_o + \sum_{c=1}^{c_\textrm{i}} [\mathbf{K}]_{o, c}\, [\mathsf{X}]_{c, i, j}

  • Shared dense layer across space: At every pixel (i, j), it multiplies the c_\textrm{i}-vector of input channels by the same c_\textrm{o} \times c_\textrm{i} weight matrix \mathbf{K}.
  • Roles: Cross-channel feature mixing, projecting channel count up or down (c_\textrm{i} \to c_\textrm{o}), and adding per-pixel nonlinearity (when followed by ReLU).

Implementing 1×1 Convolution as One Matrix Product

Reshaping (c_i, H, W) to (c_i, H*W) Without im2col Duplication

def corr2d_multi_in_out_1x1(X, K):
    """A 1x1 convolution as one matrix product."""
    c_i, h, w = X.shape
    c_o = K.shape[0]
    Y = d2l.matmul(d2l.reshape(K, (c_o, c_i)), d2l.reshape(X, (c_i, h * w)))
    return d2l.reshape(Y, (c_o, h, w))
X = d2l.normal(0, 1, (3, 3, 3))
K = d2l.normal(0, 1, (2, 3, 1, 1))
Y1 = corr2d_multi_in_out_1x1(X, K)
Y2 = corr2d_multi_in_out(X, K)
float(d2l.reduce_sum(d2l.abs(Y1 - Y2)))
0.0

1×1 Bottlenecks: 8.5× Fewer Parameters & MACs

Breaking the c_i * c_o Cost of Wide 3×3 Convolutions

  • Direct 3 \times 3 (256 \to 256): Costs 589{,}824 weights and 1.85 \times 10^9 MACs at 56 \times 56.
  • Bottleneck (256 \xrightarrow{1\times 1} 64 \xrightarrow{3\times 3} 64 \xrightarrow{1\times 1} 256): Uses only 16{,}384 + 36{,}864 + 16{,}384 = 69{,}632 weights (8.5\times reduction).
  • Mandatory nonlinearities: Interleaving ReLU prevents the 3 linear layers from collapsing into one rank-64 matrix.

Beyond 1×1 Bottlenecks: Grouped & Depthwise Convs

Factorizing All-to-All Channel Wiring in Modern CNNs

  • Grouped convolution (g groups): Splits channels into g independent groups, cutting weights and MACs by g\times (ResNeXt, RegNet).
  • Depthwise-separable (g = c_\textrm{i} + 1 \times 1): Decouples spatial filtering (c_\textrm{i} k^2) from channel mixing (c_\textrm{o} c_\textrm{i}), saving \approx 8\text{--}9\times on 3 \times 3 layers (MobileNet, ConvNeXt).

Concept Check: Multi-Channel Arithmetic & Scaling

Concept Check

Work out these three calculations:

  1. Hand cross-correlation: For the two-channel 3 \times 3 input and 2 \times 2 kernel in our earlier worked example, we found [\mathbf{Y}]_{0,0} = 19 + 37 = 56. Compute the first-row, second-column entry [\mathbf{Y}]_{0,1}.
  2. LeNet’s second conv layer: Maps c_\textrm{i} = 6 to c_\textrm{o} = 16 channels with unpadded 5 \times 5 kernels on a 14 \times 14 input. What are the output shape, parameter count, and multiply-adds?
  3. Resolution doubling test: If you double input image resolution from 224 \times 224 to 448 \times 448, by what exact factor do a convolutional layer’s (a) parameters, (b) multiply-adds, and (c) activation memory change?

Answers: (1) Channel 0 top-right window: 1{\cdot}0 + 2{\cdot}1 + 4{\cdot}2 + 5{\cdot}3 = 25. Channel 1 top-right window: 2{\cdot}1 + 3{\cdot}2 + 5{\cdot}3 + 6{\cdot}4 = 47. Sum: 25 + 47 = 72. (2) Output shape: 16 \times 10 \times 10. Parameters: 16 \times 6 \times 25 + 16 = 2{,}416. MACs: 10^2 \times 2{,}400 = 240{,}000. (3) (a) Parameters: 1\times (unchanged); (b) Multiply-adds: 4\times (h_\textrm{o} w_\textrm{o} quadruples); (c) Activation memory: 4\times.

Summary & Key Takeaways

Section Synthesis

Concept / Mechanism Mathematical / Tensor Form Scaling Cost Architectural Role
Multi-Input Conv \mathbf{Y} = \sum_{c=1}^{c_\textrm{i}} \mathbf{X}_c \star \mathbf{K}_c \mathsf{K} \in \mathbb{R}^{c_\textrm{i} \times k_\textrm{h} \times k_\textrm{w}} Fuses color or multi-feature cues across channels
Multi-Output Conv \mathbf{Y}_o = u_o + \sum_{c=1}^{c_\textrm{i}} \mathbf{X}_c \star \mathbf{K}_{o,c} c_\textrm{o} c_\textrm{i} k_\textrm{h} k_\textrm{w} + c_\textrm{o} params Extracts c_\textrm{o} parallel feature maps; grows as H, W shrink
Compute & Memory \textrm{MACs} = h_\textrm{o} w_\textrm{o} c_\textrm{o} c_\textrm{i} k_\textrm{h} k_\textrm{w} \textrm{Activations} = h_\textrm{o} w_\textrm{o} c_\textrm{o} Early layers dominate RAM; deep wide layers dominate params
1 \times 1 Convolution \mathbf{Y} = \textrm{reshape}(\mathbf{K}\,\textrm{reshape}(\mathsf{X})) c_\textrm{o} c_\textrm{i} + c_\textrm{o} params Per-pixel channel MLP; bottleneck projections cut cost 8.5\times

Next (§6.5): How to summarize local neighborhoods, absorb small spatial jitters, and collapse spatial feature maps into translation-invariant vectors via pooling layers.