Deep learning: Forward pass of a small CNN

Run a complete convolutional network by hand-written code.

What to build

A program that computes class scores for an image with one convolution layer, ReLU, max pooling and a dense layer.

Requirements

  • The image has one channel. Each of the F kernels (with its bias) produces a feature map; apply ReLU and 2 × 2 max pooling with stride 2 (dropping a last odd row or column). Flatten all pooled maps, kernel by kernel, row by row. The dense layer has one weight row and bias per class.
  • Print features: and the flattened values, then scores: and the class scores, then class K for the highest score (the first on a tie); numbers written with Python's g format.

Input

Line 1: the image size n. Then n rows. Then F and the kernel size k; for each kernel, k rows and then its bias. Then the number of classes, and for each class a line of weights followed by its bias.

Output

Three lines.

Constraints

n ≤ 12; k ≤ 4; F ≤ 4.

Example

Input:

3
1 0 0
0 1 0
0 0 1
1 2
1 0
0 1
0
2
1 0
-1 3

Output:

features: 2
scores: 2 1
class 0

Why it works

Convolution finds where each pattern occurs, pooling keeps the strongest response in each region, and the dense layer weighs those responses into a score for each class.

Examples

Example 1 · Input
3
1 0 0
0 1 0
0 0 1
1 2
1 0
0 1
0
2
1 0
-1 3
Expected output
features: 2
scores: 2 1
class 0
Example 2 · Input
4
1 1 0 0
1 1 0 0
0 0 0 0
0 0 0 0
1 1
1
0
2
1 0 0 0 0
0 0 0 1 0.5
Expected output
features: 1 0 0 0
scores: 1 0.5
class 0

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

Hints

Hint 1

For each kernel: valid convolution with stride 1, add its bias, apply ReLU, then 2 × 2 max pooling with stride 2.

Hint 2

Flatten the pooled maps in kernel order, row by row, then apply the dense layer.

Solution

Show one correct solution (try it yourself first)
n = int(input())
image = [list(map(float, input().split())) for _ in range(n)]
f, k = map(int, input().split())
features = []
for _ in range(f):
    kernel = [list(map(float, input().split())) for _ in range(k)]
    bias = float(input())
    size = n - k + 1
    fmap = [[max(0.0, sum(image[i + a][j + b] * kernel[a][b] for a in range(k) for b in range(k)) + bias) for j in range(size)] for i in range(size)]
    half = size // 2
    for i in range(half):
        for j in range(half):
            features.append(max(fmap[2 * i + a][2 * j + b] for a in range(2) for b in range(2)))
scores = []
for _ in range(int(input())):
    *weights, bias = map(float, input().split())
    scores.append(sum(w * v for w, v in zip(weights, features)) + bias)
print("features: " + " ".join(f"{v + 0.0:g}" for v in features))
print("scores: " + " ".join(f"{v + 0.0:g}" for v in scores))
print("class", scores.index(max(scores)))

Lesson: Pooling and convolutional networks →