AttributeError: to

Armin_Dashti: <p>I’m learning Object Detection from this <a href="https://pytorch.org/tutorials/intermediate/torchvision_tutorial.html" rel="noopener nofollow ugc">tutorial</a>. But when I try to run this code, I got a AttributeError: to<br>
Why? I’m using CPU.<br>
Here is my code:</p>
<pre><code class="lang-auto">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!")
</code></pre>
<p>here is my error:</p>
<pre><code class="lang-auto">AttributeError                            Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_13236/2062630411.py in &lt;module&gt;
     17 num_epochs = 10
     18 for epoch in range(num_epochs):
---&gt; 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):
---&gt; 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 &lt;genexpr&gt;(.0)
     26 
     27     for images, targets in metric_logger.log_every(data_loader, print_freq, header):
---&gt; 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
--&gt; 546         raise AttributeError(name)
    547 
    548     @property

AttributeError: to
</code></pre>

ptrblck: <p>The code tries to call the <code>.to()</code> method on a tensor while a <code>PIL.Image</code> is used:</p>
<pre><code class="lang-python">~\anaconda3\lib\site-packages\detection\engine.py in &lt;genexpr&gt;(.0)
     26 
     27     for images, targets in metric_logger.log_every(data_loader, print_freq, header):
---&gt; 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
--&gt; 546         raise AttributeError(name)
    547 
    548     @property
</code></pre>
<p>(see the file names for the source of the error)</p>
<p>Make sure to pass tensors to the corresponding method and it should work.</p>