Solution to the problem from the Research Institute for System Studies of the Russian Academy of Sciences, supported by the Russian Neural Network Association.
Given a grayscale image and the result of a convolution operation with an unknown kernel. The task is to determine the convolution kernel weights.
- Convolution window size is known: h × w (odd numbers)
- Convolution was performed in mode='same' - zero padding is added to the image with width (w - 1)/2 on each side and (h - 1)/2 on top/bottom
- Pixel values in the original image and convolution kernel are integers
- The convolution result matches the size of the original image
No external libraries - uses only built-in Python capabilities
- Custom implementation of Gaussian elimination for solving linear systems
- Custom matrix rank computation for selecting linearly independent equations
- Python 3.6+
- No external dependencies
py main.py- First line - two numbers m, n - image dimensions.
- Next m lines with n comma-separated numbers - original image matrix A.
- Then two odd numbers h, w - convolution kernel dimensions.
- After that m lines with n comma-separated numbers - convolution result B.
All numbers are integers.
Dimensions satisfy: 3 ≤ m, n ≤ 1000, 3 ≤ h, w ≤ 11, h ≤ m, w ≤ n.
h lines, each with w comma-separated integers - convolution kernel weights.
Input:
3,5
9,0,46,57,49
19,23,41,23,88
47,71,92,8,86
3,3
256,1096,1586,2501,1300
1135,1014,1028,2221,1341
1465,2149,1830,936,1860
Output:
6,1,2
15,11,11
-15,12,-10
Input:
6,4
68,74,98,40
82,13,45,54
83,51,88,89
91,71,7,40
31,1,96,82
22,97,63,17
3,3
571,757,-167,-745
1203,3152,2777,994
1004,2371,1858,115
1692,2417,2833,1366
1646,3077,2159,-546
1448,2124,1274,1595
Output:
3,-2,-1
14,-5,-4
14,6,15
- For each position (i,j) in the output matrix, a linear equation is formed based on the h×w window of the input matrix
- Linearly independent equations are selected (matrix rank check using Gaussian elimination)
- The system of linear equations is solved using Gaussian elimination with partial pivoting
- The result is rounded to integers and reshaped into an h×w convolution kernel
- A 180° rotation (flip along both axes) is applied to obtain the final kernel
read_ints_csv()- reads comma-separated numbers from a linematrix_rank()- computes matrix rank using Gaussian eliminationsolve_linear_system()- solves linear system using Gaussian eliminationflatten()- converts 2D list to 1Dreshape()- converts 1D list to 2D matrixreverse_2d()- rotates matrix by 180°patch_row()- extracts convolution window with padding