__call__() takes 2 positional arguments but 3 were given

Hi, I am trying to implement mask R-CNN according to this tutorial TorchVision Object Detection Finetuning Tutorial — PyTorch Tutorials 2.1.1+cu121 documentation I copied the code to my colab environment but it raises the following error when I execute the function main() :

TypeError: Caught TypeError in DataLoader worker process 0.
Original Traceback (most recent call last):
File “/usr/local/lib/python3.7/dist-packages/torch/utils/data/_utils/worker.py”, line 198, in _worker_loop
data = fetcher.fetch(index)
File “/usr/local/lib/python3.7/dist-packages/torch/utils/data/_utils/fetch.py”, line 44, in fetch
data = [self.dataset[idx] for idx in possibly_batched_index]
File “/usr/local/lib/python3.7/dist-packages/torch/utils/data/_utils/fetch.py”, line 44, in
data = [self.dataset[idx] for idx in possibly_batched_index]
File “/usr/local/lib/python3.7/dist-packages/torch/utils/data/dataset.py”, line 272, in getitem
return self.dataset[self.indices[idx]]
File “”, line 66, in getitem
img, target = self.transforms(img, target)
TypeError: call() takes 2 positional arguments but 3 were given

This issue :

seems to be a similar error to mine, but I didnt create my own class for transforms, and I tried applying
To.Tensor() last and still gave me the same error.

any help is appreciated :slight_smile:

Can you send your code? Do you use a custom dataset or a default one.

The code is copied exactly from this tutorial, I used the same dataset as in the tutorial.
the only line I wrote is to execute the main() function.

Hmm that is wierd because I am running it with no problems. Could you try and run the colab version here to see if you get any errors. Also outside of the training loop can you do this

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

just to see if you get an error doing that.

Thanks man ! I finally got it.
The versions are identical, I also ran the line
next(iter(data_loader))
and it did raise the same error.

Weirdly enough, the problem seems to be related to how I import transforms…
When I use this :

import torchvision.transforms as T

it raises an error, but when I replaced it with :

import transforms as T

It worked fine.
No idea why!

Huh that is weird glad you fixed it though.

It seems that for this particular tutorial it is required to use the transforms script in references/detection/transforms.py from the pytorch repo rather than the torchvision.transforms module.

1 Like

Thank you so much Boudissa !

1 Like

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

try this
from torchvision import transforms as T