From 732ff8f16d4ff305c54498ae01ec2b228e85abb9 Mon Sep 17 00:00:00 2001 From: fateshelled <53618876+fateshelled@users.noreply.github.com> Date: Tue, 27 Sep 2022 21:45:15 +0900 Subject: [PATCH 01/14] add for onnxruntime --- yolox_ros_cpp/yolox_cpp/CMakeLists.txt | 45 +++++- .../yolox_cpp/include/yolox_cpp/config.h.in | 3 +- .../yolox_cpp/include/yolox_cpp/yolox.hpp | 6 +- .../include/yolox_cpp/yolox_onnxruntime.hpp | 43 ++++++ .../yolox_cpp/src/yolox_onnxruntime.cpp | 136 ++++++++++++++++++ .../launch/yolox_onnxruntime.launch.py | 108 ++++++++++++++ .../yolox_ros_cpp/src/yolox_ros_cpp.cpp | 11 ++ 7 files changed, 346 insertions(+), 6 deletions(-) create mode 100644 yolox_ros_cpp/yolox_cpp/include/yolox_cpp/yolox_onnxruntime.hpp create mode 100644 yolox_ros_cpp/yolox_cpp/src/yolox_onnxruntime.cpp create mode 100644 yolox_ros_cpp/yolox_ros_cpp/launch/yolox_onnxruntime.launch.py diff --git a/yolox_ros_cpp/yolox_cpp/CMakeLists.txt b/yolox_ros_cpp/yolox_cpp/CMakeLists.txt index 62ef630..96cf7a7 100644 --- a/yolox_ros_cpp/yolox_cpp/CMakeLists.txt +++ b/yolox_ros_cpp/yolox_cpp/CMakeLists.txt @@ -15,12 +15,16 @@ if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") add_compile_options(-Wall -Wextra -Wpedantic) endif() +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + # build option -option(YOLOX_USE_OPENVINO "Use OpenVINO" ON) -option(YOLOX_USE_TENSORRT "Use TensorRT" ON) +option(YOLOX_USE_OPENVINO "Use OpenVINO" ON) +option(YOLOX_USE_TENSORRT "Use TensorRT" ON) +option(YOLOX_USE_ONNXRUNTIME "Use ONNXRuntime" ON) set(ENABLE_OPENVINO OFF) set(ENABLE_TENSORRT OFF) +set(ENABLE_ONNXRUNTIME OFF) # find dependencies find_package(ament_cmake REQUIRED) @@ -41,17 +45,44 @@ if(YOLOX_USE_TENSORRT) find_library(NVPARSERS NAMES nvparsers) find_library(NVONNXPARSER NAMES nvonnxparser) find_library(NVONNXPARSERRUNTIME NAMES nvonnxparser_runtime) + if(NOT CUDA_FOUND) + message(WARNING " CUDA ${CUDA_FOUND}") + endif() + if(NOT NVINFER) + message(WARNING " NVINFER ${NVINFER}") + endif() + if(NOT NVINFERPLUGIN) + message(WARNING " NVINFERPLUGIN ${NVINFERPLUGIN}") + endif() + if(NOT NVPARSERS) + message(WARNING " NVPARSERS ${NVPARSERS}") + endif() + if(NOT NVONNXPARSER) + message(WARNING " NVONNXPARSER ${NVONNXPARSER}") + endif() + # message(WARNING " NVONNXPARSERRUNTIME ${NVONNXPARSERRUNTIME}") #not use if( CUDA_FOUND AND NVINFER AND NVINFERPLUGIN AND NVPARSERS AND NVONNXPARSER ) set(ENABLE_TENSORRT ON) set(SRC ${SRC} src/yolox_tensorrt.cpp) endif() endif() +if(YOLOX_USE_ONNXRUNTIME) + find_library(ONNXRUNTIME NAMES onnxruntime) + if(NOT ONNXRUNTIME) + message(WARNING " ONNXRUNTIME ${ONNXRUNTIME}") + endif() + if(ONNXRUNTIME) + set(ENABLE_ONNXRUNTIME ON) + set(SRC ${SRC} src/yolox_onnxruntime.cpp) + endif() +endif() message(STATUS " ENABLE_OPENVINO: ${ENABLE_OPENVINO}") message(STATUS " ENABLE_TENSORRT: ${ENABLE_TENSORRT}") +message(STATUS " ENABLE_ONNXRUNTIME: ${ENABLE_ONNXRUNTIME}") -if(NOT ENABLE_OPENVINO AND NOT ENABLE_TENSORRT) - message(WARNING "skip building yolox_cpp, no OpenVINO and TensorRT found") +if(NOT ENABLE_OPENVINO AND NOT ENABLE_TENSORRT AND NOT ENABLE_ONNXRUNTIME) + message(WARNING "skip building yolox_cpp, no OpenVINO, TensorRT and ONNXRuntime found") return() endif() @@ -96,6 +127,12 @@ if(ENABLE_TENSORRT) CUDA ) endif() +if(ENABLE_ONNXRUNTIME) + target_link_libraries(yolox_cpp + onnxruntime + ) +endif() + if(NOT WIN32) diff --git a/yolox_ros_cpp/yolox_cpp/include/yolox_cpp/config.h.in b/yolox_ros_cpp/yolox_cpp/include/yolox_cpp/config.h.in index 59ec1e8..ae5276e 100644 --- a/yolox_ros_cpp/yolox_cpp/include/yolox_cpp/config.h.in +++ b/yolox_ros_cpp/yolox_cpp/include/yolox_cpp/config.h.in @@ -3,5 +3,6 @@ #cmakedefine ENABLE_OPENVINO #cmakedefine ENABLE_TENSORRT +#cmakedefine ENABLE_ONNXRUNTIME -#endif // _YOLOX_CPP_CONFIG_H_ \ No newline at end of file +#endif // _YOLOX_CPP_CONFIG_H_ diff --git a/yolox_ros_cpp/yolox_cpp/include/yolox_cpp/yolox.hpp b/yolox_ros_cpp/yolox_cpp/include/yolox_cpp/yolox.hpp index 188c58b..d9865e2 100644 --- a/yolox_ros_cpp/yolox_cpp/include/yolox_cpp/yolox.hpp +++ b/yolox_ros_cpp/yolox_cpp/include/yolox_cpp/yolox.hpp @@ -11,5 +11,9 @@ #include "yolox_tensorrt.hpp" #endif +#ifdef ENABLE_ONNXRUNTIME + #include "yolox_onnxruntime.hpp" +#endif + -#endif \ No newline at end of file +#endif diff --git a/yolox_ros_cpp/yolox_cpp/include/yolox_cpp/yolox_onnxruntime.hpp b/yolox_ros_cpp/yolox_cpp/include/yolox_cpp/yolox_onnxruntime.hpp new file mode 100644 index 0000000..73ae0c5 --- /dev/null +++ b/yolox_ros_cpp/yolox_cpp/include/yolox_cpp/yolox_onnxruntime.hpp @@ -0,0 +1,43 @@ +#ifndef _YOLOX_CPP_YOLOX_ONNX_HPP +#define _YOLOX_CPP_YOLOX_ONNX_HPP + +#include +#include +#include +#include +#include +#include + +#include "onnxruntime/core/session/onnxruntime_cxx_api.h" + +#include "core.hpp" +#include "coco_names.hpp" + +namespace yolox_cpp{ + class YoloXONNXRuntime: public AbcYoloX{ + public: + YoloXONNXRuntime(file_name_t path_to_model, + int intra_num_threads, int inter_num_threadsint=1, + std::string device="cuda", int device_id=0, + float nms_th=0.45, float conf_th=0.3, std::string model_version="0.1.1rc0"); + std::vector inference(const cv::Mat& frame) override; + + private: + int inter_num_threads_ = 1; + int intra_num_threads_ = 1; + int device_id_ = 0; + std::string device_; + + Ort::Session session_{nullptr}; + Ort::Env env_{ORT_LOGGING_LEVEL_WARNING, "Default"}; + + Ort::Value input_tensor_{nullptr}; + Ort::Value output_tensor_{nullptr}; + std::string input_name_; + std::string output_name_; + std::vector> input_buffer_; + std::vector> output_buffer_; + }; +} + +#endif diff --git a/yolox_ros_cpp/yolox_cpp/src/yolox_onnxruntime.cpp b/yolox_ros_cpp/yolox_cpp/src/yolox_onnxruntime.cpp new file mode 100644 index 0000000..b5f6a3f --- /dev/null +++ b/yolox_ros_cpp/yolox_cpp/src/yolox_onnxruntime.cpp @@ -0,0 +1,136 @@ +#include "yolox_cpp/yolox_onnxruntime.hpp" + +namespace yolox_cpp{ + + YoloXONNXRuntime::YoloXONNXRuntime(file_name_t path_to_model, + int intra_num_threads, int inter_num_threads, + std::string device, int device_id, + float nms_th, float conf_th, std::string model_version) + :AbcYoloX(nms_th, conf_th, model_version), + intra_num_threads_(intra_num_threads), inter_num_threads_(inter_num_threads), + device_(device), device_id_(device_id) + { + try + { + Ort::SessionOptions session_options; + // controls whether the operators in the graph run sequentially or in parallel. + // Usually when a model has many branches, setting this option to false will provide better performance. + session_options.SetExecutionMode(ExecutionMode::ORT_SEQUENTIAL); + + // // [ Not Support CUDA Execution Provider ] + // // you can set session_options.SetInterOpNumThreads to control + // // the number of threads used to parallelize the execution of the graph (across nodes). + // session_options.SetExecutionMode(ExecutionMode::ORT_PARALLEL); + + session_options.SetInterOpNumThreads(this->inter_num_threads_); + session_options.SetIntraOpNumThreads(this->intra_num_threads_); + + if(this->device_ == "cuda") + { + OrtCUDAProviderOptions cuda_option; + cuda_option.device_id = this->device_id_; + session_options.AppendExecutionProvider_CUDA(cuda_option); + } + + this->session_ = Ort::Session(this->env_, + path_to_model.c_str(), + session_options); + } + catch (std::exception &e) + { + std::cerr << e.what() << std::endl; + throw e; + } + + Ort::AllocatorWithDefaultOptions ort_alloc; + + // Allocate input memory buffer + std::cout << "input:" << std::endl; + this->input_name_ = this->session_.GetInputName(0, ort_alloc); + std::cout << " name: " << this->input_name_ << std::endl; + auto input_info = this->session_.GetInputTypeInfo(0); + auto input_shape_info = input_info.GetTensorTypeAndShapeInfo(); + std::vector input_shape = input_shape_info.GetShape(); + ONNXTensorElementDataType input_tensor_type = input_shape_info.GetElementType(); + this->input_h_ = input_shape[2]; + this->input_w_ = input_shape[3]; + + std::cout << " shape:" << std::endl; + for (size_t i = 0; i < input_shape.size(); i++) + { + std::cout << " - " << input_shape[i] << std::endl; + } + std::cout << " tensor_type: " << input_tensor_type << std::endl; + + size_t input_byte_count = sizeof(float) * input_shape_info.GetElementCount(); + std::unique_ptr input_buffer = std::make_unique(input_byte_count); + // auto input_memory_info = Ort::MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeDefault); + auto input_memory_info = Ort::MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeCPU); + + this->input_tensor_ = Ort::Value::CreateTensor(input_memory_info, + input_buffer.get(), input_byte_count, + input_shape.data(), input_shape.size(), + input_tensor_type); + this->input_buffer_.emplace_back(std::move(input_buffer)); + + // Allocate output memory buffer + std::cout << "outputs" << std::endl; + this->output_name_ = this->session_.GetOutputName(0, ort_alloc); + std::cout << " name: " << this->output_name_ << std::endl; + + auto output_info = this->session_.GetOutputTypeInfo(0); + auto output_shape_info = output_info.GetTensorTypeAndShapeInfo(); + auto output_shape = output_shape_info.GetShape(); + auto output_tensor_type = output_shape_info.GetElementType(); + + std::cout << " shape:" << std::endl; + for (size_t i = 0; i < output_shape.size(); i++) + { + std::cout << " - " << output_shape[i] << std::endl; + } + std::cout << " tensor_type: " << output_tensor_type << std::endl; + + size_t output_byte_count = sizeof(float) * output_shape_info.GetElementCount(); + std::unique_ptr output_buffer = std::make_unique(output_byte_count); + // auto output_memory_info = Ort::MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeDefault); + auto output_memory_info = Ort::MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeCPU); + + this->output_tensor_ = Ort::Value::CreateTensor(output_memory_info, + output_buffer.get(), output_byte_count, + output_shape.data(), output_shape.size(), + output_tensor_type); + this->output_buffer_.emplace_back(std::move(output_buffer)); + + // Prepare GridAndStrides + generate_grids_and_stride(this->input_w_, this->input_h_, this->strides_, this->grid_strides_); + } + + std::vector YoloXONNXRuntime::inference(const cv::Mat& frame) + { + // preprocess + cv::Mat pr_img = static_resize(frame); + + float *blob_data = (float *)(this->input_buffer_[0].get()); + blobFromImage(pr_img, blob_data); + + const char* input_names_[] = {this->input_name_.c_str()}; + const char* output_names_[] = {this->output_name_.c_str()}; + + // Inference + Ort::RunOptions run_options; + this->session_.Run(run_options, + input_names_, + &this->input_tensor_, 1, + output_names_, + &this->output_tensor_, 1); + + float* net_pred = (float *)this->output_buffer_[0].get(); + + // post process + float scale = std::min(input_w_ / (frame.cols*1.0), input_h_ / (frame.rows*1.0)); + std::vector objects; + decode_outputs(net_pred, this->grid_strides_, objects, this->bbox_conf_thresh_, scale, frame.cols, frame.rows); + return objects; + } + +} diff --git a/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_onnxruntime.launch.py b/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_onnxruntime.launch.py new file mode 100644 index 0000000..34aa4e9 --- /dev/null +++ b/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_onnxruntime.launch.py @@ -0,0 +1,108 @@ +import os +import sys +import launch +import launch_ros.actions +from launch.actions import DeclareLaunchArgument +from launch.substitutions import LaunchConfiguration +from launch_ros.actions import ComposableNodeContainer +from launch_ros.descriptions import ComposableNode + +def generate_launch_description(): + launch_args = [ + DeclareLaunchArgument( + "video_device", + default_value="/dev/video0", + description="input video source" + ), + DeclareLaunchArgument( + "model_path", + default_value="./install/yolox_ros_cpp/share/yolox_ros_cpp/weights/onnx/yolox_nano.onnx", + description="yolox model path." + ), + DeclareLaunchArgument( + "model_version", + default_value="0.1.1rc0", + description="yolox model version." + ), + DeclareLaunchArgument( + "device", + default_value="CPU", + description="model device. CPU, GPU, MYRIAD, etc..." + ), + DeclareLaunchArgument( + "conf", + default_value="0.30", + description="yolox confidence threshold." + ), + DeclareLaunchArgument( + "nms", + default_value="0.45", + description="yolox nms threshold" + ), + DeclareLaunchArgument( + "imshow_isshow", + default_value="true", + description="" + ), + DeclareLaunchArgument( + "src_image_topic_name", + default_value="/image_raw", + description="topic name for source image" + ), + DeclareLaunchArgument( + "publish_image_topic_name", + default_value="/yolox/image_raw", + description="topic name for publishing image with bounding box drawn" + ), + DeclareLaunchArgument( + "publish_boundingbox_topic_name", + default_value="/yolox/bounding_boxes", + description="topic name for publishing bounding box message." + ), + ] + container = ComposableNodeContainer( + name='yolox_container', + namespace='', + package='rclcpp_components', + executable='component_container', + composable_node_descriptions=[ + ComposableNode( + package='v4l2_camera', + plugin='v4l2_camera::V4L2Camera', + name='v4l2_camera', + parameters=[{ + "video_device": LaunchConfiguration("video_device"), + "image_size": [640,480] + }]), + ComposableNode( + package='yolox_ros_cpp', + plugin='yolox_ros_cpp::YoloXNode', + name='yolox_ros_cpp', + parameters=[{ + "model_path": LaunchConfiguration("model_path"), + "model_type": "onnxruntime", + "model_version": LaunchConfiguration("model_version"), + "device": LaunchConfiguration("device"), + "conf": LaunchConfiguration("conf"), + "nms": LaunchConfiguration("nms"), + "imshow_isshow": LaunchConfiguration("imshow_isshow"), + "src_image_topic_name": LaunchConfiguration("src_image_topic_name"), + "publish_image_topic_name": LaunchConfiguration("publish_image_topic_name"), + "publish_boundingbox_topic_name": LaunchConfiguration("publish_boundingbox_topic_name"), + }], + ), + ], + output='screen', + ) + + rqt = launch_ros.actions.Node( + package="rqt_graph", executable="rqt_graph", + ) + + return launch.LaunchDescription( + launch_args + + [ + container, + # rqt_graph, + ] + ) diff --git a/yolox_ros_cpp/yolox_ros_cpp/src/yolox_ros_cpp.cpp b/yolox_ros_cpp/yolox_ros_cpp/src/yolox_ros_cpp.cpp index faccc04..c4f74f3 100644 --- a/yolox_ros_cpp/yolox_ros_cpp/src/yolox_ros_cpp.cpp +++ b/yolox_ros_cpp/yolox_ros_cpp/src/yolox_ros_cpp.cpp @@ -38,7 +38,18 @@ namespace yolox_ros_cpp{ RCLCPP_ERROR(this->get_logger(), "yolox_cpp is not built with OpenVINO"); rclcpp::shutdown(); #endif + }else if(this->model_type_ == "onnxruntime"){ + #ifdef ENABLE_ONNXRUNTIME + RCLCPP_INFO(this->get_logger(), "Model Type is ONNXRuntime"); + this->yolox_ = std::make_unique(this->model_path_, 1, 1);//, + // this->device_, 0 + // this->nms_th_, this->conf_th_, this->model_version_); + #else + RCLCPP_ERROR(this->get_logger(), "yolox_cpp is not built with ONNXRuntime"); + rclcpp::shutdown(); + #endif } + RCLCPP_INFO(this->get_logger(), "model loaded"); this->sub_image_ = image_transport::create_subscription( this, this->src_image_topic_name_, From 766106d0865915db9aea0f65afc44900d36d88ad Mon Sep 17 00:00:00 2001 From: fateshelled <53618876+fateshelled@users.noreply.github.com> Date: Tue, 27 Sep 2022 22:19:34 +0900 Subject: [PATCH 02/14] update node/launch parameter --- .../include/yolox_cpp/yolox_onnxruntime.hpp | 10 +++--- .../yolox_cpp/src/yolox_onnxruntime.cpp | 14 ++++---- .../include/yolox_ros_cpp/yolox_ros_cpp.hpp | 11 ++++--- .../launch/yolox_onnxruntime.launch.py | 26 ++++++++++++--- .../launch/yolox_openvino.launch.py | 4 +-- .../launch/yolox_openvino_ncs2.launch.py | 2 +- .../launch/yolox_tensorrt.launch.py | 10 +++--- .../launch/yolox_tensorrt_jetson.launch.py | 4 +-- .../yolox_ros_cpp/src/yolox_ros_cpp.cpp | 32 ++++++++++++++----- 9 files changed, 75 insertions(+), 38 deletions(-) diff --git a/yolox_ros_cpp/yolox_cpp/include/yolox_cpp/yolox_onnxruntime.hpp b/yolox_ros_cpp/yolox_cpp/include/yolox_cpp/yolox_onnxruntime.hpp index 73ae0c5..147c5c7 100644 --- a/yolox_ros_cpp/yolox_cpp/include/yolox_cpp/yolox_onnxruntime.hpp +++ b/yolox_ros_cpp/yolox_cpp/include/yolox_cpp/yolox_onnxruntime.hpp @@ -17,16 +17,16 @@ namespace yolox_cpp{ class YoloXONNXRuntime: public AbcYoloX{ public: YoloXONNXRuntime(file_name_t path_to_model, - int intra_num_threads, int inter_num_threadsint=1, - std::string device="cuda", int device_id=0, + int intra_op_num_threads, int inter_op_num_threads=1, + bool use_cuda=true, int device_id=0, float nms_th=0.45, float conf_th=0.3, std::string model_version="0.1.1rc0"); std::vector inference(const cv::Mat& frame) override; private: - int inter_num_threads_ = 1; - int intra_num_threads_ = 1; + int intra_op_num_threads_ = 1; + int inter_op_num_threads_ = 1; int device_id_ = 0; - std::string device_; + bool use_cuda_ = true; Ort::Session session_{nullptr}; Ort::Env env_{ORT_LOGGING_LEVEL_WARNING, "Default"}; diff --git a/yolox_ros_cpp/yolox_cpp/src/yolox_onnxruntime.cpp b/yolox_ros_cpp/yolox_cpp/src/yolox_onnxruntime.cpp index b5f6a3f..9213b37 100644 --- a/yolox_ros_cpp/yolox_cpp/src/yolox_onnxruntime.cpp +++ b/yolox_ros_cpp/yolox_cpp/src/yolox_onnxruntime.cpp @@ -3,12 +3,12 @@ namespace yolox_cpp{ YoloXONNXRuntime::YoloXONNXRuntime(file_name_t path_to_model, - int intra_num_threads, int inter_num_threads, - std::string device, int device_id, + int intra_op_num_threads, int inter_op_num_threads, + bool use_cuda, int device_id, float nms_th, float conf_th, std::string model_version) :AbcYoloX(nms_th, conf_th, model_version), - intra_num_threads_(intra_num_threads), inter_num_threads_(inter_num_threads), - device_(device), device_id_(device_id) + intra_op_num_threads_(intra_op_num_threads), inter_op_num_threads_(inter_op_num_threads), + use_cuda_(use_cuda), device_id_(device_id) { try { @@ -22,10 +22,10 @@ namespace yolox_cpp{ // // the number of threads used to parallelize the execution of the graph (across nodes). // session_options.SetExecutionMode(ExecutionMode::ORT_PARALLEL); - session_options.SetInterOpNumThreads(this->inter_num_threads_); - session_options.SetIntraOpNumThreads(this->intra_num_threads_); + session_options.SetInterOpNumThreads(this->inter_op_num_threads_); + session_options.SetIntraOpNumThreads(this->intra_op_num_threads_); - if(this->device_ == "cuda") + if(this->use_cuda_) { OrtCUDAProviderOptions cuda_option; cuda_option.device_id = this->device_id_; diff --git a/yolox_ros_cpp/yolox_ros_cpp/include/yolox_ros_cpp/yolox_ros_cpp.hpp b/yolox_ros_cpp/yolox_ros_cpp/include/yolox_ros_cpp/yolox_ros_cpp.hpp index e7fd3eb..2c9b0a4 100644 --- a/yolox_ros_cpp/yolox_ros_cpp/include/yolox_ros_cpp/yolox_ros_cpp.hpp +++ b/yolox_ros_cpp/yolox_ros_cpp/include/yolox_ros_cpp/yolox_ros_cpp.hpp @@ -30,11 +30,14 @@ namespace yolox_ros_cpp{ std::string model_path_; std::string model_type_; std::string model_version_; - std::string device_; + int tensorrt_device_; + std::string openvino_device_; + bool onnxruntime_use_cuda_; + int onnxruntime_device_id_; + int onnxruntime_intra_op_num_threads_; + int onnxruntime_inter_op_num_threads_; float conf_th_; float nms_th_; - int image_width_; - int image_height_; std::string src_image_topic_name_; std::string publish_image_topic_name_; @@ -52,4 +55,4 @@ namespace yolox_ros_cpp{ bool imshow_ = true; }; } -#endif \ No newline at end of file +#endif diff --git a/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_onnxruntime.launch.py b/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_onnxruntime.launch.py index 34aa4e9..9ed121e 100644 --- a/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_onnxruntime.launch.py +++ b/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_onnxruntime.launch.py @@ -25,9 +25,24 @@ def generate_launch_description(): description="yolox model version." ), DeclareLaunchArgument( - "device", - default_value="CPU", - description="model device. CPU, GPU, MYRIAD, etc..." + "onnxruntime/use_cuda", + default_value="true", + description="onnxruntime use cuda." + ), + DeclareLaunchArgument( + "onnxruntime/device_id", + default_value="0", + description="onnxruntime gpu device id." + ), + DeclareLaunchArgument( + "onnxruntime/inter_op_num_threads", + default_value="2", + description="onnxruntime inter_op_num_threads." + ), + DeclareLaunchArgument( + "onnxruntime/intra_op_num_threads", + default_value="1", + description="onnxruntime intra_op_num_threads." ), DeclareLaunchArgument( "conf", @@ -82,7 +97,10 @@ def generate_launch_description(): "model_path": LaunchConfiguration("model_path"), "model_type": "onnxruntime", "model_version": LaunchConfiguration("model_version"), - "device": LaunchConfiguration("device"), + "onnxruntime/use_cuda": LaunchConfiguration("onnxruntime/use_cuda"), + "onnxruntime/device_id": LaunchConfiguration("onnxruntime/device_id"), + "onnxruntime/inter_op_num_threads": LaunchConfiguration("onnxruntime/inter_op_num_threads"), + "onnxruntime/intra_op_num_threads": LaunchConfiguration("onnxruntime/intra_op_num_threads"), "conf": LaunchConfiguration("conf"), "nms": LaunchConfiguration("nms"), "imshow_isshow": LaunchConfiguration("imshow_isshow"), diff --git a/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_openvino.launch.py b/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_openvino.launch.py index 376b724..24ee170 100644 --- a/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_openvino.launch.py +++ b/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_openvino.launch.py @@ -25,7 +25,7 @@ def generate_launch_description(): description="yolox model version." ), DeclareLaunchArgument( - "device", + "openvino/device", default_value="CPU", description="model device. CPU, GPU, MYRIAD, etc..." ), @@ -82,7 +82,7 @@ def generate_launch_description(): "model_path": LaunchConfiguration("model_path"), "model_type": "openvino", "model_version": LaunchConfiguration("model_version"), - "device": LaunchConfiguration("device"), + "openvino/device": LaunchConfiguration("openvino/device"), "conf": LaunchConfiguration("conf"), "nms": LaunchConfiguration("nms"), "imshow_isshow": LaunchConfiguration("imshow_isshow"), diff --git a/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_openvino_ncs2.launch.py b/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_openvino_ncs2.launch.py index cb8ce20..1cc9577 100644 --- a/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_openvino_ncs2.launch.py +++ b/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_openvino_ncs2.launch.py @@ -77,7 +77,7 @@ def generate_launch_description(): "model_path": LaunchConfiguration("model_path"), "model_type": "openvino", "model_version": LaunchConfiguration("model_version"), - "device": "MYRIAD", + "openvino/device": "MYRIAD", "conf": LaunchConfiguration("conf"), "nms": LaunchConfiguration("nms"), "imshow_isshow": LaunchConfiguration("imshow_isshow"), diff --git a/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_tensorrt.launch.py b/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_tensorrt.launch.py index 37220c6..50800a5 100644 --- a/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_tensorrt.launch.py +++ b/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_tensorrt.launch.py @@ -21,8 +21,8 @@ def generate_launch_description(): description="yolox model path." ), DeclareLaunchArgument( - "device", - default_value="'0'", + "tensorrt/device", + default_value="0", description="GPU index. Set in string type. ex '0'" ), DeclareLaunchArgument( @@ -84,7 +84,7 @@ def generate_launch_description(): "model_path": LaunchConfiguration("model_path"), "model_type": "tensorrt", "model_version": LaunchConfiguration("model_version"), - "device": LaunchConfiguration("device"), + "tensorrt/device": LaunchConfiguration("device"), "conf": LaunchConfiguration("conf"), "nms": LaunchConfiguration("nms"), "imshow_isshow": LaunchConfiguration("imshow_isshow"), @@ -102,9 +102,9 @@ def generate_launch_description(): ) return launch.LaunchDescription( - launch_args + + launch_args + [ container, # rqt ] - ) \ No newline at end of file + ) diff --git a/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_tensorrt_jetson.launch.py b/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_tensorrt_jetson.launch.py index f0cd2e7..57a7e41 100644 --- a/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_tensorrt_jetson.launch.py +++ b/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_tensorrt_jetson.launch.py @@ -79,7 +79,7 @@ def generate_launch_description(): "model_path": LaunchConfiguration("model_path"), "model_type": "tensorrt", "model_version": LaunchConfiguration("model_version"), - "device": "0", + "tensorrt/device": "0", "conf": LaunchConfiguration("conf"), "nms": LaunchConfiguration("nms"), "imshow_isshow": LaunchConfiguration("imshow_isshow"), @@ -94,4 +94,4 @@ def generate_launch_description(): return launch.LaunchDescription( launch_args + [container] - ) \ No newline at end of file + ) diff --git a/yolox_ros_cpp/yolox_ros_cpp/src/yolox_ros_cpp.cpp b/yolox_ros_cpp/yolox_ros_cpp/src/yolox_ros_cpp.cpp index c4f74f3..395c186 100644 --- a/yolox_ros_cpp/yolox_ros_cpp/src/yolox_ros_cpp.cpp +++ b/yolox_ros_cpp/yolox_ros_cpp/src/yolox_ros_cpp.cpp @@ -23,7 +23,7 @@ namespace yolox_ros_cpp{ if(this->model_type_ == "tensorrt"){ #ifdef ENABLE_TENSORRT RCLCPP_INFO(this->get_logger(), "Model Type is TensorRT"); - this->yolox_ = std::make_unique(this->model_path_, std::stoi(this->device_), + this->yolox_ = std::make_unique(this->model_path_, this->tenorrt_device_, this->nms_th_, this->conf_th_, this->model_version_); #else RCLCPP_ERROR(this->get_logger(), "yolox_cpp is not built with TensorRT"); @@ -32,7 +32,7 @@ namespace yolox_ros_cpp{ }else if(this->model_type_ == "openvino"){ #ifdef ENABLE_OPENVINO RCLCPP_INFO(this->get_logger(), "Model Type is OpenVINO"); - this->yolox_ = std::make_unique(this->model_path_, this->device_, + this->yolox_ = std::make_unique(this->model_path_, this->openvino_device_, this->nms_th_, this->conf_th_, this->model_version_); #else RCLCPP_ERROR(this->get_logger(), "yolox_cpp is not built with OpenVINO"); @@ -41,9 +41,12 @@ namespace yolox_ros_cpp{ }else if(this->model_type_ == "onnxruntime"){ #ifdef ENABLE_ONNXRUNTIME RCLCPP_INFO(this->get_logger(), "Model Type is ONNXRuntime"); - this->yolox_ = std::make_unique(this->model_path_, 1, 1);//, - // this->device_, 0 - // this->nms_th_, this->conf_th_, this->model_version_); + this->yolox_ = std::make_unique(this->model_path_, + this->onnxruntime_intra_op_num_threads_, + this->onnxruntime_inter_op_num_threads_, + this->onnxruntime_use_cuda_, this->onnxruntime_device_id_, + this->nms_th_, this->conf_th_, this->model_version_ + ); #else RCLCPP_ERROR(this->get_logger(), "yolox_cpp is not built with ONNXRuntime"); rclcpp::shutdown(); @@ -69,7 +72,12 @@ namespace yolox_ros_cpp{ this->declare_parameter("model_path", "src/YOLOX-ROS/weights/openvino/yolox_tiny.xml"); this->declare_parameter("conf", 0.3f); this->declare_parameter("nms", 0.45f); - this->declare_parameter("device", "CPU"); + this->declare_parameter("tensorrt/device", 0); + this->declare_parameter("openvino/device", "CPU"); + this->declare_parameter("onnxruntime/use_cuda", true); + this->declare_parameter("onnxruntime/device_id", 0); + this->declare_parameter("onnxruntime/inter_op_num_threads", 1); + this->declare_parameter("onnxruntime/intra_op_num_threads", 1); this->declare_parameter("model_type", "openvino"); this->declare_parameter("model_version", "0.1.1rc0"); this->declare_parameter("src_image_topic_name", "image_raw"); @@ -80,7 +88,12 @@ namespace yolox_ros_cpp{ this->get_parameter("model_path", this->model_path_); this->get_parameter("conf", this->conf_th_); this->get_parameter("nms", this->nms_th_); - this->get_parameter("device", this->device_); + this->get_parameter("tensorrt/device", this->tensorrt_device_); + this->get_parameter("openvino/device", this->openvino_device_); + this->get_parameter("onnxruntime/use_cuda", this->onnxruntime_use_cuda_); + this->get_parameter("onnxruntime/device_id", this->onnxruntime_device_id_); + this->get_parameter("onnxruntime/inter_op_num_threads", this->onnxruntime_inter_op_num_threads_); + this->get_parameter("onnxruntime/intra_op_num_threads", this->onnxruntime_intra_op_num_threads_); this->get_parameter("model_type", this->model_type_); this->get_parameter("model_version", this->model_version_); this->get_parameter("src_image_topic_name", this->src_image_topic_name_); @@ -91,7 +104,10 @@ namespace yolox_ros_cpp{ RCLCPP_INFO(this->get_logger(), "Set parameter model_path: '%s'", this->model_path_.c_str()); RCLCPP_INFO(this->get_logger(), "Set parameter conf: %f", this->conf_th_); RCLCPP_INFO(this->get_logger(), "Set parameter nms: %f", this->nms_th_); - RCLCPP_INFO(this->get_logger(), "Set parameter device: %s", this->device_.c_str()); + RCLCPP_INFO(this->get_logger(), "Set parameter tensorrt/device: %i", this->tensorrt_device_); + RCLCPP_INFO(this->get_logger(), "Set parameter openvino/device: %s", this->openvino_device_.c_str()); + RCLCPP_INFO(this->get_logger(), "Set parameter onnxruntime/use_cuda: %i", this->onnxruntime_use_cuda_); + RCLCPP_INFO(this->get_logger(), "Set parameter onnxruntime/device_id: %i", this->onnxruntime_device_id_); RCLCPP_INFO(this->get_logger(), "Set parameter model_type: '%s'", this->model_type_.c_str()); RCLCPP_INFO(this->get_logger(), "Set parameter model_version: '%s'", this->model_version_.c_str()); RCLCPP_INFO(this->get_logger(), "Set parameter src_image_topic_name: '%s'", this->src_image_topic_name_.c_str()); From 7d35a0393b9efe9d638a8e3cdd97c9ba857faebe Mon Sep 17 00:00:00 2001 From: fateshelled <53618876+fateshelled@users.noreply.github.com> Date: Tue, 27 Sep 2022 22:28:37 +0900 Subject: [PATCH 03/14] fix typo --- yolox_ros_cpp/yolox_ros_cpp/src/yolox_ros_cpp.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/yolox_ros_cpp/yolox_ros_cpp/src/yolox_ros_cpp.cpp b/yolox_ros_cpp/yolox_ros_cpp/src/yolox_ros_cpp.cpp index 395c186..396e1b3 100644 --- a/yolox_ros_cpp/yolox_ros_cpp/src/yolox_ros_cpp.cpp +++ b/yolox_ros_cpp/yolox_ros_cpp/src/yolox_ros_cpp.cpp @@ -23,7 +23,7 @@ namespace yolox_ros_cpp{ if(this->model_type_ == "tensorrt"){ #ifdef ENABLE_TENSORRT RCLCPP_INFO(this->get_logger(), "Model Type is TensorRT"); - this->yolox_ = std::make_unique(this->model_path_, this->tenorrt_device_, + this->yolox_ = std::make_unique(this->model_path_, this->tensorrt_device_, this->nms_th_, this->conf_th_, this->model_version_); #else RCLCPP_ERROR(this->get_logger(), "yolox_cpp is not built with TensorRT"); From 91c0743090ca2cd556d1f383564838bae97653da Mon Sep 17 00:00:00 2001 From: fateshelled <53618876+fateshelled@users.noreply.github.com> Date: Tue, 27 Sep 2022 22:30:38 +0900 Subject: [PATCH 04/14] fix parameter --- yolox_ros_cpp/yolox_ros_cpp/launch/yolox_tensorrt.launch.py | 2 +- .../yolox_ros_cpp/launch/yolox_tensorrt_jetson.launch.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_tensorrt.launch.py b/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_tensorrt.launch.py index 50800a5..2dbbbe3 100644 --- a/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_tensorrt.launch.py +++ b/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_tensorrt.launch.py @@ -84,7 +84,7 @@ def generate_launch_description(): "model_path": LaunchConfiguration("model_path"), "model_type": "tensorrt", "model_version": LaunchConfiguration("model_version"), - "tensorrt/device": LaunchConfiguration("device"), + "tensorrt/device": LaunchConfiguration("tensorrt/device"), "conf": LaunchConfiguration("conf"), "nms": LaunchConfiguration("nms"), "imshow_isshow": LaunchConfiguration("imshow_isshow"), diff --git a/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_tensorrt_jetson.launch.py b/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_tensorrt_jetson.launch.py index 57a7e41..9f8d2fa 100644 --- a/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_tensorrt_jetson.launch.py +++ b/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_tensorrt_jetson.launch.py @@ -79,7 +79,7 @@ def generate_launch_description(): "model_path": LaunchConfiguration("model_path"), "model_type": "tensorrt", "model_version": LaunchConfiguration("model_version"), - "tensorrt/device": "0", + "tensorrt/device": 0, "conf": LaunchConfiguration("conf"), "nms": LaunchConfiguration("nms"), "imshow_isshow": LaunchConfiguration("imshow_isshow"), From d9ad80c6c730b1a9182992389e2fd1f71a30bc1e Mon Sep 17 00:00:00 2001 From: fateshelled <53618876+fateshelled@users.noreply.github.com> Date: Tue, 27 Sep 2022 23:05:00 +0900 Subject: [PATCH 05/14] update README.md --- yolox_ros_cpp/README.md | 39 ++++++++++++++++--- .../launch/yolox_onnxruntime.launch.py | 2 +- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/yolox_ros_cpp/README.md b/yolox_ros_cpp/README.md index 21b3b68..881503c 100644 --- a/yolox_ros_cpp/README.md +++ b/yolox_ros_cpp/README.md @@ -7,8 +7,9 @@ - OpenCV 4.x - OpenVINO 2021.* - TensorRT 8.x * +- ONNXRuntime * -※ Either one of OpenVINO or TensorRT is required. +※ Either one of OpenVINO or TensorRT or ONNXRuntime is required. ※ Model convert script is not supported OpenVINO 2022.* @@ -96,7 +97,7 @@ git clone --recursive https://github.com/fateshelled/YOLOX-ROS -b dev_cpp ``` -### Model Convert +### Model Convert or Download #### OpenVINO ```bash cd ~/ros2_ws @@ -114,6 +115,14 @@ cd ~/ros2_ws ./src/YOLOX-ROS/weights/tensorrt/convert.bash yolox_nano 16 ``` +#### ONNXRuntime +```bash +cd ~/ros2_ws + +# Download onnx model +./src/YOLOX-ROS/weights/onnx/download.bash yolox_nano +``` + #### PINTO_model_zoo - Support PINTO_model_zoo model - Download model using the following script. @@ -178,11 +187,29 @@ If you want to show image with bounding box drawn, subscribe from host jetson or ros2 launch yolox_ros_cpp yolox_tensorrt_jetson.launch.py ``` +#### ONNXRuntime +```bash +# run YOLOX_nano +ros2 launch yolox_ros_cpp yolox_onnxruntime.launch.py +``` + ### Parameter #### OpenVINO example - `model_path`: ./install/yolox_ros_cpp/share/yolox_ros_cpp/weights/openvino/yolox_nano.xml - `model_version`: 0.1.1rc0 -- `device`: CPU +- `openvino/device`: CPU +- `conf`: 0.3 +- `nms`: 0.45 +- `imshow_isshow`: true +- `src_image_topic_name`: image_raw +- `publish_image_topic_name`: yolox/image_raw +- `publish_boundingbox_topic_name`: yolox/bounding_boxes + + +#### TensorRT example. +- `model_path`: ./install/yolox_ros_cpp/share/yolox_ros_cpp/weights/tensorrt/yolox_nano.trt +- `model_version`: 0.1.1rc0 +- `tensorrt/device`: 0 - `conf`: 0.3 - `nms`: 0.45 - `imshow_isshow`: true @@ -194,7 +221,10 @@ ros2 launch yolox_ros_cpp yolox_tensorrt_jetson.launch.py #### TensorRT example. - `model_path`: ./install/yolox_ros_cpp/share/yolox_ros_cpp/weights/tensorrt/yolox_nano.trt - `model_version`: 0.1.1rc0 -- `device`: "0" +- `onnxruntime/use_cuda`: true +- `onnxruntime/device_id`: 0 +- `onnxruntime/inter_op_num_threads`: 1 +- `onnxruntime/intra_op_num_threads`: 1 - `conf`: 0.3 - `nms`: 0.45 - `imshow_isshow`: true @@ -202,7 +232,6 @@ ros2 launch yolox_ros_cpp yolox_tensorrt_jetson.launch.py - `publish_image_topic_name`: yolox/image_raw - `publish_boundingbox_topic_name`: yolox/bounding_boxes -`device` is GPU id. Must be specified as a `string` type. ### Reference Reference from YOLOX demo code. diff --git a/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_onnxruntime.launch.py b/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_onnxruntime.launch.py index 9ed121e..194bf2a 100644 --- a/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_onnxruntime.launch.py +++ b/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_onnxruntime.launch.py @@ -36,7 +36,7 @@ def generate_launch_description(): ), DeclareLaunchArgument( "onnxruntime/inter_op_num_threads", - default_value="2", + default_value="1", description="onnxruntime inter_op_num_threads." ), DeclareLaunchArgument( From 89562b7f18ef0bf6557147aab17b95dbae96c955 Mon Sep 17 00:00:00 2001 From: fateshelled <53618876+fateshelled@users.noreply.github.com> Date: Thu, 29 Sep 2022 21:17:42 +0900 Subject: [PATCH 06/14] update README.md --- yolox_ros_cpp/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/yolox_ros_cpp/README.md b/yolox_ros_cpp/README.md index 881503c..598c165 100644 --- a/yolox_ros_cpp/README.md +++ b/yolox_ros_cpp/README.md @@ -218,8 +218,8 @@ ros2 launch yolox_ros_cpp yolox_onnxruntime.launch.py - `publish_boundingbox_topic_name`: yolox/bounding_boxes -#### TensorRT example. -- `model_path`: ./install/yolox_ros_cpp/share/yolox_ros_cpp/weights/tensorrt/yolox_nano.trt +#### ONNXRuntime example. +- `model_path`: ./install/yolox_ros_cpp/share/yolox_ros_cpp/weights/onnx/yolox_nano.onnx - `model_version`: 0.1.1rc0 - `onnxruntime/use_cuda`: true - `onnxruntime/device_id`: 0 From 14ebdf631cf9a95a3a6d6a189ce0c98d2bd523d5 Mon Sep 17 00:00:00 2001 From: fateshelled <53618876+fateshelled@users.noreply.github.com> Date: Thu, 29 Sep 2022 21:45:39 +0900 Subject: [PATCH 07/14] add param use_parallel for onnxruntime --- yolox_ros_cpp/README.md | 3 +++ .../include/yolox_cpp/yolox_onnxruntime.hpp | 3 ++- .../yolox_cpp/src/yolox_onnxruntime.cpp | 23 ++++++++++--------- .../include/yolox_ros_cpp/yolox_ros_cpp.hpp | 1 + .../launch/yolox_onnxruntime.launch.py | 10 ++++++-- .../yolox_ros_cpp/src/yolox_ros_cpp.cpp | 4 ++++ 6 files changed, 30 insertions(+), 14 deletions(-) diff --git a/yolox_ros_cpp/README.md b/yolox_ros_cpp/README.md index 598c165..480a617 100644 --- a/yolox_ros_cpp/README.md +++ b/yolox_ros_cpp/README.md @@ -222,9 +222,12 @@ ros2 launch yolox_ros_cpp yolox_onnxruntime.launch.py - `model_path`: ./install/yolox_ros_cpp/share/yolox_ros_cpp/weights/onnx/yolox_nano.onnx - `model_version`: 0.1.1rc0 - `onnxruntime/use_cuda`: true +- `onnxruntime/use_parallel`: false - `onnxruntime/device_id`: 0 - `onnxruntime/inter_op_num_threads`: 1 + - if use_parallel is true, the number of threads used to parallelize the execution of the graph (across nodes). - `onnxruntime/intra_op_num_threads`: 1 + - the number of threads to use to run the model - `conf`: 0.3 - `nms`: 0.45 - `imshow_isshow`: true diff --git a/yolox_ros_cpp/yolox_cpp/include/yolox_cpp/yolox_onnxruntime.hpp b/yolox_ros_cpp/yolox_cpp/include/yolox_cpp/yolox_onnxruntime.hpp index 147c5c7..a7dfa6f 100644 --- a/yolox_ros_cpp/yolox_cpp/include/yolox_cpp/yolox_onnxruntime.hpp +++ b/yolox_ros_cpp/yolox_cpp/include/yolox_cpp/yolox_onnxruntime.hpp @@ -18,7 +18,7 @@ namespace yolox_cpp{ public: YoloXONNXRuntime(file_name_t path_to_model, int intra_op_num_threads, int inter_op_num_threads=1, - bool use_cuda=true, int device_id=0, + bool use_cuda=true, int device_id=0, bool use_parallel=false, float nms_th=0.45, float conf_th=0.3, std::string model_version="0.1.1rc0"); std::vector inference(const cv::Mat& frame) override; @@ -27,6 +27,7 @@ namespace yolox_cpp{ int inter_op_num_threads_ = 1; int device_id_ = 0; bool use_cuda_ = true; + bool use_parallel_ = false; Ort::Session session_{nullptr}; Ort::Env env_{ORT_LOGGING_LEVEL_WARNING, "Default"}; diff --git a/yolox_ros_cpp/yolox_cpp/src/yolox_onnxruntime.cpp b/yolox_ros_cpp/yolox_cpp/src/yolox_onnxruntime.cpp index 9213b37..0172332 100644 --- a/yolox_ros_cpp/yolox_cpp/src/yolox_onnxruntime.cpp +++ b/yolox_ros_cpp/yolox_cpp/src/yolox_onnxruntime.cpp @@ -4,25 +4,26 @@ namespace yolox_cpp{ YoloXONNXRuntime::YoloXONNXRuntime(file_name_t path_to_model, int intra_op_num_threads, int inter_op_num_threads, - bool use_cuda, int device_id, + bool use_cuda, int device_id, bool use_parallel, float nms_th, float conf_th, std::string model_version) :AbcYoloX(nms_th, conf_th, model_version), intra_op_num_threads_(intra_op_num_threads), inter_op_num_threads_(inter_op_num_threads), - use_cuda_(use_cuda), device_id_(device_id) + use_cuda_(use_cuda), device_id_(device_id), use_parallel_(use_parallel) { try { Ort::SessionOptions session_options; - // controls whether the operators in the graph run sequentially or in parallel. - // Usually when a model has many branches, setting this option to false will provide better performance. - session_options.SetExecutionMode(ExecutionMode::ORT_SEQUENTIAL); - // // [ Not Support CUDA Execution Provider ] - // // you can set session_options.SetInterOpNumThreads to control - // // the number of threads used to parallelize the execution of the graph (across nodes). - // session_options.SetExecutionMode(ExecutionMode::ORT_PARALLEL); - - session_options.SetInterOpNumThreads(this->inter_op_num_threads_); + session_options.SetGraphOptimizationLevel(GraphOptimizationLevel::ORT_ENABLE_ALL); + if(this->use_parallel_) + { + session_options.SetExecutionMode(ExecutionMode::ORT_PARALLEL); + session_options.SetInterOpNumThreads(this->inter_op_num_threads_); + } + else + { + session_options.SetExecutionMode(ExecutionMode::ORT_SEQUENTIAL); + } session_options.SetIntraOpNumThreads(this->intra_op_num_threads_); if(this->use_cuda_) diff --git a/yolox_ros_cpp/yolox_ros_cpp/include/yolox_ros_cpp/yolox_ros_cpp.hpp b/yolox_ros_cpp/yolox_ros_cpp/include/yolox_ros_cpp/yolox_ros_cpp.hpp index 2c9b0a4..44463c5 100644 --- a/yolox_ros_cpp/yolox_ros_cpp/include/yolox_ros_cpp/yolox_ros_cpp.hpp +++ b/yolox_ros_cpp/yolox_ros_cpp/include/yolox_ros_cpp/yolox_ros_cpp.hpp @@ -34,6 +34,7 @@ namespace yolox_ros_cpp{ std::string openvino_device_; bool onnxruntime_use_cuda_; int onnxruntime_device_id_; + bool onnxruntime_use_parallel_; int onnxruntime_intra_op_num_threads_; int onnxruntime_inter_op_num_threads_; float conf_th_; diff --git a/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_onnxruntime.launch.py b/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_onnxruntime.launch.py index 194bf2a..ec38250 100644 --- a/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_onnxruntime.launch.py +++ b/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_onnxruntime.launch.py @@ -34,15 +34,20 @@ def generate_launch_description(): default_value="0", description="onnxruntime gpu device id." ), + DeclareLaunchArgument( + "onnxruntime/use_parallel", + default_value="false", + description="if use_parallel is true, you can set inter_op_num_threads." + ), DeclareLaunchArgument( "onnxruntime/inter_op_num_threads", default_value="1", - description="onnxruntime inter_op_num_threads." + description="control the number of threads used to parallelize the execution of the graph (across nodes)." ), DeclareLaunchArgument( "onnxruntime/intra_op_num_threads", default_value="1", - description="onnxruntime intra_op_num_threads." + description="ontrols the number of threads to use to run the model." ), DeclareLaunchArgument( "conf", @@ -99,6 +104,7 @@ def generate_launch_description(): "model_version": LaunchConfiguration("model_version"), "onnxruntime/use_cuda": LaunchConfiguration("onnxruntime/use_cuda"), "onnxruntime/device_id": LaunchConfiguration("onnxruntime/device_id"), + "onnxruntime/use_parallel": LaunchConfiguration("onnxruntime/use_parallel"), "onnxruntime/inter_op_num_threads": LaunchConfiguration("onnxruntime/inter_op_num_threads"), "onnxruntime/intra_op_num_threads": LaunchConfiguration("onnxruntime/intra_op_num_threads"), "conf": LaunchConfiguration("conf"), diff --git a/yolox_ros_cpp/yolox_ros_cpp/src/yolox_ros_cpp.cpp b/yolox_ros_cpp/yolox_ros_cpp/src/yolox_ros_cpp.cpp index 396e1b3..73595ba 100644 --- a/yolox_ros_cpp/yolox_ros_cpp/src/yolox_ros_cpp.cpp +++ b/yolox_ros_cpp/yolox_ros_cpp/src/yolox_ros_cpp.cpp @@ -45,6 +45,7 @@ namespace yolox_ros_cpp{ this->onnxruntime_intra_op_num_threads_, this->onnxruntime_inter_op_num_threads_, this->onnxruntime_use_cuda_, this->onnxruntime_device_id_, + this->onnxruntime_use_parallel_, this->nms_th_, this->conf_th_, this->model_version_ ); #else @@ -76,6 +77,7 @@ namespace yolox_ros_cpp{ this->declare_parameter("openvino/device", "CPU"); this->declare_parameter("onnxruntime/use_cuda", true); this->declare_parameter("onnxruntime/device_id", 0); + this->declare_parameter("onnxruntime/use_parallel", false); this->declare_parameter("onnxruntime/inter_op_num_threads", 1); this->declare_parameter("onnxruntime/intra_op_num_threads", 1); this->declare_parameter("model_type", "openvino"); @@ -92,6 +94,7 @@ namespace yolox_ros_cpp{ this->get_parameter("openvino/device", this->openvino_device_); this->get_parameter("onnxruntime/use_cuda", this->onnxruntime_use_cuda_); this->get_parameter("onnxruntime/device_id", this->onnxruntime_device_id_); + this->get_parameter("onnxruntime/use_parallel", this->onnxruntime_use_parallel_); this->get_parameter("onnxruntime/inter_op_num_threads", this->onnxruntime_inter_op_num_threads_); this->get_parameter("onnxruntime/intra_op_num_threads", this->onnxruntime_intra_op_num_threads_); this->get_parameter("model_type", this->model_type_); @@ -108,6 +111,7 @@ namespace yolox_ros_cpp{ RCLCPP_INFO(this->get_logger(), "Set parameter openvino/device: %s", this->openvino_device_.c_str()); RCLCPP_INFO(this->get_logger(), "Set parameter onnxruntime/use_cuda: %i", this->onnxruntime_use_cuda_); RCLCPP_INFO(this->get_logger(), "Set parameter onnxruntime/device_id: %i", this->onnxruntime_device_id_); + RCLCPP_INFO(this->get_logger(), "Set parameter onnxruntime/use_parallel: %i", this->onnxruntime_use_parallel_); RCLCPP_INFO(this->get_logger(), "Set parameter model_type: '%s'", this->model_type_.c_str()); RCLCPP_INFO(this->get_logger(), "Set parameter model_version: '%s'", this->model_version_.c_str()); RCLCPP_INFO(this->get_logger(), "Set parameter src_image_topic_name: '%s'", this->src_image_topic_name_.c_str()); From 3c59c98128d3872b5a857b95bbad8a21d24ac3e1 Mon Sep 17 00:00:00 2001 From: fateshelled <53618876+fateshelled@users.noreply.github.com> Date: Thu, 29 Sep 2022 23:13:42 +0900 Subject: [PATCH 08/14] support custom class labels --- yolox_ros_cpp/README.md | 5 +- .../include/yolox_cpp/coco_names.hpp | 2 +- .../yolox_cpp/include/yolox_cpp/core.hpp | 10 ++- .../yolox_cpp/include/yolox_cpp/utils.hpp | 34 +++++--- .../include/yolox_cpp/yolox_onnxruntime.hpp | 3 +- .../include/yolox_cpp/yolox_openvino.hpp | 3 +- .../include/yolox_cpp/yolox_tensorrt.hpp | 3 +- .../yolox_cpp/src/yolox_onnxruntime.cpp | 5 +- .../yolox_cpp/src/yolox_openvino.cpp | 6 +- .../yolox_cpp/src/yolox_tensorrt.cpp | 5 +- yolox_ros_cpp/yolox_ros_cpp/CMakeLists.txt | 1 + .../include/yolox_ros_cpp/yolox_ros_cpp.hpp | 2 + .../yolox_ros_cpp/labels/coco_names.txt | 80 +++++++++++++++++++ .../launch/yolox_onnxruntime.launch.py | 6 ++ .../launch/yolox_openvino.launch.py | 6 ++ .../launch/yolox_openvino_ncs2.launch.py | 6 ++ .../launch/yolox_tensorrt.launch.py | 6 ++ .../launch/yolox_tensorrt_jetson.launch.py | 6 ++ .../yolox_ros_cpp/src/yolox_ros_cpp.cpp | 25 +++++- 19 files changed, 184 insertions(+), 30 deletions(-) create mode 100644 yolox_ros_cpp/yolox_ros_cpp/labels/coco_names.txt diff --git a/yolox_ros_cpp/README.md b/yolox_ros_cpp/README.md index 480a617..ece9941 100644 --- a/yolox_ros_cpp/README.md +++ b/yolox_ros_cpp/README.md @@ -196,6 +196,7 @@ ros2 launch yolox_ros_cpp yolox_onnxruntime.launch.py ### Parameter #### OpenVINO example - `model_path`: ./install/yolox_ros_cpp/share/yolox_ros_cpp/weights/openvino/yolox_nano.xml +- `class_labels_path`: '' - `model_version`: 0.1.1rc0 - `openvino/device`: CPU - `conf`: 0.3 @@ -208,6 +209,7 @@ ros2 launch yolox_ros_cpp yolox_onnxruntime.launch.py #### TensorRT example. - `model_path`: ./install/yolox_ros_cpp/share/yolox_ros_cpp/weights/tensorrt/yolox_nano.trt +- `class_labels_path`: '' - `model_version`: 0.1.1rc0 - `tensorrt/device`: 0 - `conf`: 0.3 @@ -220,12 +222,13 @@ ros2 launch yolox_ros_cpp yolox_onnxruntime.launch.py #### ONNXRuntime example. - `model_path`: ./install/yolox_ros_cpp/share/yolox_ros_cpp/weights/onnx/yolox_nano.onnx +- `class_labels_path`: '' - `model_version`: 0.1.1rc0 - `onnxruntime/use_cuda`: true - `onnxruntime/use_parallel`: false - `onnxruntime/device_id`: 0 - `onnxruntime/inter_op_num_threads`: 1 - - if use_parallel is true, the number of threads used to parallelize the execution of the graph (across nodes). + - if `onnxruntime/use_parallel` is true, the number of threads used to parallelize the execution of the graph (across nodes). - `onnxruntime/intra_op_num_threads`: 1 - the number of threads to use to run the model - `conf`: 0.3 diff --git a/yolox_ros_cpp/yolox_cpp/include/yolox_cpp/coco_names.hpp b/yolox_ros_cpp/yolox_cpp/include/yolox_cpp/coco_names.hpp index 6d40860..31cb6ec 100644 --- a/yolox_ros_cpp/yolox_cpp/include/yolox_cpp/coco_names.hpp +++ b/yolox_ros_cpp/yolox_cpp/include/yolox_cpp/coco_names.hpp @@ -2,7 +2,7 @@ #define _YOLOX_CPP_COCO_NAMES_HPP namespace yolox_cpp{ - static const char* COCO_CLASSES[] = { + static const std::vector COCO_CLASSES = { "person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck", "boat", "traffic light", "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat", "dog", "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee", diff --git a/yolox_ros_cpp/yolox_cpp/include/yolox_cpp/core.hpp b/yolox_ros_cpp/yolox_cpp/include/yolox_cpp/core.hpp index 04d3294..501c0ef 100644 --- a/yolox_ros_cpp/yolox_cpp/include/yolox_cpp/core.hpp +++ b/yolox_ros_cpp/yolox_cpp/include/yolox_cpp/core.hpp @@ -29,17 +29,19 @@ namespace yolox_cpp{ public: AbcYoloX(){} AbcYoloX(float nms_th=0.45, float conf_th=0.3, - std::string model_version="0.1.1rc0") + std::string model_version="0.1.1rc0", + int num_classes=80) :nms_thresh_(nms_th), bbox_conf_thresh_(conf_th), - model_version_(model_version) - {} + model_version_(model_version), num_classes_(num_classes) + { + } virtual std::vector inference(const cv::Mat& frame) = 0; protected: int input_w_; int input_h_; float nms_thresh_; float bbox_conf_thresh_; - int num_classes_ = 80; + int num_classes_; std::string model_version_; const std::vector mean_ = {0.485, 0.456, 0.406}; const std::vector std_ = {0.229, 0.224, 0.225}; diff --git a/yolox_ros_cpp/yolox_cpp/include/yolox_cpp/utils.hpp b/yolox_ros_cpp/yolox_cpp/include/yolox_cpp/utils.hpp index 6d0979d..ff45978 100644 --- a/yolox_ros_cpp/yolox_cpp/include/yolox_cpp/utils.hpp +++ b/yolox_ros_cpp/yolox_cpp/include/yolox_cpp/utils.hpp @@ -1,6 +1,9 @@ #ifndef _YOLOX_CPP_UTILS_HPP #define _YOLOX_CPP_UTILS_HPP +#include +#include +#include #include #include "core.hpp" #include "coco_names.hpp" @@ -8,19 +11,31 @@ namespace yolox_cpp{ namespace utils{ - static void draw_objects(cv::Mat bgr, const std::vector& objects) + static std::vector read_class_labels_file(file_name_t file_name) { + std::vector class_names; + std::ifstream ifs(file_name); + std::string buff; + if(ifs.fail()){ + return class_names; + } + while (getline(ifs, buff)) { + if (buff == "") + continue; + class_names.push_back(buff); + } + return class_names; + } - // cv::Mat image = bgr.clone(); + static void draw_objects(cv::Mat bgr, const std::vector& objects, const std::vector& class_names=COCO_CLASSES) + { for (size_t i = 0; i < objects.size(); i++) { const Object& obj = objects[i]; - // fprintf(stderr, "%d = %.5f at %.2f %.2f %.2f x %.2f\n", obj.label, obj.prob, - // obj.rect.x, obj.rect.y, obj.rect.width, obj.rect.height); - - cv::Scalar color = cv::Scalar(color_list[obj.label][0], color_list[obj.label][1], color_list[obj.label][2]); + int color_index = obj.label % 80; + cv::Scalar color = cv::Scalar(color_list[color_index][0], color_list[color_index][1], color_list[color_index][2]); float c_mean = cv::mean(color)[0]; cv::Scalar txt_color; if (c_mean > 0.5){ @@ -32,7 +47,7 @@ namespace yolox_cpp{ cv::rectangle(bgr, obj.rect, color * 255, 2); char text[256]; - sprintf(text, "%s %.1f%%", COCO_CLASSES[obj.label], obj.prob * 100); + sprintf(text, "%s %.1f%%", class_names[obj.label].c_str(), obj.prob * 100); int baseLine = 0; cv::Size label_size = cv::getTextSize(text, cv::FONT_HERSHEY_SIMPLEX, 0.4, 1, &baseLine); @@ -41,14 +56,11 @@ namespace yolox_cpp{ int x = obj.rect.x; int y = obj.rect.y + 1; - //int y = obj.rect.y - label_size.height - baseLine; if (y > bgr.rows) y = bgr.rows; - //if (x + label_size.width > bgr.cols) - //x = bgr.cols - label_size.width; cv::rectangle(bgr, cv::Rect(cv::Point(x, y), cv::Size(label_size.width, label_size.height + baseLine)), - txt_bk_color, -1); + txt_bk_color, -1); cv::putText(bgr, text, cv::Point(x, y + label_size.height), cv::FONT_HERSHEY_SIMPLEX, 0.4, txt_color, 1); diff --git a/yolox_ros_cpp/yolox_cpp/include/yolox_cpp/yolox_onnxruntime.hpp b/yolox_ros_cpp/yolox_cpp/include/yolox_cpp/yolox_onnxruntime.hpp index a7dfa6f..f34d209 100644 --- a/yolox_ros_cpp/yolox_cpp/include/yolox_cpp/yolox_onnxruntime.hpp +++ b/yolox_ros_cpp/yolox_cpp/include/yolox_cpp/yolox_onnxruntime.hpp @@ -19,7 +19,8 @@ namespace yolox_cpp{ YoloXONNXRuntime(file_name_t path_to_model, int intra_op_num_threads, int inter_op_num_threads=1, bool use_cuda=true, int device_id=0, bool use_parallel=false, - float nms_th=0.45, float conf_th=0.3, std::string model_version="0.1.1rc0"); + float nms_th=0.45, float conf_th=0.3, std::string model_version="0.1.1rc0", + int num_classes=80); std::vector inference(const cv::Mat& frame) override; private: diff --git a/yolox_ros_cpp/yolox_cpp/include/yolox_cpp/yolox_openvino.hpp b/yolox_ros_cpp/yolox_cpp/include/yolox_cpp/yolox_openvino.hpp index 26a65d8..e217852 100644 --- a/yolox_ros_cpp/yolox_cpp/include/yolox_cpp/yolox_openvino.hpp +++ b/yolox_ros_cpp/yolox_cpp/include/yolox_cpp/yolox_openvino.hpp @@ -16,7 +16,8 @@ namespace yolox_cpp{ class YoloXOpenVINO: public AbcYoloX{ public: YoloXOpenVINO(file_name_t path_to_model, std::string device_name, - float nms_th=0.45, float conf_th=0.3, std::string model_version="0.1.1rc0"); + float nms_th=0.45, float conf_th=0.3, std::string model_version="0.1.1rc0", + int num_classes=80); std::vector inference(const cv::Mat& frame) override; private: diff --git a/yolox_ros_cpp/yolox_cpp/include/yolox_cpp/yolox_tensorrt.hpp b/yolox_ros_cpp/yolox_cpp/include/yolox_cpp/yolox_tensorrt.hpp index 769a8de..4c45108 100644 --- a/yolox_ros_cpp/yolox_cpp/include/yolox_cpp/yolox_tensorrt.hpp +++ b/yolox_ros_cpp/yolox_cpp/include/yolox_cpp/yolox_tensorrt.hpp @@ -34,7 +34,8 @@ namespace yolox_cpp{ class YoloXTensorRT: public AbcYoloX{ public: YoloXTensorRT(file_name_t path_to_engine, int device=0, - float nms_th=0.45, float conf_th=0.3, std::string model_version="0.1.1rc0"); + float nms_th=0.45, float conf_th=0.3, std::string model_version="0.1.1rc0", + int num_classes=80); std::vector inference(const cv::Mat& frame) override; private: diff --git a/yolox_ros_cpp/yolox_cpp/src/yolox_onnxruntime.cpp b/yolox_ros_cpp/yolox_cpp/src/yolox_onnxruntime.cpp index 0172332..058e9b2 100644 --- a/yolox_ros_cpp/yolox_cpp/src/yolox_onnxruntime.cpp +++ b/yolox_ros_cpp/yolox_cpp/src/yolox_onnxruntime.cpp @@ -5,8 +5,9 @@ namespace yolox_cpp{ YoloXONNXRuntime::YoloXONNXRuntime(file_name_t path_to_model, int intra_op_num_threads, int inter_op_num_threads, bool use_cuda, int device_id, bool use_parallel, - float nms_th, float conf_th, std::string model_version) - :AbcYoloX(nms_th, conf_th, model_version), + float nms_th, float conf_th, std::string model_version, + int num_classes) + :AbcYoloX(nms_th, conf_th, model_version, num_classes), intra_op_num_threads_(intra_op_num_threads), inter_op_num_threads_(inter_op_num_threads), use_cuda_(use_cuda), device_id_(device_id), use_parallel_(use_parallel) { diff --git a/yolox_ros_cpp/yolox_cpp/src/yolox_openvino.cpp b/yolox_ros_cpp/yolox_cpp/src/yolox_openvino.cpp index 79d27a0..3043a8d 100644 --- a/yolox_ros_cpp/yolox_cpp/src/yolox_openvino.cpp +++ b/yolox_ros_cpp/yolox_cpp/src/yolox_openvino.cpp @@ -4,8 +4,10 @@ namespace yolox_cpp{ using namespace InferenceEngine; YoloXOpenVINO::YoloXOpenVINO(file_name_t path_to_model, std::string device_name, - float nms_th, float conf_th, std::string model_version) - :AbcYoloX(nms_th, conf_th, model_version), device_name_(device_name) + float nms_th, float conf_th, std::string model_version, + int num_classes) + :AbcYoloX(nms_th, conf_th, model_version, num_classes), + device_name_(device_name) { // Step 1. Initialize inference engine core std::cout << "Initialize Inference engine core" << std::endl; diff --git a/yolox_ros_cpp/yolox_cpp/src/yolox_tensorrt.cpp b/yolox_ros_cpp/yolox_cpp/src/yolox_tensorrt.cpp index 1c5fb12..6a66f7b 100644 --- a/yolox_ros_cpp/yolox_cpp/src/yolox_tensorrt.cpp +++ b/yolox_ros_cpp/yolox_cpp/src/yolox_tensorrt.cpp @@ -3,8 +3,9 @@ namespace yolox_cpp{ YoloXTensorRT::YoloXTensorRT(file_name_t path_to_engine, int device, - float nms_th, float conf_th, std::string model_version) - :AbcYoloX(nms_th, conf_th, model_version), + float nms_th, float conf_th, std::string model_version, + int num_classes) + :AbcYoloX(nms_th, conf_th, model_version, num_classes), DEVICE_(device) { cudaSetDevice(this->DEVICE_); diff --git a/yolox_ros_cpp/yolox_ros_cpp/CMakeLists.txt b/yolox_ros_cpp/yolox_ros_cpp/CMakeLists.txt index 6074dbd..277899a 100644 --- a/yolox_ros_cpp/yolox_ros_cpp/CMakeLists.txt +++ b/yolox_ros_cpp/yolox_ros_cpp/CMakeLists.txt @@ -95,6 +95,7 @@ install(TARGETS yolox_ros_cpp_components install(DIRECTORY launch + labels ../../weights DESTINATION share/${PROJECT_NAME} ) diff --git a/yolox_ros_cpp/yolox_ros_cpp/include/yolox_ros_cpp/yolox_ros_cpp.hpp b/yolox_ros_cpp/yolox_ros_cpp/include/yolox_ros_cpp/yolox_ros_cpp.hpp index 44463c5..59f4c98 100644 --- a/yolox_ros_cpp/yolox_ros_cpp/include/yolox_ros_cpp/yolox_ros_cpp.hpp +++ b/yolox_ros_cpp/yolox_ros_cpp/include/yolox_ros_cpp/yolox_ros_cpp.hpp @@ -39,6 +39,8 @@ namespace yolox_ros_cpp{ int onnxruntime_inter_op_num_threads_; float conf_th_; float nms_th_; + std::vector class_names_; + std::string class_labels_path_; std::string src_image_topic_name_; std::string publish_image_topic_name_; diff --git a/yolox_ros_cpp/yolox_ros_cpp/labels/coco_names.txt b/yolox_ros_cpp/yolox_ros_cpp/labels/coco_names.txt new file mode 100644 index 0000000..941cb4e --- /dev/null +++ b/yolox_ros_cpp/yolox_ros_cpp/labels/coco_names.txt @@ -0,0 +1,80 @@ +person +bicycle +car +motorcycle +airplane +bus +train +truck +boat +traffic light +fire hydrant +stop sign +parking meter +bench +bird +cat +dog +horse +sheep +cow +elephant +bear +zebra +giraffe +backpack +umbrella +handbag +tie +suitcase +frisbee +skis +snowboard +sports ball +kite +baseball bat +baseball glove +skateboard +surfboard +tennis racket +bottle +wine glass +cup +fork +knife +spoon +bowl +banana +apple +sandwich +orange +broccoli +carrot +hot dog +pizza +donut +cake +chair +couch +potted plant +bed +dining table +toilet +tv +laptop +mouse +remote +keyboard +cell phone +microwave +oven +toaster +sink +refrigerator +book +clock +vase +scissors +teddy bear +hair drier +toothbrush diff --git a/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_onnxruntime.launch.py b/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_onnxruntime.launch.py index ec38250..3db59e6 100644 --- a/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_onnxruntime.launch.py +++ b/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_onnxruntime.launch.py @@ -19,6 +19,11 @@ def generate_launch_description(): default_value="./install/yolox_ros_cpp/share/yolox_ros_cpp/weights/onnx/yolox_nano.onnx", description="yolox model path." ), + DeclareLaunchArgument( + "class_labels_path", + default_value="''", + description="if use custom model, set class name labels. " + ), DeclareLaunchArgument( "model_version", default_value="0.1.1rc0", @@ -100,6 +105,7 @@ def generate_launch_description(): name='yolox_ros_cpp', parameters=[{ "model_path": LaunchConfiguration("model_path"), + "class_labels_path": LaunchConfiguration("class_labels_path"), "model_type": "onnxruntime", "model_version": LaunchConfiguration("model_version"), "onnxruntime/use_cuda": LaunchConfiguration("onnxruntime/use_cuda"), diff --git a/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_openvino.launch.py b/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_openvino.launch.py index 24ee170..b45357b 100644 --- a/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_openvino.launch.py +++ b/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_openvino.launch.py @@ -19,6 +19,11 @@ def generate_launch_description(): default_value="./install/yolox_ros_cpp/share/yolox_ros_cpp/weights/openvino/yolox_nano.xml", description="yolox model path." ), + DeclareLaunchArgument( + "class_labels_path", + default_value="''", + description="if use custom model, set class name labels. " + ), DeclareLaunchArgument( "model_version", default_value="0.1.1rc0", @@ -80,6 +85,7 @@ def generate_launch_description(): name='yolox_ros_cpp', parameters=[{ "model_path": LaunchConfiguration("model_path"), + "class_labels_path": LaunchConfiguration("class_labels_path"), "model_type": "openvino", "model_version": LaunchConfiguration("model_version"), "openvino/device": LaunchConfiguration("openvino/device"), diff --git a/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_openvino_ncs2.launch.py b/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_openvino_ncs2.launch.py index 1cc9577..ba0fddf 100644 --- a/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_openvino_ncs2.launch.py +++ b/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_openvino_ncs2.launch.py @@ -19,6 +19,11 @@ def generate_launch_description(): default_value="./install/yolox_ros_cpp/share/yolox_ros_cpp/weights/openvino/yolox_tiny.xml", description="yolox model path." ), + DeclareLaunchArgument( + "class_labels_path", + default_value="''", + description="if use custom model, set class name labels. " + ), DeclareLaunchArgument( "model_version", default_value="0.1.1rc0", @@ -75,6 +80,7 @@ def generate_launch_description(): name='yolox_ros_cpp', parameters=[{ "model_path": LaunchConfiguration("model_path"), + "class_labels_path": LaunchConfiguration("class_labels_path"), "model_type": "openvino", "model_version": LaunchConfiguration("model_version"), "openvino/device": "MYRIAD", diff --git a/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_tensorrt.launch.py b/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_tensorrt.launch.py index 2dbbbe3..9149c78 100644 --- a/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_tensorrt.launch.py +++ b/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_tensorrt.launch.py @@ -20,6 +20,11 @@ def generate_launch_description(): default_value="./install/yolox_ros_cpp/share/yolox_ros_cpp/weights/tensorrt/yolox_nano.trt", description="yolox model path." ), + DeclareLaunchArgument( + "class_labels_path", + default_value="''", + description="if use custom model, set class name labels. " + ), DeclareLaunchArgument( "tensorrt/device", default_value="0", @@ -82,6 +87,7 @@ def generate_launch_description(): name='yolox_ros_cpp', parameters=[{ "model_path": LaunchConfiguration("model_path"), + "class_labels_path": LaunchConfiguration("class_labels_path"), "model_type": "tensorrt", "model_version": LaunchConfiguration("model_version"), "tensorrt/device": LaunchConfiguration("tensorrt/device"), diff --git a/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_tensorrt_jetson.launch.py b/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_tensorrt_jetson.launch.py index 9f8d2fa..d5f837d 100644 --- a/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_tensorrt_jetson.launch.py +++ b/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_tensorrt_jetson.launch.py @@ -20,6 +20,11 @@ def generate_launch_description(): default_value="./install/yolox_ros_cpp/share/yolox_ros_cpp/weights/tensorrt/yolox_nano.trt", description="yolox model path." ), + DeclareLaunchArgument( + "class_labels_path", + default_value="''", + description="if use custom model, set class name labels. " + ), DeclareLaunchArgument( "model_version", default_value="0.1.1rc0", @@ -77,6 +82,7 @@ def generate_launch_description(): name='yolox_ros_cpp', parameters=[{ "model_path": LaunchConfiguration("model_path"), + "class_labels_path": LaunchConfiguration("class_labels_path"), "model_type": "tensorrt", "model_version": LaunchConfiguration("model_version"), "tensorrt/device": 0, diff --git a/yolox_ros_cpp/yolox_ros_cpp/src/yolox_ros_cpp.cpp b/yolox_ros_cpp/yolox_ros_cpp/src/yolox_ros_cpp.cpp index 73595ba..648b619 100644 --- a/yolox_ros_cpp/yolox_ros_cpp/src/yolox_ros_cpp.cpp +++ b/yolox_ros_cpp/yolox_ros_cpp/src/yolox_ros_cpp.cpp @@ -20,11 +20,23 @@ namespace yolox_ros_cpp{ cv::namedWindow(this->WINDOW_NAME_, cv::WINDOW_AUTOSIZE); } + if(this->class_labels_path_!="") + { + RCLCPP_INFO(this->get_logger(), "read class labels from '%s'", this->class_labels_path_.c_str()); + this->class_names_ = utils::read_class_labels_file(this->class_labels_path_); + } + else + { + this->class_names_ = COCO_CLASSES; + } + RCLCPP_INFO(this->get_logger(), "NUM CLASSES: %d", this->class_names_.size()); + if(this->model_type_ == "tensorrt"){ #ifdef ENABLE_TENSORRT RCLCPP_INFO(this->get_logger(), "Model Type is TensorRT"); this->yolox_ = std::make_unique(this->model_path_, this->tensorrt_device_, - this->nms_th_, this->conf_th_, this->model_version_); + this->nms_th_, this->conf_th_, this->model_version_, + this->class_names_.size()); #else RCLCPP_ERROR(this->get_logger(), "yolox_cpp is not built with TensorRT"); rclcpp::shutdown(); @@ -33,7 +45,8 @@ namespace yolox_ros_cpp{ #ifdef ENABLE_OPENVINO RCLCPP_INFO(this->get_logger(), "Model Type is OpenVINO"); this->yolox_ = std::make_unique(this->model_path_, this->openvino_device_, - this->nms_th_, this->conf_th_, this->model_version_); + this->nms_th_, this->conf_th_, this->model_version_, + this->class_names_.size()); #else RCLCPP_ERROR(this->get_logger(), "yolox_cpp is not built with OpenVINO"); rclcpp::shutdown(); @@ -46,7 +59,8 @@ namespace yolox_ros_cpp{ this->onnxruntime_inter_op_num_threads_, this->onnxruntime_use_cuda_, this->onnxruntime_device_id_, this->onnxruntime_use_parallel_, - this->nms_th_, this->conf_th_, this->model_version_ + this->nms_th_, this->conf_th_, this->model_version_, + this->class_names_.size() ); #else RCLCPP_ERROR(this->get_logger(), "yolox_cpp is not built with ONNXRuntime"); @@ -71,6 +85,7 @@ namespace yolox_ros_cpp{ { this->declare_parameter("imshow_isshow", true); this->declare_parameter("model_path", "src/YOLOX-ROS/weights/openvino/yolox_tiny.xml"); + this->declare_parameter("class_labels_path", ""); this->declare_parameter("conf", 0.3f); this->declare_parameter("nms", 0.45f); this->declare_parameter("tensorrt/device", 0); @@ -88,6 +103,7 @@ namespace yolox_ros_cpp{ this->get_parameter("imshow_isshow", this->imshow_); this->get_parameter("model_path", this->model_path_); + this->get_parameter("class_labels_path", this->class_labels_path_); this->get_parameter("conf", this->conf_th_); this->get_parameter("nms", this->nms_th_); this->get_parameter("tensorrt/device", this->tensorrt_device_); @@ -105,6 +121,7 @@ namespace yolox_ros_cpp{ RCLCPP_INFO(this->get_logger(), "Set parameter imshow_isshow: %i", this->imshow_); RCLCPP_INFO(this->get_logger(), "Set parameter model_path: '%s'", this->model_path_.c_str()); + RCLCPP_INFO(this->get_logger(), "Set parameter class_labels_path: '%s'", this->class_labels_path_.c_str()); RCLCPP_INFO(this->get_logger(), "Set parameter conf: %f", this->conf_th_); RCLCPP_INFO(this->get_logger(), "Set parameter nms: %f", this->nms_th_); RCLCPP_INFO(this->get_logger(), "Set parameter tensorrt/device: %i", this->tensorrt_device_); @@ -132,7 +149,7 @@ namespace yolox_ros_cpp{ auto elapsed = std::chrono::duration_cast(end - now); RCLCPP_INFO(this->get_logger(), "Inference: %f FPS", 1000.0f / elapsed.count()); - yolox_cpp::utils::draw_objects(frame, objects); + yolox_cpp::utils::draw_objects(frame, objects, this->class_names_); if(this->imshow_){ cv::imshow(this->WINDOW_NAME_, frame); auto key = cv::waitKey(1); From c963d903c036cc463758824ae54df491eeccbcf0 Mon Sep 17 00:00:00 2001 From: fateshelled <53618876+fateshelled@users.noreply.github.com> Date: Thu, 29 Sep 2022 23:24:43 +0900 Subject: [PATCH 09/14] Update README.md --- yolox_ros_cpp/README.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/yolox_ros_cpp/README.md b/yolox_ros_cpp/README.md index ece9941..edbcbd9 100644 --- a/yolox_ros_cpp/README.md +++ b/yolox_ros_cpp/README.md @@ -11,6 +11,8 @@ ※ Either one of OpenVINO or TensorRT or ONNXRuntime is required. +※ ONNXRuntime support CPU or CUDA execute provider. + ※ Model convert script is not supported OpenVINO 2022.* ※ YOLOX is not required. @@ -196,7 +198,9 @@ ros2 launch yolox_ros_cpp yolox_onnxruntime.launch.py ### Parameter #### OpenVINO example - `model_path`: ./install/yolox_ros_cpp/share/yolox_ros_cpp/weights/openvino/yolox_nano.xml -- `class_labels_path`: '' +- `class_labels_path`: "" + - if not set, use coco_names. + - See [here](https://github.com/fateshelled/YOLOX-ROS/blob/dev_cpp/yolox_ros_cpp/yolox_ros_cpp/labels/coco_names.txt) for label format. - `model_version`: 0.1.1rc0 - `openvino/device`: CPU - `conf`: 0.3 @@ -209,7 +213,7 @@ ros2 launch yolox_ros_cpp yolox_onnxruntime.launch.py #### TensorRT example. - `model_path`: ./install/yolox_ros_cpp/share/yolox_ros_cpp/weights/tensorrt/yolox_nano.trt -- `class_labels_path`: '' +- `class_labels_path`: "" - `model_version`: 0.1.1rc0 - `tensorrt/device`: 0 - `conf`: 0.3 @@ -222,7 +226,7 @@ ros2 launch yolox_ros_cpp yolox_onnxruntime.launch.py #### ONNXRuntime example. - `model_path`: ./install/yolox_ros_cpp/share/yolox_ros_cpp/weights/onnx/yolox_nano.onnx -- `class_labels_path`: '' +- `class_labels_path`: "" - `model_version`: 0.1.1rc0 - `onnxruntime/use_cuda`: true - `onnxruntime/use_parallel`: false From 8470a439dd0e3917706f6aedcda1127b9f829e54 Mon Sep 17 00:00:00 2001 From: fateshelled <53618876+fateshelled@users.noreply.github.com> Date: Fri, 30 Sep 2022 00:00:49 +0900 Subject: [PATCH 10/14] add param num_classes --- yolox_ros_cpp/README.md | 3 +++ .../include/yolox_ros_cpp/yolox_ros_cpp.hpp | 1 + .../yolox_ros_cpp/launch/yolox_onnxruntime.launch.py | 6 ++++++ .../yolox_ros_cpp/launch/yolox_openvino.launch.py | 6 ++++++ .../yolox_ros_cpp/launch/yolox_openvino_ncs2.launch.py | 6 ++++++ .../yolox_ros_cpp/launch/yolox_tensorrt.launch.py | 6 ++++++ .../launch/yolox_tensorrt_jetson.launch.py | 6 ++++++ yolox_ros_cpp/yolox_ros_cpp/src/yolox_ros_cpp.cpp | 10 ++++++---- 8 files changed, 40 insertions(+), 4 deletions(-) diff --git a/yolox_ros_cpp/README.md b/yolox_ros_cpp/README.md index edbcbd9..9d029b5 100644 --- a/yolox_ros_cpp/README.md +++ b/yolox_ros_cpp/README.md @@ -201,6 +201,7 @@ ros2 launch yolox_ros_cpp yolox_onnxruntime.launch.py - `class_labels_path`: "" - if not set, use coco_names. - See [here](https://github.com/fateshelled/YOLOX-ROS/blob/dev_cpp/yolox_ros_cpp/yolox_ros_cpp/labels/coco_names.txt) for label format. +- `num_classes`: 80 - `model_version`: 0.1.1rc0 - `openvino/device`: CPU - `conf`: 0.3 @@ -214,6 +215,7 @@ ros2 launch yolox_ros_cpp yolox_onnxruntime.launch.py #### TensorRT example. - `model_path`: ./install/yolox_ros_cpp/share/yolox_ros_cpp/weights/tensorrt/yolox_nano.trt - `class_labels_path`: "" +- `num_classes`: 80 - `model_version`: 0.1.1rc0 - `tensorrt/device`: 0 - `conf`: 0.3 @@ -227,6 +229,7 @@ ros2 launch yolox_ros_cpp yolox_onnxruntime.launch.py #### ONNXRuntime example. - `model_path`: ./install/yolox_ros_cpp/share/yolox_ros_cpp/weights/onnx/yolox_nano.onnx - `class_labels_path`: "" +- `num_classes`: 80 - `model_version`: 0.1.1rc0 - `onnxruntime/use_cuda`: true - `onnxruntime/use_parallel`: false diff --git a/yolox_ros_cpp/yolox_ros_cpp/include/yolox_ros_cpp/yolox_ros_cpp.hpp b/yolox_ros_cpp/yolox_ros_cpp/include/yolox_ros_cpp/yolox_ros_cpp.hpp index 59f4c98..21f7756 100644 --- a/yolox_ros_cpp/yolox_ros_cpp/include/yolox_ros_cpp/yolox_ros_cpp.hpp +++ b/yolox_ros_cpp/yolox_ros_cpp/include/yolox_ros_cpp/yolox_ros_cpp.hpp @@ -39,6 +39,7 @@ namespace yolox_ros_cpp{ int onnxruntime_inter_op_num_threads_; float conf_th_; float nms_th_; + int num_classes_; std::vector class_names_; std::string class_labels_path_; diff --git a/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_onnxruntime.launch.py b/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_onnxruntime.launch.py index 3db59e6..552c3cf 100644 --- a/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_onnxruntime.launch.py +++ b/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_onnxruntime.launch.py @@ -24,6 +24,11 @@ def generate_launch_description(): default_value="''", description="if use custom model, set class name labels. " ), + DeclareLaunchArgument( + "num_classes", + default_value="80", + description="num classes." + ), DeclareLaunchArgument( "model_version", default_value="0.1.1rc0", @@ -106,6 +111,7 @@ def generate_launch_description(): parameters=[{ "model_path": LaunchConfiguration("model_path"), "class_labels_path": LaunchConfiguration("class_labels_path"), + "num_classes": LaunchConfiguration("num_classes"), "model_type": "onnxruntime", "model_version": LaunchConfiguration("model_version"), "onnxruntime/use_cuda": LaunchConfiguration("onnxruntime/use_cuda"), diff --git a/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_openvino.launch.py b/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_openvino.launch.py index b45357b..46fb322 100644 --- a/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_openvino.launch.py +++ b/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_openvino.launch.py @@ -24,6 +24,11 @@ def generate_launch_description(): default_value="''", description="if use custom model, set class name labels. " ), + DeclareLaunchArgument( + "num_classes", + default_value="80", + description="num classes." + ), DeclareLaunchArgument( "model_version", default_value="0.1.1rc0", @@ -86,6 +91,7 @@ def generate_launch_description(): parameters=[{ "model_path": LaunchConfiguration("model_path"), "class_labels_path": LaunchConfiguration("class_labels_path"), + "num_classes": LaunchConfiguration("num_classes"), "model_type": "openvino", "model_version": LaunchConfiguration("model_version"), "openvino/device": LaunchConfiguration("openvino/device"), diff --git a/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_openvino_ncs2.launch.py b/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_openvino_ncs2.launch.py index ba0fddf..ae67f4c 100644 --- a/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_openvino_ncs2.launch.py +++ b/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_openvino_ncs2.launch.py @@ -24,6 +24,11 @@ def generate_launch_description(): default_value="''", description="if use custom model, set class name labels. " ), + DeclareLaunchArgument( + "num_classes", + default_value="80", + description="num classes." + ), DeclareLaunchArgument( "model_version", default_value="0.1.1rc0", @@ -81,6 +86,7 @@ def generate_launch_description(): parameters=[{ "model_path": LaunchConfiguration("model_path"), "class_labels_path": LaunchConfiguration("class_labels_path"), + "num_classes": LaunchConfiguration("num_classes"), "model_type": "openvino", "model_version": LaunchConfiguration("model_version"), "openvino/device": "MYRIAD", diff --git a/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_tensorrt.launch.py b/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_tensorrt.launch.py index 9149c78..374a118 100644 --- a/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_tensorrt.launch.py +++ b/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_tensorrt.launch.py @@ -25,6 +25,11 @@ def generate_launch_description(): default_value="''", description="if use custom model, set class name labels. " ), + DeclareLaunchArgument( + "num_classes", + default_value="80", + description="num classes." + ), DeclareLaunchArgument( "tensorrt/device", default_value="0", @@ -88,6 +93,7 @@ def generate_launch_description(): parameters=[{ "model_path": LaunchConfiguration("model_path"), "class_labels_path": LaunchConfiguration("class_labels_path"), + "num_classes": LaunchConfiguration("num_classes"), "model_type": "tensorrt", "model_version": LaunchConfiguration("model_version"), "tensorrt/device": LaunchConfiguration("tensorrt/device"), diff --git a/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_tensorrt_jetson.launch.py b/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_tensorrt_jetson.launch.py index d5f837d..792dac3 100644 --- a/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_tensorrt_jetson.launch.py +++ b/yolox_ros_cpp/yolox_ros_cpp/launch/yolox_tensorrt_jetson.launch.py @@ -25,6 +25,11 @@ def generate_launch_description(): default_value="''", description="if use custom model, set class name labels. " ), + DeclareLaunchArgument( + "num_classes", + default_value="80", + description="num classes." + ), DeclareLaunchArgument( "model_version", default_value="0.1.1rc0", @@ -83,6 +88,7 @@ def generate_launch_description(): parameters=[{ "model_path": LaunchConfiguration("model_path"), "class_labels_path": LaunchConfiguration("class_labels_path"), + "num_classes": LaunchConfiguration("num_classes"), "model_type": "tensorrt", "model_version": LaunchConfiguration("model_version"), "tensorrt/device": 0, diff --git a/yolox_ros_cpp/yolox_ros_cpp/src/yolox_ros_cpp.cpp b/yolox_ros_cpp/yolox_ros_cpp/src/yolox_ros_cpp.cpp index 648b619..554d754 100644 --- a/yolox_ros_cpp/yolox_ros_cpp/src/yolox_ros_cpp.cpp +++ b/yolox_ros_cpp/yolox_ros_cpp/src/yolox_ros_cpp.cpp @@ -29,14 +29,13 @@ namespace yolox_ros_cpp{ { this->class_names_ = COCO_CLASSES; } - RCLCPP_INFO(this->get_logger(), "NUM CLASSES: %d", this->class_names_.size()); if(this->model_type_ == "tensorrt"){ #ifdef ENABLE_TENSORRT RCLCPP_INFO(this->get_logger(), "Model Type is TensorRT"); this->yolox_ = std::make_unique(this->model_path_, this->tensorrt_device_, this->nms_th_, this->conf_th_, this->model_version_, - this->class_names_.size()); + this->num_classes_); #else RCLCPP_ERROR(this->get_logger(), "yolox_cpp is not built with TensorRT"); rclcpp::shutdown(); @@ -46,7 +45,7 @@ namespace yolox_ros_cpp{ RCLCPP_INFO(this->get_logger(), "Model Type is OpenVINO"); this->yolox_ = std::make_unique(this->model_path_, this->openvino_device_, this->nms_th_, this->conf_th_, this->model_version_, - this->class_names_.size()); + this->num_classes_); #else RCLCPP_ERROR(this->get_logger(), "yolox_cpp is not built with OpenVINO"); rclcpp::shutdown(); @@ -60,7 +59,7 @@ namespace yolox_ros_cpp{ this->onnxruntime_use_cuda_, this->onnxruntime_device_id_, this->onnxruntime_use_parallel_, this->nms_th_, this->conf_th_, this->model_version_, - this->class_names_.size() + this->num_classes_ ); #else RCLCPP_ERROR(this->get_logger(), "yolox_cpp is not built with ONNXRuntime"); @@ -86,6 +85,7 @@ namespace yolox_ros_cpp{ this->declare_parameter("imshow_isshow", true); this->declare_parameter("model_path", "src/YOLOX-ROS/weights/openvino/yolox_tiny.xml"); this->declare_parameter("class_labels_path", ""); + this->declare_parameter("num_classes", 80); this->declare_parameter("conf", 0.3f); this->declare_parameter("nms", 0.45f); this->declare_parameter("tensorrt/device", 0); @@ -104,6 +104,7 @@ namespace yolox_ros_cpp{ this->get_parameter("imshow_isshow", this->imshow_); this->get_parameter("model_path", this->model_path_); this->get_parameter("class_labels_path", this->class_labels_path_); + this->get_parameter("num_classes", this->num_classes_); this->get_parameter("conf", this->conf_th_); this->get_parameter("nms", this->nms_th_); this->get_parameter("tensorrt/device", this->tensorrt_device_); @@ -122,6 +123,7 @@ namespace yolox_ros_cpp{ RCLCPP_INFO(this->get_logger(), "Set parameter imshow_isshow: %i", this->imshow_); RCLCPP_INFO(this->get_logger(), "Set parameter model_path: '%s'", this->model_path_.c_str()); RCLCPP_INFO(this->get_logger(), "Set parameter class_labels_path: '%s'", this->class_labels_path_.c_str()); + RCLCPP_INFO(this->get_logger(), "Set parameter num_classes: %i", this->num_classes_); RCLCPP_INFO(this->get_logger(), "Set parameter conf: %f", this->conf_th_); RCLCPP_INFO(this->get_logger(), "Set parameter nms: %f", this->nms_th_); RCLCPP_INFO(this->get_logger(), "Set parameter tensorrt/device: %i", this->tensorrt_device_); From 0568db09c1a53319d897e982199ba5a2a6f83d9d Mon Sep 17 00:00:00 2001 From: fateshelled <53618876+fateshelled@users.noreply.github.com> Date: Sat, 1 Oct 2022 22:26:51 +0900 Subject: [PATCH 11/14] add dockefile for onnxruntime --- .../docker/onnxruntime/docker-compose.yaml | 22 +++++ yolox_ros_cpp/docker/onnxruntime/dockerfile | 81 +++++++++++++++++++ .../docker/onnxruntime/ros_entrypoint.sh | 3 + 3 files changed, 106 insertions(+) create mode 100644 yolox_ros_cpp/docker/onnxruntime/docker-compose.yaml create mode 100644 yolox_ros_cpp/docker/onnxruntime/dockerfile create mode 100755 yolox_ros_cpp/docker/onnxruntime/ros_entrypoint.sh diff --git a/yolox_ros_cpp/docker/onnxruntime/docker-compose.yaml b/yolox_ros_cpp/docker/onnxruntime/docker-compose.yaml new file mode 100644 index 0000000..0b9fa6f --- /dev/null +++ b/yolox_ros_cpp/docker/onnxruntime/docker-compose.yaml @@ -0,0 +1,22 @@ +version: '3.4' +services: + yolox_ros: + container_name: yolox_onnxruntime + # build: + # context: . + # args: + # - BASE_TAB=11.4.2-cudnn8-devel-ubuntu20.04 + image: fateshelled/onnxruntime_yolox_ros:latest + network_mode: host + runtime: nvidia + environment: + - DISPLAY=$DISPLAY + volumes: + - $HOME/ros2_ws:/root/ros2_ws + - /tmp/.X11-unix:/tmp/.X11-unix + devices: + - "/dev/video0:/dev/video0" + working_dir: /root/ros2_ws + tty: true + command: bash + diff --git a/yolox_ros_cpp/docker/onnxruntime/dockerfile b/yolox_ros_cpp/docker/onnxruntime/dockerfile new file mode 100644 index 0000000..efac5ae --- /dev/null +++ b/yolox_ros_cpp/docker/onnxruntime/dockerfile @@ -0,0 +1,81 @@ +ARG BASE_TAG=11.4.2-cudnn8-devel-ubuntu20.04 +FROM nvcr.io/nvidia/cuda:${BASE_TAG} +ENV DEBIAN_FRONTEND=noninteractive + +ENV PATH /usr/local/nvidia/bin:/usr/local/cuda/bin:${PATH} +RUN apt update && \ + apt install -y python3-dev python3-pip \ + ca-certificates g++ gcc make git aria2 && \ + apt -y clean && \ + rm -rf /var/lib/apt/lists/* && \ + aria2c -q -d /tmp -o cmake-3.21.0-linux-x86_64.tar.gz https://github.com/Kitware/CMake/releases/download/v3.21.0/cmake-3.21.0-linux-x86_64.tar.gz && \ + tar -zxf /tmp/cmake-3.21.0-linux-x86_64.tar.gz --strip=1 -C /usr +RUN python3 -m pip install -U pip && \ + python3 -m pip install -U numpy setuptools wheel && \ + python3 -m pip cache purge + +WORKDIR /workdir +RUN git clone --depth 1 --recursive https://github.com/microsoft/onnxruntime -b v1.12.1 && \ + cd onnxruntime && \ + ./build.sh --cudnn_home /usr/lib/x86_64-linux-gnu/ \ + --cuda_home /usr/local/cuda \ + --use_cuda \ + --config RelWithDebInfo \ + --build_shared_lib \ + --parallel \ + --build_wheel \ + --skip_tests && \ + cd build/Linux/RelWithDebInfo && \ + make install && \ + python3 -m pip install dist/*.whl && \ + rm -r /workdir/onnxruntime + +# Install ROS2 +RUN apt update && apt install locales && \ + locale-gen en_US en_US.UTF-8 && \ + update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8 && \ + apt -y clean && \ + rm -rf /var/lib/apt/lists/* +ENV LANG=en_US.UTF-8 + +RUN apt update && \ + apt install -y curl gnupg2 lsb-release && \ + curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key -o /usr/share/keyrings/ros-archive-keyring.gpg && \ + echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] http://packages.ros.org/ros2/ubuntu $(lsb_release -cs) main" | tee /etc/apt/sources.list.d/ros2.list > /dev/null && \ + sed -i -e 's/ubuntu .* main/ubuntu focal main/g' /etc/apt/sources.list.d/ros2.list && \ + apt update && \ + apt install -y ros-foxy-ros-base \ + python3-colcon-common-extensions \ + ros-foxy-v4l2-camera \ + ros-foxy-cv-bridge \ + ros-foxy-rqt-graph \ + ros-foxy-rqt-image-view && \ + rm -rf /var/lib/apt/lists/* &&\ + apt -y clean && \ + pip install -U pip && \ + pip install catkin_pkg && \ + pip install empy && \ + pip install lark && \ + python3 -m pip cache purge + +WORKDIR /workdir +ENV YOLOX_VERSION=0.3.0 +RUN git clone --depth 1 https://github.com/Megvii-BaseDetection/YOLOX -b $YOLOX_VERSION && \ + cd YOLOX && \ + # python3 -m pip install -r requirements.txt && \ + python3 -m pip install -U numpy \ + torch>=1.7 \ + opencv_python \ + loguru \ + tqdm \ + torchvision \ + thop \ + ninja \ + tabulate && \ + python3 -m pip install --no-deps . && \ + python3 -m pip cache purge + +COPY ./ros_entrypoint.sh /ros_entrypoint.sh +RUN echo "source /ros_entrypoint.sh" >> /root/.bashrc +# ENTRYPOINT ["/ros_entrypoint.sh"] +CMD ["bash"] diff --git a/yolox_ros_cpp/docker/onnxruntime/ros_entrypoint.sh b/yolox_ros_cpp/docker/onnxruntime/ros_entrypoint.sh new file mode 100755 index 0000000..a79cc7a --- /dev/null +++ b/yolox_ros_cpp/docker/onnxruntime/ros_entrypoint.sh @@ -0,0 +1,3 @@ +#!/bin/bash +source /opt/ros/foxy/setup.bash +source /usr/share/colcon_argcomplete/hook/colcon-argcomplete.bash From 62241928b9209f0695a0e739c4684be0180a3341 Mon Sep 17 00:00:00 2001 From: fateshelled <53618876+fateshelled@users.noreply.github.com> Date: Sat, 1 Oct 2022 22:26:57 +0900 Subject: [PATCH 12/14] fix --- yolox_ros_cpp/yolox_ros_cpp/src/yolox_ros_cpp.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/yolox_ros_cpp/yolox_ros_cpp/src/yolox_ros_cpp.cpp b/yolox_ros_cpp/yolox_ros_cpp/src/yolox_ros_cpp.cpp index 554d754..d842378 100644 --- a/yolox_ros_cpp/yolox_ros_cpp/src/yolox_ros_cpp.cpp +++ b/yolox_ros_cpp/yolox_ros_cpp/src/yolox_ros_cpp.cpp @@ -23,11 +23,11 @@ namespace yolox_ros_cpp{ if(this->class_labels_path_!="") { RCLCPP_INFO(this->get_logger(), "read class labels from '%s'", this->class_labels_path_.c_str()); - this->class_names_ = utils::read_class_labels_file(this->class_labels_path_); + this->class_names_ = yolox_cpp::utils::read_class_labels_file(this->class_labels_path_); } else { - this->class_names_ = COCO_CLASSES; + this->class_names_ = yolox_cpp::COCO_CLASSES; } if(this->model_type_ == "tensorrt"){ From 42268743e465befa7269197eb999152f303524af Mon Sep 17 00:00:00 2001 From: fateshelled <53618876+fateshelled@users.noreply.github.com> Date: Sat, 1 Oct 2022 22:49:16 +0900 Subject: [PATCH 13/14] Update README.md --- yolox_ros_cpp/README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/yolox_ros_cpp/README.md b/yolox_ros_cpp/README.md index 9d029b5..5cc878d 100644 --- a/yolox_ros_cpp/README.md +++ b/yolox_ros_cpp/README.md @@ -91,6 +91,24 @@ docker run --rm -it \ /bin/bash ``` +#### ONNXRuntime +```bash +# base image is "nvcr.io/nvidia/cuda:11.4.2-cudnn8-devel-ubuntu20.04" +docker pull fateshelled/onnxruntime_yolox_ros:latest + +xhost + +docker run --rm -it \ + --network host \ + --gpus all \ + --privileged \ + -v $HOME/ros2_ws:/root/ros2_ws \ + -v /tmp/.X11-unix:/tmp/.X11-unix \ + -w /root/ros2_ws \ + -e DISPLAY=$DISPLAY \ + --device /dev/video0:/dev/video0 \ + fateshelled/onnxruntime_yolox_ros:latest \ + /bin/bash +``` ### Clone YOLOX-ROS ```bash From 400a00115682fc3e8a8ab12e74d98c5a4451a2cf Mon Sep 17 00:00:00 2001 From: fateshelled <53618876+fateshelled@users.noreply.github.com> Date: Sun, 2 Oct 2022 21:46:52 +0900 Subject: [PATCH 14/14] Update README.md --- yolox_ros_cpp/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/yolox_ros_cpp/README.md b/yolox_ros_cpp/README.md index 5cc878d..178b7cd 100644 --- a/yolox_ros_cpp/README.md +++ b/yolox_ros_cpp/README.md @@ -138,6 +138,7 @@ cd ~/ros2_ws #### ONNXRuntime ```bash cd ~/ros2_ws +source /opt/ros/foxy/setup.bash # Download onnx model ./src/YOLOX-ROS/weights/onnx/download.bash yolox_nano @@ -159,6 +160,7 @@ cd ~/ros2_ws # source /opt/intel/openvino_2021/bin/setupvars.sh cd ~/ros2_ws +source /opt/ros/foxy/setup.bash colcon build --symlink-install source ./install/setup.bash ```