我们可以在Linux上运行TensorFlow Lite吗?或仅适用于Android和iOS



嗨,有可能在Linux平台上运行TensorFlow Lite吗?如果是,那么我们如何在Java/C /Python中编写代码以在Linux平台上加载和运行模型?我熟悉Bazel,并使用TensorFlow Lite成功地制作了Android和iOS应用程序。

我认为其他答案是完全错误的。

看,我会告诉你我的经验...我已经与Django合作了很多年,而且我一直在使用普通的Tensorflow,但是在该模型中拥有4或5个或更多型号存在问题同一项目。我不知道您是否知道Gunicorn Nginx。这会产生工人,因此,如果您有4种机器学习模型,则每个工人都会成倍增加,如果您有3个工人,您将拥有12个在RAM中预装的模型。这一点根本不是有效的,因为如果RAM溢出您的项目,您的项目将下降或服务响应较慢。

因此,这就是Tensorflow Lite的来源。从张量流模型切换到Tensorflow Lite可以改善并使事情更有效。荒谬的时间减少。此外,可以配置Django和Gunicorn,以便同时预载并编译模型。因此,每次API用用完时,它只会生成预测,这可以帮助您使每个API拨打一秒钟的时间。目前,我有一个由14个型号和9名工人进行的生产项目,您可以从RAM方面理解它的规模。除了进行数千次额外的计算外,在机器学习之外,API调用也不超过2秒。现在,如果我使用了普通的张量流,则至少需要4或5秒。

总而言之,如果您可以使用Tensorflow Lite,我每天在Windows,MacOS和Linux中使用它,则根本不需要使用Docker。只是一个python文件,仅此而已。如果您有疑问,您可以问我没有任何问题。

这里一个示例项目Django Tensorflow Lite

可以运行(但比原始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'])

我同意nouvellie。这是可能的,值得实施时间。我在Ubuntu 18.04 32处理器服务器上开发了一个模型,并将该模型导出到Tflite。该模型在我的Ubuntu服务器上以178秒运行。在带有4GB内存的Raspberry 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安装:

pip3安装https://dl.google.com/coral/coral/python/tflite_runtime-2.1.0.post1-cp37-cp37m-linux_x86_64.whl

然后以:

导入软件包

来自tflite_runtime.interpreter导入解释器

以前的帖子显示了如何使用tflite。

来自Tensorflow Lite

TensorFlow Lite是TensorFlow的移动设备和嵌入式设备的轻量级解决方案。

Tensorflow Lite是嵌入式设备的张量叉。对于PC,只需使用原始张量。

来自github tensorflow:

TensorFlow是一个开源软件库

TensorFlow提供稳定的Python API和C API,以及没有API向后兼容保证,例如C ,GO,Java,Java,JavaScript和Swift。

我们支持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!'

是的,即使使用Docker容器,您也可以在Linux平台上编译Tensorflow Lite。请参阅演示:https://sconedocs.github.io/tensorflowlite/

最新更新