"transforms" helper function has no attribute ToTensor

Armin_Dashti: <p>I was reading a <a href="https://pytorch.org/tutorials/intermediate/torchvision_tutorial.html" rel="noopener nofollow ugc">tutorial</a> on the Pytorch website. Part of the tutorial used the “transforms” helper function.<br>
So far it’s no problem.<br>
The next step used an attribute named ToTensor() from the helper function.<br>
I got an error.<br>
AttributeError: module ‘detection.transforms’ has no attribute ‘ToTensor’<br>
I went to the source of the helper function to find out what is going on.<br>
I couldn’t find an attribute named ToTensor.<br>
Why does it use the attribute in the tutorial?</p>

harpone: <p>that’s a torchvision transform: <a href="https://pytorch.org/vision/stable/generated/torchvision.transforms.ToTensor.html?highlight=totensor#torchvision.transforms.ToTensor" class="inline-onebox" rel="noopener nofollow ugc">ToTensor — Torchvision 0.12 documentation</a></p>

InnovArul: <p>I think that the tutorial was written based on an old version (v0.8.2) of Torchvision source code.<br>
You can see <code>ToTensor()</code> method in the file:</p><aside class="onebox githubblob" data-onebox-src="https://github.com/pytorch/vision/blob/v0.8.2/references/detection/transforms.py">
  <header class="source">

      <a href="https://github.com/pytorch/vision/blob/v0.8.2/references/detection/transforms.py" target="_blank" rel="noopener">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/pytorch/vision/blob/v0.8.2/references/detection/transforms.py" target="_blank" rel="noopener">pytorch/vision/blob/v0.8.2/references/detection/transforms.py</a></h4>


      <pre><code class="lang-py">import random
import torch

from torchvision.transforms import functional as F


def _flip_coco_person_keypoints(kps, width):
    flip_inds = [0, 2, 1, 4, 3, 6, 5, 8, 7, 10, 9, 12, 11, 14, 13, 16, 15]
    flipped_data = kps[:, flip_inds]
    flipped_data[..., 0] = width - flipped_data[..., 0]
    # Maintain COCO convention that if visibility == 0, then x, y = 0
    inds = flipped_data[..., 2] == 0
    flipped_data[inds] = 0
    return flipped_data


class Compose(object):
    def __init__(self, transforms):
        self.transforms = transforms

</code></pre>



  This file has been truncated. <a href="https://github.com/pytorch/vision/blob/v0.8.2/references/detection/transforms.py" target="_blank" rel="noopener">show original</a>

  </article>

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

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

<p>Further, if you look at the <a href="https://colab.research.google.com/github/pytorch/tutorials/blob/gh-pages/_downloads/torchvision_finetuning_instance_segmentation.ipynb">Google Colab file</a>, you can notice that somewhere down the line, they checkout a specific Github tag (<code>v0.8.2</code>) before executing the code.</p>
<pre><code class="lang-auto"># Download TorchVision repo to use some files from
# references/detection
git clone https://github.com/pytorch/vision.git
cd vision
git checkout v0.8.2

cp references/detection/utils.py ../
cp references/detection/transforms.py ../
cp references/detection/coco_eval.py ../
cp references/detection/engine.py ../
cp references/detection/coco_utils.py ../
</code></pre>