Case 1. Convolution in pure PHP

Implementation in pure PHP

We implement the convolution operation by hand – without any libraries or ready-made ML algorithms – and see how a filter highlights features of an image. The image is a 5 × 5 matrix of brightness values (0 – black, 255 – white), the filter (kernel) is a 3 × 3 matrix of weights. The kernel slides over the image with stride 1 and no padding; at every position the pixels are multiplied by the weights and summed, giving one value of the feature map. The kernel is not flipped (cross-correlation), as in most CNN implementations.

 
<?php

include 'code.php';

$image = [
    [
10, 10, 10, 200, 200],
    [
10, 10, 10, 200, 200],
    [
10, 10, 10, 200, 200],
    [
10, 10, 10, 200, 200],
    [
10, 10, 10, 200, 200],
];

// Vertical edge filter
$verticalEdgeKernel = [
    [-
1, 0, 1],
    [-
1, 0, 1],
    [-
1, 0, 1],
];

// Horizontal edge filter
$horizontalEdgeKernel = [
    [-
1, -1, -1],
    [ 
0,  0,  0],
    [ 
1,  1,  1],
];

// Inverted vertical filter (sign of the weights is flipped)
$reverseVerticalKernel = [
    [ 
1, 0, -1],
    [ 
1, 0, -1],
    [ 
1, 0, -1],
];

// Same edge, lower contrast
$lowContrastImage = [
    [
10, 10, 10, 100, 100],
    [
10, 10, 10, 100, 100],
    [
10, 10, 10, 100, 100],
    [
10, 10, 10, 100, 100],
    [
10, 10, 10, 100, 100],
];

echo 
'Input image:' . PHP_EOL;
printMatrix($image);
echo 
PHP_EOL;

echo 
'Vertical edge kernel:' . PHP_EOL;
printMatrix($verticalEdgeKernel);
echo 
PHP_EOL;

echo 
'Feature map:' . PHP_EOL;
printMatrix(convolve($image, $verticalEdgeKernel));
echo 
PHP_EOL;

echo 
'Feature map (horizontal edge kernel):' . PHP_EOL;
printMatrix(convolve($image, $horizontalEdgeKernel));
echo 
PHP_EOL;

echo 
'Feature map (inverted vertical kernel):' . PHP_EOL;
printMatrix(convolve($image, $reverseVerticalKernel));
echo 
PHP_EOL;

echo 
'Feature map (low contrast image):' . PHP_EOL;
printMatrix(convolve($lowContrastImage, $verticalEdgeKernel));

Example:

image = [
    [10, 10, 10, 200, 200],
    [10, 10, 10, 200, 200],
    [10, 10, 10, 200, 200],
    [10, 10, 10, 200, 200],
    [10, 10, 10, 200, 200],
]

kernel = [
    [-1, 0, 1],
    [-1, 0, 1],
    [-1, 0, 1],
]

stride = 1, padding = 0
Result: Memory: 0 Mb Time running: < 0.001 sec.
Input image:
10	10	10	200	200
10	10	10	200	200
10	10	10	200	200
10	10	10	200	200
10	10	10	200	200

Vertical edge kernel:
-1	0	1
-1	0	1
-1	0	1

Feature map:
0	570	570
0	570	570
0	570	570

Feature map (horizontal edge kernel):
0	0	0
0	0	0
0	0	0

Feature map (inverted vertical kernel):
0	-570	-570
0	-570	-570
0	-570	-570

Feature map (low contrast image):
0	270	270
0	270	270
0	270	270

The vertical edge kernel gives 0 on a uniform dark area and 570 on the two windows that capture the dark-to-light transition (3 × (200 − 10) = 570). It is one edge: two neighbouring windows both see it. The horizontal kernel gives only zeros, because the image has no brightness change from top to bottom. Inverting the weights flips the sign (−570): the filter reacts to the direction of the change. A lower contrast (100 instead of 200) gives a weaker response (270). In a real CNN the kernel weights are not set by hand but learned from data.