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

Issue with pad layer, keyword 'constant' unexpected #29

Open
matthew-fotech opened this issue Dec 3, 2021 · 2 comments
Open

Issue with pad layer, keyword 'constant' unexpected #29

matthew-fotech opened this issue Dec 3, 2021 · 2 comments

Comments

@matthew-fotech
Copy link

 
layer.weight.data = torch.from_numpy(numpy_helper.to_array(weight))
Traceback (most recent call last):
 File "/Users/fotech/Onnx-pytorch.py", line 7, in <module>
 pytorch_model = ConvertModel(onnx_model)
 File "/Users/fotech/opt/anaconda3/envs/onnx_ML/lib/python3.9/site-packages/onnx2pytorch/convert/model.py", line 122, in __init__
    for op_id, op_name, op in convert_operations(
 File "/Users/fotech/opt/anaconda3/envs/onnx_ML/lib/python3.9/site-packages/onnx2pytorch/convert/operations.py", line 190, in convert_operations
    op = Pad(**extract_attributes(node))
TypeError: __init__() got an unexpected keyword argument 'constant'

got this error when trying to run ConvertModel using the process described in the readme, any idea what might be causing it?

@Talmaj
Copy link
Owner

Talmaj commented Dec 17, 2021

Can you please provide the content of node?
It should contain mode="constant" attribute. There is some bug there.
The easiest to do that is to enter the python debugger.
If you are running the code in ipython notebook write. %debug in the next cell, then print(node). Paste the output here.
If you are running the code from the command line, run it with: python -m pdb filename.py to enter the debugger.

@romain-pierre1
Copy link

romain-pierre1 commented Mar 15, 2022

I ran into this issue and propose the following workaround:

The content of the problematic node of op_type Pad is:
[name: "mode" s: "constant" type: STRING , name: "pads" ints: 0 ints: 0 ints: 0 ints: 0 ints: 0 ints: 0 ints: 0 ints: 0 type: INTS , name: "value" f: 0.0 type: FLOAT ]

The first two items get parsed correctly and correspond to the keywords of the Pad operator defined as:

class Pad(Operator):
    def __init__(self, mode="constant", padding=None):

The problem lies with the third item of the node attributes (which name is value and is a 0 of type float. The attribute parser will add a constant entry to the keyword dict:

elif attr.name == "value":
            kwargs["constant"] = extract_attr_values(attr)

Hence the error message : __init__() got an unexpected keyword argument 'constant'

To fix this, we can do the following:

onnx_model = onnx.load("my_model.onnx")

for i, node in enumerate(onnx_model.graph.node):
    if node.op_type == 'Pad': 
        del node.attribute[2] # delete the problematic third item

Note that you should print the node content to make sure which items generate the wrong behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants