Simplify your ONNX model
Project description
ONNX Simplifier
ONNX is great, but sometimes too complicated.
Background
One day I wanted to export the following simple reshape operation to ONNX:
import torch
class JustReshape(torch.nn.Module):
def __init__(self):
super(JustReshape, self).__init__()
def forward(self, x):
return x.view((x.shape[0], x.shape[1], x.shape[3], x.shape[2]))
net = JustReshape()
model_name = 'just_reshape.onnx'
dummy_input = torch.randn(2, 3, 4, 5)
torch.onnx.export(net, dummy_input, model_name, input_names=['input'], output_names=['output'])
The input shape in this model is static, so what I expected is
However, I got the following complicated model instead:
Our solution
ONNX Simplifier is presented to simplify the ONNX model. It infers the whole computation graph and then replaces the redundant operators with their constant outputs (a.k.a. constant folding).
Web version
We have published ONNX Simplifier on convertmodel.com. It works out of the box and doesn't need any installation. Note that it runs in the browser locally and your model is completely safe.
Python version
pip3 install -U pip && pip3 install onnxsim
Then
onnxsim input_onnx_model output_onnx_model
For more advanced features, try the following command for help message
onnxsim -h
Demonstration
An overall comparison between a complicated model and its simplified version:
In-script workflow
If you would like to embed ONNX simplifier python package in another script, it is just that simple.
import onnx
from onnxsim import simplify
# load your predefined ONNX model
model = onnx.load(filename)
# convert model
model_simp, check = simplify(model)
assert check, "Simplified ONNX model could not be validated"
# use model_simp as a standard ONNX model object
You can see more details of the API in onnxsim/onnx_simplifier.py
Projects Using ONNX Simplifier
- MXNet
- MMDetection
- YOLOv5
- ncnn
- ...
Chat
We created a Chinese QQ group for ONNX!
ONNX QQ Group (Chinese): 1021964010, verification code: nndab. Welcome to join!
For English users, I'm active on the ONNX Slack. You can find and chat with me (daquexian) there.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for onnxsim-0.4.26-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7a8178e715c85959e844e70d0c557ec525d2c1070611dfa7172f10595640d85a |
|
MD5 | 8c8668620d8c9812d344d20f5efc4361 |
|
BLAKE2b-256 | 611d46c1fc0ac1257ea07a705bf1b06be5077a3699ac2234317039d842a12cd9 |
Hashes for onnxsim-0.4.26-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5b6ff129bebeb1ddd82b8115bc1ac3bc58409d38ddf6cde1a7969c38ec4c2a3d |
|
MD5 | a99082b48686b3dfb1b2489e2e11593f |
|
BLAKE2b-256 | 370996f3b479524b383ddaf93e16d84689afa39241996f54014fc4ed901faf38 |
Hashes for onnxsim-0.4.26-cp311-cp311-macosx_10_15_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c2daf8604f98fd237dc42ac9dc47768a41b1078473afa6947a8a2c7fd3798c1f |
|
MD5 | 74969dcb8094862ca6a992e4f1b9dc85 |
|
BLAKE2b-256 | 95929b8836b8fbe3eeed2c4fe2d9197754a8fd40bbd22a5a880c5613ae05035d |
Hashes for onnxsim-0.4.26-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8657a875a05eac2edbc3a49c0adafef57c87c3c929b24ebaef3f087cb8af4753 |
|
MD5 | e9849f6c398806df89a43da4f2a0a9f3 |
|
BLAKE2b-256 | 93a567805dc4fe35a273caa3cfc892e52ae40d9fbcb95b2075d749c7e6d6a170 |
Hashes for onnxsim-0.4.26-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 46ad68cf1e234dc919b9f4e8cb2a9053ea108d05f93d66a24d07e3a00209c743 |
|
MD5 | 976fcec764d423320143664c720ae556 |
|
BLAKE2b-256 | ac8e0ee4c86a61b762b8fa414bacf640e5a05a9273b48227a7376c85a582c674 |
Hashes for onnxsim-0.4.26-cp310-cp310-macosx_10_15_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ef23d64ba5d6a1bfec10dd2bbe11db3ab9655037d9c18799d64278d7a756d856 |
|
MD5 | 7286caa323899171863c67a5343b8daf |
|
BLAKE2b-256 | 6d6aa3c29db8770a6b31c63f60acf0082c9bcc08579e5bf9086320a228d01108 |
Hashes for onnxsim-0.4.26-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 968395202b0500d68290da610ec88e1a1d2ce90185c00b69b4318362d62c0772 |
|
MD5 | dc51e17117701ca109dab08b337d3551 |
|
BLAKE2b-256 | 705c4f72cb164963f3fe9847fdf17e52b4a39ee112774b21c51daffb8bdcca1d |
Hashes for onnxsim-0.4.26-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 72c155856281a1f8c6843eda2eed254da6de397d46fe2e77a950ddc5c4929882 |
|
MD5 | 098298f1db343b64d9dce609dfb2bdca |
|
BLAKE2b-256 | 1c55a76bd495a5718e81ddf12f9feffe3b37bd4b55ed2dd5c4c755bbb4a89441 |
Hashes for onnxsim-0.4.26-cp39-cp39-macosx_10_15_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b89d0f4c6dfa27a8eab7f65934ab3a6561a487494f05ac5cf5d0df915baa26b8 |
|
MD5 | c532d83d2e2a37e95731ac381c3366ed |
|
BLAKE2b-256 | ee806d17e74cbca1e2320b09aea04bcfd1072cad220c80b20b77796cd372264f |
Hashes for onnxsim-0.4.26-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 581dccf1a2a20aa81fa88eb3f2db237ae361bfbfca422b3b39ca120863d8129c |
|
MD5 | 2791ec444dd2f781012da485a131f3df |
|
BLAKE2b-256 | 284e2a87cf8b120f6fd4390344bd723c1946725878473a6d5ee38dba92292fbc |
Hashes for onnxsim-0.4.26-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1204edd8753aa2491190040c95b845623e1291e440eecd107fbefd05255bee14 |
|
MD5 | 1f7374dd85764c00d8d44d78504a859b |
|
BLAKE2b-256 | 52745e7a94593aef95cb44d54b4ee7da794da1263dc8dddddbf7c7fa7e1f0b28 |
Hashes for onnxsim-0.4.26-cp38-cp38-macosx_10_15_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 77ba41e3fa5d98d74b421bc9f10311ab1740b51bf5c18cd947c5b7138820f5cd |
|
MD5 | d3db38dc3acf69f4c35dab74b6a09f64 |
|
BLAKE2b-256 | 37ed2a81f9a2dbec31a91e248e8984c2644eb9ba1e06c6ff0e475f92b8ad44d7 |
Hashes for onnxsim-0.4.26-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dcbf744cc6949052ba885f9418cf85df003c2c2c803c3c52356943eb2dafb760 |
|
MD5 | b37575b218f4688fa123793d523cd6db |
|
BLAKE2b-256 | ead793b1a60e82a2349cee3a31703f23e2648cbfefae22eb0f2750970a2052bf |
Hashes for onnxsim-0.4.26-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 36fd7cd43be03131d1c5852d9b4eab8851d5765e2e91277afd910f777549958a |
|
MD5 | d8fda727ca858aac4d13551d3d7db757 |
|
BLAKE2b-256 | 365ae81e4447dc2f36dc514003bb659987066778ddc8ad9cdbf5b2d87a01de04 |