Patchify images dynamically in pytorch dataset

I’m using the patchify pypi module to first patchify images and save them as a new dataset and then load them back as individual images in a pytorch dataset.
It’s just:

`patches_img = patchify(image, (patch_size, patch_size, 3), step=patch_size)`

for all images and then I’ll load the individual patches in just like any images.

What I’d like to do is something like this

    class Dataset(object):
        #images are the full pre patchified images
        def __init__(self, images: List[str]):
            self.images = images 
            self.transformer = Transform()
            
        
        def __getitem__(self, idx: int):
            image = self.images[idx]
            #IMPORTANT switch to color later
            patches = patchify(image, (patch_size, patch_size, 3), step=patch_size)
    
            image_patches_tensor = self.transformer(patches)
            return image_patches tensor
        
        def __len__(self):
            return len(self.files)

how would I do that?

Thanks in advance!

Are all images the same size? If the answer is yes, we can assume that each image is patched into P patches and there are N original images. Then you can modify the dataset to be sth like this:

  class Dataset(object):
        #images are the full pre patchified images
        def __init__(self, images: List[str], P):
            self.images = images 
            self.transformer = Transform()
            self.num_patches = P
            
        
        def __getitem__(self, idx: int):
            img_idx = idx // self.num_patches
            patch_idx = idx % self.num_patches
            image = self.images[img_idx]
            #IMPORTANT switch to color later
            patches = patchify(image, (patch_size, patch_size, 3), step=patch_size)
    
            image_patches_tensor = self.transformer(patches[patch_idx])
            return image_patches tensor

Hi arman,

thanks, this is something I did already, I was to use pytorch’s native unfold and have it done on the fly as transformatinos.
They are not the same size, but above works regardless if use // integer division by patch_size.

thanks for the reply, apologies for overlooking it for so long.