树莓派4B 镜像烧录和模型部署 64位


前言

最近做项目需要将训练好的图像分类模型部署到树莓派上,将步骤记录下来,让需要的人参考

一、我的前期准备

1.使用百度开源框架训练的模型,差不多每句代码都有注释
2.树莓派4B/8G(供电为5v 3A 这个很重要)
键盘 鼠标 显示屏
3.镜像文件(这个镜像里面没有vnc需要自己安装)提取码:g4rz
4.烧入镜像文件
如何没有显示屏键盘和鼠标可以在网上找没有显示屏的配置方法,使用自己的笔记本去控制树莓派,这个我没有试,就不在这里献丑了

二、配置步骤

1.下载文件并编译文件

sudo apt-get update
sudo apt-get install gcc g++ make wget unzip libopencv-dev pkg-config
wget https://www.cmake.org/files/v3.10/cmake-3.10.3.tar.gz
tar -zxvf cmake-3.10.3.tar.gz
cd cmake-3.10.3
./configure
make
sudo make install
pip3 install numpy==1.13.3 pillow==8.1.0
apt install python3-dev
apt install python3-matplotlib
apt install python3-opencv
git clone https://github.com/hang245141253/lemon.git
cd ./lemon/wheels
pip3 install paddlelite-2.8rc0-cp37-cp37m-linux_aarch64.whl
cd ..
cd ./lemon/code

如果没有pip3请先安装pip3
这个github上的项目是柠檬分类的需要自己在修改一些东西,下面会讲

c++运行代码:

sh cmake.sh
sh run.sh

python运行代码:(二选其一)

python3 lemon.py

2.部署

将用paddle生成的modle.nb文件替换掉 /lemon/code/models 中的modle.nb
在这个过程中你可能会发现用户权限不够,无法删除和修改
在命令窗口使用sudo chmod -R 777 工作目录(你希望获得root权限的文件夹或者文件)就可以删除和修改了
最后需要修改 lemon/code中的lemon.py文件中的内容就可以在命令窗口运行并成功预测了。
lemon.py具体如下
修改前:

import os
import cv2
import numpy as np
from PIL import Image
from paddlelite.lite import *


def preprocess(img):
    '''
    预测图片预处理
    '''
    #resize
    img = img.resize((224, 224), Image.BILINEAR) #Image.BILINEAR双线性插值
    img = np.array(img).astype('float32')
    
    # HWC to CHW 
    img = img.transpose((2, 0, 1))
    
    #Normalize
    img = img / 255         #像素值归一化
    mean = [0.31169346, 0.25506335, 0.12432463]   
    std = [0.34042713, 0.29819837, 0.1375536]
    img[0] = (img[0] - mean[0]) / std[0]
    img[1] = (img[1] - mean[1]) / std[1]
    img[2] = (img[2] - mean[2]) / std[2]
    
    return img

def run(image, predictor):
    '''
    执行预测
    '''
    image_data = np.array(preprocess(image)).flatten().tolist()

    input_tensor = predictor.get_input(0)
    input_tensor.resize([1, 3, 224, 224])
    input_tensor.set_float_data(image_data)

    predictor.run()

    output_tensor = predictor.get_output(0)
    
    lab = np.argmax(output_tensor.numpy())  #argmax():返回最大数的索引
    print("result: {}".format(label_list[lab]))


if __name__ == '__main__':
    
    config = MobileConfig()
    config.set_model_from_file('./models/model.nb')
    predictor = create_paddle_predictor(config)
    label_list = ['0:優良', '1:良', '2:加工品', '3:規格外']
    
    cap=cv2.VideoCapture(-1)
    cap.set(3,224)
    cap.set(4,224)
    if cap.isOpened() != True:
        print("Error: Please check the camera")
        exit(-1)
    while(1):
        _ , Vshow = cap.read()
        img = Image.fromarray(cv2.cvtColor(Vshow, cv2.COLOR_BGRA2RGB))#PIL图像和cv2图像转化
        run(img, predictor)
        
        cv2.imshow('Capture', Vshow)
        if cv2.waitKey(1)==ord('q'):
            print('完成')
            break

    cap.release()
    cv2.destroyAllWindows()

修改后:

import os
import cv2
import numpy as np
from PIL import Image
from paddlelite.lite import *


def preprocess(img):
    '''
    预测图片预处理
    '''
    #resize
    img = img.resize((224, 224), Image.BILINEAR) #Image.BILINEAR双线性插值
    img = np.array(img).astype('float32')
    
    # HWC to CHW 
    img = img.transpose((2, 0, 1))
    
    #Normalize
    img = img / 255         #像素值归一化
    mean=[0.17572595, 0.24292262, 0.22183217]
    std= [0.16002724, 0.16570544, 0.1406275]
    img[0] = (img[0] - mean[0]) / std[0]
    img[1] = (img[1] - mean[1]) / std[1]
    img[2] = (img[2] - mean[2]) / std[2]
    
    return img

def run(image, predictor):
    '''
    执行预测
    '''
    image_data = np.array(preprocess(image)).flatten().tolist()

    input_tensor = predictor.get_input(0)
    input_tensor.resize([1, 3, 224, 224])
    input_tensor.set_float_data(image_data)

    predictor.run()

    output_tensor = predictor.get_output(0)
    
    lab = np.argmax(output_tensor.numpy())  #argmax():返回最大数的索引
    print("result: {}".format(label_list[lab]))


if __name__ == '__main__':
    
    config = MobileConfig()
    config.set_model_from_file('./models/model.nb')
    predictor = create_paddle_predictor(config)
    label_list = ["0:harm","1:Recyclable","2:other","3:Kitchen","4:nothing"]
    
    cap=cv2.VideoCapture(-1)
    cap.set(3,224)
    cap.set(4,224)
    if cap.isOpened() != True:
        print("Error: Please check the camera")
        exit(-1)
    while(1):
        _ , Vshow = cap.read()
        img = Image.fromarray(cv2.cvtColor(Vshow, cv2.COLOR_BGRA2RGB))#PIL图像和cv2图像转化
        run(img, predictor)
        
        cv2.imshow('Capture', Vshow)
        if cv2.waitKey(1)==ord('q'):
            print('完成')
            break

    cap.release()
    cv2.destroyAllWindows()

总结

如果有什么问题,欢迎批评指正

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值