使用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_imgs
和inference_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