Last active
January 15, 2020 09:29
-
-
Save avmoldovan/03da509b555dc4afc89f76d2c7cc5d4c to your computer and use it in GitHub Desktop.
Memory strided im2col
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
#from https://gist.githubusercontent.com/anirudhshenoy/5d99c087f0943b1306bbfedf4cc879a0/raw/2fe0cbbd4d70157db39a8eea1cefe77d960f4b06/memory_strided_im2col.py | |
from skimage.util.shape import view_as_windows | |
def memory_strided_im2col(x, kernel): | |
output_shape = (x.shape[0] - kernel.shape[0]) + 1 | |
return view_as_windows(x, kernel.shape).reshape(output_shape*output_shape, kernel.shape[0]*2) | |
#view_as_windows has an additional step parameter that can be used with different strides | |
### usage: | |
# input_matrix = np.array([[3,9,0], [2, 8, 1], [1,4,8]]) | |
# kernel = np.array([[8,9], [4,4]]) | |
# output_shape = (input_matrix.shape[0] - kernel.shape[0]) + 1 | |
# mem_strided_mat = memory_strided_im2col(input_matrix, kernel) | |
# mem_strided_conv = np.dot(kernel.flatten(), mem_strided_mat) + biasmem_strided_conv = mem_strided_conv.reshape(output_shape, output_shape) | |
#PS: x_newview = np.lib.stride_tricks.as_strided(x, shape = (5, 4), strides = (8,8)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment