簡體   English   中英

TensorFlow Lite:toco_convert 用於任意大小的輸入張量

[英]TensorFlow Lite: toco_convert for arbitrary sized input tensor

考慮將我的 TensorFlow 模型轉換為 Flatbuf 格式 ( .tflite )。

但是,我的模型允許輸入任意大小,即您可以一次分類一項或 N 項。 當我嘗試轉換時,它會引發錯誤,因為我的輸入/輸出設備之一是NoneType類型。

想想TensorFlow MNIST 教程之類的東西,在計算圖中,我們的輸入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)錯誤: TypeError: __int__ returned non-int (type NoneType)

查看tf.contrib.lite.toco_convert文檔,我們有“input_tensors:輸入張量列表。使用 foo.get_shape() 和 foo.dtype 計算類型和形狀。”。 所以這就是我們失敗的地方。 但我不確定是否有我應該使用的參數或允許我導出這樣的模型的東西

這個問題已經在最新的轉換器代碼中解決了。 您可以傳遞一個輸入張量,其中第一個維度為None (第一個維度通常是批處理),轉換器將正確處理它。

順便說一句,在調用解釋器之前,您可以調用interpreter.resize_tensor_input來更改批大小。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM