标签: tensorflow-lite

Tensorflow使用python将PB文件转换为TFLITE

我是Tensorflow的新手。训练后我将模型保存为pb文件,我想使用tensorflow mobile,并且使用TFLITE文件很重要。问题是在谷歌搜索转换器后发现的大多数示例都是在终端或cmd上命令的。能否请您分享一个使用python代码转换为tflite文件的示例?

谢谢

python tensorflow tensorflow-lite

4
推荐指数
3
解决办法
1万
查看次数

如何知道Tensorflow Lite模型的输入/输出功能信息?

我是移动开发人员。我想用各种Tensorflow精简版机型(.tflite含)MLKit

但是有一些问题,我不知道如何知道.tflite模型的输入/输出功能信息(这些将是设置参数)。

有什么办法知道吗?

对不起,英语不好,谢谢。


更新(18.06.13。):

我找到了这个网站https://lutzroeder.github.io/Netron/。这可以根据您上载的模型(如.mlmode或类似模型.tflite)可视化图形并查找输入/输出形式。

这是示例屏幕截图! https://lutzroeder.github.io/Netron示例

machine-learning deep-learning tensorflow-lite firebase-mlkit

4
推荐指数
1
解决办法
1983
查看次数

Tensorflow lite 模型给出错误的输出

我正在开发一个带有回归预测的深度学习模型。我创建了一个 tflite 模型,但它的预测与原始模型不同,而且完全错误。这是我的过程:

我用 keras 训练了我的模型

model = Sequential()
model.add(Dense(100, input_dim=x.shape[1], activation='relu')) # Hidden 1
model.add(Dense(50, activation='relu')) # Hidden 2
model.add(Dense(1)) # Output
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(x,y,verbose=0,epochs=500)
Run Code Online (Sandbox Code Playgroud)

并将我的模型保存为 h5 文件

model.save("keras_model.h5")
Run Code Online (Sandbox Code Playgroud)

然后通过 TocoConverter 将 h5 文件转换为 tflile 格式

converter = tf.contrib.lite.TocoConverter.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)

当我用相同的输入测试两个文件时,原始 keras 模型给出了合理的输出,但转换后的模型给出了不合理的输出。

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

# Get input and output tensors.
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

# Test model on random input data.
input_shape = …
Run Code Online (Sandbox Code Playgroud)

keras tensorflow tensorflow-lite toco

4
推荐指数
1
解决办法
3170
查看次数


怎么把.pb转换成TFLite格式?

我下载了在Azure认知服务中训练过的模型的retrained_graph.pbretrained_labels.txt文件。现在,我想使用该模型制作一个Android应用程序,为此,我必须将其转换为TFLite格式。我使用了toco,但出现以下错误:

ValueError: Invalid tensors 'input' were found.
Run Code Online (Sandbox Code Playgroud)

我基本上是在按照本教程操作,在第4步中遇到问题,并直接复制粘贴了终端代码:https : //heartbeat.fritz.ai/neural-networks-on-mobile-devices-with-tensorflow-lite-a-tutorial- 85b41f53230c

tensorflow tensorflow-lite

4
推荐指数
3
解决办法
1万
查看次数

为什么TensorFlow Lite比台式机上的TensorFlow慢?

我目前正在研究单图像超分辨率,并且设法冻结了现有的检查点文件并将其转换为tensorflow lite。但是,当使用.tflite文件执行推理时,对一幅图像进行升采样所花费的时间至少是使用.ckpt文件恢复模型时所花费的时间的四倍。

使用.ckpt文件的推理是使用session.run()完成的,而使用.tflite文件的推理是使用解释器.invoke()完成的。两种操作都是在典型PC上运行的Ubuntu 18 VM上完成的。

我要查找有关此问题的更多信息是top在单独的终端中运行,以查看执行任一操作时的CPU利用率。.ckpt文件的利用率达到270%,而.tflite文件的利用率保持在100%左右。

interpreter.set_tensor(input_details[0]['index'], input_image_reshaped)
interpreter.set_tensor(input_details[1]['index'], input_bicubic_image_reshaped)
start = time.time()
interpreter.invoke()
end = time.time()
Run Code Online (Sandbox Code Playgroud)

y = self.sess.run(self.y_, feed_dict={self.x: image.reshape(1, image.shape[0], image.shape[1], ch), self.x2: bicubic_image.reshape(1, self.scale * image.shape[0], self.scale * image.shape[1], ch), self.dropout: 1.0, self.is_training: 0})
Run Code Online (Sandbox Code Playgroud)

一种假设是没有为多线程配置tensorflow lite,另一种认为是针对ARM处理器(而不是我的计算机所运行的Intel处理器)优化了tensorflow lite,因此速度较慢。但是,我不能肯定地说,也不知道如何找到问题的根源-希望外面有人对此有更多的了解?

tensorflow tensorflow-lite

4
推荐指数
1
解决办法
1006
查看次数

如何在 AWS Lambda 上使用 Tensorflow Lite

我正在尝试在 AWS Lambda 上托管一个我编译为 .tflite 的小模型。使用 tensorflow 网站上提供的 python 3.6 或 python 3.7 tflite 轮文件,我压缩了我的包/代码,然后上传到 S3 并链接到 lambda 并留出空间。但是,当我测试我的函数时,它在尝试加载 tflite 时崩溃了。最初,它无法加载共享对象文件。这是错误

[ERROR] Runtime.ImportModuleError: Unable to import module 'lambda_predict': No module named '_interpreter_wrapper')
Run Code Online (Sandbox Code Playgroud)

我找到了这个共享对象文件并将其移动到本地目录中,然后又出现了另一个错误

Unable to import module 'lambda_predict': /lib64/libm.so.6: version `GLIBC_2.27' not found (required by /var/task/_interpreter_wrapper.so)
Run Code Online (Sandbox Code Playgroud)

我的基本系统是 Ubuntu (Bionic Beaver) 这两个错误都来自导入 tflite

python amazon-web-services aws-lambda tensorflow tensorflow-lite

4
推荐指数
2
解决办法
1971
查看次数

Tensorflow js VS Tensorflow Lite

一个相当开放的问题。

如果我想在浏览器上部署机器学习(对象检测)模型,可能是在 webapp 上开始(在手机上查看),那么当前的区别是什么。

据我所知,tensorflowjs 和 tensorflowlite 都兼容这种部署。(我听说 tensorflowlite 更胜一筹,但是很想知道优缺点(如果有的话))

它们之间的主要区别是什么?tensorflowjs 也是一个不错的选择吗?

tensorflow tensorflow-lite tensorflow.js

4
推荐指数
1
解决办法
1828
查看次数

TFLite 的硬刷

我有一个用 Tensorflow.Keras 编写的自定义神经网络,并应用 hard-swish 函数作为激活(如 MobileNetV3 论文中使用的那样):

$$h-swish = x \cdot \frac{ReLU6(x+3)}{6}$$)。

执行:

def swish(x):
    return x * tf.nn.relu6(x+3) / 6
Run Code Online (Sandbox Code Playgroud)

我正在运行量化感知训练并在最后编写一个 protobuf 文件。然后,我使用此代码转换为 tflite(并最终将其部署在 EdgeTPU 上):

tflite_convert --output_file test.tflite --graph_def_file=test.pb --inference_type=QUANTIZED_UINT8 --input_arrays=input_1 --output_arrays=conv2d_3/Sigmoid --mean_values=0 --std_dev_values=255 --default_ranges_min=0 --default_ranges_max=6
Run Code Online (Sandbox Code Playgroud)

这非常有效,当我不除以 6 时,但是,除以 6 时出现此错误:

Unimplemented: this graph contains an operator of type Div for which the quantized form is not yet implemented.
Run Code Online (Sandbox Code Playgroud)

我使用 TF 1.14 进行训练,昨晚使用 TF 1.15 构建以转换为 TFLITE;我正在努力让 TF 2.x 解决一些奇怪的 HDF5 不兼容问题,但如果有人知道如何规避这个问题,那就太好了……谢谢!

python tensorflow tensorflow-lite tpu

4
推荐指数
1
解决办法
1714
查看次数

Android - TFLite OD - 无法从具有 4320000 字节的 Java 缓冲区复制到具有 307200 字节的 TensorFlowLite 张量 (normalized_input_image_tensor)

我正在尝试运行我自己的自定义模型来进行对象检测。我从 Google Cloud - Vision ( https://console.cloud.google.com/vision/ )创建了我的数据集(我对图像进行了装箱和标记),它看起来像这样:

在此处输入图片说明

训练模型后,我从这里下载了 TFLite 文件(labelmap.txt、model.tflite 和一个 json 文件):

在此处输入图片说明

然后,我将它们添加到 Android 对象检测示例 ( https://github.com/tensorflow/examples/tree/master/lite/examples/object_detection/android )。

在此处输入图片说明

但是当我运行该项目时,它崩溃了:

2020-07-12 18:03:05.160 14845-14883/? E/AndroidRuntime: FATAL EXCEPTION: inference
    Process: org.tensorflow.lite.examples.detection, PID: 14845
    java.lang.IllegalArgumentException: Cannot copy to a TensorFlowLite tensor (normalized_input_image_tensor) with 307200 bytes from a Java Buffer with 4320000 bytes.
        at org.tensorflow.lite.Tensor.throwIfSrcShapeIsIncompatible(Tensor.java:423)
        at org.tensorflow.lite.Tensor.setTo(Tensor.java:189)
        at org.tensorflow.lite.NativeInterpreterWrapper.run(NativeInterpreterWrapper.java:154)
        at org.tensorflow.lite.Interpreter.runForMultipleInputsOutputs(Interpreter.java:343)
        at org.tensorflow.lite.examples.detection.tflite.TFLiteObjectDetectionAPIModel.recognizeImage(TFLiteObjectDetectionAPIModel.java:197)
        at org.tensorflow.lite.examples.detection.DetectorActivity$2.run(DetectorActivity.java:182)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.os.HandlerThread.run(HandlerThread.java:67)
Run Code Online (Sandbox Code Playgroud)

我尝试将参数TF_OD_API_IS_QUANTIZED更改为 false 并将labelOffset 更改为 0,并且我还将这一行从 TFLiteObjectDetectionAPIModel.java …

android google-cloud-platform tensorflow tensorflow-lite

4
推荐指数
1
解决办法
3303
查看次数