Deep Learning · MT2 · section

One complete CNN pipeline

Convolutional neural networks

Question. Input 128 × 128 × 3 → convolution 3 × 3, 16 filters, stride 1, no padding → pooling 2 × 2, stride 2 → convolution 3 × 3, 32 filters, stride 1, no padding → pooling 2 × 2, stride 2 → dense 240 → dense 120 → dense 5 with softmax. Find every output shape and trainable parameter count. Assume one bias per receiving filter or dense neuron.

Given

Input = 128 × 128 × 3.

Conv: 3 × 3,16 filters,stride 1,valid → pool 2,stride 2.

Conv: 3 × 3,32 filters,stride 1,valid → pool 2,stride 2.

Flatten → dense 240 → dense 120 → dense 5.

One bias per learned filter/dense neuron.

To find

Find every output shape and layer parameter count, then the network total.

Formula — which one and why

Complete CNN calculation. Carry each output shape into the next layer. Convolution changes spatial size and channel count; pooling changes spatial size; flattening converts all activations into dense-layer inputs.

H,W,C = feature-map height, width and channels; K = kernel size; S = stride; P_pad = padding. One bias is assumed for each learned filter or dense neuron unless specified otherwise.

N_{\mathrm{out}}=\left\lfloor\frac{N+2P_{\mathrm{pad}}-K}{S}\right\rfloor+1LaTeX: N_{\mathrm{out}}=\left\lfloor\frac{N+2P_{\mathrm{pad}}-K}{S}\right\rfloor+1
P_{\mathrm{conv}}=(K^2C_{\mathrm{in}}+1)C_{\mathrm{out}}LaTeX: P_{\mathrm{conv}}=(K^2C_{\mathrm{in}}+1)C_{\mathrm{out}}
n_{\mathrm{flat}}=HWCLaTeX: n_{\mathrm{flat}}=HWC
P_{\mathrm{dense}}=(n_{\mathrm{in}}+1)n_{\mathrm{out}}LaTeX: P_{\mathrm{dense}}=(n_{\mathrm{in}}+1)n_{\mathrm{out}}
P_{\mathrm{pool}}=P_{\mathrm{flatten}}=0LaTeX: P_{\mathrm{pool}}=P_{\mathrm{flatten}}=0

Yaad rakhne ka cue: Ek layer ka output, next layer ka input; shape aur parameters alag count.

Solution — step by step

Step 1. Conv 1 size. (128 − 3)/1 + 1 = 126; output 126 × 126 × 16. Parameters = (3 × 3 × 3 + 1) × 16 = 448.

Follow all eight layers from the question. Each row keeps the output shape separate from its learned-parameter count. Tap the diagram to enlarge.

Step 2. Pool 1 size. \left\lfloor\frac{126 - 2}{2}\right\rfloor+1LaTeX: \left\lfloor\frac{126 - 2}{2}\right\rfloor+1 = 63; output 63 × 63 × 16. Parameters = 0.

Step 3. Conv 2 size. (63 − 3)/1 + 1 = 61; output 61 × 61 × 32. Input channels are now 16, not the original 3. Parameters = (3 × 3 × 16 + 1) × 32 = 4,640.

Step 4. Pool 2 size. \left\lfloor\frac{61 - 2}{2}\right\rfloor+1LaTeX: \left\lfloor\frac{61 - 2}{2}\right\rfloor+1 = \left\lfloor 29.5\right\rfloorLaTeX: \left\lfloor 29.5\right\rfloor + 1 = 30; output 30 × 30 × 32. Parameters = 0.

Step 5. Flatten. 30 × 30 × 32 = 28,800 values. Parameters = 0.

Step 6. Dense 240. 28,800 weights per neuron plus one bias, multiplied by 240: (28,800 + 1) × 240 = 6,912,240.

Step 7. Dense 120. (240 + 1) × 120 = 28,920.

Step 8. Classifier. (120 + 1) × 5 = 605. Softmax itself adds no extra parameters; these 605 belong to the dense class-score layer.

Step 9. Total. 448 + 4,640 + 6,912,240 + 28,920 + 605 = 6,946,853. Most parameters come from the first dense layer.