GithubHelp home page GithubHelp logo

onnx2pytorch's People

Contributors

calvinmccarter-at-lightmatter avatar talmaj 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

onnx2pytorch's Issues

cannot import name '_LazyBatchNorm' from 'torch.nn.modules.batchnorm'

code is broken on latest torch 1.10.0

>>> import torch
>>> torch.__version__
'1.10.0+cu102'
>>> import onnx2pytorch
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<...>/lib/python3.9/site-packages/onnx2pytorch/__init__.py", line 1, in <module>
    from .convert import ConvertModel
  File "<...>/lib/python3.9/site-packages/onnx2pytorch/convert/__init__.py", line 1, in <module>
    from .model import ConvertModel
  File "<...>/lib/python3.9/site-packages/onnx2pytorch/convert/model.py", line 14, in <module>
    from onnx2pytorch.constants import (
  File "<...>/lib/python3.9/site-packages/onnx2pytorch/constants.py", line 4, in <module>
    from onnx2pytorch.operations import (
  File "<...>/lib/python3.9/site-packages/onnx2pytorch/operations/__init__.py", line 2, in <module>
    from .batchnorm import BatchNormWrapper
  File "<...>/lib/python3.9/site-packages/onnx2pytorch/operations/batchnorm.py", line 4, in <module>
    from torch.nn.modules.batchnorm import _BatchNorm, _LazyBatchNorm
ImportError: cannot import name '_LazyBatchNorm' from 'torch.nn.modules.batchnorm' (<...>/lib/python3.9/site-packages/torch/nn/modules/batchnorm.py)

TypeError: list indices must be integers or slices, not list

File "/opt/conda/envs/torch/lib/python3.7/site-packages/torch/nn/modules/module.py", line 727, in _call_impl result = self.forward(*input, **kwargs) File "/opt/conda/envs/torch/lib/python3.7/site-packages/onnx2pytorch-0.4.1-py3.7.egg/onnx2pytorch/convert/model.py", line 300, in forward File "/opt/conda/envs/torch/lib/python3.7/site-packages/torch/nn/modules/module.py", line 727, in _call_impl result = self.forward(*input, **kwargs) File "/opt/conda/envs/torch/lib/python3.7/site-packages/onnx2pytorch-0.4.1-py3.7.egg/onnx2pytorch/operations/gather.py", line 15, in forward TypeError: list indices must be integers or slices, not list

when convert dlrm onnx to pytorch model, I got this error, and I debug the data shape is torch.Size([4, 2]) selection shape is torch.Size([1, 128]) , the data and the selection shape is mismatch, I find that dlrm has dynamic axes.
any idea to solve this error? thanks very much!

RuntimeError: Given groups=1, weight of size [32, 3, 3, 3], expected input[1, 224, 4, 225] to have 3 channels, but got 224 channels instead

  1. Classifier Model trained on tensorflow efficientnetb0 architecture on 224*224 images with 3 channels and this model converted to .onnx format.
  2. Then, .onnx to pytorch model in .pth format.
  3. While loading a model on 224*224 images getting an error like
    RuntimeError: Given groups=1, weight of size [32, 3, 3, 3], expected input[1, 224, 4, 225] to have 3 channels, but got 224 channels instead

Unable to convert yolov4 onnx file

python onnxtoPytorch.py Traceback (most recent call last): File "onnxtoPytorch.py", line 5, in <module> pytorch_model = ConvertModel(onnx_model) File "/home/sharoze/.local/lib/python3.8/site-packages/onnx2pytorch/convert/model.py", line 63, in __init__ for op_id, op_name, op in convert_operations(onnx_model, batch_dim): File "/home/sharoze/.local/lib/python3.8/site-packages/onnx2pytorch/convert/operations.py", line 182, in convert_operations raise NotImplementedError( NotImplementedError: Conversion not implemented for op_type=Softplus.

UserWarning: NumPy array is not writeable

When converting an onnx model to pytorch I'm getting this warning:

UserWarning: The given NumPy array is not writeable, and PyTorch does not support non-writeable tensors. This means you can write to the underlying (supposedly non-writeable) NumPy array using the tensor. You may want to copy the array to protect its data or make it writeable before converting it to a tensor

It's caused by a method load_params() in onnx2pytorch\convert\layer.py and line 138 in onnx2pytorch\convert\model.py.

For example when this torch.from_numpy(numpy_helper.to_array(tensor)) is modified to use the numpy copy torch.from_numpy(np.copy(numpy_helper.to_array(tensor))) then all the warnings are gone.

Please, can you comment on this?

change torch.max to torch.amax when reducing on multiple dimensions

I was trying to convert Keras's GlobalMaxPooling2D to PyTorch through ONNX but failed. After a rough investigation, I notice that onnx2pytorch will convert ReduceMax to torch.max, however, torch.max can only support one-dimension reduction while ReduceMax sometimes supports two-dimension reduction. In this case, we may use torch.amax instead of torch.max.

Script to reproduce is as follows:

# Build the model
import tensorflow as tf
import tf2onnx
x = tf.keras.layers.Input((32, 32, 3))
y = tf.keras.layers.GlobalMaxPooling2D()(x)
model = tf.keras.Model(x, y)
model.summary()
# Convert the model
input_shape = model.layers[0].input_shape[0]
spec = (tf.TensorSpec(input_shape, tf.float32, name="input"),)
_, _ = tf2onnx.convert.from_keras(model, input_signature=spec, \
        opset=15, output_path="temp.onnx")
from onnx2pytorch import ConvertModel
import onnx
onnx_model = onnx.load("temp.onnx")
torch_model = ConvertModel(onnx_model, experimental=True)

# Predict
import torch
import numpy as np
input = np.random.rand(10, *input_shape[1:])
input = torch.from_numpy(input)
torch_model.double()
pred = torch_model(input)
pred = pred.detach().numpy()
print("The prediction is: ", pred.shape)

You can also access the code below:
https://colab.research.google.com/drive/1mwoJDtjroZ6ynNtaFdu-2EjX9eVTd-pS?usp=sharing

The crash information is as follows:

/usr/local/lib/python3.7/dist-packages/onnx2pytorch/convert/model.py in forward(self, *input_list, **input_dict)
    222                     activations[out_op_id] = output
    223             else:
--> 224                 activations[out_op_id] = op(*in_activations)
    225 
    226             # Remove activations that are no longer needed

TypeError: max() received an invalid combination of arguments - got (Tensor, dim=tuple, keepdim=bool), but expected one of:
 * (Tensor input)
 * (Tensor input, Tensor other, *, Tensor out)
 * (Tensor input, int dim, bool keepdim, *, tuple of Tensors out)
 * (Tensor input, name dim, bool keepdim, *, tuple of Tensors out)

To fix this bug, my suggestion is to add a guard on ReduceMax (line 200 in convert/operations.py) as follows:

200         elif node.op_type == "ReduceMax":
201             kwargs = dict(keepdim=True)
202             kwargs.update(extract_attributes(node))
+ 203             if isinstance(kwargs["dim"], (tuple, list)) and len(kwargs["dim"]) > 1: 
+ 204                 op = partial(torch.amax, **kwargs)
+ 205             else:
206                 op = partial(torch.max, **kwargs)

Another idea is to just replace torch.max with torch.amax, but it seems that these two still have some differences so I am not sure such replacement is safe.

Please see if it is fine, I can contribute to a pull request.

Issue with pad layer, keyword 'constant' unexpected

 
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?

DepthToSpace

Currently DepthToSpace throws a NotImplemented error, any plan to support this?

Thanks

Refactor test cases

Based on #47, it looks like the test cases need to be refactored based on the new onnx/pytorch versions so that the CI doesn't fail.

Once this is fixed, the CI for #46 needs to be restarted again.

onnx2pytorch does not have __version__

Python 3.9.7 | packaged by conda-forge | (default, Sep 29 2021, 19:23:11) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import onnx2pytorch
>>> onnx2pytorch.__version__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'onnx2pytorch' has no attribute '__version__'
>>> 

Handle indices output in MaxPool

If the ONNX MaxPool node has two outputs (ie Y and Indices), the corresponding nn.MaxPool{}d module should be initialized with return_indices=True.

(I hope to take care of this in my next PR...)

Fail when converting the ReduceMax op type. Perhaps not considering the torch.max's return value

I was trying to convert a Keras model to PyTorch through ONNX but failed.
The information of my targeted model is as follows

_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
input_1 (InputLayer)         [(None, 32, 3)]           0
_________________________________________________________________
global_max_pooling1d         (None, 3)                 0
_________________________________________________________________
dense (Dense)                (None, 96)                384
_________________________________________________________________
reshape (Reshape)            (None, 32, 3)             0
=================================================================
Total params: 384
Trainable params: 384
Non-trainable params: 0
_________________________________________________________________

The script to reproduce is as follows:

# Build the model
import tensorflow as tf
import tf2onnx
x = tf.keras.layers.Input((32,3))
in1 = tf.keras.layers.GlobalMaxPooling1D()(x)
in2 = tf.keras.layers.Dense(96)(in1)
y = tf.keras.layers.Reshape((32,3))(in2)
model = tf.keras.Model(x, y)
model.summary()

# Convert the model
input_shape = model.layers[0].input_shape[0]
spec = (tf.TensorSpec(input_shape, tf.float32, name="input"),)
_, _ = tf2onnx.convert.from_keras(model, input_signature=spec, \
        opset=15, output_path="temp.onnx")
from onnx2pytorch import ConvertModel
import onnx
onnx_model = onnx.load("temp.onnx")
torch_model = ConvertModel(onnx_model, experimental=True)

# Predict
import torch
import numpy as np
input = np.random.rand(10, *input_shape[1:])
input = torch.from_numpy(input)
torch_model.double()
pred = torch_model(input)
pred = pred.detach().numpy()
print("The prediction is: ", pred.shape)

You may access the code here:
https://colab.research.google.com/drive/1EtuxhHjy3QdmCf4v6DSpN9jsde2SeNpW?usp=sharing

The crash information is as follows:

/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py in linear(input, weight, bias)
   1846     if has_torch_function_variadic(input, weight, bias):
   1847         return handle_torch_function(linear, (input, weight, bias), input, weight, bias=bias)
-> 1848     return torch._C._nn.linear(input, weight, bias)
   1849 
   1850 

TypeError: linear(): argument 'input' (position 1) must be Tensor, not torch.return_types.max

Without very deep investigation, I assume this problem is caused by torch.max()'s output, which is torch.return_types.max instead of a torch tensor, while the linear layer expect the input to be a tensor.

I guess the fix of this bug would be change torch.max(**kwargs) to torch.max(**kwargs)[0] to make the output data type to be tensor. But I am new to this project and don't know how to write some fixable codes. Can you check if this is actually a bug and how can we fix it?

list index out of range

I am facing this error when converting an onnx model using ConvertModel function:

/usr/local/lib/python3.7/dist-packages/onnx2pytorch/convert/operations.py in convert_operations(onnx_graph, opset_version, batch_dim, enable_pruning)
    157 
    158                 # check if next node Add to add bias
--> 159                 next_node = onnx_graph.node[i + 1]
    160                 next_params = [
    161                     weights[par_name]

IndexError: list index (74) out of range

my code:

onnx_model = onnx.load(load_model_path)
torch_model = ConvertModel(onnx_model)

Some of the tox tests are failing

When I run tox on the current tests and models, I get some errors.

  • yolo_v4.onnx gives me rather substantial mismatch with ONNXruntime on test_onnx2pytorch2onnx (Mismatched elements: 659440 / 689520 (95.6%) -- Max absolute difference: 1.4529846 -- Max relative difference: 98651.)

  • fast_neural_style.onnx gives me an error via F.interpolate in the Resize operator:

    result = self.forward(*input, **kwargs)
onnx2pytorch/operations/resize.py:61: in forward
    return super().forward(inp, torch.tensor([]), scales, torch.tensor([]))
onnx2pytorch/operations/resize.py:48: in forward
    return F.interpolate(
...
        if input.dim() == 3 and mode == "nearest":
            return torch._C._nn.upsample_nearest1d(input, output_size, scale_factors)
        if input.dim() == 4 and mode == "nearest":
>           return torch._C._nn.upsample_nearest2d(input, output_size, scale_factors)
E           TypeError: upsample_nearest2d() received an invalid combination of arguments - got (Tensor, NoneType, list), but expected one of:
E            * (Tensor input, tuple of ints output_size, tuple of floats scale_factors)
E                 didn't match because some of the arguments have invalid types: (Tensor, !NoneType!, !list!)
E            * (Tensor input, tuple of ints output_size, float scales_h, float scales_w, *, Tensor out)
.tox/py38/lib/python3.8/site-packages/torch/nn/functional.py:3535: TypeError

In terms of my environment, I get the following from pip freeze:

attrs==20.3.0
certifi==2020.12.5
distlib==0.3.1
filelock==3.0.12
iniconfig==1.1.1
numpy==1.20.2
onnx==1.9.0
onnxruntime==1.7.0
packaging==20.9
pluggy==0.13.1
protobuf==4.0.0rc2
py==1.10.0
pyparsing==2.4.7
pytest==6.2.3
six==1.15.0
toml==0.10.2
torch==1.4.0
tox==3.23.0
typing-extensions==3.7.4.3
virtualenv==20.4.4```

If MatMul the last node

/onnx2pytorch/convert/operations.py line 159, IndexError: list index out of range occurs if MatMul is the last node in the graph.

Class `ConvertModel` 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 onnx2pytorch git+https://github.com/onnx/tensorflow-onnx

import tensorflow as tf
import onnx

from onnx2pytorch import ConvertModel

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 = ConvertModel(onnx_model)

Error Message

---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
[<ipython-input-3-a685e6e12ecf>](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 = ConvertModel(onnx_model)

1 frames
[/usr/local/lib/python3.7/dist-packages/onnx2pytorch/convert/operations.py](https://localhost:8080/#) in convert_operations(onnx_graph, opset_version, batch_dim, enable_pruning)
    278             if op is None:
    279                 raise NotImplementedError(
--> 280                     "Conversion not implemented for op_type={}.".format(node.op_type)
    281                 )
    282             else:

NotImplementedError: Conversion not implemented for op_type=ReduceSumSquare.

Refactor ConvertModel and Loop

There is a lot of code similarities in ConvertModel and Loop. Try to refactor it in such a way that the code is re-used.
Creating a base class for both of them or one inheriting another one would be a viable option.

Debug mode modifies the ONNX protobuf, changing behavior of forward pass

The behavior of the forward pass may change when debug=True because debug mode has the side effect of modifying the ONNX protobuf. This causes a problem in particular when a model has multiple outputs: the forward pass will only return the first output. The other output nodes get consumed and are not returned.

Forward pass of Reshape

I'm having difficulty understanding what's going on in the forward pass of Reshape. It seems to me that shape = [x if x != 0 else input.size(i) for i, x in enumerate(shape)] is exactly what the ONNX spec calls for. Is the additional work being done for correctness purposes, or for speedup purposes?

Deleting the additional code is necessary to get onnx2pytorch to work on the Nvidia BERT-Large model here, so perhaps there should be a flag set during init that controls whether this additional work is performed.

Error import _LazyBatchNorm

from torch.nn.modules.batchnorm import _BatchNorm, _LazyBatchNorm ImportError: cannot import name '_LazyBatchNorm' from 'torch.nn.modules.batchnorm' (/Users/xx/.pyenv/versions/3.9.6/lib/python3.9/site-packages/torch/nn/modules/batchnorm.py)

Incorrect output shape when setting pool_size as (odd, even) on MaxPooling2D

I was trying to convert a Keras to ONNX then to PyTorch through onnx2pytorch but failed.

The model I was trying to convert is as follows with input shape as (1, 32, 32, 3):

layer_stack = [
    keras.layers.MaxPooling2D(pool_size=(2,1), strides=1, padding='same'),
    keras.layers.Flatten(),
    keras.layers.Dense(100)
]

After adding the patch from this pr: #28
There still occurs one error as follows:

/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py in linear(input, weight, bias)
   1846     if has_torch_function_variadic(input, weight, bias):
   1847         return handle_torch_function(linear, (input, weight, bias), input, weight, bias=bias)
-> 1848     return torch._C._nn.linear(input, weight, bias)
   1849 
   1850 

RuntimeError: mat1 and mat2 shapes cannot be multiplied (1x3069 and 3072x100)

After some investigations, I think the bug lies in how onnx2pytorch infers the padding on some special cases.
Below is what I have done for debugging:
I find that when setting the pool_size parameter to be (odd, even) or (even, odd), the actual MaxPooling2D's output size is not (1, 32, 32, 3) even though we set the padding to be 'same' (which means the output size should be the same as input size) And that's why crash happens. Run below code to see what's going on:

# generate model
import keras
x = keras.layers.Input((32,32,3))
layer_stack = [
    keras.layers.MaxPooling2D(pool_size=(2,1), strides=1, padding='same'),
]
layer_input = x
for layer in layer_stack:
  y = layer(layer_input)
  layer_input = y
model = keras.Model(x, y)
model.summary()

# convert model to onnx
import tensorflow as tf
import tf2onnx
# transform model
input_shape = model.layers[0].input_shape[0]
spec = (tf.TensorSpec(input_shape, tf.float32, name="input"),)
model, _ = tf2onnx.convert.from_keras(model, input_signature=spec, \
    opset=15, output_path="temp.onnx")

# load onnx to pytorch using onnx2pytorch
import onnx
from onnx2pytorch import ConvertModel
import numpy as np
import torch
onnx_model = onnx.load("temp.onnx")
model = ConvertModel(onnx_model)
input = np.random.rand(1, 32, 32, 3)
torch_input = torch.from_numpy(input)
model.double()
res = model(torch_input)
res.size()

image

The output torch size is [1, 32, 33, 3] instead of [1, 32, 32, 3]. I initially thought it may be the bug of tf2onnx that incorrectly generate the padding parameter. However, when I directly inference the onnx model using onnxruntime, it can output the correct size [1, 32, 32, 3]. The code to inference onnx model is as follows:

import onnxruntime as ort
session = ort.InferenceSession("temp.onnx")
input_name = session.get_inputs()[0].name
output_name = session.get_outputs()[0].name
input = np.random.rand(1, 32, 32, 3).astype('float32')
result = session.run([output_name], {input_name: input})
print(result[0].shape)

After such cross-checking, I then think it may be a bug inside onnx2pytorch. So I checked the generated model and find the following information:

ConvertModel(
  (Transpose_max_pooling2d_1/MaxPool__6:0): Transpose()
  (MaxPool_max_pooling2d_1/MaxPool_raw_output___4:0): Sequential(
    (0): ConstantPad2d(padding=(0, 1, 0, 0), value=0)
    (1): MaxPool2d(kernel_size=(2, 1), stride=(1, 1), padding=0, dilation=1, ceil_mode=False)
  )
  (Transpose_max_pooling2d_1): Transpose()
)

I then manually try to change the padding parameter and find out that if we change the padding parameter in ConstantPad2d from (0, 1, 0, 0) to (0, 0, 1, 0). Pytorch can predict the correct shape.

In conclusion, I assume it may be a bug inside onnx2pytorch's padding inference phase.
All mentioned code above can be accessed through this colab link:
https://colab.research.google.com/drive/1LnPiSZy08rmZFRsGoEPjsCJFyBPL0_K5?usp=sharing

Try to use Matlab CNN Resnet50 pre-trained model to convert ONNX to use in Pytorch

I first use onnx2pytorch to convert onnx to pytorch, and then use it to use the test set to get the accuracy.
This is my part of code :

model = torch.load(" .pkl")
model.eval()
for epoch in range(1) :
    testing_loss = 0.0
    testing_correct = 0.0
    zpred, ztrue = [], []
    for test_inputs, test_labels in test_data :
        test_inputs, test_labels = test_inputs.to(device), test_labels.to(device)
        output = model(test_inputs)    #error poit
        loss = criterion(output, test_labels)
        _, pred = torch.max(output.data, 1)
        testing_loss += loss.item() 
        testing_correct += torch.sum(pred == test_labels.data)
        output = (torch.max(torch.exp(output), 1)[1]).data.cpu().numpy()
        zpred.extend(output)
        testlabel = test_labels.data.cpu().numpy()
        ztrue.extend(testlabel)
    test_loss = testing_loss / len(test_loader)
    test_acc = 100 * testing_correct.cpu().numpy() / len(test_loader)

    print('Epoch is : {}, Test Loss is : {:.4f} Test Accuracy is :{:.4f}%'.format(epoch + 1, test_loss, test_acc))

But I got this error :

sum() received an invalid combination of arguments - got (Tensor, Tensor), but expected one of :
 * (Tensor input, *, torch.dtype dtype)
 * (Tensor input, tuple of ints dim, bool keepdim, *, torch.dtype dtype, Tensor out)
 * (Tensor input, tuple of names dim, bool keepdim, *, torch.dtype dtype, Tensor out)

How can I change the second tensor to the expected attribute?

cannot import name 'ConvertModel'

I install onnx2pytorch with pip install onnx2pytorch and run sample code:

import onnx
from onnx2pytorch import ConvertModel

path_to_onnx_model = 'model.onnx'

onnx_model = onnx.load(path_to_onnx_model)

pytorch_model = ConvertModel(onnx_model)

Then it display it:

Traceback (most recent call last):
  File "onnx2pytorch.py", line 2, in <module>
    from onnx2pytorch import ConvertModel
  File "D:\机器学习\Onnx\onnx2pytorch.py", line 2, in <module>
    from onnx2pytorch import ConvertModel
ImportError: cannot import name 'ConvertModel'

My system is Windows 10, and python version is 3.6

Conversion not implemented for op_type=RandomUniformLike.

Thanks for your splendid work. It would be nice if you could tell me how to add a torch-like definition in which one of your code files. And it would be better if you can suggest to me an appropriate func in the torch to define RandomUniformLike op.

Inconsistency between pytorch and onnxruntime

I build an onnx graph with only one BatchNormalization layer and two transpose layer as follows:
image

However, the output between onnxruntime and pytorch's result is different when converting onnx to pytorch:

To reproduce:
Please access the model through this link: https://drive.google.com/file/d/1KQ-ZvdghB2Fw0b1U42M1Q8DFfJzKMKuF/view?usp=sharing

import onnx
from onnx2pytorch import ConvertModel
import numpy as np
import torch
input = np.random.rand(1,3,3,3)
onnx_model = onnx.load("incon.onnx")
torch_model = ConvertModel(onnx_model, experimental=True)
torch_input = torch.from_numpy(input)
torch_model.double()
torch_pred = torch_model(torch_input)


import onnxruntime as ort
providers = [
    ('CUDAExecutionProvider', {
        'device_id': 0,
        'arena_extend_strategy': 'kNextPowerOfTwo',
        'gpu_mem_limit': 10 * 1024 * 1024 * 1024,  # 10G
        'cudnn_conv_algo_search': 'EXHAUSTIVE',
        'do_copy_in_default_stream': True,
    }),
    'CPUExecutionProvider',
]
onnx_path = "incon.onnx"
onnx_model = ort.InferenceSession(onnx_path, providers=providers)

input_name = onnx_model.get_inputs()[0].name
output_name = onnx_model.get_outputs()[0].name
input = input.astype('float32')
onnx_pred = onnx_model.run([output_name], {input_name: input})[0]
print("When running on CPU: ", onnx_pred[0])

Result of ONNXRuntime:

Result of PyTorch:

tensor([[[[ 0.8651,  0.4476,  1.0992],
          [-1.4390, -1.9661,  0.6654],
          [-0.0661,  0.8117,  0.4977]],

         [[ 1.4929, -1.0736, -1.8301],
          [-0.3819,  0.1353, -1.3534],
          [-0.5915,  1.0055,  0.1152]],

         [[ 1.4507,  0.9089,  0.7818],
          [-0.2749,  0.6187,  0.7877],
          [-1.0552, -0.8879, -0.7635]]]], dtype=torch.float64,
       grad_fn=<PermuteBackward0>)

Result of ONNXRuntime:

[[[0.6175244  0.7708563  0.99578035]
  [0.06858005 0.03348556 0.86044073]
  [0.39565903 0.8820858  0.8081321 ]]

 [[0.7670855  0.30615166 0.08200629]
  [0.32042617 0.6754674  0.2307212 ]
  [0.27048445 0.94129366 0.6888292 ]]

 [[0.75703746 0.9117755  0.89674425]
  [0.3459281  0.8231245  0.89860445]
  [0.16000679 0.3628597  0.41474345]]]

A lot of Ops with their implementations.

Hi, developer team of onnx2pytorch.
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,

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.