1 year ago

#312882

test-img

Pavel

Why scipy.signal.convolve2d gives me the wrong answer? (convolation matrix)

I would like to convolve given matrix with kernel.

from scipy import signal
import numpy as np

matrix = np.array([[147, 52, 208, 210, 167, 41, 133, 83],
                   [63, 243, 255, 11, 220, 154, 97, 72],
                   [172, 116, 175, 169, 34, 196, 251, 248],
                   [157, 11, 1, 34, 240, 77, 93, 229],
                   [80, 140, 111, 16, 149, 216, 40, 57],
                   [105, 9, 127, 209, 168, 136, 218, 38],
                   [145, 95, 153, 28, 198, 24, 42, 206],
                   [242, 110, 106, 62, 70, 158, 45, 30],
                   [116, 42, 166, 210, 199, 122, 88, 111]])

kernel = np.array([[-1.0, 0.4, -0.4],
                   [-0.4, -0.9, -0.1],
                   [0.2, -0.2, 0.8]])

signal.convolve2d(matrix, kernel, mode="valid")

The right answer should be:

right_answer = [[-327.6, -257.2, -296.3, -193.6, -271.7, 10.3],
                [-228.5, -337.0, -378.7, -82.0, -305.3, -293.1],
                [-191.6, -103.7, -37.8, -324.9, -212.0, -251.4],
                [-201.3, -38.1, -39.1, -13.9, -323.6, -245.5],
                [1.2, -230.0, -236.5, -306.0, -221.6, -316.0],
                [-199.8, -169.9, -152.0, -263.2, -287.4, -85.4],
                [-227.0, -47.4, -175.8, 37.8, -294.1, -100.7]]

But convolve2d returns wrong answer.

wrong_answer = [[-375.6, -361.6,   -2. , -367.9, -233.6, -334.7],
                [-198.2, -129.1, -164.2, -124.9, -273.8, -449.1],
                [  36.4,   48.4, -200.7, -245.4, -105.5, -154.2],
                [-220.2, -266.7, -178.3, -379.9, -260.9,    3.2],
                [-184.2, -110.6, -400.6, -156.3, -254. , -247.6],
                [-212.4, -198.4, -113.9, -184.6,   82.8, -125. ],
                [-233.6, -240.6, -119.4, -271.2, -124. , -140.9]]

One more interesting thing. Before this I tested another example with convolve2d. And in this situation the answer was right! This example was:

example_matrix = np.array([[1, 2, 0, 3, -2, 1, -1],
                           [0, 1, 0, 2, -1, 1, 1],
                           [2, 0, 1, 1, -1, 1, 0],
                           [3, -1, 2, 0, 1, 0, -1],
                           [2, -2, 1, 2, 2, 0, -2],
                           [-1, 1, 0, 0, 0, 1, -3],
                           [0, 0, -1, 2, -1, -1, 0]])

example_kernel = np.array([[0, 1, 0],
                           [1, 3, 1],
                           [0, 1, 0]])

example_answer = signal.convolve2d(matrix, kernel, mode="valid")

And example_answer was right!!!

example_answer = [[ 5,  4,  9, -3,  5],
                  [ 3,  6,  5, -1,  3],
                  [ 0,  7,  6,  4,  1],
                  [-3,  5,  9,  9,  1],
                  [ 0,  1,  4,  2, -1]]

I don't understand, why in example convolve2d returned the right matrix, but in given matrix and kernel it returned the wrong answer. What is the problem? Please, help me.

python

scipy

conv-neural-network

convolution

0 Answers

Your Answer

Accepted video resources