Flatten Feature Map
Implement a function to transform a 3D feature map into a 1D vector, a crucial step in Convolutional Neural Networks (CNNs). This process enables the connection of convolutional layers to fully connected layers.
In CNNs, a feature map is a 3D array of size , where is the number of channels, is the height, and is the width. To feed this data into a fully connected layer, it must be flattened into a 1D vector.
The values must appear in channel, then row, then column order: all of channel 1 comes before channel 2, within a channel row 1 comes before row 2, and within a row the values keep their left-to-right order. Return the result as a flat Python list of length .
This technique is widely used in image classification tasks.
Example
flatten([[[1,2],[3,4]], [[5,6],[7,8]]])
[1,2,3,4,5,6,7,8]
- Input: a nested list with channels, each an grid. Channel 1 holds and channel 2 holds .
- Output size: the vector has entries, one per value, none added or dropped.
- Where each value lands: counting from 0, entry goes to position . So (channel , row , column ) lands at , and () lands at .
- Result: positions 0 to 3 are channel 1 read row by row (
1,2,3,4), positions 4 to 7 are channel 2 (5,6,7,8), giving[1,2,3,4,5,6,7,8]. - Notice that the column index changes fastest and the channel index slowest. Reading columns first would give for channel 1 instead.
Constraints
- Return flattened list in C, H, W order