Skip to content
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 failure: unexpected number of inputs for node x.2 (_convolution): 13 #1667

Closed
gchihiha opened this issue Dec 11, 2020 · 19 comments
Labels
bug Something isn't working Stale Stale and schedule for closing soon

Comments

@gchihiha
Copy link

Before submitting a bug report, please be aware that your issue must be reproducible with all of the following, otherwise it is non-actionable, and we can not help you:

If this is a custom dataset/training question you must include your train*.jpg, test*.jpg and results.png figures, or we can not help you. You can generate these with utils.plot_results().

🐛 Bug

A clear and concise description of what the bug is.

To Reproduce (REQUIRED)

Input:
python ./models/export.py --weights ../init/yolov5s.pt --img 640 --batch 1
Output:


Namespace(batch_size=1, img_size=[640, 640], weights='../init/yolov5s.pt')
Fusing layers...
Model Summary: 140 layers, 7.45958e+06 parameters, 0 gradients

Starting TorchScript export with torch 1.7.0...
D:\ProgramData\Anaconda3\lib\site-packages\torch\jit\_trace.py:940: TracerWarning: Encountering a list at the output of the tracer might cause the trace to be incorrect, this is only valid if the container structure does not change based on the module's inputs. Consider using a constant container instead (e.g. for `list`, use a `tuple` instead. for `dict`, use a `NamedTuple` instead). If you absolutely need this and know the side effects, pass strict=False to trace() to allow this behavior.
  _force_outplace,
TorchScript export success, saved as ../init/yolov5s.torchscript.pt

Starting ONNX export with onnx 1.8.0...
ONNX export success, saved as ../init/yolov5s.onnx

Starting CoreML export with coremltools 4.0...
Tuple detected at graph output. This will be flattened in the converted model.
Converting graph.
Adding op '1' of type const
....
Converting op x.2 : _convolution
Converting Frontend ==> MIL Ops:   2%|█                                            | 23/932 [00:00<00:00, 962.15 ops/s]
CoreML export failure: unexpected number of inputs for node x.2 (_convolution): 13
Export complete (6.27s). Visualize with https://github.com/lutzroeder/netron.

Environment

  • OS: [windows10]
  • GPU [nvidia gtx1060]

Additional context

git clone https://github.com/ultralytics/yolov5.git

python train.py --img 640 --batch 8 --epochs 300 --data ./test_train_datas/data.yaml --cfg models/yolov5s.yaml
train done
get bast.pt

python ./models/export.py --weights ./best.pt --img-size 640 640
python ./models/export.py --weights ../best.pt --img 640 --batch 1
python ./models/export.py --weights ../yolov5s.pt --img 640 --batch 1

all is :
CoreML export failure: unexpected number of inputs for node x.2 (_convolution): 13

help!

@gchihiha gchihiha added the bug Something isn't working label Dec 11, 2020
@github-actions
Copy link
Contributor

github-actions bot commented Dec 11, 2020

Hello @gchihiha, 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 [email protected].

Requirements

Python 3.8 or later with all requirements.txt dependencies installed, including torch>=1.7. To install run:

$ pip install -r requirements.txt

Environments

YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

CI CPU testing

If 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.

@gchihiha
Copy link
Author

image
image

see

Python : 3.8.6
torch : '1.7.1+cu101'
yolov5: 3.1

pip install -r requirements.txt
pip install onnx>=1.7.0
pip install coremltools==4.0
done

train command:
python train.py --img 640 --batch 4 --epochs 300 --data ./test_train_datas/data.yaml --cfg models/yolov5s.yaml --weights ./weights/yolov5s.pt

convert to onnx command:
python ./models/export.py --weights ../result/best.pt --img 640 --batch 1

result:
CoreML export failure: unexpected number of inputs for node x.2 (_convolution): 13

@MaddyThakker
Copy link

Any updates on this? I am facing a similar error.

@hfzarslan
Copy link

Same issue, Any update please

@gchihiha
Copy link
Author

gchihiha commented Dec 12, 2020

python -V
Python 3.8.6

yolov5-3.1
yolov5-3.0
yolov5-2.0

pip list
Package Version


absl-py 0.11.0
attr 0.3.1
attrs 20.3.0
cachetools 4.1.1
certifi 2020.12.5
chardet 3.0.4
coremltools 4.0
cycler 0.10.0
Cython 0.29.21
google-auth 1.23.0
google-auth-oauthlib 0.4.2
grpcio 1.34.0
idna 2.10
kiwisolver 1.3.1
Markdown 3.3.3
matplotlib 3.3.3
mpmath 1.1.0
numpy 1.19.4
oauthlib 3.1.0
onnx 1.7.0
onnx-simplifier 0.2.19
onnxoptimizer 0.1.1
onnxruntime 1.6.0
opencv-python 4.4.0.46
packaging 20.7
Pillow 8.0.1
pip 20.3.1
protobuf 3.14.0
pyasn1 0.4.8
pyasn1-modules 0.2.8
pyparsing 2.4.7
python-dateutil 2.8.1
PyYAML 5.3.1
requests 2.25.0
requests-oauthlib 1.3.0
rsa 4.6
scipy 1.5.4
setuptools 49.2.1
six 1.15.0
sympy 1.7
tensorboard 2.4.0
tensorboard-plugin-wit 1.7.0
torch 1.7.1+cu101
torchaudio 0.7.2
torchvision 0.8.2+cu101
tqdm 4.54.1
typing-extensions 3.7.4.3
urllib3 1.26.2
Werkzeug 1.0.1
wheel 0.36.1

@MaddyThakker
Copy link

@glenn-jocher can you provide some insights?

@glenn-jocher
Copy link
Member

@MaddyThakker may be caused by more recent versions of torch, might try 1.6.0.

@MaddyThakker
Copy link

@glenn-jocher it works with torch 1.6.0. Thanks!

@gchihiha
Copy link
Author

torch 1.60 ok

pip uninstall torch
pip install torch==1.6.0+cu101 torchvision==0.7.0+cu101 -f https://download.pytorch.org/whl/torch_stable.html

Installing collected packages: future, torch, torchvision
Attempting uninstall: torchvision
Found existing installation: torchvision 0.8.2+cu101
Uninstalling torchvision-0.8.2+cu101:
Successfully uninstalled torchvision-0.8.2+cu101
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
torchaudio 0.7.2 requires torch==1.7.1, but you have torch 1.6.0+cu101 which is incompatible.

image

@Lewis-luo
Copy link

Lewis-luo commented Dec 23, 2020

I alse meet this issue and i could not find torch==1.6.0, what should i do

@kellen5l
Copy link

I alse meet this issue and i could not find torch==1.6.0, what should i do

try this
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple torch==1.6.0 torchvision==0.6.1 -f https://download.pytorch.org/whl/torch_stable.html

@jbrownkramer
Copy link

I have tried to export a yolov3 model as a coreml model. I also get this error. I have tried installing each of the versions of pytorch and torchvision suggested in the comments. None of them have worked. Specifically, I get the error

AttributeError: module 'torch.nn' has no attribute 'SiLU'

@jbrownkramer
Copy link

I have tried to export a yolov3 model as a coreml model. I also get this error. I have tried installing each of the versions of pytorch and torchvision suggested in the comments. None of them have worked. Specifically, I get the error

AttributeError: module 'torch.nn' has no attribute 'SiLU'

In the yolov3 model, all the activations that throw this error are leaky relus, which export just fine. So I wrapped this conversion in a try except, and simply don't convert it if it throws an exception. It successfully exported the model.

@kir486680
Copy link

@jbrownkramer could you please share your code of try except. I tried myself but I keep getting the same error.

@jbrownkramer
Copy link

jbrownkramer commented Jan 29, 2021

@jbrownkramer could you please share your code of try except. I tried myself but I keep getting the same error.

@kir486680 Yes. So first, I want to make it clear that I first did

pip uninstall torch
pip install torch==1.6.0+cu101 torchvision==0.7.0+cu101 -f https://download.pytorch.org/whl/torch_stable.html

That eliminated the error CoreML export failure: unexpected number of inputs for node x.2 (_convolution) and started giving me the error AttributeError: module 'torch.nn' has no attribute 'SiLU'. I then edited models/export.py from

if isinstance(m.act, nn.Hardswish):
    m.act = Hardswish()
elif isinstance(m.act, nn.SiLU):
    m.act = SiLU()

to

try:
    if isinstance(m.act, nn.Hardswish):
        m.act = Hardswish()
    elif isinstance(m.act, nn.SiLU):
        m.act = SiLU()
except:
    pass

Note also that I am exporting a YOLOv3 model, which doesn't have any Hardswish or SiLU activations to convert, so simply skipping the conversion isn't a problem for me.

@kir486680
Copy link

kir486680 commented Jan 30, 2021

@jbrownkramer Thank you! It worked. However, my converted model has 3 different outputs
image
The outputs are of shape [1,3,80,80,85], [1,3,40,40,85], [1,3,20,20,85]. Do you have any clue how to parse them? The previous Yolo models that I used have a single grid output that is 125x3x3.

@jbrownkramer
Copy link

@kir486680. Each output tensor corresponds to outputs at a particular scale. So you decode each one individually and then apply non-max suppression on all of the resulting boxes. For a particular output tensor, I believe the indices are (batch,anchor box, grid_y, grid_x, elements of encoded box). This link might help https://towardsdatascience.com/yolo-v3-object-detection-53fb7d3bfe6b#:~:text=YOLO%20v2%20used%20a%20custom,the%20layers%20downsampled%20the%20input.

@mzkaramat
Copy link

Head branch from coremltool repo which is compatible with PyTorch 1.7

pip install -Uqq git+https://github.com/apple/coremltools.git@master

@github-actions
Copy link
Contributor

github-actions bot commented Mar 7, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@github-actions github-actions bot added the Stale Stale and schedule for closing soon label Mar 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Stale Stale and schedule for closing soon
Projects
None yet
Development

No branches or pull requests

9 participants