当前位置: 首页 > news >正文

PaddleClas学习3——使用PPLCNet模型对车辆朝向进行识别(c++)

使用PPLCNet模型对车辆朝向进行识别

  • 1 准备环境
  • 2 准备模型
    • 2.1 模型导出
    • 2.2 修改配置文件
  • 3 编译
    • 3.1 使用CMake生成项目文件
    • 3.2 编译
    • 3.3 执行
    • 3.4 添加后处理程序
      • 3.4.1 postprocess.h
      • 3.4.2 postprocess.cpp
      • 3.4.3 在cls.h中添加函数声明
      • 3.4.4 在cls.cpp中添加函数定义
      • 3.4.5 在main.cpp中调用
  • 4 模型预测
    • 4.1 测试结果
    • 4.2 与python预测结果对比

1 准备环境

参考上一篇:Windows PaddleSeg c++部署

2 准备模型

2.1 模型导出

对上一篇 使用PPLCNet模型对车辆朝向进行识别 训练得到模型进行转换。将该模型转为 inference 模型只需运行如下命令:

python tools\export_model.py -c .\ppcls\configs\PULC\vehicle_attribute\PPLCNet_x1_0.yaml -o Global.pretrained_model=output/PPLCNet_x1_0/best_model -o Global.save_inference_dir=./deploy/models/class_vehicle_attribute_infer

训练得到的模型
图2.1 训练得到的模型
在这里插入图片描述
图2.2 导出的模型

2.2 修改配置文件

deploy/configs/PULC/vehicle_attribute/inference_vehicle_attribute.yaml
修改Global下的infer_imgsinference_model_dir

Global:infer_imgs: "./images/PULC/vehicle_attribute/0002_c002_00030670_0.jpg"inference_model_dir: "./models/class_vehicle_attribute_infer"batch_size: 1use_gpu: Trueenable_mkldnn: Truecpu_num_threads: 10#benchmark: Falseenable_benchmark: Falseuse_fp16: Falseir_optim: Trueuse_tensorrt: Falsegpu_mem: 8000enable_profile: False

3 编译

工程整体目录结构如下:

G:/paddle/c++├── paddle_inference
G:/paddle├── PaddleClas-release-2.5

3.1 使用CMake生成项目文件

在这里插入图片描述

3.2 编译

用Visual Studio 2022打开cpp\build\clas_system.sln,将编译模式设置为Release,点击生成->生成解决方案,在cpp\build\Release文件夹内生成clas_system.exe

3.3 执行

进入到build/Release目录下,将准备的模型和图片放到clas_system.exe同级目录,build/Release目录结构如下:

Release
├──clas_system.exe                # 可执行文件
├──images         				  # 测试图片├── PULC├── vehicle_attribute├── 0002_c002_00030670_0.jpg
├──configs         				  # 配置文件├── PULC├── vehicle_attribute├── inference_vehicle_attribute.yaml
├──models      					  # 推理用到的模型├── class_vehicle_attribute_infer├── inference.pdmodel          # 预测模型的拓扑结构文件├── inference.pdiparams        # 预测模型的权重文件└── inference.pdiparams.info   # 参数额外信息,一般无需关注
├──*.dll                          # dll文件

3.4 添加后处理程序

3.4.1 postprocess.h

// postprocess.h
#include <iostream>
#include <vector>namespace PaddleClas {class VehicleAttribute {public:float color_threshold = 0.5;float type_threshold = 0.5;float direction_threshold = 0.5;std::vector<std::string> color_list = { "yellow", "orange", "green", "gray", "red", "blue", "white","golden", "brown", "black" };std::vector<std::string> type_list = { "sedan", "suv", "van", "hatchback", "mpv", "pickup", "bus","truck", "estate" };std::vector<std::string> direction_list = { "forward", "sideward", "backward" };std::string run(std::vector<float>& pred_data);};
}

3.4.2 postprocess.cpp

// postprocess.cpp#include "include/postprocess.h"
#include <string>
namespace PaddleClas {std::string VehicleAttribute::run(std::vector<float>& pred_data) {int color_num = 10;int type_num = 9;int direction_num = 3;int index_color = std::distance(&pred_data[0], std::max_element(&pred_data[0], &pred_data[0] + 10));//左闭右开int index_type = std::distance(&pred_data[0] + 10, std::max_element(&pred_data[0] + 10, &pred_data[0] + 19));int index_direction = std::distance(&pred_data[0] + 19, std::max_element(&pred_data[0] + 19, &pred_data[0] + 22));std::string color_info, type_info, direction_info;if (pred_data[index_color] >= this->color_threshold) {color_info = "Color: (" + color_list[index_color] + ", pro: " + std::to_string(pred_data[index_color]) + ")";}if (pred_data[index_type + 10] >= this->type_threshold) {type_info = "Type: (" + type_list[index_type] + ", pro: " + std::to_string(pred_data[index_type + 10]) + ")";}if (pred_data[index_direction + 19] >= this->direction_threshold) {direction_info = "Direction: (" + direction_list[index_direction] + ", pro: " + std::to_string(pred_data[index_direction + 19]) + ")";}std::string pred_res = color_info + type_info + direction_info;pred_res += "pred: ";for (int i = 0; i < pred_data.size(); i++) {if (i < 10) {if (pred_data[i] > color_threshold) {pred_res += "1, ";}else {pred_res += "0, ";}}else if (i < 19) {if (pred_data[i] > type_threshold) {pred_res += "1, ";}else {pred_res += "0, ";}}else {if (pred_data[i] > direction_threshold) {pred_res += "1, ";}else {pred_res += "0, ";}}}return pred_res;}
}//namespace

3.4.3 在cls.h中添加函数声明

// Run predictor for vehicle attribute
void Run(cv::Mat& img, std::vector<float>& out_data, std::string &pred_res,std::vector<double>& times);

3.4.4 在cls.cpp中添加函数定义

void Classifier::Run(cv::Mat& img, std::vector<float>& out_data, std::string& pred_res,std::vector<double>& times){cv::Mat srcimg;cv::Mat resize_img;img.copyTo(srcimg);auto preprocess_start = std::chrono::system_clock::now();this->resize_op_.Run(img, resize_img, this->resize_size_);//this->resize_op_.Run(img, resize_img, this->resize_short_size_);//this->crop_op_.Run(resize_img, this->crop_size_);this->normalize_op_.Run(&resize_img, this->mean_, this->std_, this->scale_);std::vector<float> input(1 * 3 * resize_img.rows * resize_img.cols, 0.0f);this->permute_op_.Run(&resize_img, input.data());auto input_names = this->predictor_->GetInputNames();auto input_t = this->predictor_->GetInputHandle(input_names[0]);input_t->Reshape({ 1, 3, resize_img.rows, resize_img.cols });auto preprocess_end = std::chrono::system_clock::now();auto infer_start = std::chrono::system_clock::now();input_t->CopyFromCpu(input.data());this->predictor_->Run();auto output_names = this->predictor_->GetOutputNames();auto output_t = this->predictor_->GetOutputHandle(output_names[0]);std::vector<int> output_shape = output_t->shape();int out_num = std::accumulate(output_shape.begin(), output_shape.end(), 1,std::multiplies<int>());out_data.resize(out_num);output_t->CopyToCpu(out_data.data());auto infer_end = std::chrono::system_clock::now();auto postprocess_start = std::chrono::system_clock::now();pred_res = this->vehicle_attribute_op.run(out_data);auto postprocess_end = std::chrono::system_clock::now();std::chrono::duration<float> preprocess_diff =preprocess_end - preprocess_start;times[0] = double(preprocess_diff.count() * 1000);std::chrono::duration<float> inference_diff = infer_end - infer_start;double inference_cost_time = double(inference_diff.count() * 1000);times[1] = inference_cost_time;std::chrono::duration<float> postprocess_diff =postprocess_end - postprocess_start;times[2] = double(postprocess_diff.count() * 1000);
}

3.4.5 在main.cpp中调用

EFINE_string(config,
"./configs/PULC/vehicle_attribute/inference_vehicle_attribute.yaml", "Path of yaml file");
DEFINE_string(c,
"", "Path of yaml file");int main(int argc, char** argv) {google::ParseCommandLineFlags(&argc, &argv, true);std::string yaml_path = "";if (FLAGS_config == "" && FLAGS_c == "") {std::cerr << "[ERROR] usage: " << std::endl<< argv[0] << " -c $yaml_path" << std::endl<< "or:" << std::endl<< argv[0] << " -config $yaml_path" << std::endl;exit(1);}else if (FLAGS_config != "") {yaml_path = FLAGS_config;}else {yaml_path = FLAGS_c;}ClsConfig config(yaml_path);config.PrintConfigInfo();std::string path(config.infer_imgs);std::vector <std::string> img_files_list;if (cv::utils::fs::isDirectory(path)) {std::vector <cv::String> filenames;cv::glob(path, filenames);for (auto f : filenames) {img_files_list.push_back(f);}}else {img_files_list.push_back(path);}std::cout << "img_file_list length: " << img_files_list.size() << std::endl;Classifier classifier(config);std::vector<double> cls_times = { 0, 0, 0 };std::vector<double> cls_times_total = { 0, 0, 0 };double infer_time;std::vector<float> out_data;std::string result;int warmup_iter = 5;bool label_output_equal_flag = true;for (int idx = 0; idx < img_files_list.size(); ++idx) {std::string img_path = img_files_list[idx];cv::Mat srcimg = cv::imread(img_path, cv::IMREAD_COLOR);if (!srcimg.data) {std::cerr << "[ERROR] image read failed! image path: " << img_path<< "\n";exit(-1);}cv::cvtColor(srcimg, srcimg, cv::COLOR_BGR2RGB);classifier.Run(srcimg, out_data, result, cls_times);std::cout << "Current image path: " << img_path << std::endl;infer_time = cls_times[0] + cls_times[1] + cls_times[2];std::cout << "Current total inferen time cost: " << infer_time << " ms."<< std::endl;std::cout << "Current inferen result: " << result << " ."<< std::endl;if (idx >= warmup_iter) {for (int i = 0; i < cls_times.size(); ++i)cls_times_total[i] += cls_times[i];}}if (img_files_list.size() > warmup_iter) {infer_time = cls_times_total[0] + cls_times_total[1] + cls_times_total[2];std::cout << "average time cost in all: "<< infer_time / (img_files_list.size() - warmup_iter) << " ms."<< std::endl;}std::string presion = "fp32";if (config.use_fp16)presion = "fp16";if (config.benchmark) {AutoLogger autolog("Classification", config.use_gpu, config.use_tensorrt,config.use_mkldnn, config.cpu_threads, 1,"1, 3, 224, 224", presion, cls_times_total,img_files_list.size());autolog.report();}return 0;
}

4 模型预测

4.1 测试结果

在这里插入图片描述
图 4.1 输入图像
在这里插入图片描述
图4.2 预测结果

4.2 与python预测结果对比

python deploy\python\predict_cls.py -c .\deploy\configs\PULC\vehicle_attribute\inference_vehicle_attribute.yaml -o Global.pretrained_model=output/PPLCNet_x1_0/best_model

在这里插入图片描述

相关文章:

  • 安装LLaMA-Factory微调chatglm3,修改自我认知
  • 奥比中光 Femto Bolt相机ROS配置
  • strtok()的用法及实现哦
  • 逻辑回归的介绍和应用
  • es模板和索引简单操作简介
  • rust宏(macro)详解
  • Selenium+Unittest+HTMLTestRunner框架更改为Selenium+Pytest+Allure(一)
  • Sui承诺向流动性质押协议投入$SUI
  • TimescaleDB-1 安装
  • 3D渲染和动画制作软件KeyShot Pro mac附加功能
  • CRM客户管理系统-超详细介绍
  • 机器人视觉
  • OTP语音芯片与可重复擦写(Flash型)语音芯片:特性比较与应用差异
  • CanEasy多场景应用,让汽车总线测试更简单
  • 差分法详解
  • ABAP的include关键字,Java的import, C的include和C4C ABSL 的import比较
  • canvas绘制圆角头像
  • IDEA 插件开发入门教程
  • laravel 用artisan创建自己的模板
  • Logstash 参考指南(目录)
  • nodejs调试方法
  • Python_OOP
  • react-core-image-upload 一款轻量级图片上传裁剪插件
  • Sequelize 中文文档 v4 - Getting started - 入门
  • Shadow DOM 内部构造及如何构建独立组件
  • uni-app项目数字滚动
  • 前端学习笔记之观察者模式
  • 如何解决微信端直接跳WAP端
  • 如何实现 font-size 的响应式
  • 如何学习JavaEE,项目又该如何做?
  • 入门到放弃node系列之Hello Word篇
  • 使用docker-compose进行多节点部署
  • 使用Tinker来调试Laravel应用程序的数据以及使用Tinker一些总结
  • 文本多行溢出显示...之最后一行不到行尾的解决
  •  一套莫尔斯电报听写、翻译系统
  • 鱼骨图 - 如何绘制?
  • 正则与JS中的正则
  • 交换综合实验一
  • #、%和$符号在OGNL表达式中经常出现
  • #QT(智能家居界面-界面切换)
  • #我与Java虚拟机的故事#连载05:Java虚拟机的修炼之道
  • $L^p$ 调和函数恒为零
  • (09)Hive——CTE 公共表达式
  • (二)Pytorch快速搭建神经网络模型实现气温预测回归(代码+详细注解)
  • (汇总)os模块以及shutil模块对文件的操作
  • (力扣记录)235. 二叉搜索树的最近公共祖先
  • (轉貼)《OOD启思录》:61条面向对象设计的经验原则 (OO)
  • .net mvc部分视图
  • .net websocket 获取http登录的用户_如何解密浏览器的登录密码?获取浏览器内用户信息?...
  • .NET 编写一个可以异步等待循环中任何一个部分的 Awaiter
  • .NET框架类在ASP.NET中的使用(2) ——QA
  • /boot 内存空间不够
  • @FeignClient 调用另一个服务的test环境,实际上却调用了另一个环境testone的接口,这其中牵扯到k8s容器外容器内的问题,注册到eureka上的是容器外的旧版本...
  • [Android]竖直滑动选择器WheelView的实现
  • [BeginCTF]真龙之力