Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated README.md #23

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,6 @@ dmypy.json

# Pyre type checker
.pyre/

# MS Code
.vscode/
33 changes: 21 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# onnx-pytorch


![Build Status](https://github.com/fumihwh/onnx-pytorch/actions/workflows/main.yml/badge.svg?branch=main)


Expand All @@ -9,14 +8,17 @@ Currently support `onnx==1.9.0` and `torch==1.8.1`.

## Installation

- From PyPI
```
### From PyPI

```bash
pip install onnx-pytorch
```

- From source
```
### From source

```bash
git clone https://github.com/fumihwh/onnx-pytorch.git
cd onnx-pytorch
pip install -r requirements.txt
pip install -e .
```
Expand All @@ -25,7 +27,8 @@ pip install -e .
## Usage

### By Command Line
```

```bash
python -m onnx_pytorch.code_gen -h

usage: code_gen.py [-h] [--onnx_model_path ONNX_MODEL_PATH] [--output_dir OUTPUT_DIR] [--overwrite OVERWRITE] [--tensor_inplace TENSOR_INPLACE] [--continue_on_error CONTINUE_ON_ERROR] [--simplify_names SIMPLIFY_NAMES]
Expand All @@ -47,26 +50,32 @@ optional arguments:
```

### By Python
```

```python
from onnx_pytorch import code_gen
code_gen.gen("/path/to/onnx_model", "/path/to/output_dir")
```

A `model.py` file and `variables` folder will be created under `output_dir`.

## Tutorial

- Download resnet18 onnx model

```wget https://github.com/onnx/models/raw/master/vision/classification/resnet/model/resnet18-v2-7.onnx```

- Use onnx-pytorch to generate pytorch code and variables.
```bash
wget https://github.com/onnx/models/raw/master/vision/classification/resnet/model/resnet18-v2-7.onnx
```

- Use onnx-pytorch to generate pytorch code and variables.

```python
from onnx_pytorch import code_gen
code_gen.gen("resnet18-v2-7.onnx", "./")
```

- Test result
```

```python
import numpy as np
import onnx
import onnxruntime
Expand Down Expand Up @@ -94,4 +103,4 @@ print(
ort_outputs[0],
atol=1e-5,
rtol=1e-5))
```
```
14 changes: 13 additions & 1 deletion onnx_pytorch/code_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,13 +247,25 @@ def run(self):
f"OpCodeGenerator is unimplemented for {n.op_type}.")
else:
try:
print(f"op_code_gen: {op_code_gen}")
print(f"n.op_type: {n.op_type}")
if hasattr(op_code_gen,
"gen_method") and n.op_type not in self.method_parts:
print("Point 1")
self.method_parts[n.op_type] = op_code_gen.gen_method()
print("Point 2")
print("Point 2A")
"""
gened = op_code_gen.gen(n, value_infos, initializers, self.rename_helper,
self.tensor_inplace)
"""
gened = op_code_gen.gen(n, value_infos, initializers)
print("Point 3")
self.add_init_part(gened["init"])
print("Point 4")
self.add_forward_part(gened["forward"])
except BaseException as e:
print(f"e: {e}")
if self.continue_on_error:
logging.warning(e)
self.add_forward_part(n.__repr__())
Expand Down Expand Up @@ -316,7 +328,7 @@ def get_model_code_generator(
assert os.path.isfile(onnx_model), f"{onnx_model} is not a file."
assert os.path.exists(
output_dir
) and overwrite is not True, f"{output_dir} is not empty and overwrite is not True."
) and overwrite, f"{output_dir} is not empty and overwrite is not True."
assert os.path.isdir(output_dir), f"{output_dir} is not directory."
kwargs["onnx_model"] = onnx.load(onnx_model)
if overwrite:
Expand Down
19 changes: 19 additions & 0 deletions onnx_pytorch/op_code_generators/Pow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import onnx
import torch

from onnx_pytorch.op_code_generators import OpCodeGenerator


class PowOpCodeGenerator(OpCodeGenerator):

def __init__(self,
onnx_ver=onnx.defs.onnx_opset_version(),
torch_ver=torch.__version__):
super(PowOpCodeGenerator, self).__init__(onnx_ver, torch_ver)

def gen(self, node, value_infos, initializers, rename_helper, tensor_inplace):
inputs_str, outputs_str = self.gen_input_output_string(
node, initializers, rename_helper, tensor_inplace)
init_str, forward_str = [], []
forward_str.append(f"{outputs_str[0]} = torch.pow({', '.join(inputs_str)})")
return {"init": init_str, "forward": forward_str}
19 changes: 19 additions & 0 deletions onnx_pytorch/op_code_generators/Range.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import onnx
import torch

from onnx_pytorch.op_code_generators import OpCodeGenerator


class RangeOpCodeGenerator(OpCodeGenerator):

def __init__(self,
onnx_ver=onnx.defs.onnx_opset_version(),
torch_ver=torch.__version__):
super(RangeOpCodeGenerator, self).__init__(onnx_ver, torch_ver)

def gen(self, node, value_infos, initializers, rename_helper, tensor_inplace):
inputs_str, outputs_str = self.gen_input_output_string(
node, initializers, rename_helper, tensor_inplace)
init_str, forward_str = [], []
forward_str.append(f"{outputs_str[0]} = torch.arange({', '.join(inputs_str)})")
return {"init": init_str, "forward": forward_str}
19 changes: 19 additions & 0 deletions onnx_pytorch/op_code_generators/Tanh.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import onnx
import torch

from onnx_pytorch.op_code_generators import OpCodeGenerator


class TanhOpCodeGenerator(OpCodeGenerator):

def __init__(self,
onnx_ver=onnx.defs.onnx_opset_version(),
torch_ver=torch.__version__):
super(TanhOpCodeGenerator, self).__init__(onnx_ver, torch_ver)

def gen(self, node, value_infos, initializers):
inputs_str, outputs_str = self.gen_input_output_string(
node, initializers, self.rename_helper, self.tensor_inplace)
init_str, forward_str = [], []
forward_str.append(f"{outputs_str[0]} = torch.tanh({inputs_str[0]})")
return {"init": init_str, "forward": forward_str}
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mpmath==1.2.1
numpy==1.20.2
numpy==1.19.5
Copy link
Owner

@fumihwh fumihwh Aug 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to change requirements
All ready modified numpy version in main branch.

onnx==1.9.0
onnxruntime==1.7.0
packaging==20.9
Expand Down