标签: tensorflow-lite

是否有使用 TF Lite 的有关 OCR 的示例/演示?

我看到 OCR 在 TFDevSummit2019 的 TFLite 使用列表中。是否有使用 TF Lite 的有关 OCR 的示例/演示?

描述:我想基于deeplearning训练一个足够小的OCR模型,转换成tflite格式,然后部署到手机上。我工作了几天,仍然无法获得 tflite 格式的 OCR 模型。

(“图片”部分的OCR,希望得到一些解决方案~~) TFDevSummit2019

ocr mobile tensorflow-lite

10
推荐指数
0
解决办法
831
查看次数

如何为 tflite 解释器调用 Flex 委托?

我有一个 TensorFlow 模型,我想将其转换为 tflite 模型,该模型将部署在 ARM64 平台上。

恰好是我的模型的两个操作(RandomStandardNormal、Softplus)似乎需要自定义实现。由于执行时间不是那么重要,我决定采用使用扩展运行时间的混合模型。我通过以下方式转换它:

graph_def_file = './model/frozen_model.pb'
inputs = ['eval_inputs']
outputs = ['model/y']

converter = tf.lite.TFLiteConverter.from_frozen_graph(graph_def_file, inputs, outputs)
converter.target_ops = [tf.lite.OpsSet.TFLITE_BUILTINS, tf.lite.OpsSet.SELECT_TF_OPS]

tflite_file_name = 'vae_' + str(tf.__version__) + '.tflite'

tflite_model = converter.convert()
open(tflite_file_name, 'wb').write(tflite_model)
Run Code Online (Sandbox Code Playgroud)

这奏效了,我最终得到了一个看似有效的 tflite 模型文件。每当我尝试使用解释器加载此模型时,都会收到错误消息(我使用 Python 还是 C++ API 都没有关系):

ERROR: Regular TensorFlow ops are not supported by this interpreter. Make sure you invoke the Flex delegate before inference.
ERROR: Node number 4 (FlexSoftplus) failed to prepare.
Run Code Online (Sandbox Code Playgroud)

我很难在 tf 网站上找到有关如何为这两个 API 调用 Flex 委托的文档。我偶然发现了一个头文件(“tensorflow/lite/delegates/flex/delegate_data.h”),它似乎与这个问题有关,但将它包含在我的 …

c++ python-3.x tensorflow tensorflow-lite

10
推荐指数
1
解决办法
5316
查看次数

Tensorflow-Lite预训练模型在Android演示中不起作用

Tensorflow-Lite Android演示版可与其提供的原始模型配合使用:mobilenet_quant_v1_224.tflite.请参阅:https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/lite

他们还提供了其他预训练的精简模型:https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/lite/g3doc/models.md

不过,我下载了一些较小的型号,从上面的链接,例如,mobilenet_v1_0.25_224.tflite,以及与此模型演示程序仅通过改变取代了原来的模型MODEL_PATH = "mobilenet_v1_0.25_224.tflite";ImageClassifier.java.该应用程序崩溃:

12-11 12:52:34.222 17713-17729 /?E/AndroidRuntime:FATAL EXCEPTION:CameraBackground进程:android.example.com.tflitecamerademo,PID:17713 java.lang.IllegalArgumentException:无法获取输入维度.第0个输入应该有602112个字节,但是找到150528个字节.在org.tensorflow.lite.NativeInterpreterWrapper.getInputDims(本机方法)在org.tensorflow.lite.NativeInterpreterWrapper.run(NativeInterpreterWrapper.java:82)在org.tensorflow.lite.Interpreter.runForMultipleInputsOutputs(Interpreter.java:112)在组织.tensorflow.lite.Interpreter.run(Interpreter.java:93)在com.example.android.tflitecamerademo.ImageClassifier.classifyFrame(ImageClassifier.java:108)在com.example.android.tflitecamerademo.Camera2BasicFragment.classifyFrame(Camera2BasicFragment.java :663)在com.example.android.tflitecamerademo.Camera2BasicFragment.access $ 900(Camera2BasicFragment.java:69)在com.example.android.tflitecamerademo.Camera2BasicFragment $ 5.run(Camera2BasicFragment.java:558)在android.os.Handler. handleCallback(Handler.java:751)位于android.os.HandlerThread.run(HandlerThread.java)的android.os.Handler.dispatchMessage(Handler.java:95)android.os.Looper.loop(Looper.java:154) :61)

原因似乎是模型所需的输入尺寸是图像尺寸的四倍.所以我修改DIM_BATCH_SIZE = 1DIM_BATCH_SIZE = 4.现在的错误是:

致命异常:CameraBackground工艺:android.example.com.tflitecamerademo,PID:18241 java.lang.IllegalArgumentException异常:不能与FLOAT32类型转换的TensorFlowLite张量类型的Java对象[[B(其是与TensorFlowLite类型UINT8兼容)在org.tensorflow.lite.Tensor.copyTo(Tensor.java:36)在org.tensorflow.lite.Interpreter.runForMultipleInputsOutputs(Interpreter.java:122)在org.tensorflow.lite.Interpreter.run(Interpreter.java:93 )在com.example.andfl.Cfl/2中,com.example.andin.Bilic文件中,com.example.andis.Ffl.cfl.获得$ 900在android.os.Handler(Camera2BasicFragment.java:69)在com.example.android.tflitecamerademo.Camera2BasicFragment $ 5.run(Camera2BasicFragment.java:558)在android.os.Handler.handleCallback(Handler.java:751) android.os.Loop上的.dispatchMessage(Handler.java:95)er.loop(Looper.java:154)在android.os.HandlerThread.run(HandlerThread.java:61)

我的问题是如何使用简化的MobileNet tflite模型来使用TF-lite Android Demo.

(我实际上尝试了其他的东西,比如使用提供的工具将TF冻结图转换为TF-lite模型,甚至使用与https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib完全相同的示例代码/lite/toco/g3doc/cmdline_examples.md,但转换后的tflite模型仍无法在Android Demo中运行.)

android tensorflow tensorflow-lite

9
推荐指数
1
解决办法
4236
查看次数

Android TensorFlow Lite 解释器:如何修复“数据类型错误:无法解析 java.lang.Float 的数据类型”

当运行 TFLite 解释器将包含浮点数的 ByteBuffer 作为输入时,应用程序抛出异常:

“数据类型错误:无法解析 java.lang.Float 的数据类型”

模型本身在 Keras 上进行训练,然后转换为 TF,然后转换为 TFLite。

对于转换,我使用了 TF(1.5.0 版)toco 转换器。

toco 的输入参数:

toco --input_file=converted.pb --output_file=model.tflite --input_format=TENSORFLOW_GRAPHDEF --input_shape=1,224,224,3 --input_array=main_input --output_array=main_output/Sigmoid --inference_type=FLOAT --output_format=TFLITE - -input_type=浮动

我还手动创建了一个 float[][][][] 数组,而不是 ByteBuffer,其维度是模型的预期输入:[1,224,224,3]

导致与 ByteBuffer 相同的错误。

请注意,我将浮点数除以 255 以获得 [0,1] 范围内的像素值。

import org.tensorflow.lite.Interpreter;
import java.nio.ByteBuffer;


public Interpreter tflite;

tflite = new Interpreter(loadModelFile(Test_TFLite.this,modelFile));

ByteBuffer bytebuffer_float = convertBitmapToByteBuffer_float(image, 1, 
    224, 3);

float out = 0;

tflite.run(bytebuffer_float,out);


private ByteBuffer convertBitmapToByteBuffer_float(Bitmap bitmap, int 
    BATCH_SIZE, int inputSize, int PIXEL_SIZE) {
        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4 …
Run Code Online (Sandbox Code Playgroud)

java android tensorflow tensorflow-lite

9
推荐指数
1
解决办法
3399
查看次数

获取 input_array 和 output_array 项,将模型转换为 tflite 格式

附注。请不要指出我将 Keras 模型直接转换为 tflite,因为我的 .h5 文件将无法直接转换为 .tflite。我以某种方式设法将我的 .h5 文件转换为 .pb

我已经按照这个Jupyter notebook 使用 Keras 进行了人脸识别。然后我将我的模型保存到一个model.h5文件中,然后model.pb使用这个.

现在我想在 Android 中使用我的 tensorflow 文件。为此,我需要使用 Tensorflow Lite,这需要我将模型转换为某种.tflite格式。

为此,我试图在此处遵循官方指南。正如你在那里看到的,它需要input_arrayoutput_array数组。如何从我的model.pb文件中获取这些内容的详细信息?

python tensorflow tensorflow-lite

9
推荐指数
1
解决办法
6453
查看次数

了解 tf.contrib.lite.TFLiteConverter 量化参数

我正在尝试使用 UINT8 量化,同时将 tensorflow 模型转换为 tflite 模型:

如果使用post_training_quantize = True,模型大小比原始 fp32 模型低 x4,所以我假设模型权重是 uint8,但是当我加载模型并通过interpreter_aligner.get_input_details()[0]['dtype']它的 float32获取输入类型时。量化模型的输出与原始模型大致相同。

converter = tf.contrib.lite.TFLiteConverter.from_frozen_graph(
        graph_def_file='tflite-models/tf_model.pb',
        input_arrays=input_node_names,
        output_arrays=output_node_names)
converter.post_training_quantize = True
tflite_model = converter.convert()
Run Code Online (Sandbox Code Playgroud)

转换模型的输入/输出:

print(interpreter_aligner.get_input_details())
print(interpreter_aligner.get_output_details())
[{'name': 'input_1_1', 'index': 47, 'shape': array([  1, 128, 128,   3], dtype=int32), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0)}]
[{'name': 'global_average_pooling2d_1_1/Mean', 'index': 45, 'shape': array([  1, 156], dtype=int32), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0)}]
Run Code Online (Sandbox Code Playgroud)

另一种选择是明确指定更多参数:模型大小比原始 fp32 模型低 x4,模型输入类型为 uint8,但模型输出更像是垃圾。

converter = tf.contrib.lite.TFLiteConverter.from_frozen_graph(
        graph_def_file='tflite-models/tf_model.pb',
        input_arrays=input_node_names,
        output_arrays=output_node_names)
converter.post_training_quantize = True …
Run Code Online (Sandbox Code Playgroud)

python quantization deep-learning tensorflow tensorflow-lite

9
推荐指数
2
解决办法
6341
查看次数

在 Tensorflow-lite 中输入具有动态尺寸的图像

我有一个 tensorflow 模型,它采用不同大小的输入图像:

inputs = layers.Input(shape=(128,None,1), name='x_input')

<tf.Tensor 'x_input:0' shape=(?, 128, ?, 1) dtype=float32>
Run Code Online (Sandbox Code Playgroud)

当我将此模型转换为 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]'.
Run Code Online (Sandbox Code Playgroud)

我无法将图像缩放到固定大小。我看到的唯一解决方案是将图像填充到某个最大尺寸并在图表中使用该尺寸,但这似乎很浪费。有没有其他方法可以使 tensorflow-lite 与动态图像尺寸一起工作?这个限制有什么理由吗?谢谢。

python keras tensorflow tensorflow-lite

9
推荐指数
1
解决办法
6313
查看次数

如何将保存的模型从 sklearn 转换为 tensorflow/lite

如果我想使用sklearn库实现分类器。有没有办法保存模型或将文件转换为已保存的tensorflow文件以便tensorflow lite以后将其转换?

machine-learning scikit-learn text-classification tensorflow tensorflow-lite

9
推荐指数
1
解决办法
2862
查看次数

Unable to load tensorflow tflite model in android studio

I have trained a TensorFlow model and convert it to TensorFlow lite using the below code:

# Convert the model
import tensorflow as tf
import numpy as np
# path to the SavedModel directory is TFLITE_PATH
converter = tf.lite.TFLiteConverter.from_saved_model(TFLITE_PATH) 

tflite_model = converter.convert()

# Save the model.
with open('model_1.tflite', 'wb') as f:
  f.write(tflite_model)
Run Code Online (Sandbox Code Playgroud)

Attaching my model_1.tflite model in case you want to investigate. I have tested it inside my python environment, where it is producing output using the below script:

import …
Run Code Online (Sandbox Code Playgroud)

android dart flutter tensorflow-lite tensorflow2.0

9
推荐指数
0
解决办法
3441
查看次数

如何在Raspi 3上使用libtensorflow-lite.a?

我想在Raspi上安装TensorFlow Lite.

我假设我可以先pip install为Raspi预建TensorFlow.我正在阅读这里交叉编译TensorFlow Lite的说明,但我不知道在生成之后该怎么做libtensorflow-lite.a.

tensorflow raspberry-pi3 tensorflow-lite

8
推荐指数
1
解决办法
329
查看次数