Skip to content
DeepForgeby svx
All problems
HardComputer Visionproj-036

CNN Predict

Problem

Full CNN prediction pass from pixels to class index.

Pipeline: zero-padded convolution with pad=1 (cross-correlation), ReLU, non-overlapping 2x2 max pooling, flatten in row-major order, dense scores W @ x + b, then softmax. Return the index of the highest probability, breaking ties by the lower index.

Hint

After pooling with k=2 the maps shrink by half; flatten, score, and take the argmax.

Test cases

case 1
in: [[[1,0,1,0],[0,1,0,1],[1,0,1,0],[0,1,0,1]],[[1,0,0],[0,1,0],[0,0,1]],[[1,-1,0.5,-0.5],[-1,1,-0.5,0.5]],[0.2,-0.2]]
expected: 0
case 2
in: [[[5,5,5,5],[5,5,5,5],[5,5,5,5],[5,5,5,5]],[[1,1,1],[1,1,1],[1,1,1]],[[1,0,0,0],[0,1,0,0]],[0,0]]
expected: 0
case 3
in: [[[1,2],[3,4]],[[1]],[[1,0,0,0],[0,0,1,0]],[0,0]]
expected: 1
case 4
in: [[[1]],[[1]],[[1],[1]],[0,0]]
expected: 0
Starter codePython
import math
def cnn_predict(image, kernel, W, b):
    # Your code here
    pass

4 test cases · run your code against them in the editor