AttributeError: module 'utils' has no attribute 'collate_fn'

I exactly copied this code from this tutorial but I got error.
model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True)
dataset = PennFudanDataset(‘PennFudanPed’, get_transform(train=True))
data_loader = torch.utils.data.DataLoader(
dataset, batch_size=2, shuffle=True, num_workers=4,
collate_fn=utils.collate_fn)

The error:
AttributeError: module ‘utils’ has no attribute ‘collate_fn’

I solved it.
The problem was a conflict between the utils package (Not related to PyTorch) and utils in PyTorch.
You can see utils.py on Github.
I wonder why PyTorch didn’t mention this issue in its tutorial.

How did you solve it? can you provide the code here? please

utils is from the detection file.

I just copied the file and then called it. like this:
from detection import utils
Then you can call collate_fn like this:
utils.collate_fn
You can download the file from github directly if you can’t find it.

1 Like

it is showing error as “no module named detection”. I have copied the utils file from detection and have placed it in the working directory.

Resolved. Had to copy Detection folder in the working directory. Thanks.

You’re welcome.
It’s a better idea if you copy detection folder. Maybe you will need files in detection like engine.py

Hi, is your main function available on GitHub?"

Many thanks.
I had the same problem and just solved it by following your guide.