When performing convolution using the im2col method, we aim to transform the convolution operation into a matrix multiplication for computational efficiency. Let's break down how the matrices for the input and filter are constructed when dealing with a convolution where the filter has depth, as in a standard convolution operation.
Given:
-
Input tensor dimensions: ( C \times H \times W = 3 \times 224 \times 224 )
-
Filter dimensions: ( K \times C \times FH \times FW = 32 \times 3 \times 3 \times 3 )
- ( K ) is the number of filters (output channels).
- ( C ) is the number of input channels (depth).
- ( FH ) and ( FW ) are the filter's height and width.
-
Stride: Assuming stride ( S = 2 ) (since output dimensions are halved).
-
Padding: Assuming no padding for simplicity.
Step 1: Compute Output Dimensions
The output spatial dimensions (( OH ) and ( OW )) are determined by:
[
OH = \left\lfloor \frac{H - FH}{S} \right\rfloor + 1 = \left\lfloor \frac{224 - 3}{2} \right\rfloor + 1 = 112
]
[
OW = \left\lfloor \frac{W - FW}{S} \right\rfloor + 1 = \left\lfloor \frac{224 - 3}{2} \right\rfloor + 1 = 112
]
Step 2: Construct the im2col Input Matrix
Each sliding window of the convolution operation corresponds to a column in the im2col matrix.
- Number of elements per window (flattened filter size):
[
N_{\text{elements per window}} = C \times FH \times FW = 3 \times 3 \times 3 = 27
]
[
\text{Total windows} = OH \times OW = 112 \times 112 = 12,544
]
- im2col Input Matrix Dimensions:
[
\text{Input matrix (im2col)}: \text{[flattened window size]} \times \text{[number of windows]} = 27 \times 12,544
]
This matrix arranges all sliding windows of the input tensor into columns, where each column represents all the elements (across depth, height, and width) covered by the filter at a particular position.
Step 3: Flatten and Organize Filters into a Matrix
Each filter is flattened into a row vector.
- Filter Matrix Dimensions:
[
\text{Filter matrix}: K \times N_{\text{elements per filter}} = 32 \times 27
]
This matrix arranges each of the ( 32 ) filters into rows, where each row contains all the weights of a single filter flattened into a vector.
Step 4: Perform Matrix Multiplication
By multiplying the filter matrix with the im2col input matrix, we simulate the convolution operation:
[
\text{Output matrix} = \text{Filter matrix} \times \text{Input matrix}
]
[
(32 \times 27) \times (27 \times 12,544) = 32 \times 12,544
]
Step 5: Reshape the Output
The output matrix is reshaped back into the tensor form:
[
\text{Output tensor dimensions}: K \times OH \times OW = 32 \times 112 \times 112
]
Summary:
Answer:
When using im2col with a filter of size 32 × 3 × 3 × 3 (32 filters, each with depth 3 and size 3 × 3):
- Input: Rearrange the input tensor into a matrix of size 27 × (112 × 112), where 27 is the number of elements per filter window (3 × 3 × 3), and 112 × 112 is the number of windows (output height × output width).
- Filter: Flatten each filter to create a matrix of size 32 × 27, where 32 is the number of filters and 27 is the number of elements in each filter.
- Convolution: Perform matrix multiplication between the filter matrix (32 × 27) and the input matrix (27 × 12,544) to get the output matrix of size 32 × 12,544, which reshapes to the output tensor of dimensions 32 × 112 × 112.