__call__() takes 2 positional arguments but 3 were given

username: Boudissa
cooked: <p>Hi, I am trying to implement mask R-CNN according to this tutorial <a href="https://pytorch.org/tutorials/intermediate/torchvision_tutorial.html" class="inline-onebox" rel="noopener nofollow ugc">TorchVision Object Detection Finetuning Tutorial — PyTorch Tutorials 2.1.1+cu121 documentation</a> I copied the code to my colab environment but it raises the following error when I execute the function main() :</p>
<p>TypeError: Caught TypeError in DataLoader worker process 0.<br>
Original Traceback (most recent call last):<br>
File “/usr/local/lib/python3.7/dist-packages/torch/utils/data/_utils/worker.py”, line 198, in _worker_loop<br>
data = fetcher.fetch(index)<br>
File “/usr/local/lib/python3.7/dist-packages/torch/utils/data/_utils/fetch.py”, line 44, in fetch<br>
data = [self.dataset[idx] for idx in possibly_batched_index]<br>
File “/usr/local/lib/python3.7/dist-packages/torch/utils/data/_utils/fetch.py”, line 44, in <br>
data = [self.dataset[idx] for idx in possibly_batched_index]<br>
File “/usr/local/lib/python3.7/dist-packages/torch/utils/data/dataset.py”, line 272, in <strong>getitem</strong><br>
return self.dataset[self.indices[idx]]<br>
File “”, line 66, in <strong>getitem</strong><br>
img, target = self.transforms(img, target)<br>
TypeError: <strong>call</strong>() takes 2 positional arguments but 3 were given</p>
<p>This issue :</p><aside class="quote quote-modified" data-post="1" data-topic="62529">
  <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/_/ac91a4/48.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/t-compose-typeerror-call-takes-2-positional-arguments-but-3-were-given/62529">T.Compose | TypeError: __call__() takes 2 positional arguments but 3 were given</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>
    I have been getting this odd error saying that I have passes too many arguments into my call() method. I am thoroughly perplexed because I am sure I have only passes 2 arguments. 
Here is my Compose class: 
class Compose(object):
    def __init__(self, transforms):
        self.transforms = transforms

    def __call__(self, image, target):
        for t in self.transforms:
            image, target = t(image, target)
        return image, target


Here’s the function that is call the method: 
d…
  </blockquote>
</aside>

<p>seems to be a similar error to mine, but I didnt create my own class for transforms, and I tried applying<br>
To.Tensor() last and still gave me the same error.</p>
<p>any help is appreciated <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>

username: Dwight_Foster
cooked: <p>Can you send your code? Do you use a custom dataset or a default one.</p>

username: Boudissa
cooked: <aside class="quote no-group quote-modified" data-username="Boudissa" data-post="1" data-topic="112782">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/boudissa/48/37859_2.png" class="avatar"> Boudissa:</div>
<blockquote>
<p><a href="https://pytorch.org/tutorials/intermediate/torchvision_tutorial.html" rel="noopener nofollow ugc">TorchVision Object Detection Finetuning Tutorial — PyTorch Tutorials 1.7.1 documentation</a></p>
</blockquote>
</aside>
<p>The code is copied exactly from this tutorial, I used the same dataset as in the tutorial.<br>
the only line I wrote is to execute the main() function.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/e/9/e995ae2c3096d9e3fbba71400da23ff4efef315f.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/e995ae2c3096d9e3fbba71400da23ff4efef315f" title="error"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/e/9/e995ae2c3096d9e3fbba71400da23ff4efef315f_2_690x373.jpeg" alt="error" data-base62-sha1="xknWeI3elgSOUJZRz3ESbdKHuOz" width="690" height="373" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/e/9/e995ae2c3096d9e3fbba71400da23ff4efef315f_2_690x373.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/e/9/e995ae2c3096d9e3fbba71400da23ff4efef315f_2_1035x559.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/e/9/e995ae2c3096d9e3fbba71400da23ff4efef315f.jpeg 2x" data-dominant-color="3B3B3C"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">error</span><span class="informations">1264×685 107 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

username: Dwight_Foster
cooked: <p>Hmm that is wierd because I am running it with no problems. Could you try and run the colab version <a href="https://colab.research.google.com/github/pytorch/vision/blob/temp-tutorial/tutorials/torchvision_finetuning_instance_segmentation.ipynb" rel="noopener nofollow ugc">here</a> to see if you get any errors. Also outside of the training loop can you do this</p>
<pre><code class="lang-auto">data_loader = torch.utils.data.DataLoader(
        dataset, batch_size=2, shuffle=True, num_workers=4,
        collate_fn=utils.collate_fn)
next(iter(data_loader))
</code></pre>
<p>just to see if you get an error doing that.</p>

username: Boudissa
cooked: <p>Thanks man ! I finally got it.<br>
The versions are identical, I also ran the line<br>
next(iter(data_loader))<br>
and it did raise the same error.</p>
<p>Weirdly enough, the problem seems to be related to how I import transforms…<br>
When I use this :</p>
<blockquote>
<p>import torchvision.transforms as T</p>
</blockquote>
<p>it raises an error, but when I replaced it with :</p>
<blockquote>
<p>import transforms as T</p>
</blockquote>
<p>It worked fine.<br>
No idea why!</p>

username: Dwight_Foster
cooked: <p>Huh that is weird glad you fixed it though.</p>

username: Boudissa
cooked: <p>It seems that for this particular tutorial it is required to use the transforms script in <code>references/detection/transforms.py</code> from the pytorch repo rather than the <code>torchvision.transforms</code> module.</p>

username: pvhdhxd
cooked: <p>Thank you so much Boudissa !</p>

username: lin_pan
cooked: <p>did you soulve the problem?  I have the same problem, too.  the code<br>
import transforms as T<br>
is correct in file tv-training-code.py.<br>
but it still doesn’t work. do you have any idea? thx~~~</p>

username: talhaanwarch
cooked: <p>try this<br>
<code>from torchvision import transforms as T</code></p>