How to load the whole dataset to GPU
I have dataset of 1550 images 3x112x112. When i am training my model I create dataset wia ImageFolder and then use DataLoader. It takes so much time because of reading from memory every time. I have enough gpu memory to load the whole dataset at once. It will be much faster. What is the best way to do it?

You can store the images as an attribute of the dataset, put it on the GPU at initialization, and let __getitem__ return images from this directly.
import torch
from torch.utils.data import Dataset, DataLoader

class GPUDataset(Dataset):
    def __init__(self):
        self.len = 1550
        self.data = torch.randn(self.len, 3, 112, 112).cuda()  # ~ 223 MB

    def __getitem__(self, index):
        return self.data[index]

    def __len__(self):
        return self.len
    
dataset = GPUDataset()
loader = DataLoader(dataset, batch_size=4)

batch = next(iter(loader))
print(batch.device, batch.shape)    # cuda:0 torch.Size([4, 3, 112, 112])

This is meant to be a quick fix for small datasets. For larger datasets, consider more advanced solutions such as lmdb.
