talmaj / onnx2pytorch Goto Github PK
View Code? Open in Web Editor NEWTransform ONNX model to PyTorch representation
License: Apache License 2.0
Transform ONNX model to PyTorch representation
License: Apache License 2.0
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```
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.
Hello
I am getting errors since HardSwish has not been implemented
F you
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__'
>>>
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()
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
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)
I build an onnx graph with only one BatchNormalization layer and two transpose layer as follows:
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]]]
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.
Hi, thank you for the amazing work! Are there plans to implement other op_types, like op_type=And, for example?
Currently DepthToSpace throws a NotImplemented error, any plan to support this?
Thanks
what should i do if i want to support some layer in this repo and recompile
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.
Can you please add this operation?
NotImplementedError: Conversion not implemented for op_type=LRN.
If you could add this operation, it would be really helpful!
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...)
Change compute_activation_dependencies
in convert/model.py
to use recursion.
Currently it is limited to two levels only, with recursion it will be "limitless".
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?
Nice tool!
But out of curiosity, what will be the scenario to use this tool?
Thanks.
This is ipython code (at colab) which makes an error.
!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)
---------------------------------------------------------------------------
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.
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.
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):
Code: from onnx2pytorch import ConvertModel
Error: ImportError: cannot import name '_LazyBatchNorm' from 'torch.nn.modules.batchnorm' (C:\Users\ayush\AppData\Local\Programs\Python\Python38\lib\site-packages\torch\nn\modules\batchnorm.py)
Hey team, thanks for this great package. Flagging interest for hte LogSoftmax conversion
NotImplementedError: Conversion not implemented for op_type=LogSoftmax.
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.
When using latest onnx2pytorch.ConvertModel to import onnx model, it raise this error:
NotImplementedError: auto_pad=VALID functionality not implemented.
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?
NotImplementedError: Conversion not implemented for op_type=If.
The onnx file was generated at huggingface.co/spaces/stevengrove/YOLO-World
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.
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)
please Help
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?
麻烦您!
I am using your library for converting my onnx model to torch. I explored the library and played around with the code but can not figure it out why batch_norm can give false outputs when batch_size>1.
The current implementation doesn't support STRINGS as attribute type. Per ONNX sepc for LSTM, one of the attributes can be activations
which is of type STRINGS. This leads to conversion error on LSTM models. Could you add STRINGS attribute type?
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!
NotImplementedError: Conversion not implemented for op_type=HardSigmoid.
dwpose github: https://github.com/IDEA-Research/DWPose?tab=readme-ov-file
dwpose onnx file: https://drive.google.com/file/d/12L8E2oAgZy4VACGSK9RaZBZrfgx7VTA2/view
/onnx2pytorch/convert/operations.py
line 159, IndexError: list index out of range
occurs if MatMul is the last node in the graph.
op=conv
auto_pad=SAME_UPPER functionality not implemented.
This is not implemented for these simple MNIST models in the Onnx Zoo:
https://github.com/onnx/models/tree/main/vision/classification/mnist
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)
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?
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
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.