GithubHelp home page GithubHelp logo

onnx2torch's People

Contributors

agoncharenko1992 avatar andreysher avatar cakeofwar42 avatar denisovap2013 avatar irkjero avatar ivkalgin avatar jan-haug avatar johnmasoner avatar karalinkw avatar monicathieu avatar niobeus avatar senysenyseny16 avatar seriousran avatar supersecurehuman avatar triple-mu avatar zhiqwang avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

onnx2torch's Issues

trained onnx model usage with pytorch

I have trained a resnet18 model in Matlab using the dnn machine learning toolbox and saved the model in onnx format. Now I am trying to use it but the predictions are wrong, in matlab the model is predicting correctly using its own variable which is mat. I think I can't figure out how to load this model properly.

model_path = "/kaggle/input/onnxmodel13/resnet18_classification.onnx"
original_model = onnx.load(model_path)

converted_model = version_converter.convert_version(original_model,13)

pytorch_model = convert(converted_model)
summary(pytorch_model.cuda(), (3, 224, 224))

image_path = '/kaggle/input/classb/Priority/4df9d3ae-ed9a-11ed-b020-a46bb6069316.jpg'

image = Image.open(image_path)


transform = transforms.Compose([
    transforms.Resize((224, 224)),  # Resize to match the input size expected by the model
    transforms.ToTensor(),  # Convert PIL Image to PyTorch tensor
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])  # Normalize pixel values
])
input_image = transform(image)


device = torch.device('cuda')
input_tensor = input_image.unsqueeze(0).to(device)


pytorch_model.eval()
with torch.no_grad():
    output = pytorch_model(input_tensor)

print(output)

_, predicted_idx = torch.max(output, 1)
predicted_class = predicted_idx.item()

class_names = ['Charlock', 'Priority', 'Stop', 'Sugarbeet']

predicted_class_name = class_names[predicted_class]
print("Predicted Class:", predicted_class_name)
print("Prediction:", output[0, predicted_class].item())

IndexError because pytorch does not support int32 for indexing like onnx

Been trying a bunch of things to solve this now but the error message isn't very helpful. It just sounds like it's just the input dtype that's handled incorrectly.

  File "/home/richard/miniconda3/envs/3.9.13/lib/python3.9/site-packages/torch/fx/graph_module.py", line 630, in wrapped_call
    raise e.with_traceback(None)
IndexError: tensors used as indices must be long, byte or bool tensors

Here is the onnx file I'm trying to convert
https://drive.google.com/file/d/1FX_D6dcYEoVssr-y29F5RLbhmhf4RmyZ/view?usp=sharing

It is supposed to take a tensor of type long. Here's an example in json:
https://drive.google.com/file/d/1yTSxwOY10g0cULEVt3KQZWSzziDpD8bC/view?usp=sharing

This is how I try to run it:

    model = torch.load(torch_path).eval().requires_grad_(False)

    tokens = torch.tensor(
        json.loads(Path(f"tests/{model_name}/tokens.json").read_text())
    ).long()
    text_encodings = torch.from_numpy(np.array(model(tokens)))

The onnx file works with onnxruntime and gives the correct result.

    tokens = np.array(
        json.loads(Path(f"tests/{model_name}/tokens.json").read_text())
    ).astype(np.int64)

    providers = ["CPUExecutionProvider"]
    m = rt.InferenceSession(onnx_path, providers=providers)
    text_encoding = m.run(output_names, dict(inputs=tokens))

Do you have any tips for what could be wrong?

Wrong shape from average pool

I've recently been trying to convert models on MONAI to onnx and then to torch. i'm having some problems getting wrong shapes when i set the deconv method to pixelshuffle.

Here is my code to better implement this.

❯ pip list | grep torch
onnx2torch                         1.5.4
pytorch-ignite                     0.4.9
torch                              1.12.1+cu116
torchaudio                         0.7.0a0+a853dff
torchfile                          0.1.0
torchio                            0.18.71
torchsummary                       1.5.1
torchvision                        0.8.2
❯ pip list | grep monai
monai                              1.1.0
❯ pip list | grep onnx
onnx                               1.13.0
onnx2torch                         1.5.4
onnxruntime                        1.13.1

script:

import onnx2torch
import torch
from monai.networks.nets import BasicUNetPlusPlus

model1 = BasicUNetPlusPlus(2, upsample = 'pixelshuffle')

batch_size = 1
x = torch.randn(batch_size, 1, 512, 512, requires_grad=True)
torch_out = model1(x)

# Export the model
torch.onnx.export(model1,               # model being run
                  x,                         # model input (or a tuple for multiple inputs)
                  "model.onnx",   # where to save the model (can be a file or file-like object)
                  export_params=True)

file = 'model.onnx'
model = onnx2torch.convert(file)

print(model)

I get the following error:

Traceback (most recent call last):
  File "/home/xxx/.local/lib/python3.7/site-packages/onnx2torch/node_converters/average_pool.py", line 30, in _
    avgpool_class = _AVGPOOL_CLASS_FROM_SPATIAL_RANK[spatial_rank]
KeyError: -2

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "torch2.py", line 18, in <module>
    model = onnx2torch.convert(file)
  File "/home/xxx/.local/lib/python3.7/site-packages/onnx2torch/converter.py", line 109, in convert
    torch_module, onnx_mapping = converter(onnx_node, onnx_graph)
  File "/home/xxx/.local/lib/python3.7/site-packages/onnx2torch/node_converters/average_pool.py", line 34, in _
    ) from exc
NotImplementedError: Average pool operation with spatial rank == -2 is not implemented

I print the code

def _(node: OnnxNode, graph: OnnxGraph) -> OperationConverterResult:
    input_value_info = graph.value_info[node.input_values[0]]
    input_shape = get_shape_from_value_info(input_value_info)
    print(input_shape)

the input shape is [0,0,0,0]

GatherND missing

While mentioned in release v1.2.0, GatherND is not implemented.
See #25.
Are there any plans to still add the operator?

Only Symmetric Padding Supported

Hello,

I converted a frozen tf network (pb) to onnx via tf2onnx.
Now I am trying to convert it to pytorch via this library.
However I get the following error:

    raise NotImplementedError(f'Only symmetric padding is implemented ({padding})')
NotImplementedError: Only symmetric padding is implemented ([1, 1, 0, 0])

Is this something I can fix? Is is fixable in general?

Best

ShapeInferenceError converting a Tile op

I'm trying to convert an ONNX RT-DETR model to PyTorch but I'm getting an error on a Tile op shape inference:

...
File "C:\Users\path\to\site-packages\onnx\shape_inference.py", line 77, in infer_shapes_path
    C.infer_shapes_path(model_path, output_path, check_type, strict_mode, data_prop)
onnx.onnx_cpp2py_export.shape_inference.InferenceError: [ShapeInferenceError] (op_type:Tile, node name: p2o.Tile.3): [ShapeInferenceError] Inferred shape and existing shape differ in rank: (0) vs (1)

the opset of the ONNX file is 16.

The ONNX file is too big to upload here.

bug: OnnxNot

OnnxNot disappears after creating fakequantized model eg using yolov3 model

torch.sum's keepdim argument should be bool not int

Hi
I discovered a common issue that frequently occurs in reduce.py.
Many places use an integer instead of a boolean for the "keepdim" parameter in torch.sum

I found someone had fixed one of the bugs: #171

Could you please fix similar issues in reduce.py

Thanks!

Function `convert` failed with layer `LayerNormalization`. However, `BatchNormalization` succeeds.

This is ipython code (at colab) which makes an error.

Code

# !pip install tensorflow==2.6.4 onnx==1.12.0 onnx2torch git+https://github.com/onnx/tensorflow-onnx

import tensorflow as tf
import onnx

from onnx2torch import convert

with tf.device("/cpu:0"):
    tf_model = tf.keras.Sequential()
    tf_model.add(tf.keras.layers.Input((123,)))
    tf_model.add(tf.keras.layers.LayerNormalization())
    tf.keras.models.save_model(
        tf_model,
        "model.tf",
        overwrite=True,
        include_optimizer=False,
        save_format=None,
        signatures=None,
        options=None,
        save_traces=True
    )
!python -m tf2onnx.convert --saved-model model.tf --output model.onnx --opset 11 --verbose
onnx_model = onnx.load("model.onnx")
encoder_pth = convert(onnx_model)

Error Message

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
[<ipython-input-2-2bc3e427f014>](https://localhost:8080/#) in <module>()
     22 get_ipython().system('python -m tf2onnx.convert --saved-model model.tf --output model.onnx --opset 11 --verbose')
     23 onnx_model = onnx.load("model.onnx")
---> 24 encoder_pth = convert(onnx_model)

1 frames
[/usr/local/lib/python3.7/dist-packages/onnx2torch/converter.py](https://localhost:8080/#) in convert(onnx_model_or_path, save_input_names, attach_onnx_mapping)
    107         )
    108 
--> 109         torch_module, onnx_mapping = converter(onnx_node, onnx_graph)
    110         if attach_onnx_mapping:
    111             setattr(torch_module, 'onnx_mapping', onnx_mapping)

[/usr/local/lib/python3.7/dist-packages/onnx2torch/node_converters/batch_norm.py](https://localhost:8080/#) in _(node, graph)
     24 def _(node: OnnxNode, graph: OnnxGraph) -> OperationConverterResult:
     25     scale_value_name = node.input_values[1]
---> 26     scale = graph.initializers[scale_value_name]
     27     scale = scale.to_torch()
     28 

KeyError: 'StatefulPartitionedCall/sequential_1/layer_normalization_1/ones:0'

NotImplementedError: "SAME_UPPER" auto_pad is not implemented

I met the NotImplementedError
When I convert onnx model yolov3-12.onnx (https://github.com/onnx/models/blob/main/vision/object_detection_segmentation/yolov3/model/yolov3-12.onnx).

ERROR:

Traceback (most recent call last):
  File "/home/anna/tools/converter/onnx2pytorch.py", line 157, in <module>
    model_converted = onnx2pytorch(args.onnx_file, verify=args.verify,
  File "/home/anna/tools/converter/onnx2pytorch.py", line 112, in onnx2pytorch
    torch_model = convert(onnx_model)
  File "/home/anna/anaconda3/envs/pytorch112-cuda116-py38/lib/python3.8/site-packages/onnx2torch/converter.py", line 110, in convert
    torch_module, onnx_mapping = converter(onnx_node, onnx_graph)
  File "/home/anna/anaconda3/envs/pytorch112-cuda116-py38/lib/python3.8/site-packages/onnx2torch/node_converters/conv.py", line 48, in _
    padding, input_padding_module = onnx_auto_pad_to_torch_padding(
  File "/home/anna/anaconda3/envs/pytorch112-cuda116-py38/lib/python3.8/site-packages/onnx2torch/utils/padding.py", line 33, in onnx_auto_pad_to_torch_padding
    raise NotImplementedError(f'"{auto_pad}" auto_pad is not implemented')
NotImplementedError: "SAME_UPPER" auto_pad is not implemented

onnx2torch version: 1.5.6

Please fix the wrong import error and wrong error messages.

OnnxResize and our pruning strategy

Our pruning strategy does not work when applied to the OnnxResize module due to the fact that the input shapes have changed, and the sizes that come to forward have not changed (if they are constant). It is necessary to remove the check during the inference and do the check during the creation of this module.

The code torch.Size(shape) does not work properly in trace mode when shape is of type Tensor.

its runs ok on normal mode but got this Error in torch.fx.trace or torch.fx.experimental.proxy_tensor.make_fx.

  File "/home/.../site-packages/onnx2torch/node_converters/expand.py", line 24, in _forward
    return input_tensor * torch.ones(torch.Size(shape), dtype=input_tensor.dtype, device=input_tensor.device)
TypeError: torch.Size() takes an iterable of 'int' (item 0 is 'Tensor')

workround:
change torch.Size(shape) to torch.Size(shape.numpy()) will solve the Error.

error: import fx from torch

when import torch.fx in converter.py line 10

attributeError: module 'torch' has no attribute '_assert'

torch:1.8

SAME_UPPER autopad method not implemented.

Hi, I have a custom UNET in ONNX and I see that when I try to convert the model into pytorch, the method mentioned above is not implemented. Would it be difficult to implement? or is there an alternative solution.

NotImplementedError: Converter is not implemented

File "/home/www/anaconda3/envs/gplsn/lib/python3.7/site-packages/onnx2torch/node_converters/registry.py", line 69, in get_converter
raise NotImplementedError(f'Converter is not implemented ({description})')
NotImplementedError: Converter is not implemented (OperationDescription(domain='', operation_type='Dropout', version=7))

onnx2torch==1.5.6

save converted yolov5

Exported the module to folder using "to_folder" and encounter "NameError: name 'onnx2torch_converter_lambda_' is not defined" error when inference after import the module. Any tips what's wrong?

AvgPool with non symmetrical padding is not implemented

I'm trying to convert a model from onnx to pytorch, however I'm get the following error:

     43 padding, padding_module = onnx_auto_pad_to_torch_padding(
     44     onnx_padding=node_attributes.get('pads', [0] * spatial_rank * 2),
     45     auto_pad=node_attributes.get('auto_pad', 'NOTSET'),
     46 )
     47 if padding_module is not None:
---> 48     raise NotImplementedError('AvgPool with non symmetrical padding is not implemented.')
     50 torch_module = avgpool_class(
     51     kernel_size=kernel_shape,
     52     stride=strides,
   (...)
     55     ceil_mode=ceil_mode == 1,
     56 )
     58 return OperationConverterResult(
     59     torch_module=torch_module,
     60     onnx_mapping=onnx_mapping_from_node(node=node),
     61 )

NotImplementedError: AvgPool with non symmetrical padding is not implemented.

Can't convert BERT model

Using the sample code in the README:

Trying to convert this model: https://huggingface.co/1-800-BAD-CODE/sentence_boundary_detection_multilang/blob/main/sbd_49lang_bert_small.onnx

This is a BERT based model.

>>> onnx_model_path = '/home/user/Downloads/sentence_boundary_detection_multilang/sbd_49lang_bert_small.onnx'
>>> torch_model_1 = convert(onnx_model_path)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/user/.local/lib/python3.11/site-packages/onnx2torch/converter.py", line 104, in convert
    converter = get_converter(
                ^^^^^^^^^^^^^^
  File "/home/user/.local/lib/python3.11/site-packages/onnx2torch/node_converters/registry.py", line 69, in get_converter
    raise NotImplementedError(f'Converter is not implemented ({description})')
NotImplementedError: Converter is not implemented (OperationDescription(domain='', operation_type='LayerNormalization', version=17))
>>> 

I get the above error.

A lot of Ops with their implementations.

Hi, developer team of onnx2torch.
I am currently developing an neural network quantization framework: https://github.com/openppl-public/ppq/tree/master/ppq.
The really interesting part is that we both need to run an onnx model with pytorch : )
I am glad to share our operator implementations with you: https://github.com/openppl-public/ppq/blob/master/ppq/executor/op/torch/default.py

We support following onnx operators by now(Still work in progress):

  1. 'Abs': Abs_forward,
  2. 'AdaptiveAvgPool2d': AdaptiveAvgPool2d_forward,
  3. 'And':And_forward,
  4. 'Add': Add_forward,
  5. 'ArgMax': ArgMax_forward,
  6. 'AveragePool': AveragePool_forward,
  7. 'BatchNormalization': BatchNormalization_forward,
  8. 'Cast': Cast_forward,
  9. 'Clip': Clip_forward,
  10. 'Concat': Concat_forward,
  11. 'Constant': Constant_forward,
  12. 'ConstantOfShape': ConstantOfShape_forward,
  13. 'Conv': Conv_forward,
  14. 'ConvTranspose': ConvTranspose_forward,
  15. 'Cos': Cos_forward,
  16. 'Div': Eltwise_forward,
  17. 'Equal': Equal_forward,
  18. 'Exp': UnaryEltwise_forward,
  19. 'Expand': Expand_forward,
  20. 'Flatten': Flatten_forward,
  21. 'Gather': Gather_forward,
  22. 'GatherElements': Gather_forward,
  23. 'GatherND': GatherND_forward,
  24. 'Gelu': Gelu_forward,
  25. 'Gemm': Gemm_forward,
  26. 'grid_sampler': Grid_sampler_forward,
  27. 'GlobalAveragePool': AveragePool_forward,
  28. 'GlobalMaxPool': MaxPool2d_forward,
  29. 'Greater': Greater_forward,
  30. 'LayerNorm': LayerNorm_forward,
  31. 'LeakyRelu': LeakyRelu_forward,
  32. 'Less': Less_forward,
  33. 'LogSoftmax': LogSoftmax_forward,
  34. 'MatMul': MatMul_forward,
  35. 'Max': Eltwise_forward,
  36. 'MaxPool': MaxPool2d_forward,
  37. 'Min': Eltwise_forward,
  38. 'Mul': Mul_forward,
  39. 'MultiHeadAttention': MultiHeadAttention_forward,
  40. 'NonMaxSuppression': _NMS_forward,
  41. 'NonZero': NonZero_forward,
  42. 'Not': Not_forward,
  43. 'Pad': Pad_forward,
  44. 'PRelu': PRelu_forward,
  45. 'Range': Range_forward,
  46. 'ReduceL2': ReduceL2_forward,
  47. 'ReduceMax': ReduceMax_forward,
  48. 'ReduceMean': ReduceMean_forward,
  49. 'ReduceSum': ReduceSum_forward,
  50. 'Relu': UnaryEltwise_forward,
  51. 'Reshape': Reshape_forward,
  52. 'Resize': Resize_forward,
  53. 'ScatterElements': ScatterElements_forward,
  54. 'ScatterND': ScatterND_forward,
  55. 'Shape': Shape_forward,
  56. 'Sigmoid': UnaryEltwise_forward,
  57. 'Sin': Sin_forward,
  58. 'Slice': Slice_forward,
  59. 'Softmax': Softmax_forward,
  60. 'Softplus': Softplus_forward,
  61. 'Split': Split_forward,
  62. 'Squeeze': Squeeze_forward,
  63. 'Sub': Eltwise_forward,
  64. 'Tile': Tile_forward,
  65. 'TopK': TopK_forward,
  66. 'Transpose': Transpose_forward,
  67. 'Unsqueeze': Unsqueeze_forward,
  68. 'Where': Where_forward,
  69. 'Sqrt': Sqrt_forward,
  70. 'Log': Log_forward,
  71. 'Floor': Floor_forward,
  72. 'RoiAlign': RoiAlign_forward,
  73. 'MMCVRoiAlign': MMCVRoiAlign_forward,
  74. 'SpaceToDepth': SpaceToDepth_forward,
  75. 'DepthToSpace': DepthToSpace_forward,
  76. 'Scale': Scale_forward, # caffe op
  77. 'Tanh': Tanh_forward,
  78. 'Pow': Pow_forward,
  79. 'Crop': Crop_forward, # caffe op
  80. 'ChannelShuffle': ChannelShuffle_forward, # caffe op
  81. 'InstanceNormalization': InstanceNormalization_forward,
  82. 'Parameter': Parameter_forward, # caffe op
  83. 'Interp': Interp_forward, # caffe op
  84. 'CaffeArgMax': CaffeArgMax_forward, # caffe op
  85. 'HardSigmoid': HardSigmoid_forward,
  86. 'HardSwish': HardSwish_forward,
  87. 'Neg': Neg_forward,
  88. 'GRU': GRU_forward,
  89. 'PPQDeviceSwitch': PPQDeviceSwitch_forward,
  90. 'Identity': Identity_forward,
  91. 'OneHot': Onehot_forward,
  92. 'Reciprocal': Reciprocal_forward,
  93. 'LSTM': LSTM_forward,

clip operation

raise NotImplementedError('Dynamic value of min/max is not implemented')

Missing parameters in GEMM layer when training with simple ONNX models

I'm using onnx2torch to train (rather simple) models provided as ONNX files. This worked fine with my models for onnx2torch version 1.5.2 but it seems that the changes in 1.5.3 around GEMM broke my workflow. The model returned from onnx2torch.convert since that version is missing its parameters as provided by model.parameters() to an optimizer. To reproduce here's an example, converting a simple Pytorch model to ONNX, then back to Pytorch using onnx2torch, then training this model:

import io

import onnx2torch
import torch

import onnx


def fit(dataset, model, loss_fn, optimizer):
    for inputs, target in dataset:
        pred = model(inputs)
        loss = loss_fn(pred, target)

        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        print(f"loss: {loss.item()}")


model = torch.nn.Sequential(
    torch.nn.Linear(20, 10),
    torch.nn.ReLU(),
    torch.nn.Linear(10, 1),
)

epochs = 2
batch_size = 16

model_bytes = io.BytesIO()
torch.onnx.export(
    model,
    torch.randn(batch_size, 20),
    model_bytes,
    do_constant_folding=False,
    training=torch.onnx.TrainingMode.TRAINING,
)

onnx_model = onnx2torch.convert(onnx.load_model_from_string(model_bytes.getvalue()))

inputs = torch.randn(32, 20)
target = torch.randn(32, 1)

ds = torch.utils.data.TensorDataset(inputs, target)
dataset = torch.utils.data.DataLoader(ds, batch_size=batch_size)

loss_fn = torch.nn.BCEWithLogitsLoss()
optimizer = torch.optim.Adam(onnx_model.parameters())

for epoch in range(epochs):
    print(f"epoch: {epoch+1}")
    fit(dataset, onnx_model, loss_fn, optimizer)

This is using Pytorch 1.3.1 and ONNX 1.13.0. It will work as expected with onnx2torch 1.5.2 but fails starting with 1.5.3.

Does not support non symmetric padding techniques.

When converting a model from onnx to pytorch I get the error,


~/.local/lib/python3.8/site-packages/onnx2torch/converter.py in convert(onnx_model_or_path, save_input_names, attach_onnx_mapping)
    120         )
    121 
--> 122         torch_module, onnx_mapping = converter(onnx_node, onnx_graph)
    123         if attach_onnx_mapping:
    124             setattr(torch_module, 'onnx_mapping', onnx_mapping)

~/.local/lib/python3.8/site-packages/onnx2torch/node_converters/conv.py in _(node, graph)
     48         dilation=node_attributes.get('dilations', 1),
     49         groups=node_attributes.get('group', 1),
---> 50         padding=onnx_padding_to_torch_padding(
     51             node_attributes.get('pads', [0] * spatial_rank * 2),
     52             node_attributes.get('auto_pad', 'NOTSET'),

~/.local/lib/python3.8/site-packages/onnx2torch/utils/common.py in onnx_padding_to_torch_padding(padding, auto_pad)
     80         half_len = len(padding) // 2
     81         if padding[:half_len] != padding[half_len:]:
---> 82             raise NotImplementedError(f'Only symmetric padding is implemented ({padding})')
     83 
     84         padding = padding[:half_len]

NotImplementedError: Only symmetric padding is implemented ([0, 0, 1, 1])```

KeyError: 'onnx::Conv_197' on converting torchvision resnet18

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

[Code Quality] Add Lint Action for python code auto-fixing

Code quality is also an important part of a project, and the earlier we add the format tool, the earlier we could control the quality of the code.
Python also has good tools to assist and automatically handle format, for example the pre-commit.

One of the simplest use cases can be found in #62 , pre-commit can help to auto remove the trailing whitespace and sort import module.

Further we can also add black and flake plugins to pre-commit, see https://pre-commit.com/#plugins for more details.

BTW, pre-commit has been widely used in many python projects , see https://github.com/pytorch/vision/blob/main/.pre-commit-config.yaml and https://github.com/google/jax/blob/main/.pre-commit-config.yaml as examples.

Is there a way to remove onnx dependency?

Hello there! Great job on the library. When I try to load the model with Torch, it requires having both ONNX and this library installed as dependencies. Is there a way to eliminate this dependencies completely, perhaps by manually rewriting the network?

KeyError: 'Tensor "onnx::Clip_2052" is not found in constant values'

Trying to convert a LayoutMl model to pytorch model getting this error :

KeyError Traceback (most recent call last)
/usr/local/lib/python3.8/dist-packages/onnx2torch/node_converters/clip.py in _(node, graph)
56 try:
---> 57 min_val = float(get_const_value(min_name, graph)) if min_name is not None else None
58 max_val = float(get_const_value(max_name, graph)) if max_name is not None else None

3 frames
KeyError: 'Tensor "onnx::Clip_2052" is not found in constant values'

The above exception was the direct cause of the following exception:

NotImplementedError Traceback (most recent call last)
/usr/local/lib/python3.8/dist-packages/onnx2torch/node_converters/clip.py in _(node, graph)
58 max_val = float(get_const_value(max_name, graph)) if max_name is not None else None
59 except KeyError as exc:
---> 60 raise NotImplementedError('Dynamic value of min/max is not implemented') from exc
61
62 torch_module = _create_torch_module(min_val=min_val, max_val=max_val)

NotImplementedError: Dynamic value of min/max is not implemented

release on conda-forge or similar?

Hi there,

Are there plans to release the package for conda installation through conda-forge or another channel? I would love to be able to conda install this package so it will track in my conda environments.

(My personal machine is on Mac OS, and I also work on a Linux server, so if I could put in requests for build OSs it would be those.)

thank you for your consideration!

Does not support the convert of xception?

I successfully converted the Xception ONNX model file to a .pt file, but the predicted data is inconsistent with the original ONNX file.

Is there any unsupported operation? but also did not throw any error message when converting.

Is There any possible way to have OnnxGemm converted into torch.nn.Linear?

Hi, It is kind of you to share your work.
Very appreciate it.

I have noticed that the GEMM layer of the onnx graph will be converted into a object of OnnxGemm.

for example:
image

then we check the pytorch model
image
and its forward
image

I wonder that is it possible to have these GEMMs converted into PyTorch native module?
And it will be much more scalable.

Thank you!!

Key error for Reshape when trying to convert ONNX model

Do you think this error occuring because the weights name is missing in the ONNX file or because the operation isn't found?

I'm getting the right results if I load and run the model with ONNX runtime at least.

  File "/home/richard/miniconda3/envs/3.9.13/lib/python3.9/site-packages/onnx2torch/converter.py", line 109, in convert
    torch_module, onnx_mapping = converter(onnx_node, onnx_graph)
  File "/home/richard/miniconda3/envs/3.9.13/lib/python3.9/site-packages/onnx2torch/node_converters/gemm.py", line 65, in _
    weights = graph.initializers[weights_value_name]
KeyError: 'Reshape__2177:0'

Here is a link to the ONNX file if you want to try it yourself
https://drive.google.com/file/d/1P_Bl7n2hbUoOhfMh_9UJfgK_N8kP_xjB/view?usp=sharing

I'm trying to make this model https://paperswithcode.com/paper/lit-zero-shot-transfer-with-locked-image-text easier to use for people

(Loop OP) need to GNN model based on keras to ONNX which can be converted into pytorch

Discussed in #138

Originally posted by Alicodee February 15, 2023
I need to use GNNExplainer which requires pytorch model.
However, my GNN model is written in keras libraries.
I tried to use this library to load my onnx model and convert into pytorch. Can Anyone help me to resolve this errors?
Here are the errors i got from it.

import tf2onnx

from keras2onnx import convert_keras

Load the Keras model

keras_model = model

Convert the Keras model to ONNX format

onnx_model, _ = tf2onnx.convert.from_keras(model)

Save the ONNX model to a file

with open('./my_onnx_model.onnx', 'wb') as f:
f.write(onnx_model.SerializeToString())

import onnx

onnx_model = onnx.load('./my_onnx_model.onnx')

import onnx
from onnx2torch import convert

torch_model_1 = convert(onnx_model)


NotImplementedError Traceback (most recent call last)
in
2 from onnx2torch import convert
3
----> 4 torch_model_1 = convert(onnx_model)
5
6 # from onnx2pytorch import ConvertModel

/working/Ali_code/custom_model_env/GNN/lib/python3.6/site-packages/onnx2torch/converter.py in convert(onnx_model_or_path, save_input_names, attach_onnx_mapping)
105 domain=onnx_node.domain,
106 operation_type=onnx_node.operation_type,
--> 107 version=version,
108 )
109

/working/Ali_code/custom_model_env/GNN/lib/python3.6/site-packages/onnx2torch/node_converters/registry.py in get_converter(operation_type, version, domain)
67 converter = _CONVERTER_REGISTRY.get(description, None)
68 if converter is None:
---> 69 raise NotImplementedError(f'Converter is not implemented ({description})')
70
71 return converter

NotImplementedError: Converter is not implemented (OperationDescription(domain='', operation_type='Loop', version=13))

require to support operator 'ArgMax'

Met the following error, hope to support operator 'ArgMax', thanks

Traceback (most recent call last):
File "anna_onnx2torch.py", line 38, in
torch_model_1 = convert(onnx_model_path)
File "/home/lin/anaconda3/envs/anna/lib/python3.8/site-packages/onnx2torch/converter.py", line 104, in convert
converter = get_converter(
File "/home/lin/anaconda3/envs/anna/lib/python3.8/site-packages/onnx2torch/node_converters/registry.py", line 69, in get_converter
raise NotImplementedError(f'Converter is not implemented ({description})')
NotImplementedError: Converter is not implemented (OperationDescription(domain='', operation_type='ArgMax', version=12))

support for layernorm

Are there any plans to support LayerNorm?
It's available in onnx-opset 17 which can be exported to from torch>=1.13
It's commonly used in transformers so would be very useful!

Segfault during GPU export of `Reshape` after importing model from ONNX

When you load a Reshape node, an OnnxReshape instance is created with custom logic for the torch.onnx export. (See https://github.com/ENOT-AutoDL/onnx2torch/blob/main/onnx2torch/node_converters/reshape.py#L32-L33 )

However, I'm getting segmentation faults when exporting the torch model using the GPU with this logic. Removing this if-condition (from the link above) entirely fixes the issue for me. What is the reason for this handling and is there a way around this or could it be extended to work with cuda too?

        if torch.onnx.is_in_onnx_export():
            return DefaultExportToOnnx.export(forward_lambda, 'Reshape', input_tensor, shape, {})

I'm running torch==1.13.1 and exporting with onnx opset 14. CPU export works fine but that's not really an option in my case unfortunately.

Standalone reproducer:

import os
import onnx2torch
import tempfile
import torch


class ReshapeModel(torch.nn.Module):
    def forward(self, x):
        return x.reshape(-1, 512)


def test_export():
    tmp_path = tempfile.mkdtemp()
    sample = torch.rand((1, 512, 1, 1))
    model = ReshapeModel()
    out_path = os.path.join(tmp_path, "temp.onnx")
    torch.onnx.export(model, sample, out_path)
    model_reconstructed = onnx2torch.convert(out_path)
    model_reconstructed.to("cuda")
    torch.onnx.export(model_reconstructed, sample, out_path)


if __name__ == "__main__":
    test_export()

NotImplementedError: Converter is not implemented

Hello :),

I installed the onnx2torch version 1.3.0 via pip.

Now I want to run the example code for converting my onnx file, but I always get the following error message:
NotImplementedError: Converter is not implemented (OperationDescription(domain='', operation_type='NonZero', version=9))

Did someone know how to solve it?

SOLUTION:
I could fix it. The problem was the "opset_version" when I converted it to the onnx file. The opset_version needs to be >=11.

Failed to convert SSDLite with MobileNetV2 backbone

Hi,

I am trying to convert this SSDLite model to pytorch but failed.
The link to the onnx file is https://drive.google.com/uc?id=1USFDPRH9JrVdGoqt27qHjRgittwc0kcO.
The error message is

>>> ssd = convert(onnxmodel)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/bo/miniconda3/envs/yolox/lib/python3.10/site-packages/onnx2torch/converter.py", line 104, in convert
    converter = get_converter(
  File "/home/bo/miniconda3/envs/yolox/lib/python3.10/site-packages/onnx2torch/node_converters/registry.py", line 69, in get_converter
    raise NotImplementedError(f'Converter is not implemented ({description})')
NotImplementedError: Converter is not implemented (OperationDescription(domain='', operation_type='Slice', version=1))

Processing of Yolo Detection layer

Hello! This is my first message, please excuse any formatting errors or offenses.

I'm trying to use this tool and I have a question about the implementation process of YOLOv5. How should I handle postprocessing if I convert the model directly? Initially, my plan was to remove layers like Detect, convert the model to ONNX, use onnx2torch, and then reassemble the model. However, this approach produces a single output when converted to ONNX, and connecting the outputs directly presents problems.

SyntaxError: future feature annotations is not defined

when import library

>>> from onnx2torch import convert
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/bin/python3/lib/python3.6/site-packages/onnx2torch/__init__.py", line 1, in <module>
    from onnx2torch.converter import convert
  File "/usr/bin/python3/lib/python3.6/site-packages/onnx2torch/converter.py", line 13, in <module>
    from onnx2torch.node_converters import get_converter
  File "/usr/bin/python3/lib/python3.6/site-packages/onnx2torch/node_converters/__init__.py", line 1, in <module>
    from onnx2torch.node_converters.activations import *
  File "/usr/bin/python3/lib/python3.6/site-packages/onnx2torch/node_converters/activations.py", line 6, in <module>
    from onnx2torch.node_converters.registry import add_converter
  File "/usr/bin/python3/lib/python3.6/site-packages/onnx2torch/node_converters/registry.py", line 7, in <module>
    from onnx2torch.onnx_graph import OnnxGraph
  File "/usr/bin/python3/lib/python3.6/site-packages/onnx2torch/onnx_graph.py", line 10, in <module>
    from onnx2torch.onnx_node import OnnxNode
  File "/usr/bin/python3/lib/python3.6/site-packages/onnx2torch/onnx_node.py", line 1
    from __future__ import annotations
    ^
SyntaxError: future feature annotations is not defined

This is pip list

Package                 Version
----------------------- ---------------------
absl-py                 1.0.0
cachetools              4.2.4
certifi                 2021.10.8
charset-normalizer      2.0.12
dataclasses             0.8
easydict                1.9
google-auth             1.35.0
google-auth-oauthlib    0.4.6
grpcio                  1.44.0
idna                    3.3
importlib-metadata      4.8.3
Markdown                3.3.6
numpy                   1.18.5
oauthlib                3.2.0
onnx                    1.11.0
onnx2torch              1.2.5
Pillow                  7.1.2
pip                     21.3.1
protobuf                3.19.4
pyasn1                  0.4.8
pyasn1-modules          0.2.8
requests                2.27.1
requests-oauthlib       1.3.1
rsa                     4.8
setuptools              59.5.0
six                     1.16.0
tensorboard             2.5.0
tensorboard-data-server 0.6.1
tensorboard-plugin-wit  1.8.1
thop                    0.0.31.post2005241907
torch                   1.9.0+cu111
torchaudio              0.9.0
torchvision             0.10.0+cu111
tqdm                    4.46.1
typing_extensions       4.1.1
urllib3                 1.26.9
Werkzeug                2.0.3
wheel                   0.37.1
zipp                    3.6.0

Is the python version(3.6.9) too old to use from __future__ import annotations ?
I need the version to run another AutoML project. Thanks.

Unable to successfully convert retinanet R50

Traceback (most recent call last):
File "/home/li/testcode/tool2/convert_dataloader.py", line 490, in
fp_model = copy.deepcopy(convert(onnx_model).to(device))

File "/home/li/.local/lib/python3.6/site-packages/onnx2torch/converter.py", line 107, in convert
version=version,
File "/home/li/.local/lib/python3.6/site-packages/onnx2torch/node_converters/registry.py", line 69, in get_converter
raise NotImplementedError(f'Converter is not implemented ({description})')
NotImplementedError: Converter is not implemented (OperationDescription(domain='', operation_type='NonZero', version=13))

I tried changing the opset but apparently it doesn't support non-zero operators. Can you tell me the details of the method the authors originally tested ? thanks

Warning happened when converting DeepLabv3

onnx2torch/node_converters/resize.py:149: UserWarning: For linear and cubic interpolation in "asymmetric" and "align_corners" coordinate_transformation_moderesults might differ significantly!

why does this warning occur and what kind of impact will it have?

raise KeyError: 'data' when inference using converted model

I tried the model:

Onnx model can inference correctly, but converted model (pytorch model) would raise KeyError: 'data'.

# Onnx inference code
import torch
import onnxruntime as ort
x = torch.ones((1, 3, 112, 112))
ort_sess = ort.InferenceSession(onnx_model_path)
outputs_ort = ort_sess.run(None, {'data': x.numpy()})
# Pytorch inference code
import onnx
onnx_model = onnx.load(onnx_model_path)
pytorch_model = ConvertModel(onnx_model)
pytorch_model(x)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.