Torchvision Object Detection Finetuning Tutorial

username: jameschartouni
cooked: <p>I am trying to implement my version of the Pytorch Object Detection Finetuning code with my own data. The link to the tutorial can be found <a href="https://pytorch.org/tutorials/intermediate/torchvision_tutorial.html" rel="nofollow noopener">here</a>.</p>
<p>I basically use all the same dataset, data loader, model architecture etc. code. Unlike the tutorial, my images are jpegs.</p>
<p>When I run the prewritten training loop, I receive an error that the PIL ‘Image’ object has no attribute ‘to’. Why am I receiving this error and how can I fix it? I hardly modified the existing tutorial code.</p>
<pre><code class="lang-auto">---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
&lt;ipython-input-71-1f6e682855f7&gt; in &lt;module&gt;
      4 for epoch in range(num_epochs):
      5     # train for one epoch, printing every 10 iterations
----&gt; 6     train_one_epoch(model, optimizer, data_loader, device, epoch, print_freq=10)
      7     # update the learning rate
      8     lr_scheduler.step()

~/Image Project/Code/pytorch/engine.py in train_one_epoch(model, optimizer, data_loader, device, epoch, print_freq)
     25 
     26     for images, targets in metric_logger.log_every(data_loader, print_freq, header):
---&gt; 27         images = list(image.to(device) for image in images)
     28         targets = [{k: v.to(device) for k, v in t.items()} for t in targets]
     29 

~/Image Project/Code/pytorch/engine.py in &lt;genexpr&gt;(.0)
     25 
     26     for images, targets in metric_logger.log_every(data_loader, print_freq, header):
---&gt; 27         images = list(image.to(device) for image in images)
     28         targets = [{k: v.to(device) for k, v in t.items()} for t in targets]
     29 

AttributeError: 'Image' object has no attribute 'to'
</code></pre>
<p>The train_one_epoch() function:</p>
<pre><code class="lang-auto">
    def train_one_epoch(model, optimizer, data_loader, device, epoch, print_freq):
    model.train()
    metric_logger = utils.MetricLogger(delimiter="  ")
    metric_logger.add_meter('lr', utils.SmoothedValue(window_size=1, fmt='{value:.6f}'))
    header = 'Epoch: [{}]'.format(epoch)

    lr_scheduler = None
    if epoch == 0:
        warmup_factor = 1. / 1000
        warmup_iters = min(1000, len(data_loader) - 1)

        lr_scheduler = utils.warmup_lr_scheduler(optimizer, warmup_iters, warmup_factor)

    for images, targets in metric_logger.log_every(data_loader, print_freq, header):
        images = list(image.to(device) for image in images)
        targets = [{k: v.to(device) for k, v in t.items()} for t in targets]

        loss_dict = model(images, targets)

        losses = sum(loss for loss in loss_dict.values())

        # reduce losses over all GPUs for logging purposes
        loss_dict_reduced = utils.reduce_dict(loss_dict)
        losses_reduced = sum(loss for loss in loss_dict_reduced.values())

        loss_value = losses_reduced.item()

        if not math.isfinite(loss_value):
            print("Loss is {}, stopping training".format(loss_value))
            print(loss_dict_reduced)
            sys.exit(1)

        optimizer.zero_grad()
        losses.backward()
        optimizer.step()

        if lr_scheduler is not None:
            lr_scheduler.step()

        metric_logger.update(loss=losses_reduced, **loss_dict_reduced)
        metric_logger.update(lr=optimizer.param_groups[0]["lr"])
</code></pre>

username: jcong
cooked: <p>Hi, did you fixed that?</p>

username: kotchwane
cooked: <p>I met the same issue, and found the answer here:</p>
<aside class="onebox githubissue">
  <header class="source">
      <a href="https://github.com/pytorch/vision/issues/2167" target="_blank" rel="nofollow noopener">github.com/pytorch/vision</a>
  </header>
  <article class="onebox-body">
    <div class="github-row">
  <div class="github-icon-container" title="Issue">
	  <svg width="60" height="60" class="github-icon" viewbox="0 0 14 16" aria-hidden="true"><path d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg>
  </div>

  <div class="github-info-container">
    <h4>
      <a href="https://github.com/pytorch/vision/issues/2167" target="_blank" rel="nofollow noopener">engine.py error while following tutorial</a>
    </h4>

    <div class="github-info">
      <div class="date">
        opened <span class="discourse-local-date" data-format="ll" data-date="2020-04-30" data-time="13:21:38" data-timezone="UTC">01:21PM - 30 Apr 20 UTC</span>
      </div>

        <div class="date">
          closed <span class="discourse-local-date" data-format="ll" data-date="2020-04-30" data-time="14:09:29" data-timezone="UTC">02:09PM - 30 Apr 20 UTC</span>
        </div>

      <div class="user">
        <a href="https://github.com/EMCP" target="_blank" rel="nofollow noopener">
          <img alt="EMCP" src="https://avatars1.githubusercontent.com/u/3691722?v=4" class="onebox-avatar-inline" width="20" height="20">
          EMCP
        </a>
      </div>
    </div>
  </div>
</div>

<div class="github-row">
  <p class="github-content">📚 Documentation
I have found this library via the examples at https://pytorch.org/tutorials/intermediate/torchvision_tutorial.html
I ran the google colab and that successfully finished.. however when...</p>
</div>

<div class="labels">
    <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">module: models</span>
    <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">module: reference scripts</span>
    <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">question</span>
    <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">topic: object detection</span>
</div>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>