当前位置:首页 > AI > 正文内容

使用爱快Docker安装Paddle对Paddle生成的模型进行预测

chanra1n2年前 (2022-07-24)AI2999
文头先放上要使用的Python推理脚本。
# -*- coding: UTF-8 -*-

import os
import cv2
import time
import threading
from shutil import copyfile
import numpy as np
from scipy.ndimage.interpolation import shift
import paddlex as pdx

#处理中文路径
import importlib,sys
importlib.reload(sys)

TEMP_SEC_CALC=0
TEMP_SEC_CALC_ERROR=0
TEMP_SUM=0
TEMP_SUM_DONE=0

model = pdx.load_model('./inference_model') #Paddle加载模型

def time_convert(seconds):
    seconds = seconds % (24 * 3600)
    hour = seconds // 3600
    seconds %= 3600
    minutes = seconds // 60
    seconds %= 60
    return "%02d:%02d:%02d" % (hour, minutes, seconds)

def paddle_predict_calc():
    global TEMP_SEC_CALC
    global TEMP_SEC_CALC_ERROR
    global TEMP_SUM
    global TEMP_SUM_DONE
    while TEMP_SUM!=TEMP_SUM_DONE:
        if TEMP_SEC_CALC:
            print('FPS:'+str(TEMP_SEC_CALC)+' Remain:'+time_convert((TEMP_SUM-TEMP_SUM_DONE)/TEMP_SEC_CALC) + ' ERROR:'+str(TEMP_SEC_CALC_ERROR))
        TEMP_SEC_CALC = 0
        time.sleep(1)

def paddle_predict(source_path,result_path,threshold_value):
    #对源路径的图片进行推理
    #对来自参数一目录下的图片进行分类,按分类名保存在参数二的目录下,当预测的准确度大于参数三时,保存该图片到参数二对应目录下
    global TEMP_SEC_CALC_ERROR
    global TEMP_SUM_DONE
    global TEMP_SEC_CALC
    isExists=os.path.exists(result_path)#判断目标文件夹是否存在
    if not isExists:
        os.makedirs(result_path)
    for dirpath, dirnames, filenames in os.walk(source_path):
        for filename in filenames:
            try:
                im = cv2.imdecode(np.fromfile(os.path.join(dirpath, filename),dtype=np.uint8),-1)
                im = im.astype('float32')
                result = model.predict(im)
                #print(os.path.join(dirpath, filename))
                isExists=os.path.exists(result_path+'/'+result[0]['category'])#判断分类文件夹是否存在
                if not isExists:
                    os.makedirs(result_path+'/'+result[0]['category'])
                if(result[0]['score']>threshold_value):
                    copyfile(os.path.join(dirpath, filename), result_path+'/'+result[0]['category']+'/'+filename)
                    #os.remove(os.path.join(dirpath, filename))  
            except:
                #print('ERROR:'+os.path.join(dirpath, filename))
                TEMP_SEC_CALC_ERROR = TEMP_SEC_CALC_ERROR + 1
            TEMP_SUM_DONE = TEMP_SUM_DONE + 1
            TEMP_SEC_CALC = TEMP_SEC_CALC + 1

def predict(source_path,result_path,threshold_value):
    #对来自参数一目录下的图片进行分类,按分类名保存在参数二的目录下,当预测的准确度大于参数三时,保存该图片到参数二对应目录下
    global TEMP_SUM
    for dirpath, dirnames, filenames in os.walk(source_path):
        for filename in filenames:
            TEMP_SUM = TEMP_SUM + 1
    print('总共发现了:'+str(TEMP_SUM)+'个文件!')
    time.sleep(2)

    main_func = threading.Thread(target=paddle_predict, args=(source_path,result_path,threshold_value))
    calc_func = threading.Thread(target=paddle_predict_calc)
    main_func.start()
    calc_func.start()

predict("imgs","output",0.9)

首先打开爱快的Docker页码,pull下来paddle的镜像image.png

然后,在容器列表新建容器,我分配了40G内存给paddle容器,你可以根据自己的需求去改

image.png

image.png

注意,环境变量处记得添加变量。值为你要设置的密码。

image.png

然后访问http://docker容器ip/hub/login,用户名是paddle,密码是你刚刚设置的。

此时,直接运行是会报错的,因为我们没有安装paddle等依赖。

pip install opencv-contrib-python scipy -i https://mirror.baidu.com/pypi/simple
apt-get update && apt install libgl1-mesa-glx libglib2.0-dev g++ -y
pip install paddlex filelock -i https://mirror.baidu.com/pypi/simple

然后新建python3项目,然后使用文头的代码即可,别忘了复制模型到你的目录下,然后更改代码对应部分!

运行效果:

image.png

ps:配合docker的浏览器进行图片采集,然后进行分类,挺好用的,分配内存尽量大一点,图片去重的时候用得到,我一般分配个30-200G内存就够用了,因为再大也用不到,浏览器会在满之前崩溃。

所以最大分配个200G内存就够了,硬盘我分配了个30T,采集个一个星期没问题。

image.png

不得不说爱快这个真的挺好用

扫描二维码推送至手机访问。

版权声明:本文由我的FPGA发布,如需转载请注明出处。

本文链接:https://myfpga.cn/index.php/post/251.html

分享给朋友:

“使用爱快Docker安装Paddle对Paddle生成的模型进行预测” 的相关文章

python基础三层深度学习网络

python基础三层深度学习网络

#coding:utf-8 #neural network class definition import numpy import scipy.spatial class neuralNetwork:   &...

简单OpenCV人脸识别

简单OpenCV人脸识别

# -*- coding: utf-8 -*- """ Created on Sat Dec  5 22:39:13 2020 @author:&nb...

玩客云安装Armbian基于PaddleLite实现图片分类和目标检查 Cortex-A5等其他可参考

玩客云安装Armbian基于PaddleLite实现图片分类和目标检查 Cortex-A5等其他可参考

本教程为本站原创,转载请注明本网站链接,否则视为侵权!如果朋友还没有安装Armbian,或者怎么折腾也折腾不好,请直接翻到文章最后下载img文件!教程中碰到出错的地方,可以重复运行代码尝试!其他的ArmV7 32位的也可以参考本文,图片分类速度:1.1帧/秒,每张图片耗时约900ms,生产用途应该是...

基于CycloneV使用Paddle Lite,并分别使用单独HPS和FPGA加速对比效果。

基于CycloneV使用Paddle Lite,并分别使用单独HPS和FPGA加速对比效果。

第一部分、仅使用HPS进行计算第一步、通过ssh链接至开发板第二步、解决apt-get存在的问题chmod 644 /usr/lib/sudo/sudoers.so && chown -R root /usr/li...

使用ZYNQ7010安装PYNQ,基于PaddleLite实现目标检测+图片分类

使用ZYNQ7010安装PYNQ,基于PaddleLite实现目标检测+图片分类

目前只使用HPS实现了目标检测和图片分类,现在正在尝试使用HS端加速卷积,,,步骤一、烧录PYNQ镜像到TF卡    略步骤二、ssh链接至开发板,使用apt-get安装依赖sudo apt-get update &&am...

ZYNQ7010在PYNQ环境下使用NPU加速神经网络推理

ZYNQ7010在PYNQ环境下使用NPU加速神经网络推理

步骤一、链接ssh并上传NPU SDK 步骤二、插入NPU,并给NPU赋予权限sudo chmod 666 /dev/sg*步骤三、修改最大字节数find /sys/devices/ -name max_sectors -exec sh -c 'ech...