简体   繁体   English

我在 python 中有一个基本的 tensorflow 模型我想将它转换为 onnx 文件

[英]I have a basic tensorflow model in python I want to convert it to onnx file

I have basic model in python tensorflow I want to save it to onnx file how can I do this.我在 python tensorflow 中有基本模型我想将它保存到 onnx 文件我该怎么做。 I have tried using onnx.save function I am getting error.我尝试使用onnx.save函数,但出现错误。

File "tenserflowbase.py", line 21, in <module> onnx.save(trained_model,'model.onxx') 
File "C:\Users\Parag_IK\Anaconda3\lib\site-packages\onnx\__init__.py", line 184, in save_model proto = write_external_data_tensors(proto, basepath)                                                                                                                                                                                                                                                           
File "C:\Users\Parag_IK\Anaconda3\lib\site packages\onnx\external_data_helper.py", line 225, in write_external_data_tensors                                               
for tensor in _get_all_tensors(model):                                                                                                                              
File "C:\Users\Parag_IK\Anaconda3\lib\site packages\onnx\external_data_helper.py", line 170, in _get_initializer_tensors                                                 
for initializer in onnx_model_proto.graph.initializer:                                                                                                              
AttributeError: 'History' object has no attribute 'graph'** 

My code is below:我的代码如下:

import onnx
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split

tf.logging.set_verbosity(tf.logging.ERROR)   

mar_budget = np.array([60, 80,  100  , 30, 50, 20, 90,  10],  dtype=float)
subs_gained = np.array([160, 200, 240, 100, 140, 80, 220, 60],  dtype=float)

for i, c in enumerate(mar_budget):
  print("{} Market budget = {} new subscribers gained".format(c, subs_gained[i]))


X_train, X_test, y_train, y_test = train_test_split(mar_budget, subs_gained, 
   random_state=42, train_size=0.8, test_size=0.2)

layer_0 = tf.keras.layers.Dense(units=1, input_shape=[1])
model = tf.keras.Sequential([layer_0])
model = tf.keras.Sequential([layer_0])

model.compile(loss='mean_squared_error', optimizer=tf.keras.optimizers.Adam(0.1))

trained_model = model.fit(X_train, y_train, epochs=1000, verbose=False)      

onnx.save(trained_model,'model.onxx')

print("Finished training the model")   
print(model.predict([80.0]))

I think if I am not wrong, in order to use onnx.save() , the model should create the graph in onnx functions.我想如果我没有错,为了使用onnx.save() ,模型应该在onnx函数中创建图形。

So I would suggest using the tf2onnx library, which has a function convert the tf session graph into onnx graph.所以我建议使用tf2onnx库,它具有将 tf 会话图转换为 onnx 图的功能。

onnx_graph = tf2onnx.tfonnx.process_tf_graph(sess.graph, ...) 

For example a full code would be:例如,完整的代码是:

import tensorflow as tf
import tf2onnx

with tf.Session() as sess:
    x = tf.placeholder(tf.float32, [2, 3], name="input")
    x_ = tf.add(x, x)
    _ = tf.identity(x_, name="output")
    onnx_graph = tf2onnx.tfonnx.process_tf_graph(sess.graph, input_names=["input:0"], output_names=["output:0"])
    model_proto = onnx_graph.make_model("test")
    with open("/tmp/model.onnx", "wb") as f:
        f.write(model_proto.SerializeToString())

Hope this helps.希望这可以帮助。

Reference: tensorflow-onnx参考: tensorflow-onnx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 tf2onnx 将 TensorFlow Model 转换为 Python 内的 ONNX - Convert TensorFlow Model to ONNX within Python using tf2onnx 我想将 Python 转换为导入 tensorflow 的 exe - I want to convert Python to exe that imported tensorflow 如何将使用 Keras model 训练的 Tensorflow 2.* 转换为.onnx 格式? - How to convert Tensorflow 2.* trained with Keras model to .onnx format? 我在 python 中有一个数组,我想将它转换为一维数组 - I have an array in python and I want to convert it to 1d array 我在 torch 中训练了一个模型,然后将其转换为 caffe,然后再转换为 tf. 现在如何将其转换为onnx? - I trained a model in torch and then convert it to caffe and after that to tf. How to convert it now to onnx? 将 onnx model 转换为 keras - Convert onnx model to keras 将 ONNX model 转换为 TensorFlow Lite - Converting ONNX model to TensorFlow Lite 如何将 Tensorflow model 转换为 tensorflow.js Z20F35E630DAF44DBFA4C3F68F5399? - How can I convert Tensorflow model to tensorflow.js model? 我可以将在 Google Colab 的 Tensorflow 上创建的“.py”或“.ipynb”模型/文件转换为 coreML 模型/文件吗? - Can I convert a “.py” or “.ipynb” model/file created on Google Colab's Tensorflow to a coreML model/ file? 我想使用 python 将 xml 文件转换为文本文件 - I want to convert a xml files into text file using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM