微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从 Tensorflow 中的 Unity ml-agents 推断预训练的 ONNX 模型

如何解决从 Tensorflow 中的 Unity ml-agents 推断预训练的 ONNX 模型

我有一个来自 Unity 的 ml-agents 的预训练模型。现在我正在尝试使用 TensorFlow 在 python 中对该模型进行推理。为此,我使用 TensorFlow Backend for ONNX 将 ONNX 模型保存为 SavedModel,以便稍后加载该模型。用于保存模型代码

import onnx
from onnx_tf.backend import prepare

onnx_model = onnx.load(model_path)  # load onnx model
tf_rep = prepare(onnx_model,logging_level='DEBUG')
tf_rep.export_graph(output_path)

加载模型和运行测试示例的代码

imported = tf.saved_model.load(
     model_dir,tags=None,options=None
)
f = imported.signatures["serving_default"]
print(f(visual_observation_0=tf.cast(forward,tf.float32),visual_observation_1=tf.cast(body,tf.float32)))

现在有几个问题。

  1. 测试的输出有 6 个输出值。 (有关 ONNX 文件的可视化图表,请参见下图)
  2. 我在尝试保存模型时收到以下消息(请参阅下面的调试信息)

不确定这里发生了什么任何帮助非常感谢

Visual model

2021-03-24 17:52:03,267 - onnx-tf - DEBUG - UnkNown op Celu in domain 'ai.onnx'.
2021-03-24 17:52:03,267 - onnx-tf - DEBUG - Fail to get since_version of ConcatFromSequence in domain '' with max_inclusive_version=9. Set to 1.
2021-03-24 17:52:03,267 - onnx-tf - DEBUG - UnkNown op ConstantFill in domain 'ai.onnx'.
2021-03-24 17:52:03,267 - onnx-tf - DEBUG - Fail to get since_version of ConvInteger in domain '' with max_inclusive_version=9. Set to 1.
2021-03-24 17:52:03,267 - onnx-tf - DEBUG - Fail to get since_version of CumSum in domain '' with max_inclusive_version=9. Set to 1.
2021-03-24 17:52:03,267 - onnx-tf - DEBUG - Fail to get since_version of DequantizeLinear in domain '' with max_inclusive_version=9. Set to 1.
2021-03-24 17:52:03,267 - onnx-tf - DEBUG - Fail to get since_version of Det in domain '' with max_inclusive_version=9. Set to 1.
2021-03-24 17:52:03,268 - onnx-tf - DEBUG - Fail to get since_version of DynamicQuantizeLinear in domain '' with max_inclusive_version=9. Set to 1.
2021-03-24 17:52:03,268 - onnx-tf - DEBUG - UnkNown op Einsum in domain 'ai.onnx'.
2021-03-24 17:52:03,268 - onnx-tf - DEBUG - Fail to get since_version of GatherElements in domain '' with max_inclusive_version=9. Set to 1.
2021-03-24 17:52:03,268 - onnx-tf - DEBUG - Fail to get since_version of GatherND in domain '' with max_inclusive_version=9. Set to 1.
2021-03-24 17:52:03,268 - onnx-tf - DEBUG - UnkNown op GreaterOrEqual in domain 'ai.onnx'.
2021-03-24 17:52:03,268 - onnx-tf - DEBUG - UnkNown op ImageScaler in domain 'ai.onnx'.
2021-03-24 17:52:03,268 - onnx-tf - DEBUG - Fail to get since_version of IsInf in domain '' with max_inclusive_version=9. Set to 1.
2021-03-24 17:52:03,268 - onnx-tf - DEBUG - UnkNown op LessOrEqual in domain 'ai.onnx'.
2021-03-24 17:52:03,269 - onnx-tf - DEBUG - Fail to get since_version of MatMulInteger in domain '' with max_inclusive_version=9. Set to 1.
2021-03-24 17:52:03,269 - onnx-tf - DEBUG - Fail to get since_version of Mod in domain '' with max_inclusive_version=9. Set to 1.
2021-03-24 17:52:03,269 - onnx-tf - DEBUG - Fail to get since_version of NonMaxSuppression in domain '' with max_inclusive_version=9. Set to 1.
2021-03-24 17:52:03,269 - onnx-tf - DEBUG - Fail to get since_version of QLinearConv in domain '' with max_inclusive_version=9. Set to 1.
2021-03-24 17:52:03,269 - onnx-tf - DEBUG - Fail to get since_version of QLinearMatMul in domain '' with max_inclusive_version=9. Set to 1.
2021-03-24 17:52:03,269 - onnx-tf - DEBUG - Fail to get since_version of QuantizeLinear in domain '' with max_inclusive_version=9. Set to 1.
2021-03-24 17:52:03,269 - onnx-tf - DEBUG - Fail to get since_version of Range in domain '' with max_inclusive_version=9. Set to 1.
2021-03-24 17:52:03,269 - onnx-tf - DEBUG - Fail to get since_version of Resize in domain '' with max_inclusive_version=9. Set to 1.
2021-03-24 17:52:03,270 - onnx-tf - DEBUG - Fail to get since_version of ReverseSequence in domain '' with max_inclusive_version=9. Set to 1.
2021-03-24 17:52:03,270 - onnx-tf - DEBUG - Fail to get since_version of RoiAlign in domain '' with max_inclusive_version=9. Set to 1.
2021-03-24 17:52:03,270 - onnx-tf - DEBUG - Fail to get since_version of Round in domain '' with max_inclusive_version=9. Set to 1.
2021-03-24 17:52:03,270 - onnx-tf - DEBUG - Fail to get since_version of ScatterElements in domain '' with max_inclusive_version=9. Set to 1.
2021-03-24 17:52:03,270 - onnx-tf - DEBUG - Fail to get since_version of ScatterND in domain '' with max_inclusive_version=9. Set to 1.
2021-03-24 17:52:03,270 - onnx-tf - DEBUG - Fail to get since_version of SequenceAt in domain '' with max_inclusive_version=9. Set to 1.
2021-03-24 17:52:03,270 - onnx-tf - DEBUG - Fail to get since_version of SequenceConstruct in domain '' with max_inclusive_version=9. Set to 1.
2021-03-24 17:52:03,270 - onnx-tf - DEBUG - Fail to get since_version of SequenceEmpty in domain '' with max_inclusive_version=9. Set to 1.
2021-03-24 17:52:03,270 - onnx-tf - DEBUG - Fail to get since_version of SequenceErase in domain '' with max_inclusive_version=9. Set to 1.
2021-03-24 17:52:03,270 - onnx-tf - DEBUG - Fail to get since_version of SequenceInsert in domain '' with max_inclusive_version=9. Set to 1.
2021-03-24 17:52:03,270 - onnx-tf - DEBUG - Fail to get since_version of SequenceLength in domain '' with max_inclusive_version=9. Set to 1.
2021-03-24 17:52:03,270 - onnx-tf - DEBUG - Fail to get since_version of SplitToSequence in domain '' with max_inclusive_version=9. Set to 1.
2021-03-24 17:52:03,271 - onnx-tf - DEBUG - Fail to get since_version of ThresholdedRelu in domain '' with max_inclusive_version=9. Set to 1.
2021-03-24 17:52:03.273323: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (onednN)to use the following cpu instructions in performance-critical operations:  AVX2 FMA
To enable them in other operations,rebuild TensorFlow with the appropriate compiler flags.
2021-03-24 17:52:03.286901: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x7f912d05cf60 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2021-03-24 17:52:03.286913: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host,Default Version
2021-03-24 17:52:07.450878: W tensorflow/python/util/util.cc:348] Sets are not currently considered sequences,but this may change in the future,so consider avoiding using them.

解决方法

好的,原来网络的输出也给出了版本和其他参数。

onnx_model = onnx.load(model_path)  # load onnx model
tf_rep = prepare(onnx_model)

print(tf_rep.inputs) # Input nodes to the model
> output: ['visual_observation_0','visual_observation_1']
print(tf_rep.outputs) # Output nodes from the model
> output: ['version_number','memory_size','continuous_actions','continuous_action_output_shape','action','is_continuous_control','action_output_shape']

输入和我预期的一样。然而,输出也有版本号、内存等。我只对 continuous_actions 感兴趣。我还必须将图像缩放到 [0,1]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?