-
-
Notifications
You must be signed in to change notification settings - Fork 16.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CoreML Export Error: export failure: 'torch._C.Node' object has no attribute 'ival' #2961
Comments
👋 Hello @samygarg, thank you for your interest in 🚀 YOLOv5! Please visit our ⭐️ Tutorials to get started, where you can find quickstart guides for simple tasks like Custom Data Training all the way to advanced concepts like Hyperparameter Evolution. If this is a 🐛 Bug Report, please provide screenshots and minimum viable code to reproduce your issue, otherwise we can not help you. If this is a custom training ❓ Question, please provide as much information as possible, including dataset images, training logs, screenshots, and a public link to online W&B logging if available. For business inquiries or professional support requests please visit https://www.ultralytics.com or email Glenn Jocher at glenn.jocher@ultralytics.com. RequirementsPython 3.8 or later with all requirements.txt dependencies installed, including $ pip install -r requirements.txt EnvironmentsYOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):
StatusIf this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training (train.py), testing (test.py), inference (detect.py) and export (export.py) on MacOS, Windows, and Ubuntu every 24 hours and on every commit. |
I have the same issue. The environment I have is: |
I also get the same issue. The environment I have is: |
Hey everyone, it appears that The solution is to comment the line before export. Optimally it should be an arg option, pull request anyone? Line 72 in 33712d6
|
Thanks @JorgeCeja, that solution worked for me. |
@JorgeCeja It solved the original issue but it still doesn't work. Tried on colab as well as macbook pro. Here's what I am getting: CoreML: starting export with coremltools 4.1...
Tuple detected at graph output. This will be flattened in the converted model.
Converting graph.
Adding op '1' of type const
Adding op '2' of type const
...
Converting op 728 : sub
Adding op '728' of type sub
Converting op 729 : add
Adding op '729' of type add
Converting op 730 : select
Converting Frontend ==> MIL Ops: 78% 545/695 [00:00<00:00, 933.85 ops/s]
CoreML: export failure: |
Thanks @JorgeCeja, the solution you provided worked for me. |
@JorgeCeja Can you share your environment? This change fixed the original issue, but I still face the same issue as @samygarg |
It seems CoreML export is broken in multiple ways currently. The export did work with the above change until very recently. However only when not specifying |
After trying many different previous commits (and different Pytorch versions) today my impression is that exporting the whole network including the Detect module to CoreML probably never worked? If anyone knows of a version/commit (and environment) where it did work I would love to know. |
I meet the same question, my enviroment: error message: hope somebody gave advice, thanks! |
@meng1994412 @haynec @JorgeCeja @samygarg good news 😃! Your original issue may now been fixed ✅ in PR #3055. Note that this does not solve CoreML export completely, but it should resolve the original error message in this issue. To receive this update you can:
Thank you for spotting this issue and informing us of the problem. Please let us know if this update resolves the issue for you, and feel free to inform us of any other issues you discover or feature requests that come to mind. Happy trainings with YOLOv5 🚀! |
@glenn-jocher using current version(hotfixed), OS: Ubuntu 20.04
@glenn-jocher using current version(hotfixed), OS: Ubuntu 20.04 Adding op '724' of type slice_by_index |
@jedikim He mentioned that this does not (yet?) fix CoreML export, it only fixes the particular issue reported in this bug report (the first post at the top). |
I try it again after update, but it output the same err result with yesterday, so as glenn-jocher mentioned above: this does not solve CoreML export completely |
Here are my two cents on this: Line 72 in 33712d6
as they said above. However, as @pocketpixels said, this will not export the complete model. Instead the outputs will be the nl outputs given by:Line 48 in 33712d6
Which means you have to do the grid scaling operations in the CoreML side, and concatenate the nl results to obtain a [n_achors x (nc+5)] matrix.Then you will need to adapt this to the input format of the Non Maxima Suppression layer:
I'm pretty new to using CoreM builder. So far I'm using this as my guideline: https://github.com/hollance/coreml-survival-guide/blob/master/MobileNetV2%2BSSDLite/ssdlite.py |
@meng1994412 @haynec @JorgeCeja @samygarg @glemarivero good news 😃! Outstanding CoreML export issues may now been fixed ✅ in a second PR #3066. This adds a
All batchnorm fusion ops have already occured at the new To receive this update you can:
Thank you for spotting this issue and informing us of the problem. Please let us know if this update resolves the issue for you, and feel free to inform us of any other issues you discover or feature requests that come to mind. Happy trainings with YOLOv5 🚀! |
yeah!!! I input a cmd: python models/export.py --train --weights yolov5s.pt --img 640 --batch 1, and it works ok with no error!!! |
Thanks for adding the |
@glemarivero yes the exported model can be used for any purpose. |
I meant that we still need to do what I put earlier. Aren't the outputs of the model still |
I agree with @pocketpixels and @glemarivero. |
Will the grid construction be included in CoreML export in the future update? |
It definitely would be desirable to have the detect module included in the CoreML output. And if and when we can get that to work it might also be worthwhile to add a CoreML NMS layer to the generated CoreML model (as discussed by @glemarivero). @glenn-jocher Do you happen to know which part of the Detect implementation the CoreML converter chokes on? Maybe it could be possible to find a workaround by reformulating one of the Pytorch operations involved? |
I looked into what is causing the export failure a bit. Lines 55 to 61 in d2a1728
If we comment out or remove those from the calculations then the CoreML conversion runs to completion (accessing and using I have not yet figured out though why these are causing problems. With |
Hi, I was able to put everything together. Take a look at this notebook: |
@glemarivero Fantastic work, thank you for sharing! |
Continuing my investigation into the cause for the error during the CoreML export of the Detect module: Lines 56 to 57 in d2a1728
With these modifications the CoreML conversion completes without errors: s = self.stride[i].item()
ag = self.anchor_grid[i].numpy()
y[..., 0:2] = (y[..., 0:2] * 2. - 0.5 + self.grid[i]) * s # xy
y[..., 2:4] = (y[..., 2:4] * 2) ** 2 * ag # wh That is if we force Pytorch to treat Clearly the above change is not the solution (as I believe it would impact inference performance), but maybe it is a good hint at what a better solution might be (for someone like @glenn-jocher who understands the code base and PyTorch better than I do)? Update: While the conversion completes, looking at the resulting graph in Netron I don't think it actually includes the box coordinate computations. Update 2: s = self.stride[i].item()
ag = self.anchor_grid[i].view(1, self.na, 1, 1, 2).numpy()
xy = (y[..., 0:2] * 2. - 0.5 + self.grid[i]) * s # xy
wh = (y[..., 2:4] * 2) ** 2 * ag # wh
y = torch.cat((xy, wh, y[..., 4:]), -1) |
@meng1994412 @glemarivero @pocketpixels to clarify, all modules including the Detect() layer are exported by export.py, no modules are missing. The Lines 57 to 58 in 251aeaf
|
but if you do how do you continue? how do you get the final bounding boxes? |
In case anyone is interested, I put together a script to output a CoreML .mlmodel that can be opened with XCode (the previous model wasn't), and can be used to preview inference results inside it. Again, I only did it for yolov5s.
|
Thanks for sharing @glemarivero. |
Nice work @pocketpixels! Thanks for sharing 🙂 |
🐛 Bug
I am trying to export the default trained YOLOv5 Model as given here to CoreML but getting an error on both Colab as well as my laptop:
CoreML: export failure: 'torch._C.Node' object has no attribute 'ival'
To Reproduce (REQUIRED)
Follow the steps mentioned here.
Expected behavior
Export the CoreML model successfully.
Environment
Colab and Macbook Pro 13 inch 2019.
The text was updated successfully, but these errors were encountered: