IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Python知识库 -> Jetson Nano——基于python API部署Paddle Infence GPU预测库(2.1.1) -> 正文阅读

[Python知识库]Jetson Nano——基于python API部署Paddle Infence GPU预测库(2.1.1)

系统环境

  • JetPack4.4
    在这里插入图片描述

如果需要此镜像的同学可以在Jetson 下载中心下载即可。

一、安装PaddlePaddle

有两种方式,因为nano官方有已经编译好的python3.6的whl,所以我们直接下载就好,不用编译。

1.直接下载或编译预测库

(1)直接下载官方编译好的Jetson nano预测库

下载地址

下载

选择python3.6版本的下载即可(注意不要下载后三个版本,因为nano的GPU型号与之不匹配,安装会报错。)
在这里插入图片描述

(2)编译官方预测库

在Jetson nano上编译paddlepaddle(带TensorRT)并跑通Paddle-Inference-Demo

2.安装whl

(1)安装依赖项

这里必须安装1.18.3版本的numpy,否则后边会报错。

pip3 install numpy==1.18.3

将下载好的whl文件传送到nano上,然后安装whl:

pip3 install paddlepaddle_gpu-2.1.1-cp36-cp36m-linux_aarch64.whl

(2)测试

打开python3:

import paddle
paddle.fluid.install_check.run_check()

报warning忽略即可,不影响使用。如果遇到报错,请继续往下看。
在这里插入图片描述

(3)报错解决

提取出报错中的关键信息:

弃用警告:imp模块弃用,取而代之的是importlib;有关其他用途,请参阅模块文档
在这里插入图片描述

解决方法
  • 打开/usr/lib/python3/dist-packages/setuptools/depends.py,将其中的imp都修改为importlib;保存时会报权限错误,所以需要给该文件修改权限,我这里直接使用命令:sudo chmod 777 /usr/lib/python3/dist-packages/setuptools/depends.py
  • 再次运行import paddle,会发现还会有类似报错,依然进行如上更换操作。
  • 遇到报错如下:

在这里插入图片描述
重装一下six模块就可以:

pip3 uninstall six
pip3 install six

二、测试Paddle Inference

1.环境准备

拉取Paddle-Inference-Demo:

git clone https://github.com/PaddlePaddle/Paddle-Inference-Demo.git

拉取比较慢的话可以在gitee上建个仓库下载,我建的仓库:https://gitee.com/irvingao/Paddle-Inference-Demo.git

2.测试跑通GPU预测模型

给可执行权限:

cd Paddle-Inference-Demo/python
chmod +x run_demo.sh

需要注意的是,需要将所有子文件夹中的run.sh最后的python修改为python3
在这里插入图片描述
在这里插入图片描述

./run_demo.sh

也可以选择运行单个模型的run.sh如果过程中有报错,请继续往下看。

3.报错解决

(1)运行demo过程中卡住

在这里插入图片描述
解决方法:
扩大运行内存 (亲测:建议至少给8G,反正6G不行)

sudo fallocate -l 8G /var/swapfile8G
sudo chmod 600 /var/swapfile8G
sudo mkswap /var/swapfile8G
sudo swapon /var/swapfile8G
sudo bash -c 'echo "/var/swapfile8G swap swap defaults 0 0" >> /etc/fstab'

扩大虚拟内存后,运行正常:
在这里插入图片描述

(2)core dumped

原因是因为JetPack4.4自带的numpy版本不对,numpy版本应为1.18.3。

在这里插入图片描述
解决方法:

pip3 uninstall numpy
python3 -m pip install numpy==1.18.3 -i https://mirror.baidu.com/pypi/simple

三、部署自己的目标检测 / 图像分类模型

1.图像分类

import cv2
import numpy as np
from paddle.inference import Config
from paddle.inference import create_predictor

# ————————————————图像预处理函数————————————————
def resize_short(img, target_size):
    """ resize_short """
    percent = float(target_size) / min(img.shape[0], img.shape[1])
    resized_width = int(round(img.shape[1] * percent))
    resized_height = int(round(img.shape[0] * percent))
    resized = cv2.resize(img, (resized_width, resized_height))
    return resized

def crop_image(img, target_size, center):
    """ crop_image """
    height, width = img.shape[:2]
    size = target_size
    if center == True:
        w_start = (width - size) / 2
        h_start = (height - size) / 2
    else:
        w_start = np.random.randint(0, width - size + 1)
        h_start = np.random.randint(0, height - size + 1)
    w_end = w_start + size
    h_end = h_start + size
    img = img[int(h_start):int(h_end), int(w_start):int(w_end), :]
    return img

def preprocess(img):
    mean = [0.485, 0.456, 0.406]
    std = [0.229, 0.224, 0.225]
    img = resize_short(img, 224)
    img = crop_image(img, 224, True)
    # bgr-> rgb && hwc->chw
    img = img[:, :, ::-1].astype('float32').transpose((2, 0, 1)) / 255
    img_mean = np.array(mean).reshape((3, 1, 1))
    img_std = np.array(std).reshape((3, 1, 1))
    img -= img_mean
    img /= img_std
    return img[np.newaxis, :]

#——————————————————————模型配置、预测相关函数——————————————————————————
def predict_config(model_file, params_file):
    # 根据预测部署的实际情况,设置Config
    config = Config()
    # 读取模型文件
    config.set_prog_file(model_file)
    config.set_params_file(params_file)
    # Config默认是使用CPU预测,若要使用GPU预测,需要手动开启,设置运行的GPU卡号和分配的初始显存。
    config.enable_use_gpu(500, 0)
    # 可以设置开启IR优化、开启内存优化。
    config.switch_ir_optim()
    config.enable_memory_optim()
    predictor = create_predictor(config)
    return predictor

def predict(image, predictor):
    img = preprocess(image)
    input_names = predictor.get_input_names()
    input_tensor = predictor.get_input_handle(input_names[0])
    input_tensor.reshape(img.shape)
    input_tensor.copy_from_cpu(img.copy())
    # 执行Predictor
    predictor.run()
    # 获取输出
    output_names = predictor.get_output_names()
    output_tensor = predictor.get_output_handle(output_names[0])
    output_data = output_tensor.copy_to_cpu()
    print("output_names", output_names)
    print("output_tensor", output_tensor)
    print("output_data", output_data)
    return output_data

# 展示结果
def post_res(label_dict, res):
    res = res.tolist()
    # print(type(res))
    # print(max(res))
    target_index = res.index(max(res))
    print("结果是:" + "   " + label_dict[target_index])

if __name__ == '__main__':
    label_dict = {0:"metal", 1:"paper", 2:"plastic", 3:"glass"}
    model_file = "PaddleInfence\model\ResNet50_trashClas_x86_model\__model__"
    params_file = "PaddleInfence\model\ResNet50_trashClas_x86_model\__params__"

    image = cv2.imread("PaddleInfence\python_demo\metal1.jpg")
    predictor = predict_config(model_file, params_file)
    res = predict(image, predictor)
    post_res(label_dict, res)

    cv2.imshow("image", image)
    cv2.waitKey()

2.目标检测模型

参考文章:

参考文献:

  Python知识库 最新文章
Python中String模块
【Python】 14-CVS文件操作
python的panda库读写文件
使用Nordic的nrf52840实现蓝牙DFU过程
【Python学习记录】numpy数组用法整理
Python学习笔记
python字符串和列表
python如何从txt文件中解析出有效的数据
Python编程从入门到实践自学/3.1-3.2
python变量
上一篇文章      下一篇文章      查看所有文章
加:2021-07-10 14:30:21  更:2021-07-10 14:30:27 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/3 15:49:10-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码