Reputation: 4100
Here is my code:
import tensorflow as tf
import numpy as np
from scipy import signal
img2 = np.array([
[10, 10, 10, 0, 0, 0],
[10, 10, 10, 0, 0, 0],
[10, 10, 10, 0, 0, 0],
[10, 10, 10, 0, 0, 0],
[10, 10, 10, 0, 0, 0],
[10, 10, 10, 0, 0, 0]
]).astype(np.float32)
k = np.array([
[1., 0., -1.],
[1., 0., -1.],
[1., 0., -1.]
]).astype(np.float32)
img_tf = tf.constant(tf.reshape(img2, (1, 6, 6, 1)), dtype=tf.float32)
k_tf = tf.constant(tf.reshape(k, (3, 3, 1, 1)), dtype=tf.float32)
conv_tf = tf.nn.conv2d(img_tf, k_tf, strides=[1, 1], padding="SAME")[0, ..., 0]
print("conv_tf: " + str(conv_tf))
np_conv = np.array(signal.convolve2d(img2 , k, "same"), np.int32)
print("sp_conv:\n" + str(np_conv))
The output is:
conv_tf: tf.Tensor(
[[-20. 0. 20. 20. 0. 0.]
[-30. 0. 30. 30. 0. 0.]
[-30. 0. 30. 30. 0. 0.]
[-30. 0. 30. 30. 0. 0.]
[-30. 0. 30. 30. 0. 0.]
[-20. 0. 20. 20. 0. 0.]], shape=(6, 6), dtype=float32)
sp_conv:
[[ 20 0 -20 -20 0 0]
[ 30 0 -30 -30 0 0]
[ 30 0 -30 -30 0 0]
[ 30 0 -30 -30 0 0]
[ 30 0 -30 -30 0 0]
[ 20 0 -20 -20 0 0]]
Now one seam as the opposite of the other one, why is there this difference?
Upvotes: 0
Views: 865
Reputation: 4100
In the end, I found the response.
The issue is scipy
does the mathematically 'correct' convolution, whereas tensorflow
does the convolution oriented to a Convolutional Neural Network (CNN) application.
Therefore, scipy
inverts the kernel before applying the convolution (as explained here) whereas tensorflow
does not.
Upvotes: 2