标签: tensorflow-lite

从 TFlite 中的 typed_output_tensor 提取输出数据

预先感谢您的支持。

我试图在 .tflite U-net 神经网络上进行推理后获取张量的输出。我使用 Tensorflow lite 图像分类代码作为基线。

我需要调整代码以执行分段任务。我的问题是如何访问推断模型的输出(128x128x1)并将结果写入图像?

我已经调试了代码并探索了许多不同的方法。不幸的是,我对 C++ 语言没有信心。我发现命令:interpreter->typed_output_tensor<float>(0)应该是我需要的,也在这里引用:https: //www.tensorflow.org/lite/guide/inference#loading_a_model。但是,我无法访问网络生成的 128x128 张量。

您可以在以下地址找到代码:https://github.com/tensorflow/tensorflow/blob/770481fb3e9126f9a29db5667f528e450d54d719/tensorflow/lite/examples/label_image/label_image.cc

有趣的部分在这里(第 217 -224 行):

const float threshold = 0.001f;

std::vector<std::pair<float, int>> top_results;

int output = interpreter->outputs()[0];
TfLiteIntArray* output_dims = interpreter->tensor(output)->dims;
// assume output dims to be something like (1, 1, ... ,size)
auto output_size = output_dims->data[output_dims->size - 1];
Run Code Online (Sandbox Code Playgroud)

我期望保存在图像中的值或保存输出张量的替代方法

c++ tensorflow tensorflow-lite

5
推荐指数
0
解决办法
1327
查看次数

将 Keras 模型转换为可在 Edge TPU 上使用的量化 Tensorflow Lite 模型

我有一个 Keras 模型,想在 Coral Edge TPU 设备上运行。为此,它需要是具有完整整数量化的 Tensorflow Lite 模型。我能够将模型转换为 TFLite 模型:

model.save('keras_model.h5')

converter = tf.lite.TFLiteConverter.from_keras_model_file("keras_model.h5")
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)
Run Code Online (Sandbox Code Playgroud)

但是当我运行时edgetpu_compiler converted_model.tflite,我收到此错误:

Edge TPU Compiler version 2.0.267685300
Invalid model: converted_model.tflite
Model not quantized
Run Code Online (Sandbox Code Playgroud)

这是因为我需要量化模型,但我不知道该怎么做。我发现这个页面告诉我如何做到这一点,但它希望我制作一个输入数据生成器。这是它提供的示例:

Edge TPU Compiler version 2.0.267685300
Invalid model: converted_model.tflite
Model not quantized
Run Code Online (Sandbox Code Playgroud)

如何调整此代码以处理我的输入数据?从哪里来num_calibration_steps?有一个更好的方法吗?(我看到了参考tf.contrib.tpu.keras_to_tpu_model但它已被弃用)

python keras tensorflow tensorflow-lite tpu

5
推荐指数
1
解决办法
1930
查看次数

构建 TensorFlowLite Swift 自定义框架

我需要从源代码构建 TensorFlowLite Swift Framework/cocoapod,然后在 Swift 项目之一中使用它而不是原始框架。

下载代码并按照说明进行操作,但在少数情况下没有成功:

1) https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/experimental/swift

a) python 配置.py

b) CocoaPods 开发人员不适用,因为它不从本地 TensofFlow 文件夹获取源代码(或者我错了?)

c) Bazel 开发人员我所做的: bazel build tensorflow/lite/experimental/swift:TensorFlowLite

bazel test tensorflow/lite/experimental/swift:Tests --swiftcopt=-enable-testing

最终

generate_xcodeproj.sh --genconfig tensorflow/lite/experimental/swift/TensorFlowLite.tulsiproj:TensorFlowLite --outputfolder ~/path/to/generated/TensorFlowLite.xcodeproj

结果我得到了一个带有 libtensorflow-lite-experimental-swift-TensorFlowLite.a 和一些idx文件的项目。它编译并看到 .a 文件(但应用程序和测试由于各种原因都没有编译),并且我不太明白如何将 .a lib 与 Swift 文件一起使用,因为没有模块/标头等. 文件。

所有这些练习完成后,就可以TensorFlowLiteSwift.podspec在 TensorFlow 的根目录和experimental/Swift文件夹中找到。它仍然取决于,TensorFlowLiteC如果我通过路径参数在另一个项目中引用这个 podspec,我可以这样做,import TensorFlowLiteC但不能import TensorFlowLiteSwift/import TensorFlowLite并且我相信 TensorFlowLiteC 仍然是从互联网上获取的,而不是本地的。

2)自定义框架路径: https ://firebase.google.com/docs/ml-kit/ios/use-custom-tflite

这里有几个问题:

a)如果我尝试仅使用内部添加了tensorflow_lite.framework的项目,那么我无法在Swift文件中引用TensorFlowLite/等。TensorFlowLiteCb) 如果我尝试遵循创建本地 Pod …

ios cocoapods swift tensorflow tensorflow-lite

5
推荐指数
1
解决办法
1518
查看次数

如何使用 python 将 .pickle 或 .dat 文件转换为 .tflite?

我有 .pickle 文件,它由面部编码数组组成,现在我想将其转换为 .tflite 扩展文件。我该怎么做?提前致谢!回答将不胜感激

computer-vision python-3.x deep-learning tensorflow tensorflow-lite

5
推荐指数
0
解决办法
2146
查看次数

视频输入上的 TFLite 推理

我有一个 SSD tflite 检测模型,正在台式计算机上使用 Python 运行。就目前而言,我的下面的脚本将单个图像作为推理的输入,并且运行良好:

    # Load TFLite model and allocate tensors.
    interpreter = tf.lite.Interpreter(model_path="model.tflite")
    interpreter.allocate_tensors()

    img_resized = Image.open(file_name)
    input_data = np.expand_dims(img_resized, axis=0)
    input_data = (np.float32(input_data) - input_mean) / input_std

    input_details = interpreter.get_input_details()
    output_details = interpreter.get_output_details()

    interpreter.set_tensor(input_details[0]['index'], input_data)
    interpreter.invoke()
    output_data = interpreter.get_tensor(output_details[0]['index'])
Run Code Online (Sandbox Code Playgroud)

如何对 .mp4 视频作为输入进行推理?

是否也可以从该视频上检测到的对象中绘制边界框?

python inference tensorflow-lite

5
推荐指数
1
解决办法
3702
查看次数

将 BERT 模型转换为 TFLite

我有使用预训练的 bert 模型构建的语义搜索引擎的代码。我想将此模型转换为 tflite,以便将其部署到 google mlkit。我想知道如何转换它。我想知道是否有可能将其转换为 tflite。这可能是因为它在官方tensorflow网站上提到: https: //www.tensorflow.org/lite/convert。但我不知道从哪里开始

代码:


from sentence_transformers import SentenceTransformer

# Load the BERT model. Various models trained on Natural Language Inference (NLI) https://github.com/UKPLab/sentence-transformers/blob/master/docs/pretrained-models/nli-models.md and 
# Semantic Textual Similarity are available https://github.com/UKPLab/sentence-transformers/blob/master/docs/pretrained-models/sts-models.md

model = SentenceTransformer('bert-base-nli-mean-tokens')

# A corpus is a list with documents split by sentences.

sentences = ['Absence of sanity', 
             'Lack of saneness',
             'A man is eating food.',
             'A man is eating a piece of bread.',
             'The girl is carrying a baby.',
             'A man …
Run Code Online (Sandbox Code Playgroud)

python tensorflow tensorflow-lite bert-language-model

5
推荐指数
1
解决办法
4493
查看次数

转换后的 tflite 文件在 flutter 中不起作用

我将 h5 文件转换为 tflite。这个h5文件是ml5 Json模型。我刚刚自己转换成h5文件。但它不适用于错误代码。

ml5模型链接:https://github.com/CodingTrain/website/tree/master/learning/ml5/7.2_pose_classifier/p5-multi/1%20-%20Data%20Collection/model2

Windows10、Python 3.6.8、tfv2.0.0

代码:

import tensorflow as tf
model=tf.keras.models.load_model("E:\h5\outputs\model.h5")
converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.experimental_new_converter = True
tflite_model = converter.convert()
open("E:\h5\converted_model.tflite", "wb").write(tflite_model) 
Run Code Online (Sandbox Code Playgroud)

我用这些代码转换了文件

这个模型文件被放入我的应用程序中。

错误代码:

[+14766 ms] D/ViewRootImpl@454dd0a[MainActivity]( 1610): ViewPostIme pointer 0
[ +133 ms] D/ViewRootImpl@454dd0a[MainActivity]( 1610): ViewPostIme pointer 1
[  +13 ms] I/tflite  ( 1610): Initialized TensorFlow Lite runtime.
[  +36 ms] I/flutter ( 1610): success
[  +12 ms] I/CameraManagerGlobal( 1610): Camera 0 facing CAMERA_FACING_BACK state now
CAMERA_STATE_OPEN for client com.example.examplerealtime API Level 2
[ …
Run Code Online (Sandbox Code Playgroud)

flutter tensorflow-lite

5
推荐指数
0
解决办法
1294
查看次数

有没有办法在 Raspberry Pi 4 上获得 GPU 加速以进行深度学习?

在 Tensorflow Lite 官方网站上,他们展示了对 Android 和 iOS 的 GPU 加速的支持,但没有提及对 Raspberry Pi 的支持。

python gpu deep-learning tensorflow-lite raspberry-pi4

5
推荐指数
1
解决办法
7995
查看次数

由于内部错误,无法在解释器上运行 tflite 模型

我正在尝试为 Android 构建一个离线翻译器。我的模型深受本指南的启发: https: //www.tensorflow.org/tutorials/text/nmt_with_attention。我只是做了一些修改以确保模型是可序列化的。(您可以在最后找到模型的代码)

该模型在我的 jupyter 笔记本上完美运行。我使用的 Tensorflow 版本:2.3.0-dev20200617,我还能够使用以下代码片段生成 tflite 文件:

converter = tf.lite.TFLiteConverter.from_keras_model(partial_model)
tflite_model = converter.convert()

with tf.io.gfile.GFile('goog_nmt_v2.tflite', 'wb') as f:
  f.write(tflite_model)
Run Code Online (Sandbox Code Playgroud)

但是,当我使用生成的 tflite 模型来获取 Android 上的预测时,它会抛出错误java.lang.IllegalArgumentException: Internal error: Failed to run on the given Interpreter: tensorflow/lite/kernels/concatenation.cc:73 t->dims->data[d] != t0->dims->data[d] (8 != 1) Node number 84 (CONCATENATION) failed to prepare.

这很奇怪,因为我提供了与我在 jupyter 笔记本中完全相同的输入尺寸。以下是用于测试(虚拟输入)模型是否在 Android 上运行的 java 代码:

 HashMap<Integer, Object> outputVal = new HashMap<>();
        for(int i=0; i<2; i++) outputVal.put(i, new float[1][5]);
        float[][] inp_test = new float[1][8];
        float[][] …
Run Code Online (Sandbox Code Playgroud)

nlp tensorflow tensorflow-lite tensorflow2.0

5
推荐指数
1
解决办法
9449
查看次数

ModuleNotFoundError:没有名为“tflite_runtime”的模块

我正在尝试在 RPI Zero 上独立安装 Tensorflow Lite。但是,一旦按照步骤安装完成。

我无法导入,

import tflite_runtime.interpreter as tflite
Run Code Online (Sandbox Code Playgroud)

我收到的错误是:

ModuleNotFoundError:没有名为“tflite_runtime”的模块

此外,根据说明,我应该看到一个静态库。我也没有看到这一点。tensorflow/lite/tools/make/gen/lib/rpi_armv6/libtensorflow-lite.a

这是我的设置:

  • Raspbian GNU/Linux 10(破坏者)
  • 树莓派零
  • 使用本指南在 Raspberry Pi 上进行本地编译
  • 尝试将 Tensorflow lite 作为独立安装
  • Python版本:3.7.3

tensorflow-lite

5
推荐指数
1
解决办法
2万
查看次数