onnx2pytorch icon indicating copy to clipboard operation
onnx2pytorch copied to clipboard

Issue with pad layer, keyword 'constant' unexpected

Open matthew-fotech opened this issue 3 years ago • 2 comments

 
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?

matthew-fotech avatar Dec 03 '21 14:12 matthew-fotech

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.

Talmaj avatar Dec 17 '21 14:12 Talmaj

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.

romain-pierre1 avatar Mar 15 '22 09:03 romain-pierre1