KeyError: 'onnx::Conv_197' on converting torchvision resnet18
Opened this issue · 1 comments
nihui commented
python 3.11
torch-2.0.0+cpu
import torch
import torchvision
import torch.onnx
model = torchvision.models.resnet18()
model = model.eval()
x = torch.rand(1, 3, 224, 224)
torch.onnx.export(model, x, "resnet18.onnx", export_params=True, opset_version=13)
from onnx2torch import convert
torch_model_1 = convert('resnet18.onnx')
============== Diagnostic Run torch.onnx.export version 2.0.0+cpu ==============
verbose: False, log level: Level.ERROR
======================= 0 NONE 0 NOTE 0 WARNING 0 ERROR ========================
Traceback (most recent call last):
File "/home/nihui/osd/ncnn-nihui/tools/pnnx/build2/src/test.py", line 15, in <module>
torch_model_1 = convert('resnet18.onnx')
^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/nihui/.local/lib/python3.11/site-packages/onnx2torch/converter.py", line 110, in convert
torch_module, onnx_mapping = converter(onnx_node, onnx_graph)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/nihui/.local/lib/python3.11/site-packages/onnx2torch/node_converters/conv.py", line 33, in _
bias = graph.initializers[bias_value_name]
~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
KeyError: 'onnx::Conv_197'
[nihui@nihuini-LC2 src]$ pip list
Package Version
----------------------- --------------------
absl-py 1.4.0
argcomplete 2.0.0
asttokens 2.2.1
attrs 22.1.0
Babel 2.10.3
backcall 0.2.0
Beaker 1.10.0
beautifulsoup4 4.12.0
blivet 3.5.0
blivet-gui 2.4.1
cached-property 1.5.2
cachetools 5.3.0
cffi 1.15.1
chardet 5.1.0
charset-normalizer 2.1.0
contourpy 1.0.7
cryptography 37.0.2
cupshelpers 1.0
cycler 0.11.0
dasbus 1.6
dbus-python 1.3.2
decorator 5.1.1
distro 1.7.0
dnspython 2.2.1
einops 0.6.0
executing 1.2.0
fb-re2 1.0.7
file-magic 0.4.0
filelock 3.9.0
fonttools 4.38.0
gitdb 4.0.10
GitPython 3.1.31
google-auth 2.16.0
google-auth-oauthlib 0.4.6
gpg 1.17.0
grpcio 1.51.1
gssapi 1.7.3
humanize 3.13.1
idna 3.3
iniconfig 1.1.1
initial-setup 0.3.95
ipython 8.10.0
jedi 0.18.2
Jinja2 3.1.2
kiwisolver 1.4.4
koji 1.32.0
langtable 0.0.61
libcomps 0.1.18
lit 15.0.7
lxml 4.9.1
Mako 1.1.4
Markdown 3.4.1
MarkupSafe 2.1.1
matplotlib 3.7.0
matplotlib-inline 0.1.6
mercurial 6.2.3
meson 1.0.1
mpmath 1.2.1
ncnn 1.0.20230426
networkx 3.0
nftables 0.1
ntpsec 1.2.2
numpy 1.23.5
oauthlib 3.2.2
olefile 0.46
onnx 1.13.1
onnx2torch 1.5.6
opencv-python 4.7.0.68
packaging 21.3
pandas 1.5.3
parso 0.8.3
Paste 3.5.0
pexpect 4.8.0
pickleshare 0.7.5
pid 2.2.3
Pillow 9.2.0
pip 22.2.2
pluggy 1.0.0
ply 3.11
portalocker 2.7.0
productmd 1.35
progressbar2 3.53.2
prompt-toolkit 3.0.36
protobuf 3.20.3
psutil 5.9.4
ptyprocess 0.6.0
pure-eval 0.2.2
pwquality 1.4.5
py 1.11.0
pyasn1 0.4.8
pyasn1-modules 0.2.8
pycairo 1.21.0
pycparser 2.20
pycrypto 2.6.1
pycups 2.0.1
pycurl 7.45.1
pygit2 1.7.1
Pygments 2.14.0
PyGObject 3.42.2
pykickstart 3.41
pyOpenSSL 21.0.0
pyparsing 3.0.9
pyparted 3.12.0
PySocks 1.7.1
pytest 7.1.3
python-augeas 1.1.0
python-dateutil 2.8.2
python-gettext 4.0
python-manatools 0.0.4
python-meh 0.50
python-utils 3.1.0
pytz 2023.3
pyudev 0.23.2
PyYAML 6.0
requests 2.28.1
requests_download 0.1.2
requests-file 1.5.1
requests-ftp 0.3.1
requests-gssapi 1.2.3
requests-oauthlib 1.3.1
rpm 4.18.0
rpmautospec 0.3.5
rsa 4.9
SciPy 1.8.1
seaborn 0.12.2
selinux 3.5
sepolicy 3.5
setools 4.4.0
setproctitle 1.2.3
setuptools 62.6.0
simpleline 1.9.0
six 1.16.0
slip 0.6.4
slip.dbus 0.6.4
smmap 5.0.0
sos 4.4
soupsieve 2.3.2.post1
SSSDConfig 2.8.2
stack-data 0.6.2
sympy 1.11.1
systemd-python 235
Tempita 0.5.2
tensorboard 2.12.0
tensorboard-data-server 0.7.0
tensorboard-plugin-wit 1.8.1
thop 0.1.1.post2209072238
toml 0.10.2
tomli 2.0.1
torch 2.0.0+cpu
torchaudio 2.0.1+cpu
torchvision 0.15.1+cpu
tqdm 4.64.1
traitlets 5.9.0
typing_extensions 4.4.0
urllib3 1.26.12
wcwidth 0.2.6
Werkzeug 2.2.3
wheel 0.38.4
07571026 commented
Hi,
I had a similar issue to yours, and I solved it.
Traceback (most recent call last):
File "d:\onnx2torch\convert.py", line 6, in <module>
torch_model = convert(model)
File "c:\Python\Python39\pyenv\lib\site-packages\onnx2torch\converter.py", line 110, in convert
torch_module, onnx_mapping = converter(onnx_node, onnx_graph)
File "c:\Python\Python39\pyenv\lib\site-packages\onnx2torch\node_converters\conv.py", line 29, in _
weights = graph.initializers[weights_value_name]
KeyError: 'conv2d_0.w_0'
I found that if a node is trainable, its category must be 'Initializer' rather than 'Constant' in ONNX.
Therefore, I changed the trainable node, which was in the 'Constant' category, to 'Initializer,' and the issue was resolved.
Could you check you node's category if Constant or not?
You can use netron check the node
If you need to change the category of a node, you can follow these steps:
- use
onnx.helper.make_tensor
to creat a tensor - use
graph.initializer.extend
to extend the tensor - use
graph.node.remove
to original node onnx.checker.check_model
check your onnx model
a demo for you:
import onnx
import numpy as np
from onnx import helper
model_path = "Orinigal.onnx"
NewModel_path = "New.onnx"
onnx_model = onnx.load(model_path)
graph = onnx_model.graph
for node in graph.node:
if node.name == "TargetNode":
for attr in node.attribute:
initializer_node = helper.make_tensor(
name=attr.t.name,
data_type = attr.t.data_type,
dims = attr.t.dims,
vals= np.frombuffer(attr.t.raw_data, np.float32)
)
graph.initializer.extend([initializer_node])
graph.node.remove(node)
onnx.checker.check_model(onnx_model)
onnx.save(onnx_model, NewModel_path)
PS: it's just demo, so you may change code and test.
If there is any question, please let me know.