AttributeError: to

I’m learning Object Detection from this tutorial. But when I try to run this code, I got a AttributeError: to
Why? I’m using CPU.
Here is my code:

from detection.engine import evaluate, train_one_epoch
from detection import utils

device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
num_classes = 2
dataset = PennFudanDataset('PennFudanPed', transforms=None)
dataset_test = PennFudanDataset('PennFudanPed', transforms=None)
indices = torch.randperm(len(dataset)).tolist()
dataset = torch.utils.data.Subset(dataset, indices[:-50])
dataset_test = torch.utils.data.Subset(dataset_test, indices[-50:])
data_loader = torch.utils.data.DataLoader(dataset, batch_size=2, shuffle=True, num_workers=0, collate_fn=utils.collate_fn)
data_loader_test = torch.utils.data.DataLoader(dataset_test, batch_size=1, shuffle=False, num_workers=0, collate_fn=utils.collate_fn)
model.to(device)
params = [p for p in model.parameters() if p.requires_grad]
optimizer = torch.optim.SGD(params, lr=0.005, momentum=0.9, weight_decay=0.0005)
lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=3, gamma=0.1)
num_epochs = 10
for epoch in range(num_epochs):
    train_one_epoch(model, optimizer, data_loader, device, epoch, print_freq=10)
    lr_scheduler.step()
    evaluate(model, data_loader_test, device=device)

print("That's it!")

here is my error:

AttributeError                            Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_13236/2062630411.py in <module>
     17 num_epochs = 10
     18 for epoch in range(num_epochs):
---> 19     train_one_epoch(model, optimizer, data_loader, device, epoch, print_freq=10)
     20     lr_scheduler.step()
     21     evaluate(model, data_loader_test, device=device)

~\anaconda3\lib\site-packages\detection\engine.py in train_one_epoch(model, optimizer, data_loader, device, epoch, print_freq, scaler)
     26 
     27     for images, targets in metric_logger.log_every(data_loader, print_freq, header):
---> 28         images = list(image.to(device) for image in images)
     29         targets = [{k: v.to(device) for k, v in t.items()} for t in targets]
     30         with torch.cuda.amp.autocast(enabled=scaler is not None):

~\anaconda3\lib\site-packages\detection\engine.py in <genexpr>(.0)
     26 
     27     for images, targets in metric_logger.log_every(data_loader, print_freq, header):
---> 28         images = list(image.to(device) for image in images)
     29         targets = [{k: v.to(device) for k, v in t.items()} for t in targets]
     30         with torch.cuda.amp.autocast(enabled=scaler is not None):

~\anaconda3\lib\site-packages\PIL\Image.py in __getattr__(self, name)
    544             )
    545             return self._category
--> 546         raise AttributeError(name)
    547 
    548     @property

AttributeError: to

The code tries to call the .to() method on a tensor while a PIL.Image is used:

~\anaconda3\lib\site-packages\detection\engine.py in <genexpr>(.0)
     26 
     27     for images, targets in metric_logger.log_every(data_loader, print_freq, header):
---> 28         images = list(image.to(device) for image in images)
     29         targets = [{k: v.to(device) for k, v in t.items()} for t in targets]
     30         with torch.cuda.amp.autocast(enabled=scaler is not None):

~\anaconda3\lib\site-packages\PIL\Image.py in __getattr__(self, name)
    544             )
    545             return self._category
--> 546         raise AttributeError(name)
    547 
    548     @property

(see the file names for the source of the error)

Make sure to pass tensors to the corresponding method and it should work.