Create a dataloader using a list of targets and a list of tensors as data

Hello everyone,

So, I am working on a small project and I am kind of stuck for like 2 hours now on a thing that seems simple so I would be very thankful if anyone can help.
So, I have a list of tensors that I called new_images and a list of labels.
I want to create a dataloader using these two.
So, I tried several things like for example a dumb list of tuples but it did not work. It showed an error but I ignored it because I know that this kind of solution would not work.
I tried creating a class dataset like this:

class DataSetCos(torch.utils.data.Dataset):
    def __init__(self, nbr_samples, new_images, new_labels, transform=None):
        self.nbr_samples=nbr_samples
        self.labels=new_labels
        self.transform = transform
    def __len__(self):
        return self.nbr_samples
    def __getitem__(self, idx):
        if torch.is_tensor(idx):
            idx = idx.tolist()
        sample = new_images[idx]
        if self.transform:
            sample = self.transform(sample)
        if (sample.float().size()[0] == 3):
            return [sample.float(), self.labels[idx]]

after that I loaded it like this:

dataset= DataSetCos(nbr_samples=100, new_images=new_images, new_labels=new_labels, transform =None)
dataloader= torch.utils.data.DataLoader(dataset,batch_size=batch_size,shuffle=False)

but when I try testing my model, I get this error IndexError: list index out of range.

Hi,

It is really hard to say what the exact problem is but it seems data loader is generating indices that is out of bound for your lists. I think changing __len__ can help. Try this:


    def __len__(self):
        return len(self.new_images)

Also, can you let me know the shape of new_image and new_labels?

Bests

Hi thx for your answer!
I will try doing that.
For the shapes, the new_images is a list of tensors of len 100 and same thing for the new_labels