Pytorch List object has no attribute ‘to’

4.3k Views Asked by At

I am new to Pytorch. I am using a pre-trained model (RESNET-50). And i am trying to train this model on MS-COCO dataset using cocoapi.

I have loaded my dataset images and annotations in train loader.

for images,labels in train_loader:
    print(type(images))
    print(type(labels))
    print(images.size())
    print(images.dim())
    steps+=1
    images, labels = images.to(device), labels.to(device)

i got the following output:

class 'torch.Tensor'

class 'list'

torch.Size([64, 3, 224, 224])

4

And i got this Error: I think it is because labels is not a Tensor?

Error output image

1

There are 1 best solutions below

0
On

In your DataLoader you can do something like:

labels = torch.as_tensor([lbl for lbl in labels])

or if your DataLoader returns a list of torch tensors, you can send them to the device:

labels = [lbl.to(device) for lbl in labels]