Onnx: ValueError: Don't know how to translate op Unsqueeze when running converted PyTorch Model

Created on 7 Oct 2018  路  4Comments  路  Source: onnx/onnx

I'm running into problems trying to use a PyTorch model (densenet121) exported as an ONNX model with Caffe2. Here is my export code

from torch import nn
import torch.onnx
import onnx
import onnx_caffe2.backend

the_model = torchvision.models.densenet121(pretrained=True)
garbage, model_inputs = preprocessing("test.jpg")
torch_out = torch.onnx._export(the_model,             
                           model_inputs,                       
                           "model_weights/chexnet-py.onnx",
                           export_params=True)

Now to test the model runs I use the following code

model = onnx.load("model_weights/chexnet-py.onnx")
garbage, model_inputs = preprocessing("text.jpg")
prepared_backend = onnx_caffe2.backend.prepare(model)
W = {model.graph.input[0].name: model_inputs.numpy()}
c2_out = prepared_backend.run(W)[0]

However this results in this error
ValueError: Don't know how to translate op Unsqueeze when running converted PyTorch Model

Most helpful comment

I found that the Caffe2 equivalence for Unsqueeze in ONNX is ExpandDims, and there is a special mapping in onnx_caffe2/backend.py around line 121 for those operators that are different only in their names and attribute names, but somehow Unsqueeze isn't presented there (have no idea why). So I manually added the mapping rules for it in the _renamed_operators and _per_op_renamed_attrs dicts and the code would look like:

    _renamed_operators = {
        'Caffe2ConvTranspose':   'ConvTranspose',
        'GlobalMaxPool':         'MaxPool',
        'GlobalAveragePool':     'AveragePool',
        'Pad':                   'PadImage',
        'Neg':                   'Negative',
        'BatchNormalization':    'SpatialBN',
        'InstanceNormalization': 'InstanceNorm',
        'MatMul':                'BatchMatMul',
        'Upsample':              'ResizeNearest',
        'Equal':                 'EQ',
        'Unsqueeze':             'ExpandDims',  # add this line
    }

    _global_renamed_attrs = {'kernel_shape': 'kernels'}
    _per_op_renamed_attrs = {
        'Squeeze':              {'axes': 'dims'},
        'Transpose':            {'perm': 'axes'},
        'Upsample':             {'mode': ''},
        'Unsqueeze':            {'axes': 'dims'},  # add this line
    }

And everything works as expected.

All 4 comments

Other info
Python 3.6
pytorch version 1.0.0a0+6f664d3
Caffe2 is latest version (attempted building from source, pip, and conda). All gave same result.
onnx 1.3.0

I also have same problem. Does anybody know the solution of this problem?

I found that the Caffe2 equivalence for Unsqueeze in ONNX is ExpandDims, and there is a special mapping in onnx_caffe2/backend.py around line 121 for those operators that are different only in their names and attribute names, but somehow Unsqueeze isn't presented there (have no idea why). So I manually added the mapping rules for it in the _renamed_operators and _per_op_renamed_attrs dicts and the code would look like:

    _renamed_operators = {
        'Caffe2ConvTranspose':   'ConvTranspose',
        'GlobalMaxPool':         'MaxPool',
        'GlobalAveragePool':     'AveragePool',
        'Pad':                   'PadImage',
        'Neg':                   'Negative',
        'BatchNormalization':    'SpatialBN',
        'InstanceNormalization': 'InstanceNorm',
        'MatMul':                'BatchMatMul',
        'Upsample':              'ResizeNearest',
        'Equal':                 'EQ',
        'Unsqueeze':             'ExpandDims',  # add this line
    }

    _global_renamed_attrs = {'kernel_shape': 'kernels'}
    _per_op_renamed_attrs = {
        'Squeeze':              {'axes': 'dims'},
        'Transpose':            {'perm': 'axes'},
        'Upsample':             {'mode': ''},
        'Unsqueeze':            {'axes': 'dims'},  # add this line
    }

And everything works as expected.

Please stop using onnx_caffe2 package (deprecated)... onnx_caffe2 is integrated into caffe2 as caffe2.python.onnx

In the Caffe2, Unsqueeze is supported well

Was this page helpful?
0 / 5 - 0 ratings