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 onnx_simplifier-0.4.22-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8a355cfa2123fe02e3e94977bc05f0136a8b6cd9c9b023ca3382ee90c5f774e2 |
|
MD5 | 159f1a3cc9b40469578026b15361413a |
|
BLAKE2b-256 | cd6155bfb60b9faa7674ab78f28c753f73c4f87d148fd274155018e8d0c40378 |
Hashes for onnx_simplifier-0.4.22-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5307820c6cd0b0f90699525908e4a13b3b260c6a44199a73d28193742992d337 |
|
MD5 | f08a1e796e712193a00f2e22cd7ee080 |
|
BLAKE2b-256 | d83df2b408fb637282ac6a7f4ed67e546818301edc9eeac9eee0f7ea321ecaae |
Hashes for onnx_simplifier-0.4.22-cp311-cp311-macosx_10_15_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 895144ba84108b52be8162e6178f4dfff16b0a30c7c7ee5a098e737d1bd2d76d |
|
MD5 | b04467f5ce78542ac8469032dd4f12d3 |
|
BLAKE2b-256 | 1a58192c1e1dcd61e5df81e73db5f431bc2824d2e1cc602a10c0fb4b6dce0d37 |
Hashes for onnx_simplifier-0.4.22-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7b6112c4d1181a4ac595568f96eb2fc10b50337629cb2988821b22ef37d85d69 |
|
MD5 | 46a7a81a9515cc49cae354af86207d56 |
|
BLAKE2b-256 | 264a83458c9423d218f8728e6ab61cec9ea32e23c9ad3b83cc310a9d9798535f |
Hashes for onnx_simplifier-0.4.22-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 209c5f83bcf9a1db869d56a8cafd6adcaef50fa56b9ec3f6eb206540cb9f53d5 |
|
MD5 | 5e7cc0006397e2b1ed5c7dfe4196c2b7 |
|
BLAKE2b-256 | f0c37fe128c91dd5cec1e2e1018c1260d7cecf6e8ba2f44ae3f674f7eb07b19e |
Hashes for onnx_simplifier-0.4.22-cp310-cp310-macosx_10_15_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3c06694796f413ff79535e87f1d5d017e8c8fcf7850e2a578457013211d5d1c9 |
|
MD5 | 5ebe2cb745f76b1306e00e2d83b7bb1b |
|
BLAKE2b-256 | ae42d4a78f2439a44f91fddb83a42a9caaa6a84197b895506a7d8077a00d01ad |
Hashes for onnx_simplifier-0.4.22-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c6f9ac8ae894e4e40cdb934dfc42b4536a0b6c91cfb5545d82472548dd73dad4 |
|
MD5 | 51d4d3b970ff93791a51d379c407dd82 |
|
BLAKE2b-256 | 233e9efe36cf8d8b82bba24e2476bace2d374c277e179beac35280a28bebf1eb |
Hashes for onnx_simplifier-0.4.22-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b0d81f6884f6613e86987d1730ce44106d08d7a3159cf19bcf9569a4c9e94456 |
|
MD5 | 6297dc816c5251fbb9336bae1815d59e |
|
BLAKE2b-256 | 63449eb13355bfd9c4011580a1499179fdf992ab7737e044b3970a90c6b7e3b5 |
Hashes for onnx_simplifier-0.4.22-cp39-cp39-macosx_10_15_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1e7d5d17707d7d0071383f9f6ca65823604a4bd0f0d5aef960dd745d30011cf3 |
|
MD5 | 90baf6bfe4e6f80c4da0295a8437e094 |
|
BLAKE2b-256 | fbed1145bf09812dab7842629fc582f293835b763bc28f540da00dffd35ba5cb |
Hashes for onnx_simplifier-0.4.22-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 40a03e492a153cc41dbd1641dddd19ade1153ec785ab7be8b4ae6dce66915865 |
|
MD5 | 793f6eb7ac2059375985e42aebc9fe9e |
|
BLAKE2b-256 | b0cac5f88e4df5d6984052ab269408b881c0b65029ca70d6b01c0ef588008da5 |
Hashes for onnx_simplifier-0.4.22-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b23ac8beb3464c8d3ec9fff12017220a0346a10624f8d945b817e5eabc0c110a |
|
MD5 | a70a8b8667b676b62efef89d19fa2d6d |
|
BLAKE2b-256 | cdcb554da46ce9c48ae8b48efc44c9f264a4ac0b1d6db53de53f35655c6e16f3 |
Hashes for onnx_simplifier-0.4.22-cp38-cp38-macosx_10_15_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 11f4e9b244fdea04c27c7f8967504217f22684112816588454d58b023baafa02 |
|
MD5 | d0565a39529ac7b538c0c3bd5c0c56e3 |
|
BLAKE2b-256 | b8c38030c7312d532ac911e871fea2f840671e041f523fd28eb15b3b9c7f44b8 |
Hashes for onnx_simplifier-0.4.22-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3b1fb141a14e9f75d4383791bd58159c8aa794b548cf1a1366355ff3ab2b9e0d |
|
MD5 | d8d2698dc4eb292c2177e160dc684eb0 |
|
BLAKE2b-256 | 2219cae8cb74ea6389b1d9f5d0fbbc196c6b8410d6460131e684dd1cea4f461d |
Hashes for onnx_simplifier-0.4.22-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ea9d46f5873a667be0e5568ee7f93d2e4032b76f3bec4d35842e536cc55494a9 |
|
MD5 | 60a03712b87fe2b058bfddd70c1c2d0b |
|
BLAKE2b-256 | cba4f503b21d77a69bb6988e112a7b551ede7421193bd356fac89fbf966e259b |