时间:2024-08-02 来源:网络搜集 关于我们 0
编辑丨阿chai带你学AI
我是来自山区、朴实、不偷电瓶的AI算法工程师阿chai,给大家分享人工智能、自动驾驶、机器人、3D感知相关的知识
。
bnn
hw_classifier = bnn.CnvClassifier(bnn.NETWORK_CNVW1A1,cifar10,bnn.RUNTIME_HW)
sw_classifier = bnn.CnvClassifier(bnn.NETWORK_CNVW1A1,cifar10,bnn.RUNTIME_SW)
进行测试:from IPython.display importdisplay
im = Image.open(car.png)
im.thumbnail((64, 64), Image.ANTIALIAS)
display(im)
car_class = hw_classifier.classify_image_details(im)
print("{: >10}{: >13}".format("[CLASS]","[RANKING]"))
for i inrange(len(car_class)):
print("{: >10}{: >10}".format(hw_classifier.classes[i],car_class[i]))
同样支持matplotlib进行数据可视化:%matplotlib inline
import matplotlib.pyplot asplt
x_pos = np.arange(len(car_class))
fig, ax = plt.subplots()
ax.bar(x_pos - 0.25, (car_class/100.0), 0.25)
ax.set_xticklabels(hw_classifier.classes, rotation=vertical)
ax.set_xticks(x_pos)
ax.set
plt.show()
这不就是Python嘛,真的是非常的方便,而且图像处理也兼容使用Pillow。文件中给出了一些图像识别的例子,大家可以去看看。改天阿chai给大家出一个从零搭建PYNQ的教程,包括模型的量化推理等等。https://github.com/Xilinx/DPU-PYNQ.git
cdDPU-PYNQ/upgrade
make
安装pynq-dpu:pip install pynq-dpu
启动jupyter-notebook:pynq get-notebooks pynq-dpu -p .
模型库在如下链接中。模型库:https://github.com/Xilinx/Vitis-AI/tree/v1.3对于DPU的设计,我们需要在自己的电脑上进行,在添加模块后,我们使用如下命令进行编译:make BOARD=
对于ZYNQ+DPU的开发过程阿chai会单独出一期,因为涉及的东西太多了。。。{
"model":"测试的模型",
"combined_model":true,
"input_width":224,
"input_height":224,
"image":"测试的路径",
"mean":[104,117,124],
"scale":1,
"format":"BGR" "threshold":0.5}
详细的操作请前往Paddle Lite的GitHub,这里只做简单的流程介绍。GitHub: https://github.com/PaddlePaddle/Paddle-Lite如果不想编译,直接在如下网址中下载编译好的文件即可。编译后的文件:https://ai.baidu.com/ai-doc/HWCE/Yk3b95s8o1.安装测试我们首先在有在开发板上编译Paddle Lite,编译的时候需要设置cmake的参数,设置LITE_WITH_FPGA=ON和LITE_WITH_ARM=ON,问就是我们都用到。对应的FPGA的编译脚本是lite/tools/build_FPGA.sh,我们执行即可。sh ./lite/tools/build_fpga.sh
make publish_inference -j2
接下来我们编译示例demo,demo也在刚才的下载链接中。板子的使用过程请参考百度官方的文档,文档介绍的非常的清楚,阿chai这里就不花时间去讲解使用过程了。然后进入demo中进行编译:# classificationcd/home/root/workspace/sample/classification/
mkdir build
cdbuild
cmake ..
make
build目录下会出现image_classify和video_classify两个可执行文件,图片预测运行image_classify文件。使用FPGA 进行resnet50进行测试:./image_classify_fpga_preprocess ../configs/resnet50/drink.json
可以看到对应的输出结果,同样detection的模型测试方式也这样操作。2.可调用的接口C++C++的主要包括预处理以及预测库的接口。预处理接口主要是使用FPGA完成图片的缩放、颜色空间转换和mean/std操作。预测库接口主要完成模型的初始化、输入参数构造、预测和结果获取。预处理接口示例:/**
* 判断输入图像是否是wc 16对齐
* width 输入图像宽度
* channel 输入图像高度
**/ bool img_is_align(int width, int channel);
/**
* 对齐后的大小
* width 输入图像宽度
* channel 输入图像高度
**/ int align_size(int width, int channel);
/**
* 分配存放图片的内存,析构函数会自动释放 (目前支持BGR->RGB RGB->BGR YUV422->BGR YUV->RGB) 图像最大分辨率支持1080p
* height 输入图像的框
* width 输入图像宽度
* in_format 输入图像格式 参考image_format
* return uint8_t* opencv Mat CV_8UC3
**/ uint8_t* mem_alloc(int img_height, int img_width, image_format in_format);
预测库使用步骤1、模型初始化,构建预测对象 std::unique_ptr
PaddleMobileConfig config;
std::string model_dir = j["model"];
config.precision = PaddleMobileConfig::FP32;
config.device = PaddleMobileConfig::kFPGA;
config.prog_file = model_dir + "/model";
config.param_file = model_dir + "/params";
config.thread_num = 4;
g_predictor = CreatePaddlePredictor
PaddleEngineKind::kPaddleMobile>(config);
2、输入输出参数 std::vector
PaddleTensor tensor;
tensor.shape = std::vector<int>({1, 3, input_height, input_width});
tensor.data = PaddleBuf(input, sizeof(input));
tensor.dtype = PaddleDType::FLOAT32;
paddle_tensor_feeds.push_back(tensor);
PaddleTensor tensor_imageshape;
tensor_imageshape.shape = std::vector<int>({1, 2});
tensor_imageshape.data = PaddleBuf(image_shape, 1 * 2 * sizeof(float));
tensor_imageshape.dtype = PaddleDType::FLOAT32;
paddle_tensor_feeds.push_back(tensor_imageshape);
PaddleTensor tensor_out;
tensor_out.shape = std::vector<int>({});
tensor_out.data = PaddleBuf();
tensor_out.dtype = PaddleDType::FLOAT32;
std::vector<PaddleTensor> outputs(1, tensor_out);
3、预测g_predictor->Run(paddle_tensor_feeds, &outputs);
4、获取结果 float *data = static_cast<float *>(outputs[0].data.data());
int size = outputs[0].shape[0];
PythonEdgeBoard系统已经安装了python环境,用户可直接使用即可,同时python接口为用户提供了paddlemobile的python安装包以及示例工程。文件名称说明paddlemobile-0.0.1.linux-aarch64-py2.tar.gzpaddlemobile的python2安装包edgeboard.py基于python的模型预测示例api.pyedgeboard.py的api示例configs.classification分类模型的配置文件目录,同C++示例的配置文件configs.detection检测模型的配置文件目录,同C++示例的配置文件models.classification分类模型的模型文件目录,同C++示例的模型文件models.detection检测模型的模型文件目录,同C++示例的模型文件安装paddlemobile-python SDK,在根目录中解压tar -xzvf home/root/workspace/paddlemobile-0.0.1.linux-aarch64-py2.tar.gz
例如使用分类模型的测试如下:python api.py -j 你测试的json文件
详细的使用说明请关注Paddle-Lite的GitHub。 End
声明:部分内容来源于网络,仅供读者学术交流之目的。文章版权归原作者所有。如有不妥,请联系删除。