AttributeError: module 'transforms' has no attribute 'Normalize'

JohnPolo: <p>Hi. I’m using torchvision 0.13.0.</p>
<p>I had this problem:</p>
<blockquote>
<p>TypeError: Expected input images to be of floating type (in range [0, 1]), but found type torch.uint8 instead</p>
</blockquote>
<p>When I was attempting to do this:</p>
<pre><code class="lang-auto">import transforms as T


def get_transform(train):
    transform = []
    # converts the image, a PIL image, into a PyTorch Tensor
    transform.append([T.PILToTensor(), T.Normalize()])
    if train:
        # during training, randomly flip the training images
        # and ground-truth for data augmentation
        transform.append(T.RandomHorizontalFlip(0.5))
    return T.Compose(transform)


model = torchvision.models.detection.fasterrcnn_resnet50_fpn(weights=True)
dataset = four_chs(root = '/home/jn/Downloads/', transforms = get_transform(train=True))   

data_loader = torch.utils.data.DataLoader(
 dataset, batch_size=1, shuffle=True, num_workers=2,
 collate_fn=utils.collate_fn)

# For Training
images,targets = next(iter(data_loader))
images = list(image for image in images)
targets = [{k: v for k, v in t.items()} for t in targets]
output = model(images,targets)   # Returns losses and detections
</code></pre>
<p>I searched some and <a href="https://stackoverflow.com/questions/64635630/pytorch-runtimeerror-expected-scalar-type-float-but-found-byte" rel="noopener nofollow ugc">found something</a> that suggested using a Normalize transform. I’m not sure how to string together multiple transforms, so I tried the code above. That returns this error:</p>
<blockquote>
<p>AttributeError: module ‘transforms’ has no attribute ‘Normalize’</p>
</blockquote>
<p>The Normalize transform <a href="https://pytorch.org/vision/stable/transforms.html" rel="noopener nofollow ugc">seems to be defined</a>, so I’m not sure why this error happens. How do I address this?</p>

ptrblck: <p>It seems you are importing a custom <code>transforms</code> module and not <code>torchvision.transforms</code>, which doesn’t seem to have the <code>Normalize</code> transformation.<br>
Could you check where this module is defined and also check why it’s used?</p>

JohnPolo: <p>Hi ptrblck. I used this section earlier in the code. Sorry I didn’t include it in first post.</p>
<pre><code class="lang-auto">if os.path.isdir("vision") == True:
    print("vision present")
else:
    Repo.clone_from("https://github.com/pytorch/vision.git", "vision")

shutil.copy('vision/references/detection/utils.py', 'utils.py')
shutil.copy('vision/references/detection/transforms.py', 'transforms.py')
shutil.copy('vision/references/detection/coco_eval.py', 'coco_eval.py')
shutil.copy('vision/references/detection/engine.py', 'engine.py')
shutil.copy('vision/references/detection/coco_utils.py', 'coco_utils.py')
</code></pre>
<p>I used that because it’s mostly the same as what’s provided here:</p><aside class="onebox allowlistedgeneric" data-onebox-src="https://colab.research.google.com/github/pytorch/tutorials/blob/gh-pages/_downloads/torchvision_finetuning_instance_segmentation.ipynb#scrollTo=l79ivkwKy357">
  <header class="source">
      <img src="https://colab.research.google.com/img/favicon.ico?vrz=colab-20220715-060045-RC00_461147733" class="site-icon" width="128" height="128">

      <a href="https://colab.research.google.com/github/pytorch/tutorials/blob/gh-pages/_downloads/torchvision_finetuning_instance_segmentation.ipynb#scrollTo=l79ivkwKy357" target="_blank" rel="noopener nofollow ugc">colab.research.google.com</a>
  </header>

  <article class="onebox-body">
    <img src="https://colab.research.google.com/img/colab_favicon_256px.png" class="thumbnail onebox-avatar" width="260" height="260">

<h3><a href="https://colab.research.google.com/github/pytorch/tutorials/blob/gh-pages/_downloads/torchvision_finetuning_instance_segmentation.ipynb#scrollTo=l79ivkwKy357" target="_blank" rel="noopener nofollow ugc">Google Colaboratory</a></h3>



  </article>

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

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

<p>That Colab tutorial relies on torchvision 0.12.0, I think, and I’m trying to use 0.13.0 at this point, so I’m using most of the same code.</p>
<p>I see “<a href="https://github.com/pytorch/vision/tree/main/torchvision/transforms" class="inline-onebox" rel="noopener nofollow ugc">vision/torchvision/transforms at main · pytorch/vision · GitHub</a>”. I should use that instead? It seems to have the transforms module, but not engine. I suppose I should copy both repos and import the requisite modules accordingly.</p>

ptrblck: <p>Thanks for the explanation.<br>
Import the <code>torchvision.transforms</code> and your local <code>transforms</code> as different modules and it should work.<br>
E.g.</p>
<pre><code class="lang-python">from transforms as det_transforms
import torchvision.transforms as transforms
</code></pre>
<p>and use these two names to separate the transformations.</p>