ไฝฟ็จLite.AI.ToolKit ๐๐๐ C++ๅทฅๅ ท็ฎฑๆฅ่ทYOLOP็ไธไบๆกไพ(https://github.com/DefTruth/lite.ai.toolkit) , ๅ ๅซONNXRuntime C++ใMNNใNCNNๅTNN็ๆฌใ
่ฅๆฏๆ็จ๏ผโค๏ธไธๅฆจ็ปไธชโญ๏ธ๐ๆฏๆไธไธๅง~ ๐๐คช๐
YOLOP C++ ็ๆฌ็ๆบ็ ๅ ๅซONNXRuntimeใMNNใNCNNๅTNNๅไธช็ๆฌ๏ผๅฏไปฅๅจ lite.ai.toolkit ๅทฅๅ ท็ฎฑไธญๆพๅฐใๆฌ้กน็ฎไธป่ฆไป็ปๅฆไฝๅบไบ lite.ai.toolkit ๅทฅๅ ท็ฎฑ๏ผ็ดๆฅไฝฟ็จYOLOPๆฅ่ทๅ จๆฏๅๅฒๅๆฃๆตใ้่ฆ่ฏดๆ็ๆฏ๏ผๆฌ้กน็ฎๆฏๅบไบMacOSไธ็ผ่ฏ็ liblite.ai.toolkit.v0.1.0.dylib ๆฅๅฎ็ฐ็๏ผๅฏนไบไฝฟ็จMacOS็็จๆท๏ผๅฏไปฅ็ดๆฅไธ่ฝฝๆฌ้กน็ฎๅ ๅซ็liblite.ai.toolkit.v0.1.0ๅจๆๅบๅๅ ถไปไพ่ตๅบ่ฟ่กไฝฟ็จใ่้MacOS็จๆท๏ผๅ้่ฆไปlite.ai.toolkit ไธญไธ่ฝฝๆบ็ ่ฟ่ก็ผ่ฏใlite.ai.toolkit c++ๅทฅๅ ท็ฎฑ็ฎๅๅ ๅซ70+ๆต่ก็ๅผๆบๆจกๅใ
ONNXRuntime C++ใMNNใNCNNๅTNN็ๆฌ็ๆจ็ๅฎ็ฐๅๅทฒๆต่ฏ้่ฟ๏ผๆฌข่ฟ็ฝๅซ~
ๅฏไปฅไปๆๆไพ็้พๆฅไธ่ฝฝ (Baidu Drive code: 8gin)ใ
Class | Pretrained ONNX Files | Rename or Converted From (Repo) | Size |
---|---|---|---|
lite::cv::detection::YOLOP | yolop-1280-1280.onnx | YOLOP | 30Mb |
lite::cv::detection::YOLOP | yolop-640-640.onnx | YOLOP | 30Mb |
lite::cv::detection::YOLOP | yolop-320-320.onnx | YOLOP | 30Mb |
MNNๆจกๅๆไปถไธ่ฝฝๅฐๅ๏ผ(Baidu Drive code: 9v63)ใ
Class | Pretrained MNN Files | Rename or Converted From (Repo) | Size |
---|---|---|---|
lite::mnn::cv::detection::YOLOP | yolop-320-320.mnn | YOLOP | 30Mb |
lite::mnn::cv::detection::YOLOP | yolop-640-640.mnn | YOLOP | 30Mb |
lite::mnn::cv::detection::YOLOP | yolop-1280-1280.mnn | YOLOP | 30Mb |
TNNๆจกๅๆไปถไธ่ฝฝๅฐๅ๏ผ(Baidu Drive code: 6o6k)ใ
Class | Pretrained TNN Files | Rename or Converted From (Repo) | Size |
---|---|---|---|
lite::tnn::cv::detection::YOLOP | yolop-320-320.opt.tnnproto&tnnmodel | YOLOP | 30Mb |
lite::tnn::cv::detection::YOLOP | yolop-640-640.opt.tnnproto&tnnmodel | YOLOP | 30Mb |
lite::tnn::cv::detection::YOLOP | yolop-1280-1280.opt.tnnproto&tnnmodel | YOLOP | 30Mb |
TNNๆจกๅๆไปถไธ่ฝฝๅฐๅ๏ผ(Baidu Drive code: sc7f)ใ
Class | Pretrained NCNN Files | Rename or Converted From (Repo) | Size |
---|---|---|---|
lite::ncnn::cv::detection::YOLOP | yolop-640-640.opt.param&bin | YOLOP | 30Mb |
ๅจlite.ai.toolkit ไธญ๏ผYOLOP็ๅฎ็ฐ็ฑปไธบ๏ผ
class LITE_EXPORTS lite::cv::detection::YOLOP;
class LITE_EXPORTS lite::mnn::cv::detection::YOLOP;
class LITE_EXPORTS lite::tnn::cv::detection::YOLOP;
class LITE_EXPORTS lite::ncnn::cv::detection::YOLOP;
่ฏฅ็ฑปๅ็ฎๅๅ
ๅซ1ๅ
ฌๅ
ฑๆฅๅฃdetect
็จไบ่ฟ่ก็ฎๆ ๆฃๆตใ
public:
void detect(const cv::Mat &mat,
std::vector<types::Boxf> &detected_boxes,
types::SegmentContent &da_seg_content,
types::SegmentContent &ll_seg_content,
float score_threshold = 0.25f, float iou_threshold = 0.45f,
unsigned int topk = 100, unsigned int nms_type = NMS::OFFSET);
detect
ๆฅๅฃ็่พๅ
ฅๅๆฐ่ฏดๆ๏ผ
- mat: cv::Mat็ฑปๅ๏ผBGRๆ ผๅผใ
- detected_boxes: Boxfๅ้๏ผๅ ๅซ่ขซๆฃๆตๅฐ็่ฝฆ่พๆก๏ผBoxfไธญๅ ๅซx1,y1,x2,y2,label,score็ญๆๅ
- da_seg_content: SegmentContent็จๆฅไฟๅญ้ขๆต็ๅฏ้ฉพ้ฉถๅบๅ็ปๆ
- ll_seg_content: SegmentContent็จๆฅไฟๅญ้ขๆต็่ช้ๅบๅ็ปๆ
- score_threshold๏ผๅ็ฑปๅพๅ๏ผ่ดจ้ๅพๅ๏ผ้ๅผ๏ผ้ป่ฎค0.45๏ผๅฐไบ่ฏฅ้ๅผ็ๆกๅฐ่ขซไธขๅผใ
- iou_threshold๏ผNMSไธญ็iou้ๅผ๏ผ้ป่ฎค0.3ใ
- topk๏ผ้ป่ฎค100๏ผๅชไฟ็ๅkไธชๆฃๆตๅฐ็็ปๆใ
- nms_type๏ผNMS็ฎๆณ็็ฑปๅ๏ผ้ป่ฎคไธบไธๅ็็ฑปๅซๅ่ชๅNMSใ
#include "lite/lite.h"
static void test_default()
{
std::string onnx_path = "..//hub/onnx/cv/yolop-640-640.onnx";
std::string test_img_path = "../resources/1.jpg";
std::string save_det_path = "../logs/1_det.jpg";
std::string save_da_path = "../logs/1_da.jpg";
std::string save_ll_path = "../logs/1_ll.jpg";
std::string save_merge_path = "../logs/1_merge.jpg";
auto *yolop = new lite::cv::detection::YOLOP(onnx_path, 16); // 16 threads
lite::types::SegmentContent da_seg_content;
lite::types::SegmentContent ll_seg_content;
std::vector<lite::types::Boxf> detected_boxes;
cv::Mat img_bgr = cv::imread(test_img_path);
yolop->detect(img_bgr, detected_boxes, da_seg_content, ll_seg_content);
if (!detected_boxes.empty() && da_seg_content.flag && ll_seg_content.flag)
{
// boxes.
cv::Mat img_det = img_bgr.clone();
lite::utils::draw_boxes_inplace(img_det, detected_boxes);
cv::imwrite(save_det_path, img_det);
std::cout << "Saved " << save_det_path << " done!" << "\n";
// da && ll seg
cv::imwrite(save_da_path, da_seg_content.class_mat);
cv::imwrite(save_ll_path, ll_seg_content.class_mat);
std::cout << "Saved " << save_da_path << " done!" << "\n";
std::cout << "Saved " << save_ll_path << " done!" << "\n";
// merge
cv::Mat img_merge = img_bgr.clone();
cv::Mat color_seg = da_seg_content.color_mat + ll_seg_content.color_mat;
cv::addWeighted(img_merge, 0.5, color_seg, 0.5, 0., img_merge);
lite::utils::draw_boxes_inplace(img_merge, detected_boxes);
cv::imwrite(save_merge_path, img_merge);
std::cout << "Saved " << save_merge_path << " done!" << "\n";
// label
if (!da_seg_content.names_map.empty() && !ll_seg_content.names_map.empty())
{
for (auto it = da_seg_content.names_map.begin(); it != da_seg_content.names_map.end(); ++it)
{
std::cout << "Default Version Detected Label: "
<< it->first << " Name: " << it->second << std::endl;
}
for (auto it = ll_seg_content.names_map.begin(); it != ll_seg_content.names_map.end(); ++it)
{
std::cout << "Default Version Detected Label: "
<< it->first << " Name: " << it->second << std::endl;
}
}
}
delete yolop;
}
#include "lite/lite.h"
static void test_mnn()
{
#ifdef ENABLE_MNN
std::string mnn_path = "../hub/mnn/cv/yolop-640-640.mnn";
std::string test_img_path = "../resources/1.jpg";
std::string save_det_path = "../logs/1_det_mnn.jpg";
std::string save_da_path = "../logs/1_da_mnn.jpg";
std::string save_ll_path = "../logs/1_ll_mnn.jpg";
std::string save_merge_path = "../logs/1_merge_mnn.jpg";
auto *yolop = new lite::mnn::cv::detection::YOLOP(mnn_path, 16); // 16 threads
lite::types::SegmentContent da_seg_content;
lite::types::SegmentContent ll_seg_content;
std::vector<lite::types::Boxf> detected_boxes;
cv::Mat img_bgr = cv::imread(test_img_path);
yolop->detect(img_bgr, detected_boxes, da_seg_content, ll_seg_content);
if (!detected_boxes.empty() && da_seg_content.flag && ll_seg_content.flag)
{
// boxes.
cv::Mat img_det = img_bgr.clone();
lite::utils::draw_boxes_inplace(img_det, detected_boxes);
cv::imwrite(save_det_path, img_det);
std::cout << "Saved " << save_det_path << " done!" << "\n";
// da && ll seg
cv::imwrite(save_da_path, da_seg_content.class_mat);
cv::imwrite(save_ll_path, ll_seg_content.class_mat);
std::cout << "Saved " << save_da_path << " done!" << "\n";
std::cout << "Saved " << save_ll_path << " done!" << "\n";
// merge
cv::Mat img_merge = img_bgr.clone();
cv::Mat color_seg = da_seg_content.color_mat + ll_seg_content.color_mat;
cv::addWeighted(img_merge, 0.5, color_seg, 0.5, 0., img_merge);
lite::utils::draw_boxes_inplace(img_merge, detected_boxes);
cv::imwrite(save_merge_path, img_merge);
std::cout << "Saved " << save_merge_path << " done!" << "\n";
// label
if (!da_seg_content.names_map.empty() && !ll_seg_content.names_map.empty())
{
for (auto it = da_seg_content.names_map.begin(); it != da_seg_content.names_map.end(); ++it)
{
std::cout << "MNN Version Detected Label: "
<< it->first << " Name: " << it->second << std::endl;
}
for (auto it = ll_seg_content.names_map.begin(); it != ll_seg_content.names_map.end(); ++it)
{
std::cout << "MNN Version Detected Label: "
<< it->first << " Name: " << it->second << std::endl;
}
}
}
delete yolop;
#endif
}
#include "lite/lite.h"
static void test_tnn()
{
#ifdef ENABLE_TNN
std::string proto_path = "../hub/tnn/cv/yolop-640-640.opt.tnnproto";
std::string model_path = "../hub/tnn/cv/yolop-640-640.opt.tnnmodel";
std::string test_img_path = "../resources/1.jpg";
std::string save_det_path = "../logs/1_det_tnn.jpg";
std::string save_da_path = "../logs/1_da_tnn.jpg";
std::string save_ll_path = "../logs/1_ll_tnn.jpg";
std::string save_merge_path = "../logs/1_merge_tnn.jpg";
auto *yolop = new lite::tnn::cv::detection::YOLOP(proto_path, model_path, 16); // 16 threads
lite::types::SegmentContent da_seg_content;
lite::types::SegmentContent ll_seg_content;
std::vector<lite::types::Boxf> detected_boxes;
cv::Mat img_bgr = cv::imread(test_img_path);
yolop->detect(img_bgr, detected_boxes, da_seg_content, ll_seg_content);
if (!detected_boxes.empty() && da_seg_content.flag && ll_seg_content.flag)
{
// boxes.
cv::Mat img_det = img_bgr.clone();
lite::utils::draw_boxes_inplace(img_det, detected_boxes);
cv::imwrite(save_det_path, img_det);
std::cout << "Saved " << save_det_path << " done!" << "\n";
// da && ll seg
cv::imwrite(save_da_path, da_seg_content.class_mat);
cv::imwrite(save_ll_path, ll_seg_content.class_mat);
std::cout << "Saved " << save_da_path << " done!" << "\n";
std::cout << "Saved " << save_ll_path << " done!" << "\n";
// merge
cv::Mat img_merge = img_bgr.clone();
cv::Mat color_seg = da_seg_content.color_mat + ll_seg_content.color_mat;
cv::addWeighted(img_merge, 0.5, color_seg, 0.5, 0., img_merge);
lite::utils::draw_boxes_inplace(img_merge, detected_boxes);
cv::imwrite(save_merge_path, img_merge);
std::cout << "Saved " << save_merge_path << " done!" << "\n";
// label
if (!da_seg_content.names_map.empty() && !ll_seg_content.names_map.empty())
{
for (auto it = da_seg_content.names_map.begin(); it != da_seg_content.names_map.end(); ++it)
{
std::cout << "TNN Version Detected Label: "
<< it->first << " Name: " << it->second << std::endl;
}
for (auto it = ll_seg_content.names_map.begin(); it != ll_seg_content.names_map.end(); ++it)
{
std::cout << "TNN Version Detected Label: "
<< it->first << " Name: " << it->second << std::endl;
}
}
}
delete yolop;
#endif
}
#include "lite/lite.h"
static void test_ncnn()
{
#ifdef ENABLE_NCNN
std::string param_path = "../hub/ncnn/cv/yolop-640-640.opt.param";
std::string bin_path = "../hub/ncnn/cv/yolop-640-640.opt.bin";
std::string test_img_path = "../resources/1.jpg";
std::string save_det_path = "../logs/1_det_ncnn.jpg";
std::string save_da_path = "../logs/1_da_ncnn.jpg";
std::string save_ll_path = "../logs/1_ll_ncnn.jpg";
std::string save_merge_path = "../logs/1_merge_ncnn.jpg";
auto *yolop = new lite::ncnn::cv::detection::YOLOP(param_path, bin_path, 16); // 16 threads
lite::types::SegmentContent da_seg_content;
lite::types::SegmentContent ll_seg_content;
std::vector<lite::types::Boxf> detected_boxes;
cv::Mat img_bgr = cv::imread(test_img_path);
yolop->detect(img_bgr, detected_boxes, da_seg_content, ll_seg_content);
if (!detected_boxes.empty() && da_seg_content.flag && ll_seg_content.flag)
{
// boxes.
cv::Mat img_det = img_bgr.clone();
lite::utils::draw_boxes_inplace(img_det, detected_boxes);
cv::imwrite(save_det_path, img_det);
std::cout << "Saved " << save_det_path << " done!" << "\n";
// da && ll seg
cv::imwrite(save_da_path, da_seg_content.class_mat);
cv::imwrite(save_ll_path, ll_seg_content.class_mat);
std::cout << "Saved " << save_da_path << " done!" << "\n";
std::cout << "Saved " << save_ll_path << " done!" << "\n";
// merge
cv::Mat img_merge = img_bgr.clone();
cv::Mat color_seg = da_seg_content.color_mat + ll_seg_content.color_mat;
cv::addWeighted(img_merge, 0.5, color_seg, 0.5, 0., img_merge);
lite::utils::draw_boxes_inplace(img_merge, detected_boxes);
cv::imwrite(save_merge_path, img_merge);
std::cout << "Saved " << save_merge_path << " done!" << "\n";
// label
if (!da_seg_content.names_map.empty() && !ll_seg_content.names_map.empty())
{
for (auto it = da_seg_content.names_map.begin(); it != da_seg_content.names_map.end(); ++it)
{
std::cout << "NCNN Version Detected Label: "
<< it->first << " Name: " << it->second << std::endl;
}
for (auto it = ll_seg_content.names_map.begin(); it != ll_seg_content.names_map.end(); ++it)
{
std::cout << "NCNN Version Detected Label: "
<< it->first << " Name: " << it->second << std::endl;
}
}
}
delete yolop;
#endif
}
- ่พๅบ็ปๆไธบ:
ๅจMacOSไธๅฏไปฅ็ดๆฅ็ผ่ฏ่ฟ่กๆฌ้กน็ฎ๏ผๆ ้ไธ่ฝฝๅ ถไปไพ่ตๅบใๅ ถไป็ณป็ปๅ้่ฆไปlite.ai.toolkit ไธญไธ่ฝฝๆบ็ ๅ ็ผ่ฏlite.ai.toolkit.v0.1.0ๅจๆๅบใ
git clone --depth=1 https://github.com/DefTruth/yolop.lite.ai.toolkit.git
cd yolop.lite.ai.toolkit
sh ./build.sh
- CMakeLists.txt่ฎพ็ฝฎ
cmake_minimum_required(VERSION 3.17)
project(yolop.lite.ai.toolkit)
set(CMAKE_CXX_STANDARD 11)
# setting up lite.ai.toolkit
set(LITE_AI_DIR ${CMAKE_SOURCE_DIR}/lite.ai.toolkit)
set(LITE_AI_INCLUDE_DIR ${LITE_AI_DIR}/include)
set(LITE_AI_LIBRARY_DIR ${LITE_AI_DIR}/lib)
include_directories(${LITE_AI_INCLUDE_DIR})
link_directories(${LITE_AI_LIBRARY_DIR})
set(OpenCV_LIBS
opencv_highgui
opencv_core
opencv_imgcodecs
opencv_imgproc
opencv_video
opencv_videoio
)
# add your executable
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/examples/build)
add_executable(lite_yolop examples/test_lite_yolop.cpp)
target_link_libraries(lite_yolop
lite.ai.toolkit
onnxruntime
MNN # need, if built lite.ai.toolkit with ENABLE_MNN=ON, default OFF
ncnn # need, if built lite.ai.toolkit with ENABLE_NCNN=ON, default OFF
TNN # need, if built lite.ai.toolkit with ENABLE_TNN=ON, default OFF
${OpenCV_LIBS}) # link lite.ai.toolkit & other libs.
- building && testing information:
[ 50%] Building CXX object CMakeFiles/lite_yolop.dir/examples/test_lite_yolop.cpp.o
[100%] Linking CXX executable lite_yolop
[100%] Built target lite_yolop
Testing Start ...
LITEORT_DEBUG LogId: ..//hub/onnx/cv/yolop-640-640.onnx
=============== Input-Dims ==============
input_node_dims: 1
input_node_dims: 3
input_node_dims: 640
input_node_dims: 640
=============== Output-Dims ==============
Output: 0 Name: det_out Dim: 0 :1
Output: 0 Name: det_out Dim: 1 :25200
Output: 0 Name: det_out Dim: 2 :6
Output: 1 Name: drive_area_seg Dim: 0 :1
Output: 1 Name: drive_area_seg Dim: 1 :2
Output: 1 Name: drive_area_seg Dim: 2 :640
Output: 1 Name: drive_area_seg Dim: 3 :640
Output: 2 Name: lane_line_seg Dim: 0 :1
Output: 2 Name: lane_line_seg Dim: 1 :2
Output: 2 Name: lane_line_seg Dim: 2 :640
Output: 2 Name: lane_line_seg Dim: 3 :640
========================================
detected num_anchors: 25200
generate_bboxes num: 62
Saved ../logs/1_det.jpg done!
Saved ../logs/1_da.jpg done!
Saved ../logs/1_ll.jpg done!
Saved ../logs/1_merge.jpg done!
Default Version Detected Label: 255 Name: drivable area
Default Version Detected Label: 255 Name: lane line
LITEORT_DEBUG LogId: ..//hub/onnx/cv/yolop-640-640.onnx
=============== Input-Dims ==============
input_node_dims: 1
input_node_dims: 3
input_node_dims: 640
input_node_dims: 640
=============== Output-Dims ==============
Output: 0 Name: det_out Dim: 0 :1
Output: 0 Name: det_out Dim: 1 :25200
Output: 0 Name: det_out Dim: 2 :6
Output: 1 Name: drive_area_seg Dim: 0 :1
Output: 1 Name: drive_area_seg Dim: 1 :2
Output: 1 Name: drive_area_seg Dim: 2 :640
Output: 1 Name: drive_area_seg Dim: 3 :640
Output: 2 Name: lane_line_seg Dim: 0 :1
Output: 2 Name: lane_line_seg Dim: 1 :2
Output: 2 Name: lane_line_seg Dim: 2 :640
Output: 2 Name: lane_line_seg Dim: 3 :640
========================================
detected num_anchors: 25200
generate_bboxes num: 62
Saved ../logs/1_det_onnx.jpg done!
Saved ../logs/1_da_onnx.jpg done!
Saved ../logs/1_ll_onnx.jpg done!
Saved ../logs/1_merge_onnx.jpg done!
ONNXRuntime Version Detected Label: 255 Name: drivable area
ONNXRuntime Version Detected Label: 255 Name: lane line
LITEMNN_DEBUG LogId: ../hub/mnn/cv/yolop-640-640.mnn
=============== Input-Dims ==============
**Tensor shape**: 1, 3, 640, 640,
Dimension Type: (CAFFE/PyTorch/ONNX)NCHW
=============== Output-Dims ==============
getSessionOutputAll done!
Output: det_out: **Tensor shape**: 1, 25200, 6,
Output: drive_area_seg: **Tensor shape**: 1, 2, 640, 640,
Output: lane_line_seg: **Tensor shape**: 1, 2, 640, 640,
========================================
detected num_anchors: 25200
generate_bboxes num: 62
Saved ../logs/1_det_mnn.jpg done!
Saved ../logs/1_da_mnn.jpg done!
Saved ../logs/1_ll_mnn.jpg done!
Saved ../logs/1_merge_mnn.jpg done!
MNN Version Detected Label: 255 Name: drivable area
MNN Version Detected Label: 255 Name: lane line
LITENCNN_DEBUG LogId: ../hub/ncnn/cv/yolop-640-640.opt.param
=============== Input-Dims ==============
Input: images: shape: c=0 h=0 w=0
=============== Output-Dims ==============
Output: det_stride_8: shape: c=0 h=0 w=0
Output: det_stride_16: shape: c=0 h=0 w=0
Output: det_stride_32: shape: c=0 h=0 w=0
Output: drive_area_seg: shape: c=0 h=0 w=0
Output: lane_line_seg: shape: c=0 h=0 w=0
========================================
generate_bboxes num: 62
Saved ../logs/1_det_ncnn.jpg done!
Saved ../logs/1_da_ncnn.jpg done!
Saved ../logs/1_ll_ncnn.jpg done!
Saved ../logs/1_merge_ncnn.jpg done!
NCNN Version Detected Label: 255 Name: drivable area
NCNN Version Detected Label: 255 Name: lane line
LITETNN_DEBUG LogId: ../hub/tnn/cv/yolop-640-640.opt.tnnproto
=============== Input-Dims ==============
images: [1 3 640 640 ]
Input Data Format: NCHW
=============== Output-Dims ==============
det_out: [1 25200 6 ]
drive_area_seg: [1 2 640 640 ]
lane_line_seg: [1 2 640 640 ]
========================================
detected num_anchors: 25200
generate_bboxes num: 62
Saved ../logs/1_det_tnn.jpg done!
Saved ../logs/1_da_tnn.jpg done!
Saved ../logs/1_ll_tnn.jpg done!
Saved ../logs/1_merge_tnn.jpg done!
TNN Version Detected Label: 255 Name: drivable area
TNN Version Detected Label: 255 Name: lane line
Testing Successful !