I have a single image of shape img.shape = (500, 439, 3)
The convolution function is
def convolution(image, kernel, stride=1, pad=0):
n_h, n_w, _ = image.shape
f = kernel.shape[0]
kernel = np.repeat(kernel[None,:], 3, axis=0)
n_H = int(((n_h + (2*pad) - f) / stride) + 1)
n_W = int(((n_w + (2*pad) - f) / stride) + 1)
n_C = 1
out = np.zeros((n_H, n_W, n_C))
for h in range(n_H):
vert_start = h*stride
vert_end = h*stride + f
for w in range(n_W):
horiz_start = w*stride
horiz_end = w*stride + f
for c in range(n_C):
a_slice_prev = image[vert_start:vert_end,
horiz_start:horiz_end, :]
s = np.multiply(a_slice_prev, kernel)
out[h, w, c] = np.sum(s, dtype=float)
return out
I want to see the image after any kernel/filter applied to the image, so I got the following
img = plt.imread('cat.png')
kernel = np.arange(25).reshape((5, 5))
out2 = convolution(img, kernel)
plt.imshow(out2)
plt.show()
I get
s = np.multiply(a_slice_prev, kernel)
ValueError: operands could not be broadcast together with shapes (5,5,3) (3,5,5)