Module ml::cnn
CNN: 2D convolution + pooling over flat CHW feature maps.
Feature maps are channels-first (CHW), flat row-major float32 buffers — deliberately not std::Tensor (which tops out at 2D and is wired for matmul-shaped Autograd::get; convolution's 4D weight tensor and 3D activations don't fit that shape). Forward-only, like the rest of std/ml's newer additions (see attention.h). Conv2D supports stride and zero-padding; no dilation, no grouped/depthwise convolution.
Structs
| FeatureMap | |
| Conv2D | out[oc][oh][ow] = bias[oc] + sum_ic sum_kh sum_kw weight[oc][ic][kh][kw] * in[ic][ohstride+kh-padding][owstride+kw-padding] (input positions outside [0,height)x[0,width) — i.e. inside the zero padding — contribute 0). Output spatial size is (dim + 2*padding - kernel)/stride + 1; caller must choose dimensions that divide evenly (no implicit ceil/floor rounding surprises). |
| ResidualBlock | ResNet basic block (He et al. 2016): out = relu(conv2(relu(conv1(x))) + shortcut(x)), conv1 3x3 with 'stride', conv2 3x3 stride 1, both padding 1. The shortcut is x itself, or a 1x1 convolution with 'stride' when the stride is not 1 or the channel count changes (the "projection" shortcut). Output side (dim - 1)/stride + 1. No batch norm: fold a trained model's batch norms into the convolutions' weights and biases. |
Functions
| feature_map_add | Residual connection: out = a + b, element by element (relu(a + b) when 'relu'); a and b must have the same shape. |
| concat_channels | Concatenates 'a' and 'b' along the channel axis: out has a.channels + b.channels channels, a's copied first, then b's. Both must share the same height/width. Used to implement U-Net's encoder- to-decoder skip connections. |