Splitting image into small batches

Hi,
I am new to Pytorch but would like to do the following.
Lets say I have 37 images with two channels and size 256x256.
In other words, my data is the shape (37,2,256,256).
I want to split each image into (256/stride)x(256/stride) images of size kernel_shape assuming circular/periodic padding. I am assuming that each small batch is independent from each other.

For example, assuming stride=1 and kernel_shape=(5,5), a data like (37,2,256,256) will become (37*256*256,2,5,5). I initially thought that Conv2D could be used for that, but after seeing examples I realized this is actually for creating convolutional layers.

I think unfold should work. This post gives you an example.

I haven’t tried yet, but I think I understand the code. Thanks!
That solves the part o breaking into patches. Do Pytorch have anything for adding the padding needed for the periodic/circular padding? I want that the every pixel has it’s own patch.
Edit: Just noticed F.pad has option for periodic boundary condition.

I was exploring the padding options and

x = torch.randn(2, 37, 256, 256)+10
S = x.shape

kh, kw = 101, 101  # kernel size
dh, dw = 1, 1  # stride

pad = (S[-1]%kw // 2, S[-1]%kw // 2, S[-2]%kh // 2, S[-2]%kh // 2, 0, 0, 0, 0)
# Pad to multiples of 32
x = F.pad(x, pad)

plt.imshow(x[0,0,:,:])

Returns
image

But selecting mode="circular" gives NotImplementedError.

---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
/home/gridsan/groups/dedalus_group/gfd22/stochastic_closures_repo/src/01-model/ml/cnn.py in <cell line: 11>()
      40 pad = (S[-1]%kw // 2, S[-1]%kw // 2, S[-2]%kh // 2, S[-2]%kh // 2, 0, 0, 0, 0)
     41 # Pad to multiples of 32
---> 42 x = F.pad(x, pad, mode="circular")
     44 plt.imshow(x[0,0,:,:])

File /state/partition1/llgrid/pkg/anaconda/anaconda3-2022b/lib/python3.8/site-packages/torch/nn/functional.py:4397, in _pad(input, pad, mode, value)
   4395         raise NotImplementedError
   4396 else:
-> 4397     raise NotImplementedError("Only 2D, 3D, 4D, 5D padding with non-constant padding are supported for now")

NotImplementedError: Only 2D, 3D, 4D, 5D padding with non-constant padding are supported for now

torch.__version__ = 1.11.0+cu113
Not sure I understand this error, because for me, this is a 4D padding.

I tried to perform the padding for each channel and snapshot and got the same error.

Similar code works for numpy.pad.

Circular padding seems to have more limitations as given in the docs so you might need to pass 4 values only:

x = torch.randn(2, 37, 256, 256)+10
S = x.shape

kh, kw = 101, 101  # kernel size
dh, dw = 1, 1  # stride

pad = (S[-1]%kw // 2, S[-1]%kw // 2, S[-2]%kh // 2, S[-2]%kh // 2, 0, 0, 0, 0)
y1 = F.pad(x, pad)
print(y1.shape)

pad = (S[-1]%kw // 2, S[-1]%kw // 2, S[-2]%kh // 2, S[-2]%kh // 2)
y2 = F.pad(x, pad, mode="circular")
print(y2.shape)