laich 3fd40a76a5 | 2 years ago | |
---|---|---|
tests | 2 years ago | |
tlx2onnx | 2 years ago | |
.gitignore | 2 years ago | |
LICENSE | 2 years ago | |
OP_LIST.md | 2 years ago | |
README.md | 2 years ago | |
README_en.md | 2 years ago | |
requirements.txt | 2 years ago | |
setup.py | 2 years ago |
简体中文 | English
TensorLayerX模型导出为ONNX。
TLX2ONNX可以将TensorLayerX模型转换为ONNX导出。
pip install tlx2onnx
git clone https://github.com/tensorlayer/TLX2ONNX.git
cd TLX2ONNX
python setup.py install
TLX2ONNX可以转换使用TensorLayerX模块子类和层构建的模型,层支持列表可以在Operator list找到.
下面给出了一个转换多层感知机的例子,代码可以从这里 找到。
import os
os.environ["TL_BACKEND"] = 'tensorflow'
import tensorlayerx as tlx
from tensorlayerx.nn import Module
from tensorlayerx.nn import Linear, Concat, Elementwise
from tlx2onnx.main import export
import onnxruntime as rt
import numpy as np
class CustomModel(Module):
def __init__(self):
super(CustomModel, self).__init__(name="custom")
self.linear1 = Linear(in_features=20, out_features=10, act=tlx.ReLU, name='relu1_1')
self.linear2 = Linear(in_features=20, out_features=10, act=tlx.ReLU, name='relu2_1')
self.concat = Concat(concat_dim=1, name='concat_layer')
def forward(self, inputs):
d1 = self.linear1(inputs)
d2 = self.linear2(inputs)
outputs = self.concat([d1, d2])
return outputs
net = CustomModel()
input = tlx.nn.Input(shape=(3, 20), init=tlx.initializers.RandomNormal())
net.set_eval()
output = net(input)
print("tlx out", output)
onnx_model = export(net, input_spec=input, path='concat.onnx')
# Infer Model
sess = rt.InferenceSession('concat.onnx')
input_name = sess.get_inputs()[0].name
output_name = sess.get_outputs()[0].name
input_data = np.array(input, dtype=np.float32)
result = sess.run([output_name], {input_name: input_data})
print('onnx out', result)
转换后的onnx文件可以通过Netron查看。
转换后的结果几乎没有损失精度。并且图中也显示了每一层的输入输出大小,这对模型的校验非常有帮助。如果你发现TensorLayerX或者TLX2ONNX对你的项目有用,请引用以下论文:
@article{tensorlayer2017,
author = {Dong, Hao and Supratak, Akara and Mai, Luo and Liu, Fangde and Oehmichen, Axel and Yu, Simiao and Guo, Yike},
journal = {ACM Multimedia},
title = {{TensorLayer: A Versatile Library for Efficient Deep Learning Development}},
url = {http://tensorlayer.org},
year = {2017}
}
@inproceedings{tensorlayer2021,
title={TensorLayer 3.0: A Deep Learning Library Compatible With Multiple Backends},
author={Lai, Cheng and Han, Jiarong and Dong, Hao},
booktitle={2021 IEEE International Conference on Multimedia \& Expo Workshops (ICMEW)},
pages={1--3},
year={2021},
organization={IEEE}
}
TensorLayerX模型导出为ONNX模型
Python Markdown
Dear OpenI User
Thank you for your continuous support to the Openl Qizhi Community AI Collaboration Platform. In order to protect your usage rights and ensure network security, we updated the Openl Qizhi Community AI Collaboration Platform Usage Agreement in January 2024. The updated agreement specifies that users are prohibited from using intranet penetration tools. After you click "Agree and continue", you can continue to use our services. Thank you for your cooperation and understanding.
For more agreement content, please refer to the《Openl Qizhi Community AI Collaboration Platform Usage Agreement》