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)
print(input_windows.shape)
> torch.Size([1, 3, 22, 22, 3, 3])
input_windows = input_windows.contiguous().view(*input_windows.size()[:-2], -1)
print(input_windows.shape)
> torch.Size([1, 3, 22, 22, 9])

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