[error] torchvision object detection finetuning tutorial

DongHwanJang: <p>This tutorial(<a href="https://pytorch.org/tutorials/intermediate/torchvision_tutorial.html" rel="noopener nofollow ugc">https://pytorch.org/tutorials/intermediate/torchvision_tutorial.html</a>) contains codes for not only 1) finetuning but also 2)modifying the model to add a different backbone.</p>
<p>In the code given for the second option, I think there is a typo in it.<br>
There is a code like below:</p>
<blockquote>
<p>roi_pooler = torchvision.ops.MultiScaleRoIAlign(featmap_names=[0],                                                 output_size=7, sampling_ratio=2)</p>
</blockquote>
<p>However, this code gives an error.<br>
If the output of the backbone network is Tensor type, then its featmap_names should be [‘0’] rather than [0]. Because of this, if you just execute the given code it makes ‘out of range’ error since it cannot find any matching feature having 0 as a key (not ‘0’)</p>
<p>This is related to this line</p><aside class="onebox githubblob" data-onebox-src="https://github.com/pytorch/vision/blob/e92b5155d7d78363826a822ccacf7c046d19245a/torchvision/models/detection/generalized_rcnn.py#L97">
  <header class="source">

      <a href="https://github.com/pytorch/vision/blob/e92b5155d7d78363826a822ccacf7c046d19245a/torchvision/models/detection/generalized_rcnn.py#L97" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/pytorch/vision/blob/e92b5155d7d78363826a822ccacf7c046d19245a/torchvision/models/detection/generalized_rcnn.py#L97" target="_blank" rel="noopener nofollow ugc">pytorch/vision/blob/e92b5155d7d78363826a822ccacf7c046d19245a/torchvision/models/detection/generalized_rcnn.py#L97</a></h4>



    <pre class="onebox"><code class="lang-py">
      <ol class="start lines" start="87" style="counter-reset: li-counter 86 ;">
          <li>        if degenerate_boxes.any():</li>
          <li>            # print the first degenrate box</li>
          <li>            bb_idx = degenerate_boxes.any(dim=1).nonzero().view(-1)[0]</li>
          <li>            degen_bb: List[float] = boxes[bb_idx].tolist()</li>
          <li>            raise ValueError("All bounding boxes should have positive height and width."</li>
          <li>                             " Found invaid box {} for target at index {}."</li>
          <li>                             .format(degen_bb, target_idx))</li>
          <li></li>
          <li>features = self.backbone(images.tensors)</li>
          <li>if isinstance(features, torch.Tensor):</li>
          <li class="selected">    features = OrderedDict([('0', features)])</li>
          <li>proposals, proposal_losses = self.rpn(images, features, targets)</li>
          <li>detections, detector_losses = self.roi_heads(features, proposals, images.image_sizes, targets)</li>
          <li>detections = self.transform.postprocess(detections, images.image_sizes, original_image_sizes)</li>
          <li></li>
          <li>losses = {}</li>
          <li>losses.update(detector_losses)</li>
          <li>losses.update(proposal_losses)</li>
          <li></li>
          <li>if torch.jit.is_scripting():</li>
          <li>    if not self._has_warned:</li>
      </ol>
    </code></pre>



  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>


ptrblck: <p>I executed the code on Colab and it finished without raising an error.<br>
Are you getting an error using your custom code or the tutorial directly?</p>

DongHwanJang: <p>I used the code directly on Colab environment. What I did is copy the suggested code and replace ‘get_instance_segmentation_model’ method with the copied code.</p>
<p>The following messages are the exact error message that I get.</p>
<pre><code class="lang-auto">/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py:2854: UserWarning: The default behavior for interpolate/upsample with float scale_factor will change in 1.6.0 to align with other frameworks/libraries, and use scale_factor directly, instead of relying on the computed output size. If you wish to keep the old behavior, please set recompute_scale_factor=True. See the documentation of nn.Upsample for details. 
  warnings.warn("The default behavior for interpolate/upsample with float scale_factor will change "
/pytorch/torch/csrc/utils/python_arg_parser.cpp:756: UserWarning: This overload of nonzero is deprecated:
	nonzero(Tensor input, *, Tensor out)
Consider using one of the following signatures instead:
	nonzero(Tensor input, *, bool as_tuple)
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
&lt;ipython-input-9-1ea96e94502e&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()

7 frames
/usr/local/lib/python3.6/dist-packages/torchvision/ops/poolers.py in setup_scales(self, features, image_shapes)
    157         # get the levels in the feature map by leveraging the fact that the network always
    158         # downsamples by a factor of 2 at each level.
--&gt; 159         lvl_min = -torch.log2(torch.tensor(scales[0], dtype=torch.float32)).item()
    160         lvl_max = -torch.log2(torch.tensor(scales[-1], dtype=torch.float32)).item()
    161         self.scales = scales

IndexError: list index out of range
</code></pre>
<p>And Recently I find a similar question that has been asked before.</p>
<aside class="quote quote-modified" data-post="1" data-topic="66549">
  <div class="title">
    <div class="quote-controls"></div>
    <img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/m/779978/48.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/confusion-re-faster-rcnn-mobilenet-example/66549">Confusion re: Faster-RCNN Mobilenet Example</a> <a class="badge-category__wrapper " href="/c/vision/5"><span data-category-id="5" data-drop-close="true" class="badge-category " title="Topics related to either pytorch/vision or vision research related topics"><span class="badge-category__name">vision</span></span></a>
  </div>
  <blockquote>
    Hi all, I have some confusion regarding the mobilenet example provided in the <a href="https://github.com/pytorch/vision/blob/master/torchvision/models/detection/faster_rcnn.py" rel="noopener nofollow ugc">F-RCNN Code</a>. 
In particular, copying the code as given in the example: 
import torch
import torchvision
from torchvision.models.detection import FasterRCNN
from torchvision.models.detection.rpn import AnchorGenerator
# load a pre-trained model for classification and return
# only the features
backbone = torchvision.models.mobilenet_v2(pretrained=False).features

# FasterRCNN needs to know the number of
# output channel…
  </blockquote>
</aside>

<p>It might be related to the version issue. But I think at least it should have no error on Colab.</p>

azer: <p>I got the same warning message when running torchvision_finetuning_instance_segmentation tutorial in colab.</p>