Skip to content

Instantly share code, notes, and snippets.

@avmoldovan
Created January 15, 2020 09:03
Show Gist options
  • Save avmoldovan/aa3722486db0c7bf476d15427753beb9 to your computer and use it in GitHub Desktop.
Save avmoldovan/aa3722486db0c7bf476d15427753beb9 to your computer and use it in GitHub Desktop.
#from https://gist.githubusercontent.com/anirudhshenoy/d4e4228ebb884066efd65299f1d169cb/raw/bbe787ecbcf014e6e64e0ee8b9217923167c626d/naive_im2col.py
def im2col(x, kernel):
kernel_shape = kernel.shape[0]
rows = []
# Assuming Padding = 0, stride = 1
for row in range(x.shape[0] - 1):
for col in range(x.shape[1] - 1):
window = x[row: row + kernel_shape, col: col + kernel_shape]
rows.append(window.flatten())
return np.transpose(np.array(rows))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment