Reputation: 23256
I was watching Andrew Ng's videos on CNN and wanted to to convolve a 6 x 6
image with a 3 x 3
filter. The way I approached this with numpy is as follows:
image = np.ones((6,6))
filter = np.ones((3,3))
convolved = np.convolve(image, filter)
Running this gives an error saying:
ValueError: object too deep for desired array
I could comprehend from the numpy documentation of convolve on how to correctly use the convolve
method.
Also, is there a way I could do a strided convolutions with numpy?
Upvotes: 3
Views: 6625
Reputation: 19885
np.convolve
function, unfortunately, only works for 1-D convolution. That's why you get an error; you need a function that allows you to perform 2-D convolution.
However, even if it did work, you actually have the wrong operation. What is called convolution in machine learning is more properly termed cross-correlation in mathematics. They're actually almost the same; convolution involves flipping the filter matrix followed by performing cross-correlation.
To solve your problem, you can look at scipy.signal.correlate
(also, don't use filter
as a name, as you'll shadow the inbuilt function):
from scipy.signal import correlate
image = np.ones((6, 6))
f = np.ones((3, 3))
correlate(image, f)
Output:
array([[1., 2., 3., 3., 3., 3., 2., 1.],
[2., 4., 6., 6., 6., 6., 4., 2.],
[3., 6., 9., 9., 9., 9., 6., 3.],
[3., 6., 9., 9., 9., 9., 6., 3.],
[3., 6., 9., 9., 9., 9., 6., 3.],
[3., 6., 9., 9., 9., 9., 6., 3.],
[2., 4., 6., 6., 6., 6., 4., 2.],
[1., 2., 3., 3., 3., 3., 2., 1.]])
This is the standard setting of full cross-correlation. If you want to remove elements which would rely on the zero-padding, pass mode='valid'
:
from scipy.signal import correlate
image = np.ones((6, 6))
f = np.ones((3, 3))
correlate(image, f, mode='valid')
Output:
array([[9., 9., 9., 9.],
[9., 9., 9., 9.],
[9., 9., 9., 9.],
[9., 9., 9., 9.]])
Upvotes: 5