我有一个tensorflow模型,它接受不同大小的输入图像:
inputs = layers.Input(shape=(128,None,1), name='x_input')
<tf.Tensor 'x_input:0' shape=(?, 128, ?, 1) dtype=float32>
当我将此模型转换为tensorflow-lite时,它会抱怨:
converter = tf.lite.TFLiteConverter.from_frozen_graph(
graph_def_file, input_arrays, output_arrays)
tflite_model = converter.convert()
ValueError: None is only supported in the 1st dimension.
Tensor 'x_input_1' has invalid shape '[None, 128, None, 1]'.
我无法将图像缩放到固定大小。我看到的唯一解决方案是将图像填充到最大尺寸,并在图形中使用该图像,但这似乎很浪费。还有其他方法可以使tensorflow-lite与动态图像尺寸一起使用吗?对此限制有任何理由吗?谢谢。
参考方案
是的,您可以在TF-Lite中使用动态张量。无法将形状直接设置为[None, 128, None, 1]
的原因是,通过这种方式,将来可以轻松支持更多语言。此外,它充分利用了静态内存分配方案。对于打算用于具有低计算能力的小型设备的框架来说,这是一个明智的设计选择。
以下是有关如何动态设置张量大小的步骤:
0.冻结
似乎您是从冻结的GraphDef(即*.pb
文件)进行转换。假设冻结的模型的输入形状为[None, 128, None, 1]
。
1.转换步骤。
在此步骤中,将输入大小设置为模型可以接受的任何有效大小。例如:
tflite_convert \
--graph_def_file='model.pb' \
--output_file='model.tflite' \
--input_shapes=1,128,80,1 \ # <-- here, you set an
# arbitrary valid shape
--input_arrays='input' \
--output_arrays='Softmax'
2.推论步骤
诀窍是在推理过程中实时使用TF-Lite API的函数interpreter::resize_tensor_input(...)
。我将提供它的python实现。 Java和C ++实现应相同(因为它们具有相似的API):
from tensorflow.contrib.lite.python import interpreter
# Load the *.tflite model and get input details
model = Interpreter(model_path='model.tflite')
input_details = model.get_input_details()
# Your network currently has an input shape (1, 128, 80 , 1),
# but suppose you need the input size to be (2, 128, 200, 1).
model.resize_tensor_input(
input_details[0]['index'], (2, 128, 200, 1))
model.allocate_tensors()
而已。现在,只要您的网络体系结构允许输入这样的形状,就可以将该模型用于形状为(2, 128, 200, 1)
的图像。请注意,每次进行这样的重塑时,您都必须执行model.allocate_tensors()
,这样效率很低。强烈建议避免在程序中过多使用此功能。
我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。
Python:传递记录器是个好主意吗? - python我的Web服务器的API日志如下:started started succeeded failed 那是同时收到的两个请求。很难说哪一个成功或失败。为了彼此分离请求,我为每个请求创建了一个随机数,并将其用作记录器的名称logger = logging.getLogger(random_number) 日志变成[111] started [222] start…
Python-Excel导出 - python我有以下代码:import pandas as pd import requests from bs4 import BeautifulSoup res = requests.get("https://www.bankier.pl/gielda/notowania/akcje") soup = BeautifulSoup(res.cont…
Python:如何根据另一列元素明智地查找一列中的空单元格计数? - pythondf = pd.DataFrame({'user': ['Bob', 'Jane', 'Alice','Jane', 'Alice','Bob', 'Alice'], 'income…
Python pytz时区函数返回的时区为9分钟 - python由于某些原因,我无法从以下代码中找出原因:>>> from pytz import timezone >>> timezone('America/Chicago') 我得到:<DstTzInfo 'America/Chicago' LMT-1 day, 18:09:00 STD…