转换为 Tensorflow Lite 时 tf.ParseExampleV2 的问题:“op 既不是自定义操作也不是弹性操作”

问题描述

请原谅我的英语。

我一直在尝试处理 tensorflow (v2.x) 的 Estimators API,但是当我尝试使用此代码将模型从 tf.estimators 转换为 tflite 时:

import tensorflow as tf

import numpy as np

feature_name = "features"
feature_columns = [tf.feature_column.numeric_column(feature_name,shape=[2])]

classifier = tf.estimator.LinearClassifier(
    feature_columns=feature_columns,n_classes=2,model_dir="Z:\\tests\\iris")

feature_spec = {'features': tf.io.FixedLenFeature(shape=[2],dtype=np.float32)}
serving_fn = tf.estimator.export.build_parsing_serving_input_receiver_fn(feature_spec)
classifier.export_saved_model(export_dir_base='Z:\\tests\\iris\\',serving_input_receiver_fn=serving_fn)

saved_model_obj = tf.saved_model.load("Z:\\tests\\iris\\1613055608")
concrete_func = saved_model_obj.signatures['serving_default']

converter = tf.lite.TFLiteConverter.from_concrete_functions([concrete_func])
print(saved_model_obj.signatures.keys())
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.experimental_new_converter = True

tflite_model = converter.convert()

with open('Z:\\tests\\model.tflite_estimators','wb') as f:
    f.write(tflite_model)

我收到以下错误

ConverterError: C:\Users\\.....\tensorflow\python\saved_model\load.py:909:0: error: 'tf.ParseExampleV2' op is neither a custom op nor a flex op
C:\Users\\.....\tensorflow\python\saved_model\load.py:859:0: note: called from
P:\\.....\sanstitre3.py:19:0: note: called from
C:\Users\\.....\AppData\Local\Continuum\anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py:465:0: note: called from
C:\Users\\.....\AppData\Local\Continuum\anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py:578:0: note: called from
<ipython-input-115-f30bf3b642d5>:1:0: note: called from
C:\Users\\.....\AppData\Local\Continuum\anaconda3\lib\site-packages\IPython\core\interactiveshell.py:3343:0: note: called from
C:\Users\\.....\AppData\Local\Continuum\anaconda3\lib\site-packages\IPython\core\interactiveshell.py:3263:0: note: called from
C:\Users\\.....\AppData\Local\Continuum\anaconda3\lib\site-packages\IPython\core\interactiveshell.py:3072:0: note: called from
C:\Users\\.....\AppData\Local\Continuum\anaconda3\lib\site-packages\IPython\core\async_helpers.py:68:0: note: called from
<unkNown>:0: error: Failed while converting: 'main': Ops that can be supported by the flex runtime (enabled via setting the -emit-select-tf-ops flag):
    tf.ParseExampleV2 {dense_shapes = [#tf.shape<2>],device = "",num_sparse = 0 : i64,result_segment_sizes = dense<[0,1,0]> : vector<6xi32>}

互联网上有人已经提议在 converter.experimental_new_converter = True :

添加这两行
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS,tf.lite.OpsSet.SELECT_TF_OPS]

它编译没有错误,只是警告,但是当我将 TFLite 模型放在我的 STM32 上时,它给了我错误 TOOL ERROR/ UnkNown layer type FlexParseExampleV2,stopping.

有人可以帮我吗?

祝你有个美好的一天

解决方法

TensorFlow Lite Micro 不支持 Flex 委托,因此无法在 MCU 上运行 Selece TF ops。您可以尝试使用(例如)keras 顺序 API 重构您的模型,使其仅使用 TFLite 操作进行转换。

上下文:https://github.com/tensorflow/tensorflow/issues/34350#issuecomment-579027135

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...