如何为onnx转换设置环境变量TF_Keras = 1? [英] How to set environment variable TF_Keras = 1 for onnx conversion?
本文介绍了如何为onnx转换设置环境变量TF_Keras = 1?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
最近更新到tensorflow 2.0,将我的.h5模型添加到.onnx遇到了麻烦.曾经是一个非常简单的过程,但是现在我遇到了一个问题.当我运行以下代码时:
Recently updated to tensorflow 2.0 and am having trouble getting my .h5 models into .onnx . Used to be a very simple procedure but now I am having an issue. When I run the following code:
# onnx testing
import onnx
import keras2onnx
import os
import tensorflow as tf
from tensorflow.keras.models import load_model
folder = r'\\rdnas'
os.chdir(folder)
#os.environ["TF_KERAS"]='1'
model_loc = folder+'\\model.h5'
model = tf.keras.models.load_model(model_loc)
model.summary()
# Onnx covnersion
onnx_model = keras2onnx.convert_keras(model)
temp_model_file = 'model.onnx'
onnx.save_model(onnx_model, temp_model_file)
运行代码时,出现以下错误
When I run the code I get the following error
Using TensorFlow backend.
Can't import tf2onnx module, so the conversion on a model with any custom/lambda layer will fail!
Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
DVA_input (Dense) (None, 512) 670720
_________________________________________________________________
dropout_3 (Dropout) (None, 512) 0
_________________________________________________________________
dense_3 (Dense) (None, 256) 131328
_________________________________________________________________
dropout_4 (Dropout) (None, 256) 0
_________________________________________________________________
dense_4 (Dense) (None, 128) 32896
_________________________________________________________________
dropout_5 (Dropout) (None, 128) 0
_________________________________________________________________
dense_5 (Dense) (None, 10) 1290
_________________________________________________________________
Predicted_Volume (Dense) (None, 1) 11
=================================================================
Total params: 836,245
Trainable params: 836,245
Non-trainable params: 0
_________________________________________________________________
Traceback (most recent call last):
File "<ipython-input-1-f9d072fc6a73>", line 19, in <module>
onnx_model = keras2onnx.convert_keras(model)
File "C:\Users\JTBLONIGAN\AppData\Local\Continuum\anaconda3\envs\Keras-GPU\lib\site-packages\keras2onnx\main.py", line 67, in convert_keras
" Please set environment variable TF_KERAS = 1.")
Exception: This is a tensorflow keras model, but keras standalone converter is used. Please set environment variable TF_KERAS = 1.
我尝试使用注释行
os.environ["TF_KERAS"]='1' but nothing seemed to happen.
有人遇到过这个问题,或者知道如何更改该变量以转换模型吗?感谢任何帮助.
Has anyone had this issue or know how to change that variable to convert the model? Appreciate any help.
keras2onnx==1.6.0
Keras==2.2.4
onnx==1.6.0
tensorflow==2.0.0
推荐答案
应该在导入模块之前设置环境变量,因为模块会在导入时检查变量 :>
Environment variables should be set before importing modules, as modules check the variables at import:
import os
os.environ['TF_KERAS'] = '1'
import onnx
这篇关于如何为onnx转换设置环境变量TF_Keras = 1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文