我正在尝试调试我的tflite模型,该模型使用自定义操作。我找到了操作名称(in *.pb)和操作ID(in *.tflite)之间的对应关系,并且我正在进行逐层比较(以确保输出差异始终在范围内1e-4(因为它在最后爆炸) ,我想找到我的自定义层失败的确切位置)如下:
方法1:我使用get_tensor如下方式获取输出:
from tensorflow.contrib.lite.python import interpreter
# load the model
model = interpreter.Interpreter(model_path='model.tflite')
model.allocate_tensors()
# get tensors
for i in tensor_ids:
tensor_output[i] = model.get_tensor(i)
Run Code Online (Sandbox Code Playgroud)
它显示的随机值完全不足(与 TensorFlow 模型的输出相比)。
方法2:只转换*.pb到某一层,然后重复,基本上:
创建一个*.pb,使其仅包含从input到 的网络layer_1。
转换为tflite(因此输出现在为layer_1)并使用 TensorFlow 检查 TF-Lite 的输出。
layer_2对, layer_3, ...重复步骤 1-2 outputs。
此方法需要更多的工作和执行,但它正确地表明,对于内置操作,模型的输出tflite是pb相同的,并且仅在我的自定义操作中开始有所不同(而在方法 1中,输出立即与第一层不同) 。
问:为什么行为
get_tensor …
Tensorflow 推出了 XLA 编译器,它编译针对 LLVM 的后端 C++ 张量流。我对 XLA 的理解是,只要有 LLVM -> 设备支持,它就是支持通用加速设备的一步。
Tensorflow lite 最近发布,取代了 Tensorflow Mobile,似乎工作重点是针对嵌入式和移动设备,并且明显关注嵌入式 DSP 和 GPU 作为这些环境中常见的可选处理器。Tensorflow lite 似乎将操作移交给 Android NNAPI(神经网络 API),并支持 TensorFlow OP 的子集。
所以这就引出了一个问题:Google 将朝哪个方向支持非 CUDA 设备?XLA 是否还有超出我描述范围的用例?
你能帮我理解为什么他们- IMAGE_MEAN和/ IMAGE_STD吗?
private static final float IMAGE_MEAN = 127.5f;
private static final float IMAGE_STD = 127.5f;
//...
@Override
protected void addPixelValue(int pixelValue) {
imgData.putFloat((((pixelValue >> 16) & 0xFF) - IMAGE_MEAN) / IMAGE_STD);
imgData.putFloat((((pixelValue >> 8) & 0xFF) - IMAGE_MEAN) / IMAGE_STD);
imgData.putFloat(((pixelValue & 0xFF) - IMAGE_MEAN) / IMAGE_STD);
}
Run Code Online (Sandbox Code Playgroud)
@Override
protected void addPixelValue(int pixelValue) {
imgData.put((byte) ((pixelValue >> 16) & 0xFF));
imgData.put((byte) ((pixelValue >> 8) & 0xFF));
imgData.put((byte) …Run Code Online (Sandbox Code Playgroud) 我一直在使用 Tensorflow 的 TFLite 研究量化。据我了解,可以量化我的模型权重(以便使用更少的 4 倍内存来存储它们),但这并不一定意味着模型不会将其转换回浮点数来运行它。我还了解到,要仅使用 int 运行我的模型,我需要设置以下参数:
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
converter.inference_input_type = tf.uint8
converter.inference_output_type = tf.uint8
Run Code Online (Sandbox Code Playgroud)
我想知道tf.lite.Interpreter设置了这些参数的加载模型与未设置这些参数的加载模型之间有什么区别。我试图对此进行调查.get_tensor_details(),但没有发现任何差异。
我正在尝试使用 CameraX api 使用 tflite 模型分析相机预览帧。
本文档描述了使用 ImageAnalyzer 处理传入帧。目前帧是作为 YUV 传入的,我不确定如何将 YUV 图像数据传递给需要形状输入 (BATCHxWIDTHxHEIGHTx3) 的 tflite 模型。在旧的 API 中,您可以指定预览输出格式并将其更改为 rgb,但是此页面特别说明“CameraX 以 YUV_420_888 格式生成图像”。
首先,我希望有人找到了一种将 RGB 传递给分析器而不是 YUV 的方法,其次,如果没有,有人可以建议一种将 YUV 图像传递给 TFLite 解释器的方法吗?传入的图像对象是 ImageProxy 类型,它有 3 个平面,Y、U 和 V。
这是重现错误的简单代码:
import os
os.environ["CUDA_VISIBLE_DEVICES"]="-1"
import numpy as np
from keras.models import Sequential
from keras.layers import Conv1D, Flatten, Dense
import tensorflow as tf
model_path = 'test.h5'
model = Sequential()
model.add(Conv1D(8,(5,), input_shape=(100,1)))
model.add(Flatten())
model.add(Dense(1))
model.compile(loss='mse', optimizer='adam')
model.save(model_path)
model = tf.keras.models.load_model(model_path, compile=False)
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
interpreter = tf.lite.Interpreter(model_content=tflite_model)
interpreter.resize_tensor_input(interpreter.get_input_details()[0]['index'], (2,100,1))
interpreter.resize_tensor_input(interpreter.get_output_details()[0]['index'], (2,1))
interpreter.allocate_tensors()
Run Code Online (Sandbox Code Playgroud)
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-3-ad8e2eea467f> in <module>
27 interpreter.resize_tensor_input(interpreter.get_output_details()[0]['index'], (2,1))
28
---> 29 interpreter.allocate_tensors()
<>/tensorflow/lite/python/interpreter.py in allocate_tensors(self)
240 def allocate_tensors(self):
241 self._ensure_safe()
--> 242 …Run Code Online (Sandbox Code Playgroud) 我正在尝试将onnx模型转换为tflite,我面临执行行错误tf_rep.export_graph(tf_model_path)。这个问题之前在 SO 中被问过,但没有提供明确的解决方案。
安装要求:tensorflow: 2.12.0, onnx 1.14.0, onnx-tf 1.10.0,Python 3.10.12
import torch
import onnx
import tensorflow as tf
import onnx_tf
from torchvision.models import resnet50
# Load the PyTorch ResNet50 model
pytorch_model = resnet50(pretrained=True)
pytorch_model.eval()
# Export the PyTorch model to ONNX format
input_shape = (1, 3, 224, 224)
dummy_input = torch.randn(input_shape)
onnx_model_path = 'resnet50.onnx'
torch.onnx.export(pytorch_model, dummy_input, onnx_model_path, opset_version=12, verbose=False)
# Load the ONNX model
onnx_model = onnx.load(onnx_model_path)
# Convert …Run Code Online (Sandbox Code Playgroud) 我有一个.pb模型,我想将其用作自定义MLKit模型。MLKit仅支持.tflite模型,但即使在我使用tocoTensorFlow Lite模型后,文件大小对于Firebase还是太大(95 MB,仅允许40 MB)。
有没有一种方法可以量化图,然后转换为TFLite或量化.tflite图?
当我做前者时,我收到以下错误消息: Unsupported TensorFlow op: Dequantize) for which the quantized form is not yet implemented. Sorry, and patches welcome (that's a relatively fun patch to write, mostly providing the actual quantized arithmetic code for this op).
我有一个自定义 CNN 模型,我已将其转换为 .tflite 格式并将其部署在我的 Android 应用程序上。但是,我无法弄清楚如何在使用 tensorflow lite 进行推理时进行批处理。
从这个 Google doc看来,您必须设置模型的输入格式。但是,本文档使用的是带有 Firebase API 的代码示例,我不打算使用它。
更具体:
我想一次推断多个 100x100x3 图像,因此输入大小为N x100x100x3。
题:
如何使用 TF lite 做到这一点?
我下面这个关于创建使用TensorFlow精简版上的协同合作模型制作自定义模型教程。
import pathlib
path = pathlib.Path('/content/employee_pics')
count = len(list(path.glob('*/*.jpg')))
count
data = ImageClassifierDataLoader.from_folder(path)
train_data, test_data = data.split(0.5)
Run Code Online (Sandbox Code Playgroud)
我有第 2 步的问题:
model = image_classifier.create(train_data)
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:ValueError: Expect x to be a non-empty array or dataset。
难道我做错了什么?不过,示例中提供的数据集运行良好。为什么?
tensorflow-lite ×10
tensorflow ×7
android ×4
keras ×4
python ×3
google-mlkit ×1
hexagon-dsp ×1
onnx ×1
quantization ×1
tf.keras ×1