Deep learning: Shapes and parameters of a CNN

Plan a convolutional network before building it.

What to build

A program that computes each layer's output shape and parameter count in a CNN.

Requirements

  • Layers: conv F K S P (F kernels of size K × K, stride S, padding P), pool K S, flatten, dense N.
  • Print conv: FxHxW, P parameters, pool: CxHxW, 0 parameters, flatten: N, 0 parameters or dense: N, P parameters for each layer, then total T.

Input

Line 1: channels, height and width of the input. Line 2: l. Then l layer lines.

Output

l + 1 lines.

Constraints

l ≤ 30; shapes stay positive.

Example

Input:

1 28 28
4
conv 8 3 1 1
pool 2 2
flatten
dense 10

Output:

conv: 8x28x28, 80 parameters
pool: 8x14x14, 0 parameters
flatten: 1568, 0 parameters
dense: 10, 15690 parameters
total 15770

Why it works

Each layer's output shape is the next layer's input shape, so working through the layers in order gives every shape and every parameter count.

Examples

Example 1 · Input
1 28 28
4
conv 8 3 1 1
pool 2 2
flatten
dense 10
Expected output
conv: 8x28x28, 80 parameters
pool: 8x14x14, 0 parameters
flatten: 1568, 0 parameters
dense: 10, 15690 parameters
total 15770
Example 2 · Input
3 32 32
5
conv 16 5 1 0
pool 2 2
conv 32 3 2 1
flatten
dense 100
Expected output
conv: 16x28x28, 1216 parameters
pool: 16x14x14, 0 parameters
conv: 32x7x7, 4640 parameters
flatten: 1568, 0 parameters
dense: 100, 156900 parameters
total 162756

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

conv F K S P: output (F, (H + 2P − K) // S + 1, same for W), parameters F × C × K × K + F.

Hint 2

pool K S has no parameters; flatten gives C × H × W; dense N has inputs × N + N parameters.

Solution

Show one correct solution (try it yourself first)
c, h, w = map(int, input().split())
flat = None
total = 0
for _ in range(int(input())):
    parts = input().split()
    kind = parts[0]
    if kind == "conv":
        f, k, s, p = map(int, parts[1:])
        params = f * c * k * k + f
        c, h, w = f, (h + 2 * p - k) // s + 1, (w + 2 * p - k) // s + 1
        print(f"conv: {c}x{h}x{w}, {params} parameters")
    elif kind == "pool":
        k, s = map(int, parts[1:])
        h, w = (h - k) // s + 1, (w - k) // s + 1
        params = 0
        print(f"pool: {c}x{h}x{w}, 0 parameters")
    elif kind == "flatten":
        flat = c * h * w
        params = 0
        print(f"flatten: {flat}, 0 parameters")
    else:
        n = int(parts[1])
        params = flat * n + n
        flat = n
        print(f"dense: {n}, {params} parameters")
    total += params
print("total", total)

Lesson: Pooling and convolutional networks →