【问题标题】:Tensorflow Lite quantization on Raspberry Pi 0 in image classification problem图像分类问题中 Raspberry Pi 0 上的 Tensorflow Lite 量化
【发布时间】:2020-07-02 09:35:17
【问题描述】:

我正在为 Raspberry Pi 0 W 开发图像分类模型/程序。我想知道是否可以进行代码升级以加速图像处理。

一般信息:

  • 主要模型是在 EfficientNetB5 上训练的
  • 图像尺寸为 240x320 灰度
  • 在 Raspberry 上,应该是图像分类,没有“直播”和物体检测的可能性
  • 我承认 Raspberry Pi 0 W 不是 TF 的最佳匹配,但无论如何也许有加速的方法
  • 目前在 60 秒内预测一张图像,这太多了

我对此的想法是,也许我应该训练具有较低维度的模型,也许主模型的learning_rate 会影响 rpi 的速度?

下面我附上两个脚本。

Tensorflow save_model 转化为 tf_lite 量化模型

import tensorflow as tf
import tensorflow_hub as hub
from tensorflow.keras.models import load_model

model = load_model('../models/effnet_v22.h5')

TFLITE_QUANT_MODEL = "../tflite_models/effnet_v22_quant.tflite"

run_model = tf.function(lambda x : model(x))

# Save the concrete function.
concrete_func = run_model.get_concrete_function(
    tf.TensorSpec(model.inputs[0].shape, model.inputs[0].dtype)
)

# Convert the model to quantized version with post-training quantization
converter = tf.lite.TFLiteConverter.from_concrete_functions([concrete_func])
converter.optimizations = [tf.lite.Optimize.OPTIMIZE_FOR_SIZE]
tflite_quant_model = converter.convert()
open(TFLITE_QUANT_MODEL, "wb").write(tflite_quant_model)

print("TFLite Quantized Model Is Created")

Raspberry Pi 0 上的一次图像处理

import tensorflow as tf
import numpy as np
import matplotlib.image as img
import cv2

# uploading tflite model
tflite_interpreter =tf.lite.Interpreter(
    model_path='../../tflite_models/effnet_v22_quant.tflite')

# taking pre-trained model parameters 
input_details = tflite_interpreter.get_input_details()
output_details = tflite_interpreter.get_output_details()

img_width = input_details[0]['shape'][2]
img_height = input_details[0]['shape'][1]

# uploading and processing the image to be predicted
testimg=img.imread('../img/c21.jpg')
testimg=cv2.resize(testimg, (img_width,img_height))
testimg=cv2.cvtColor(testimg, cv2.COLOR_BGR2GRAY)
testimg=testimg[np.newaxis, ..., np.newaxis]
testimg=np.array(testimg, dtype=np.float32)

# resizing tflite's tensors
tflite_interpreter.resize_tensor_input(input_details[0]['index'], (1, img_height, img_width, 1))
tflite_interpreter.resize_tensor_input(output_details[0]['index'], (1, 8))
tflite_interpreter.allocate_tensors()

input_details = tflite_interpreter.get_input_details()
output_details = tflite_interpreter.get_output_details()

tflite_interpreter.set_tensor(input_details[0]['index'], testimg)
tflite_interpreter.invoke()
tflite_model_predictions = tflite_interpreter.get_tensor(output_details[0]['index'])

# TFLite prediction results

classes = np.array([101,102,104,105, 107, 110, 113, 115]) # class array creation
mat = np.vstack([classes, tflite_model_predictions]) 
np.set_printoptions(suppress=True, precision = 10) # to get rid of scientific numbers

if np.max(mat[1,:]) > 0.50:
    theclass = int(mat[0, np.argmax(mat[1,:])])
else:
    theclass = "NO_CLASS"

print(mat)

print("The predicted class is", theclass)

【问题讨论】:

    标签: python tensorflow raspberry-pi computer-vision artificial-intelligence


    【解决方案1】:

    您正在使用具有近 30M 参数的 EfficientNet-B5 模型。即使您从 Tensorflow Lite 和量化方法中受益,但假设您使用像 Pixel 4 那样的高性能 CPU,很难获得低于 30ms 的推理延迟。考虑到您使用的是非常有限的嵌入式系统,这是正常的一次推理需要 60 秒。

    有一个解释清楚的网页关于 EfficientNet-lite 模型的延迟。在这里,你可以访问,https://blog.tensorflow.org/2020/03/higher-accuracy-on-vision-models-with-efficientnet-lite.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-13
      • 1970-01-01
      • 2018-01-08
      • 1970-01-01
      • 1970-01-01
      • 2021-04-16
      • 2019-09-04
      相关资源
      最近更新 更多