【发布时间】:2018-04-25 18:14:47
【问题描述】:
正在考虑将我的 TensorFlow 模型转换为 Flatbuf 格式 (.tflite)。
但是,我的模型允许输入任意大小,即您可以一次对一个项目或 N 个项目进行分类。当我尝试转换时,它会引发错误,因为我的输入/输出设备之一是 NoneType 类型。
想想像TensorFlow MNIST tutorial 这样的东西,在计算图中,我们的输入x 的形状是[None, 784]。
从tflite dev guide,您可以将模型转换为 FlatBuf,如下所示:
import tensorflow as tf
img = tf.placeholder(name="img", dtype=tf.float32, shape=(1, 64, 64, 3))
val = img + tf.constant([1., 2., 3.]) + tf.constant([1., 4., 4.])
out = tf.identity(val, name="out")
with tf.Session() as sess:
tflite_model = tf.contrib.lite.toco_convert(sess.graph_def, [img], [out])
open("converteds_model.tflite", "wb").write(tflite_model)
但是,这对我不起作用。 MWE 可能是:
import tensorflow as tf
img = tf.placeholder(name="inputs", dtype=tf.float32, shape=(None, 784))
out = tf.identity(inputs, name="out")
with tf.Session() as sess:
tflite_model = tf.contrib.lite.toco_convert(sess.graph_def, [img], [out])
open("converteds_model.tflite", "wb").write(tflite_model)
错误:TypeError: __int__ returned non-int (type NoneType)
查看 tf.contrib.lite.toco_convert 文档,我们有“input_tensors:输入张量列表。类型和形状是使用 foo.get_shape() 和 foo.dtype 计算的。”。这就是我们可能失败的地方。但我不确定是否有我应该使用的参数或可以让我导出这样的模型的东西
【问题讨论】:
-
如果您的模型没有完整的形状信息,您可能需要使用 input_shapes=None 指定它,也就是说,如果您导出到 SavedModel 并将其提供给 tflite_convert,您可能有自从该工具完成后的更多外观。
标签: python tensorflow