dbsystel / yolov5-coreml-tools

Scripts for exporting YOLOv5 models to CoreML and benchmarking it.
Apache License 2.0
68 stars 9 forks source link

Getting error torch.nn.modules.module.ModuleAttributeError: 'Detect' object has no attribute 'inplace' #2

Open samygarg opened 3 years ago

samygarg commented 3 years ago

Thank you very much for creating this repo.

I placed the default yolov5s.pt file in the root directory and ran the following command:

poetry run coreml-export --model-input-path yolov5s.pt

But getting this error:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/samygarg/Documents/yolov5-coreml-tools/src/coreml_export/main.py", line 318, in main
    model(sampleInput)
  File "/Users/samygarg/Library/Caches/pypoetry/virtualenvs/coreml-tools-LHeNloI_-py3.7/lib/python3.7/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/Users/samygarg/Documents/yolov5/models/yolo.py", line 117, in forward
    return self.forward_once(x, profile)  # single-scale inference, train
  File "/Users/samygarg/Documents/yolov5/models/yolo.py", line 148, in forward_once
    x = m(x)  # run
  File "/Users/samygarg/Library/Caches/pypoetry/virtualenvs/coreml-tools-LHeNloI_-py3.7/lib/python3.7/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/Users/samygarg/Documents/yolov5/models/yolo.py", line 55, in forward
    if self.inplace:
  File "/Users/samygarg/Library/Caches/pypoetry/virtualenvs/coreml-tools-LHeNloI_-py3.7/lib/python3.7/site-packages/torch/nn/modules/module.py", line 779, in __getattr__
    type(self).__name__, name))
torch.nn.modules.module.ModuleAttributeError: 'Detect' object has no attribute 'inplace'

Please let me know how I can get it to work.

Leon0402 commented 3 years ago

Your welcome!

Could you please answer the following questions:

From a first guess I would say you use an (out of the box) incompatible combination of yolov5, pytorch and coremltools. I might be able to include a fix for this if I know more details.

For a first test, I would recommend downloading the yolov5s version4 model and using it with the repo as is. This should work out of the box.

samygarg commented 3 years ago

@Leon0402 Thank you very much your quick reply.

I switched to the glenn-jocher-patch-4 branch and used the pretrained v4 model and it seemed to work. Not sure why this is though.

Similarly, it works for other branches like the C6 branch.

When I run the same on the master branch, I get the earlier error:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/samygarg/Documents/yolov5-coreml-tools/src/coreml_export/main.py", line 318, in main
    model(sampleInput)
  File "/Users/samygarg/Library/Caches/pypoetry/virtualenvs/coreml-tools-LHeNloI_-py3.7/lib/python3.7/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/Users/samygarg/Documents/yolov5/models/yolo.py", line 117, in forward
    return self.forward_once(x, profile)  # single-scale inference, train
  File "/Users/samygarg/Documents/yolov5/models/yolo.py", line 148, in forward_once
    x = m(x)  # run
  File "/Users/samygarg/Library/Caches/pypoetry/virtualenvs/coreml-tools-LHeNloI_-py3.7/lib/python3.7/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/Users/samygarg/Documents/yolov5/models/yolo.py", line 55, in forward
    if self.inplace:
  File "/Users/samygarg/Library/Caches/pypoetry/virtualenvs/coreml-tools-LHeNloI_-py3.7/lib/python3.7/site-packages/torch/nn/modules/module.py", line 779, in __getattr__
    type(self).__name__, name))
torch.nn.modules.module.ModuleAttributeError: 'Detect' object has no attribute 'inplace'
Leon0402 commented 3 years ago

Where do you get the pretrained model from?

On the Bottom you can download the Yolov5s version Version 4 https://github.com/ultralytics/yolov5/releases/tag/v4.0

Then you need to check out the source code for exactly this model: git switch tags/v4 -b putSomeBranchTagHere

Then you need to add a setup.py with the dependencies, so poetry installs all dependencies of yolov5 correctly.

Therefore: The pretrained model has to match the source code it was pretrained with!

If you use the model from https://github.com/ultralytics/yolov5/releases/tag/v5.0, then checkout the v5.0 tag.

As I said this should work with v2.0 , v3.0, v4.0 tag. There might be issues with the v5.0 tag, I haven't tried it yet. The reasons why the version used is so important is because yolo uses newer pytorch version and features, but coremltools (the library used for export) doesn't support the newer versions. So in order to use the v5 model I probably need to update the coremltools lib (if there is one) or even include a patch. I included a patch for v4 version otherwise it wouldn't work at the moment.