apple / coremltools

Core ML tools contain supporting tools for Core ML model conversion, editing, and validation.
https://coremltools.readme.io
BSD 3-Clause "New" or "Revised" License
4.46k stars 647 forks source link

Convert PyTorch FasterRCNN to CoreML fails #802

Open hovhanns opened 4 years ago

hovhanns commented 4 years ago

🐞Describe the bug

Trying to convert PyTorch FasterRCNN model to CoreML, but getting runtime error temporary: the only valid use of a module is looking up an attribute but found = prim::SetAttr[name="_has_warned"](%self, %6)

Trace

Traceback (most recent call last): File "/home/harut/Projects/Python/venvs/main.py", line 20, in main() File "/home/harut/Projects/Python/venvs/main.py", line 13, in main mlmodel = coremltools.converters.convert( File "/home/harut/Projects/Python/venvs/venv/lib/python3.8/site-packages/coremltools/converters/_converters_entry.py", line 292, in convert proto_spec = _convert( File "/home/harut/Projects/Python/venvs/venv/lib/python3.8/site-packages/coremltools/converters/mil/converter.py", line 120, in _convert prog = frontend_converter(model, *kwargs) File "/home/harut/Projects/Python/venvs/venv/lib/python3.8/site-packages/coremltools/converters/mil/converter.py", line 62, in call return load(args, **kwargs) File "/home/harut/Projects/Python/venvs/venv/lib/python3.8/site-packages/coremltools/converters/mil/frontend/torch/load.py", line 73, in load converter = TorchConverter(torchscript, inputs, outputs, cut_at_symbols) File "/home/harut/Projects/Python/venvs/venv/lib/python3.8/site-packages/coremltools/converters/mil/frontend/torch/converter.py", line 140, in init raw_graph, params_dict = self._expand_and_optimize_ir(self.torchscript) File "/home/harut/Projects/Python/venvs/venv/lib/python3.8/site-packages/coremltools/converters/mil/frontend/torch/converter.py", line 328, in _expand_and_optimize_ir graph, params = _torch._C._jit_pass_lower_graph( RuntimeError: temporary: the only valid use of a module is looking up an attribute but found = prim::SetAttr[name="_has_warned"](%self, %6) :

To Reproduce

import torch
import torchvision
import coremltools
model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True)
model.eval()
scripted_model = torch.jit.script(model)

mlmodel = coremltools.converters.convert(
    scripted_model,
    inputs=[coremltools.TensorType(shape=(1, 3, 512, 512))],
)

System environment (please complete the following information):

TobyRoseman commented 3 years ago

Thanks @hovhanns for creating this issue. Using your code I can reproduce this issue.

This is related to #1085. The error message is actually coming from a PyTorch binary. The user who created #1085 in the coremltools repository also created pytorch/pytorch#52889 in the PyTorch repository for the same issue.

hovhanns commented 3 years ago

@TobyRoseman seems we have some workaround here, please see the comment here. pytorch/pytorch/#41674

sdgandhi commented 3 years ago

@hovhanns the workaround says to rewrite your model architecture — which is not really a workaround. How can we help address this issue?