python中使用tflite推理网络

如果我们得到了tflite文件,如何在python中使用?这里可以在tensorflow库的帮助下或者tflite_runtime库的帮助下使用

1、使用tensorflow库

tensorflow库中有个lite子库,是为tflite而设计的
给出示例代码:

import tensorflow as tf
import cv2
import numpy as np


def preprocess(image):	# 输入图像预处理
    image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    image = cv2.resize(image, (64, 64))
    tensor = np.expand_dims(image, axis=[0, -1])
    tensor = tensor.astype('float32')
    return tensor


# API文档:https://www.tensorflow.org/api_docs/python/tf/lite/Interpreter#args_1

emotion_model_tflite = tf.lite.Interpreter("output.tflite")	# 加载tflite模型

emotion_model_tflite.allocate_tensors()  # 预先计划张量分配以优化推理
tflife_input_details = emotion_model_tflite.get_input_details()  # 获取输入节点的细节
tflife_output_details = emotion_model_tflite.get_output_details()  # 获取输出节点的细节

# 加载并处理成输入张量,和keras推理或者tensorflow推理的输入张量一样
img = cv2.imread("1fae49da5f2472cf260e3d0aa08d7e32.jpeg")
input_tensor = preprocess(img)

# 填入输入tensor
emotion_model_tflite.set_tensor(tflife_input_details[0]['index'], input_tensor)
# 运行推理
emotion_model_tflite.invoke()
# 获取推理结果
custom = emotion_model_tflite.get_tensor(tflife_output_details[0]['index'])

print(custom)

2、使用tflite_runtime库

见名知意,tflite_runtime就是tflite的运行环境库。因为tensorflow毕竟太大了,如果我们只是想使用tflite模型推理,那么使用该库是个不错的选择

2.1 安装tflite_runtime库

首先在安装 TensorFlow Lite 解释器根据你的平台和python版本,下载对应的whl文件,然后使用pip安装即可:pip install 下载的whl文件路径
在这里插入图片描述

2.2 推理

先给出代码:

import tflite_runtime.interpreter as tflite	# 改动一
import cv2
import numpy as np


def preprocess(image):
    image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    image = cv2.resize(image, (64, 64))
    tensor = np.expand_dims(image, axis=[0, -1])
    tensor = tensor.astype('float32')
    return tensor




emotion_model_tflite = tflite.Interpreter("output.tflite")	# 改动二

emotion_model_tflite.allocate_tensors() 
tflife_input_details = emotion_model_tflite.get_input_details()
tflife_output_details = emotion_model_tflite.get_output_details()

img = cv2.imread("1fae49da5f2472cf260e3d0aa08d7e32.jpeg")
input_tensor = preprocess(img)


emotion_model_tflite.set_tensor(tflife_input_details[0]['index'], input_tensor)

emotion_model_tflite.invoke()

custom = emotion_model_tflite.get_tensor(tflife_output_details[0]['index'])

print(custom)

  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
TensorFlow Lite(TFLite)是一个开源的轻量级库,用于在移动设备、边缘设备和嵌入式设备上运行机器学习模型。它可以在资源有限的设备上快速、高效地运行模型,同时保持模型的准确性和可靠性。TFLite支持多种硬件和操作系统,包括Android、iOS、Linux和Microcontroller等。 使用TFLite可以让开发者快速将机器学习模型部署到移动设备上,实现本地化计算,避免了在网络传输可能出现的延迟和带宽问题。这对于需要快速响应用户操作、对数据隐私有要求的应用场景非常有用。 接下来,我们将介绍如何使用TFLite来加载、运行和优化机器学习模型。 1. 安装TFLite使用TFLite,我们首先需要安装TFLite库。可以通过以下命令安装: ``` pip install tensorflow==2.5.0 pip install tflite==2.5.0 ``` 注意:如果您使用的是不同版本的TensorFlow,请确保安装与您的TensorFlow版本相对应的TFLite版本。 2. 加载模型 在使用TFLite之前,我们需要先将机器学习模型转换为TFLite格式。可以使用TensorFlow的模型转换工具将已经训练好的模型转换为TFLite格式。转换工具包括命令行工具和Python API,可以根据需要选择使用。 转换完成后,我们可以使用TFLite Python API加载模型: ``` import tensorflow as tf # 加载TFLite模型 interpreter = tf.lite.Interpreter(model_path="model.tflite") interpreter.allocate_tensors() ``` 加载模型后,我们需要为模型设置输入和输出张量。可以使用以下代码获取输入和输出张量的详细信息: ``` # 获取输入和输出张量的详细信息 input_details = interpreter.get_input_details() output_details = interpreter.get_output_details() print("Input details: ", input_details) print("Output details: ", output_details) ``` 3. 运行模型 使用TFLite运行模型非常简单。我们只需要将输入数据传递给模型,并使用`invoke()`方法运行模型: ``` # 准备输入数据 input_data = ... # 将输入数据传递给模型 interpreter.set_tensor(input_details[0]['index'], input_data) # 运行模型 interpreter.invoke() # 获取输出数据 output_data = interpreter.get_tensor(output_details[0]['index']) ``` 4. 优化模型 优化模型可以让我们在资源有限的设备上获得更好的性能。TFLite提供了多种优化技术,包括模型量化、模型裁剪和模型加速等。 - 模型量化 模型量化是一种将浮点模型转换为定点模型的技术。定点模型使用整数表示权重和激活值,可以在硬件上更快地进行计算。TFLite提供了多种量化技术,包括动态范围量化和全局范围量化等。 以下是使用动态范围量化优化模型的示例代码: ``` # 加载TFLite模型 interpreter = tf.lite.Interpreter(model_path="model.tflite") interpreter.allocate_tensors() # 获取输入和输出张量的详细信息 input_details = interpreter.get_input_details() output_details = interpreter.get_output_details() # 设置模型量化参数 interpreter.quantization = tf.lite.OpsSet.SELECT_TF_OPS interpreter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8] # 运行模型 interpreter.set_tensor(input_details[0]['index'], input_data) interpreter.invoke() output_data = interpreter.get_tensor(output_details[0]['index']) ``` - 模型裁剪 模型裁剪是一种减少模型大小和计算量的技术。它通过删除模型冗余的权重和神经元来实现。TFLite提供了多种裁剪技术,包括权重裁剪和通道裁剪等。 以下是使用权重裁剪优化模型的示例代码: ``` # 加载TFLite模型 interpreter = tf.lite.Interpreter(model_path="model.tflite") interpreter.allocate_tensors() # 获取输入和输出张量的详细信息 input_details = interpreter.get_input_details() output_details = interpreter.get_output_details() # 设置模型裁剪参数 interpreter.allocate_tensors() interpreter.resize_tensor_input(input_details[0]['index'], (1, new_input_shape)) interpreter.resize_tensor_input(output_details[0]['index'], (1, new_output_shape)) interpreter.allocate_tensors() # 运行模型 interpreter.set_tensor(input_details[0]['index'], input_data) interpreter.invoke() output_data = interpreter.get_tensor(output_details[0]['index']) ``` - 模型加速 模型加速是一种通过硬件加速来加快模型推理速度的技术。TFLite提供了多种加速技术,包括GPU加速和NNAPI加速等。 以下是使用NNAPI加速优化模型的示例代码: ``` # 加载TFLite模型 interpreter = tf.lite.Interpreter(model_path="model.tflite") interpreter.allocate_tensors() # 设置模型加速参数 interpreter.set_tensor(interpreter.get_input_details()[0]['index'], input_data) interpreter.invoke() # 获取NNAPI代理 from tflite_runtime.interpreter import load_delegate delegate = load_delegate('libnngpu_delegate.so') # 使用NNAPI加速 interpreter = tf.lite.Interpreter(model_path="model.tflite", experimental_delegates=[delegate]) interpreter.allocate_tensors() # 运行模型 interpreter.set_tensor(interpreter.get_input_details()[0]['index'], input_data) interpreter.invoke() output_data = interpreter.get_tensor(interpreter.get_output_details()[0]['index']) ``` 总结 TFLite是一个非常强大的库,可以让我们在移动设备、边缘设备和嵌入式设备上运行机器学习模型。在使用TFLite时,我们需要先将模型转换为TFLite格式,然后使用TFLite Python API加载模型、设置输入和输出张量,并使用`invoke()`方法运行模型。我们还可以使用TFLite提供的多种优化技术来优化模型,包括模型量化、模型裁剪和模型加速等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我是一个对称矩阵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值