Img2col operation in Pytorch

Has anyone know this operation in Pytorch ?

What is your use case?
Maybe you could use tensor.unfold?

For example, I want to make one tensor (n,c,w,h) into tensor (n,c,w,h, k*k) where k is the kernel size
use img2col (like caffe) operation to do this.

If you would like to use a sliding window of kernel size k, you could try the following code (originally created by @fmassa) :

n, c, w, h = 1, 3, 24, 24
k = 3
stride = 1
x = torch.randn(n, c, w, h)
input_windows = x.unfold(2, k, stride).unfold(3, k, stride)
> torch.Size([1, 3, 22, 22, 3, 3])
input_windows = input_windows.contiguous().view(*input_windows.size()[:-2], -1)
> torch.Size([1, 3, 22, 22, 9])

Your width and height will be smaller as in a vanilla convolution without padding.

1 Like

Thank you very much !!!