Lite AI Toolkit 使用教程
项目介绍
Lite AI Toolkit 是一个轻量级的 C++ 工具包,支持多种 AI 模型,包括 ONNXRuntime、MNN、TNN 和 TensorRT。该项目旨在提供一个简单易用的接口,以便开发者可以快速集成和部署 AI 模型。Lite AI Toolkit 不仅提供了预构建的库和测试资源,还支持混合使用 MNN 和 ONNXRuntime,使得开发者可以根据需要选择合适的后端。
项目快速启动
下载和安装
首先,从 GitHub 下载 Lite AI Toolkit 的预构建库和测试资源:
export LITE_AI_TAG_URL=https://github.com/DefTruth/lite.ai.toolkit/releases/download/v0.2.0
wget ${LITE_AI_TAG_URL}/lite-ort1.17.1+ocv4.9.0+ffmpeg4.2.2-linux-x86_64.tgz
wget ${LITE_AI_TAG_URL}/yolov5s.onnx
wget ${LITE_AI_TAG_URL}/test_yolov5.jpg
编译和运行示例
- 设置 CMakeLists.txt 文件:
set(lite_ai_toolkit_DIR YOUR-PATH-TO-LITE-INSTALL)
find_package(lite_ai_toolkit REQUIRED PATHS ${lite_ai_toolkit_DIR})
add_executable(lite_yolov5 test_lite_yolov5.cpp)
target_link_libraries(lite_yolov5 ${lite_ai_toolkit_LIBS})
- 编译测试工程:
mkdir build && cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
make -j8
- 运行测试:
./lite_yolov5
应用案例和最佳实践
目标检测示例
以下是一个使用 YoloV5 进行目标检测的示例代码:
#include "lite/lite.h"
int main() {
std::string onnx_path = "yolov5s.onnx";
std::string test_img_path = "test_yolov5.jpg";
std::string save_img_path = "test_results.jpg";
auto *yolov5 = new lite::cv::detection::YoloV5(onnx_path);
std::vector<lite::types::Boxf> detected_boxes;
cv::Mat img_bgr = cv::imread(test_img_path);
yolov5->detect(img_bgr, detected_boxes);
lite::utils::draw_boxes_inplace(img_bgr, detected_boxes);
cv::imwrite(save_img_path, img_bgr);
delete yolov5;
return 0;
}
混合使用 MNN 和 ONNXRuntime
Lite AI Toolkit 支持混合使用 MNN 和 ONNXRuntime。以下是一个示例:
#include "lite/lite.h"
int main() {
// 使用 YoloV5 进行目标检测
std::string onnx_path = "yolov5s.onnx";
auto *yolov5 = new lite::cv::detection::YoloV5(onnx_path);
// 使用 ONNXRuntime 或 MNN 实现自定义分类器
// ...
delete yolov5;
return 0;
}
典型生态项目
相关项目
- ONNXRuntime: 一个高性能的推理引擎,支持多种硬件平台。
- MNN: 一个轻量级的深度学习推理引擎,适用于移动设备。
- TNN: 腾讯优图实验室推出的高性能、轻量级的深度学习推理引擎。
- TensorRT: NVIDIA 推出的高性能深度学习推理优化器和运行时库。
这些项目与 Lite AI Toolkit 结合使用,可以进一步提升 AI 模型的性能和部署灵活性。