【发布时间】:2019-05-20 17:00:24
【问题描述】:
嗨,有没有可能在 linux 平台上运行 tensorflow lite?如果是,那么我们如何在 java/C++/python 中编写代码以在 linux 平台上加载和运行模型?熟悉bazel,使用tensorflow lite成功制作了Android和ios应用。
【问题讨论】:
标签: linux tensorflow-lite
嗨,有没有可能在 linux 平台上运行 tensorflow lite?如果是,那么我们如何在 java/C++/python 中编写代码以在 linux 平台上加载和运行模型?熟悉bazel,使用tensorflow lite成功制作了Android和ios应用。
【问题讨论】:
标签: linux tensorflow-lite
TensorFlow Lite 是 TensorFlow 面向移动和嵌入式设备的轻量级解决方案。
Tensorflow lite 是用于嵌入式设备的 tensorflow 的一个分支。对于 PC,只需使用原始的 tensorflow。
TensorFlow 是一个开源软件库
TensorFlow 提供稳定的 Python API 和 C API,但没有 C++、Go、Java、JavaScript 和 Swift 等 API 向后兼容性保证。
我们支持 Linux、Mac 和 Windows 上的 CPU 和 GPU 包。
>>> import tensorflow as tf >>> tf.enable_eager_execution() >>> tf.add(1, 2) 3 >>> hello = tf.constant('Hello, TensorFlow!') >>> hello.numpy() 'Hello, TensorFlow!'
【讨论】:
可以运行(但运行速度会比原来的 tf 慢)
例子
# Load TFLite model and allocate tensors.
interpreter = tf.lite.Interpreter(model_path=graph_file)
interpreter.allocate_tensors()
# Get input and output tensors.
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
# Get quantization info to know input type
quantization = None
using_type = input_details[0]['dtype']
if dtype is np.uint8:
quantization = input_details[0]['quantization']
# Get input shape
input_shape = input_details[0]['shape']
# Input tensor
input_data = np.zeros(dtype=using_type, shape=input_shape)
# Set input tensor, run and get output tensor
interpreter.set_tensor(input_details[0]['index'], input_data)
interpreter.invoke()
output_data = interpreter.get_tensor(output_details[0]['index'])
【讨论】:
是的,即使使用 Docker 容器,您也可以编译 Tensorflow Lite 以在 Linux 平台上运行。查看演示:https://sconedocs.github.io/tensorflowlite/
【讨论】:
我觉得其他的答案都大错特错。
看,我告诉你我的经验...我已经使用 Django 多年,并且一直在使用普通的 tensorflow,但是在 4 或 5 个或更多模型中存在问题同一个项目。 我不知道你是否知道 Gunicorn + Nginx。这会生成工人,所以如果你有 4 个机器学习模型,对于每个工人它乘以,如果你有 3 个工人,你将在 RAM 中预加载 12 个模型。这根本没有效率,因为如果 RAM 溢出,您的项目将会失败,或者实际上服务响应会变慢。
这就是 Tensorflow lite 的用武之地。从 tensorflow 模型切换到 tensorflow lite 可以改进并让事情变得更加高效。时间被荒谬地缩短了。 此外,可以配置 Django 和 Gunicorn,以便同时预加载和编译模型。因此,每次 API 用完时,它只会生成预测,这有助于您将每个 API 调用缩短到几分之一秒。 目前我有一个正在生产的项目,有 14 个模型和 9 个工人,你可以理解它在 RAM 方面的大小。 除了进行数千次额外计算之外,在机器学习之外,API 调用所用时间不超过 2 秒。 现在,如果我使用普通的 tensorflow,至少需要 4 或 5 秒。
综上所述,如果可以使用 tensorflow lite,我在 Windows、MacOS 和 Linux 中每天都在使用,完全没有必要使用 Docker。只是一个python文件,就是这样。如果您有任何疑问,可以毫无问题地问我。
这里是一个示例项目 Django + Tensorflow Lite
【讨论】:
我同意 Nouvellie 的观点。这是可能的并且值得花时间实施。我在我的 Ubuntu 18.04 32 处理器服务器上开发了一个模型并将模型导出到 tflite。该模型在我的 ubuntu 服务器上运行了 178 秒。在我的 4GB 内存的树莓派 pi4 上,tflite 实现在 85 秒内运行,不到我服务器时间的一半。当我在我的服务器上安装 tflite 时,运行时间下降到 22 秒,性能提高了 8 倍,现在比 rpi4 快了近 4 倍。
要为 python 安装,我不必构建包,但可以在这里使用预构建的解释器之一:
https://www.tensorflow.org/lite/guide/python
我有 Ubuntu 18.04 和 python 3.7.7。所以我用 Linux python 3.7 包运行 pip install:
pip3 安装 https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp37-cp37m-linux_x86_64.whl
然后导入包:
从 tflite_runtime.interpreter 导入解释器
以前的帖子展示了如何使用 tflite。
【讨论】: