TensorFlow Lite runtime在树莓派4B上的使用

距离上次在树莓派4B上整东西已经有一段时间了,这边在上面再实验下TensorFlow Lite的配置,具体可参考官网

Python 快速入门  |  TensorFlow Litehttps://tensorflow.google.cn/lite/guide/python?hl=zh-cn 这里博主只想看下前面博客中获得的TensorFlow Lite模型在树莓派上的结果,所以暂时只安装解释器,如下命令安装下TensorFlow Lite

pip3 install https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp37-cp37m-linux_armv7l.whl

 使用FileZilla客户端方便上传文件到树莓派

 树莓派上的python IDE采用的pycharm社区版

代码如下:(代码中所用到的imagenet_classes.txt在博客中也用到了,可从此下载)

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Load the TFLite model in TFLite Interpreter

import tflite_runtime.interpreter as tflite
import cv2
import numpy as np
import time

def get_img_np_nchw(filename):
    image = cv2.imread(filename)
    image_cv1 = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    image_cv1 = cv2.resize(image_cv1, (256, 341))
    image_cv = image_cv1[58:282,16:240]
    print(image_cv.shape)

    miu = np.array([0.485, 0.456, 0.406])
    std = np.array([0.229, 0.224, 0.225])
    img_np = np.array(image_cv, dtype=np.float32) / 255.
    r = (img_np[:, :, 0] - miu[0]) / std[0]
    g = (img_np[:, :, 1] - miu[1]) / std[1]
    b = (img_np[:, :, 2] - miu[2]) / std[2]
    img_np_t = np.array([r, g, b])
    img_n = img_np_t.transpose(2, 0, 1)  # 2轴变为了第一个维度
    print(img_np.shape)

    img_np_nchw = np.expand_dims(img_np, axis=0)
    return img_np_nchw

interpreter = tflite.Interpreter('model.tflite')
interpreter.allocate_tensors()

img = get_img_np_nchw('2008_002682.jpg')

print(img.shape)

input  = interpreter.get_input_details()[0]
output = interpreter.get_output_details()[0]

interpreter.set_tensor(input['index'], img)

t_model = time.perf_counter()
interpreter.invoke()
print(f'do inference cost:{time.perf_counter() - t_model:.8f}s')

output = interpreter.get_tensor(output['index'])
print(output.shape)

with open('imagenet_classes.txt') as f:
    classes = [line.strip() for line in f.readlines()]


top_k=5
arr = np.array([1, 3, 2, 4, 5, 7, 0])
top_k_idx=arr.argsort()[::-1][0:top_k]
print(top_k_idx)


top_k_idx=output[0].argsort()[::-1][0:top_k]
print(top_k_idx)

prediction = [[classes[idx], output[0][idx]] for idx in top_k_idx]
print(prediction)

执行结果如下:

/home/pi/python3.7/bin/python3.7 /home/pi/test_TFLite/test.py
(224, 224, 3)
(224, 224, 3)
(1, 224, 224, 3)
do inference cost:2.01668834s
(1, 1000)
[5 4 3 1 2]
[111 499 683 418 813]
[['nematode', 0.13178234], ['cleaver', 0.04592353], ['oboe', 0.038898617], ['ballpoint', 0.027091475], ['spatula', 0.026938131]]
nematode
cleaver

Process finished with exit code 0

可以看到不需要装tensorflow的整个库,即可以进行推断,由于这边图像预处理方式和博客不同,所以得到的结果也不一致,但可以看到这边的ct需要2s多了,远远大于PC上的时间。

博主这里将前面博客中预处理后的图片数据保存了下来,并上传到树莓派上

img = image.load_img('2008_002682.jpg', target_size=(224, 224))
img = image.img_to_array(img)
img = preprocess_input(img)
img = np.expand_dims(img, axis=0)
print(img.shape)
np.save('array',img)

然后重新对这个加载后的图片数据跑了下预测,上面代码修改如下一处即可

#img = get_img_np_nchw('2008_002682.jpg')
img = np.load('array.npy')

执行结果如下:

 分类结果和博客中一致,但速度需要1.8s,后面会尝试结合Coral USB来加速下,看下时间能到多少

https://github.com/tensorflow/examples/tree/master/lite/examples/image_classification/raspberry_pi

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

竹叶青lvye

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

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

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

打赏作者

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

抵扣说明:

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

余额充值