Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scipy convolve2d outputs wrong values

Here is my code which I used for checking the correctness of convolve2d

import numpy as np
from scipy.signal import convolve2d

X = np.random.randint(5, size=(10,10))
K = np.random.randint(5, size=(3,3))
print "Input's top-left corner:"
print X[:3,:3]
print 'Kernel:'
print K

print 'Hardcording the calculation of a valid convolution (top-left)'
print (X[:3,:3]*K)
print 'Sums to'
print (X[:3,:3]*K).sum()
print 'However the top-left value of the convolve2d result'
Y = convolve2d(X, K, 'valid')
print Y[0,0]

On my computer this results in the following:

Input's top-left (3x3) corner:
[[0 0 0]
 [1 1 2]
 [1 3 0]]
Kernel:
[[4 1 1]
 [0 3 3]
 [2 1 2]]
Hardcording the calculation of a valid convolution (top-left)
[[0 0 0]
 [0 3 6]
 [2 3 0]]
Sums to
14
However the top-left value of the convolve2d result
10

Background story: I've been debugging a convnet library, and somehow the gradients were always wrong. After a few weeks I concluded that everything should be working fine, so I checked the convolve2d function by bare hand.

like image 228
botcs Avatar asked Oct 25 '16 19:10

botcs


2 Answers

I think the problem is that you did not do what SciPy implemented. I won't dwell on the details or the foundations but only provide you with a solution:

Reverse the kernel.

>>> import numpy as np

>>> arr = np.array([[0, 0, 0],
                    [1, 1, 2],
                    [1, 3, 0]])

>>> kernel = np.array([[4, 1, 1],
                       [0, 3, 3],
                       [2, 1, 2]])

>>> from scipy.signal import convolve2d

>>> convolve2d(arr, kernel[::-1, ::-1])
array([[ 0,  0,  0,  0,  0],
       [ 2,  3,  7,  4,  4],
       [ 5, 13, 14, 12,  0],
       [ 4, 14, 16,  6,  8],
       [ 1,  4,  7, 12,  0]])

>>> convolve2d(arr, kernel[::-1, ::-1], 'valid')
array([[14]])
like image 99
MSeifert Avatar answered Nov 18 '22 20:11

MSeifert


The expression (X[:3,:3]*K).sum() is not correct. For convolution, you have to reverse the kernel, e.g. (X[:3,:3]*K[::-1,::-1]).sum()

like image 7
Warren Weckesser Avatar answered Nov 18 '22 20:11

Warren Weckesser