使用Tensorflow Lite对模型进行加速

1、什么是Tensorflow Lite?
TensorFlow Lite 是一组工具,可帮助开发者在移动设备、嵌入式设备和 IoT 设备上运行 TensorFlow 模型。它支持设备端机器学习推断,延迟较低,并且二进制文件很小。
2、Tensorflow Lite的开发流程
Tensorflow Lite包含两个组件,分别是:
Tensorflow Lite转换器
转换器的目的是将Tensorflow模型转换成可供Tensorflow Lite解释器可用的模型格式,并可引入优化以减小二进制文件的大小和提高性能。
Tensorflow Lite解释器
它可在手机、嵌入式 Linux 设备和微控制器等很多不同类型的硬件上运行经过专门优化的模型。
使用 TensorFlow Lite 的工作流包括如下步骤:
2.1 选择模型
2.2 转换模型
使用Tensorflow Lite转换器将模型转换微Tensorflow Lite的格式。
2.3 部署模型
使用Tensorflow Lite解释器在设备端运行模型
2.4 优化模型
使用模型优化工具包缩减模型大小并提高效率,同时最大限度降低对准确率的影响。

3、代码示例讲解
3.1选择模型
本例子用tensorflow自带的mobilenet网络模型结构。

import tensorflow as tf
from tensorflow.keras.applications.mobilenet_v2 import MobileNetV2

model = MobileNetV2(weights='imagenet')
model_save_path = 'model//tf_keras_mobilenet'
model.save(filepath=model_save_path)
print(model.summary())

2、转换模型
使用Tensorflow Lite转换器将模型转换成Tensorflow Lite格式的模型

import tensorflow as tf

# save tf_lite
# Converting a tf.Keras model to a TensorFlow Lite model.
model = 'model//tf_keras_mobilenet'
converter = tf.lite.TFLiteConverter.from_saved_model(model)

# # float16
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.target_spec.supported_types = [tf.compat.v1.lite.constants.FLOAT16]
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS, tf.lite.OpsSet.SELECT_TF_OPS]
converter.allow_custom_ops = True

tflite_model = converter.convert()
open('model//tf_keras_mobilenet//mobilenet_model.tflite', 'wb').write(tflite_model)

3、部署模型
使用Tensorflow Lite解释器来推理模型

import tensorflow as tf
import numpy as np
import cv2
import time

tf_lite_path = 'model//tf_keras_mobilenet//mobilenet_model.tflite'

interpreter = tf.lite.Interpreter(model_path=tf_lite_path)
interpreter.allocate_tensors()

input_details = interpreter.get_input_details()
print('input_details:',input_details)
output_details = interpreter.get_output_details()
print('output_details:',output_details)

start = time.time()
img = cv2.imread('girl.jpg')
img = img.astype(np.float32)

print(img.dtype)
img = cv2.resize(img,dsize=(224,224))
input_data = tf.expand_dims(img,axis=0)
print('input_data.shape:',input_data.shape)

interpreter.set_tensor(input_details[0]['index'], input_data)
interpreter.invoke()
output_data = [interpreter.get_tensor(output_details[i]['index']) for i in range(len(output_details))]
print('time:',time.time()-start)  # float16 time: 0.12467169761657715
print(output_data)

4、优化模型还没吃透,再研究研究

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
XNNPACK是Facebook开发的一个高效的神经网络计算库,可以加速许多神经网络模型的推理。同时,TensorFlow Lite是一个轻量级的机器学习库,可以在移动设备等资源有限的环境中运行。为了更好地利用硬件资源,我们可以使用XNNPACK来加速TensorFlow Lite模型的推理。以下是使用XNNPACK委托运行TensorFlow Lite模型的步骤: 1. 导入相关库 ```python import tensorflow as tf from tensorflow.lite.python.interpreter import Interpreter from tensorflow.lite.python.interpreter import load_delegate ``` 2. 加载模型 ```python interpreter = Interpreter(model_path="your_tflite_model.tflite") interpreter.allocate_tensors() ``` 3. 加载XNNPACK委托 ```python delegate = load_delegate('libnpx.so') interpreter = Interpreter(model_path="your_tflite_model.tflite", experimental_delegates=[delegate]) interpreter.allocate_tensors() ``` 4. 运行模型 ```python input_details = interpreter.get_input_details() output_details = interpreter.get_output_details() # 假设输入是一个形状为(1, 224, 224, 3)的图像 input_data = np.random.rand(1, 224, 224, 3).astype(np.float32) interpreter.set_tensor(input_details[0]['index'], input_data) interpreter.invoke() output_data = interpreter.get_tensor(output_details[0]['index']) ``` 这样,我们就可以使用XNNPACK委托运行TensorFlow Lite模型了。使用XNNPACK可以显著提高模型推理的速度,并且在移动设备等资源有限的环境中特别有用。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值