Last active
June 27, 2020 15:10
-
-
Save Alescontrela/d4b02697facb6ed0a8eac345cccea11d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def convolution(image, filt, bias, s=1): | |
''' | |
Confolves `filt` over `image` using stride `s` | |
''' | |
(n_f, n_c_f, f, _) = filt.shape # filter dimensions | |
n_c, in_dim, _ = image.shape # image dimensions | |
out_dim = int((in_dim - f)/s)+1 # calculate output dimensions | |
# ensure that the filter dimensions match the dimensions of the input image | |
assert n_c == n_c_f, "Dimensions of filter must match dimensions of input image" | |
out = np.zeros((n_f,out_dim,out_dim)) # create the matrix to hold the values of the convolution operation | |
# convolve each filter over the image | |
for curr_f in range(n_f): | |
curr_y = out_y = 0 | |
# move filter vertically across the image | |
while curr_y + f <= in_dim: | |
curr_x = out_x = 0 | |
# move filter horizontally across the image | |
while curr_x + f <= in_dim: | |
# perform the convolution operation and add the bias | |
out[curr_f, out_y, out_x] = np.sum(filt[curr_f] * image[:,curr_y:curr_y+f, curr_x:curr_x+f]) + bias[curr_f] | |
curr_x += s | |
out_x += 1 | |
curr_y += s | |
out_y += 1 | |
return out |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Shouldn't image and filt be 2D arrays?