├── .gitmodules
├── CMakeLists.txt
├── README.md
├── doc
├── hopenet_graph.png
├── posenet.png
├── rcnn.png
├── retinaface.png
├── yolact.png
├── yolo.png
└── yolov5.png
├── include
└── ros_ncnn
│ ├── gpu_support.h
│ ├── ncnn_config.h.in
│ ├── ncnn_fast_rcnn.h
│ ├── ncnn_hopenet.h
│ ├── ncnn_pfld.h
│ ├── ncnn_posenet.h
│ ├── ncnn_retinaface.h
│ ├── ncnn_ultraface.h
│ ├── ncnn_utils.h
│ ├── ncnn_yolact.h
│ ├── ncnn_yolo.h
│ └── ncnn_yolov5.h
├── launch
├── hopenet.launch
├── retinaface.launch
├── ultraface.launch
├── yolact.launch
├── yolo.launch
└── yolov5.launch
├── msg
├── Euler.msg
├── FaceObject.msg
├── Object.msg
├── Rectangle.msg
└── Vector2D.msg
├── package.xml
└── src
├── faster_rcnn_node.cpp
├── hopenet_node.cpp
├── ncnn_fast_rcnn.cpp
├── ncnn_hopenet.cpp
├── ncnn_pfld.cpp
├── ncnn_posenet.cpp
├── ncnn_retinaface.cpp
├── ncnn_ultraface.cpp
├── ncnn_yolact.cpp
├── ncnn_yolo.cpp
├── ncnn_yolov5.cpp
├── pfld_node.cpp
├── posenet_node.cpp
├── retinaface_node.cpp
├── ultraface_node.cpp
├── yolact_node.cpp
├── yolo_node.cpp
└── yolov5_node.cpp
/.gitmodules:
--------------------------------------------------------------------------------
1 | [submodule "assets"]
2 | path = assets
3 | url = https://github.com/nilseuropa/ncnn_models
4 |
--------------------------------------------------------------------------------
/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | cmake_minimum_required(VERSION 2.8.3)
2 | project(ros_ncnn)
3 |
4 | add_compile_options(-std=c++11 -DUSE_AVX_INSTRUCTIONS=ON)
5 |
6 | option(GPU_SUPPORT "Enable GPU support (Vulkan)" OFF)
7 |
8 | find_package(catkin REQUIRED COMPONENTS
9 | roscpp
10 | roslib
11 | cv_bridge
12 | image_transport
13 | image_geometry
14 | sensor_msgs
15 | message_generation
16 | )
17 |
18 | add_message_files(
19 | FILES
20 | Vector2D.msg
21 | Rectangle.msg
22 | FaceObject.msg
23 | Object.msg
24 | Euler.msg
25 | )
26 |
27 | generate_messages(
28 | DEPENDENCIES
29 | std_msgs
30 | )
31 |
32 | catkin_package(
33 | CATKIN_DEPENDS
34 | roscpp
35 | roslib
36 | cv_bridge
37 | image_transport
38 | image_geometry
39 | sensor_msgs
40 | message_generation
41 | )
42 |
43 | include_directories(
44 | "include"
45 | ${catkin_INCLUDE_DIRS}
46 | )
47 |
48 | find_package(ncnn REQUIRED)
49 | if (${ncnn_FOUND})
50 | message("-- NCNN found.")
51 | message("-- NCNN_VULKAN flag is ${NCNN_VULKAN}")
52 | if (${NCNN_VULKAN})
53 | message("-- AUTO-ENABLING GPU_SUPPORT")
54 | set(GPU_SUPPORT ON)
55 | endif()
56 | include_directories(${ncnn_INCLUDE})
57 | endif()
58 |
59 | find_package(OpenCV REQUIRED COMPONENTS
60 | core highgui imgproc imgcodecs
61 | )
62 | include_directories(
63 | ${OpenCV_INCLUDE_DIRS}
64 | )
65 |
66 | if (GPU_SUPPORT)
67 | message("-- GPU support is ENABLED")
68 | find_package(Vulkan) # REQUIRES ncnn to be built with vulkan
69 | if (${VULKAN_FOUND})
70 | message("-- Vulkan found.")
71 | else()
72 | message("-- ERROR: AUTO-DISABLING GPU_SUPPORT, because Vulkan was not found")
73 | set(GPU_SUPPORT OFF)
74 | endif()
75 | else()
76 | message("-- GPU support is DISABLED")
77 | endif()
78 |
79 | # NCNN config header exports GPU_SUPPORT definition towards source files
80 | configure_file("include/ros_ncnn/ncnn_config.h.in" "ros_ncnn/ncnn_config.h")
81 | include_directories(${CMAKE_CURRENT_BINARY_DIR})
82 |
83 | # PFLD
84 | add_executable(pfld_node src/pfld_node.cpp src/ncnn_pfld.cpp)
85 | add_dependencies( pfld_node ros_ncnn_generate_messages_cpp)
86 | target_link_libraries(pfld_node ${catkin_LIBRARIES} ncnn ${OpenCV_LIBS})
87 |
88 | # HOPENET
89 | add_executable(hopenet_node src/hopenet_node.cpp src/ncnn_hopenet.cpp)
90 | add_dependencies( hopenet_node ros_ncnn_generate_messages_cpp)
91 | target_link_libraries(hopenet_node ${catkin_LIBRARIES} ncnn ${OpenCV_LIBS})
92 |
93 | # RETINAFACE
94 | add_executable(retinaface_node src/retinaface_node.cpp src/ncnn_retinaface.cpp)
95 | add_dependencies( retinaface_node ros_ncnn_generate_messages_cpp)
96 | target_link_libraries(retinaface_node ${catkin_LIBRARIES} ncnn ${OpenCV_LIBS})
97 |
98 | # ULTRAFACE
99 | add_executable(ultraface_node src/ultraface_node.cpp src/ncnn_ultraface.cpp)
100 | add_dependencies( ultraface_node ros_ncnn_generate_messages_cpp)
101 | target_link_libraries(ultraface_node ${catkin_LIBRARIES} ncnn ${OpenCV_LIBS})
102 |
103 | # YOLACT
104 | add_executable(yolact_node src/yolact_node.cpp src/ncnn_yolact.cpp)
105 | add_dependencies( yolact_node ros_ncnn_generate_messages_cpp)
106 | target_link_libraries(yolact_node ${catkin_LIBRARIES} ncnn ${OpenCV_LIBS})
107 |
108 | # YOLO v2/v3
109 | add_executable(yolo_node src/yolo_node.cpp src/ncnn_yolo.cpp)
110 | add_dependencies( yolo_node ros_ncnn_generate_messages_cpp)
111 | target_link_libraries(yolo_node ${catkin_LIBRARIES} ncnn ${OpenCV_LIBS})
112 |
113 | # YOLO v5
114 | add_executable(yolov5_node src/yolov5_node.cpp src/ncnn_yolov5.cpp)
115 | add_dependencies( yolov5_node ros_ncnn_generate_messages_cpp)
116 | target_link_libraries(yolov5_node ${catkin_LIBRARIES} ncnn ${OpenCV_LIBS})
117 |
118 | # POSENET
119 | add_executable(posenet_node src/posenet_node.cpp src/ncnn_posenet.cpp)
120 | add_dependencies( posenet_node ros_ncnn_generate_messages_cpp)
121 | target_link_libraries(posenet_node ${catkin_LIBRARIES} ncnn ${OpenCV_LIBS})
122 |
123 | # FASTER RCNN
124 | add_executable(faster_rcnn_node src/faster_rcnn_node.cpp src/ncnn_fast_rcnn.cpp)
125 | add_dependencies( faster_rcnn_node ros_ncnn_generate_messages_cpp)
126 | target_link_libraries(faster_rcnn_node ${catkin_LIBRARIES} ncnn ${OpenCV_LIBS})
127 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # ROS_NCNN #
2 |
3 | This is a ROS package for NCNN, a high-performance neural network inference framework *- by Tencent -* optimized for mobile platforms:
4 |
5 | - ARM NEON assembly level optimization
6 | - Sophisticated memory management and data structure design, very low memory footprint
7 | - Supports multi-core parallel computing acceleration
8 | - Supports GPU acceleration via the next-generation low-overhead Vulkan API
9 | - The overall library size is less than 700K, and can be easily reduced to less than 300K
10 | - Extensible model design, supports 8bit quantization and half-precision floating point storage
11 | - Can import caffe/pytorch/mxnet/onnx models
12 |
13 |
14 |
15 | ## Setting up ##
16 |
17 | ### Library ###
18 |
19 | - [Build for NVIDIA Jetson](https://github.com/Tencent/ncnn/wiki/how-to-build#build-for-nvidia-jetson)
20 | - [Build for Linux x86](https://github.com/Tencent/ncnn/wiki/how-to-build#build-for-linux-x86)
21 | - [Build for Windows x64 using VS2017](https://github.com/Tencent/ncnn/wiki/how-to-build#build-for-windows-x64-using-visual-studio-community-2017)
22 | - [Build for MacOSX](https://github.com/Tencent/ncnn/wiki/how-to-build#build-for-macosx)
23 | - [Build for Raspberry Pi 3](https://github.com/Tencent/ncnn/wiki/how-to-build#build-for-raspberry-pi-3)
24 | - [Build for ARM Cortex-A family with cross-compiling](https://github.com/Tencent/ncnn/wiki/how-to-build#build-for-arm-cortex-a-family-with-cross-compiling)
25 | - [Build for Android](https://github.com/Tencent/ncnn/wiki/how-to-build#build-for-android)
26 | - [Build for iOS on MacOSX with xcode](https://github.com/Tencent/ncnn/wiki/how-to-build#build-for-ios-on-macosx-with-xcode)
27 | - [Build for iOS on Linux with cctools-port](https://github.com/Tencent/ncnn/wiki/how-to-build#build-for-ios-on-linux-with-cctools-port)
28 | - [Build for Hisilicon platform with cross-compiling](https://github.com/Tencent/ncnn/wiki/how-to-build#build-for-hisilicon-platform-with-cross-compiling)
29 |
30 |
31 |
32 | ## ROS package ##
33 |
34 | * Clone this repository into your catkin workspace.
35 | * Initialize and update submodule `ncnn-assets` *( this is a collection of some popular models )*
36 | * Compile the workspace.
37 | * CMake script is going to autodetect whether the **ncnn library** is built with **Vulkan** or not. _( All nodes will utilize the GPU if Vulkan is enabled. )_
38 |
39 | #### General launch parameters ####
40 | ```xml
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 | ```
52 |
53 | ### YOLACT ###
54 | 
55 |
56 | **Publisher**
57 | ```yaml
58 | # Object message
59 | Header header
60 | Rectangle boundingbox # Vector2D position and size
61 | string label
62 | float32 probability
63 | ```
64 | **Params**
65 | * _probability_threshold_ - default 0.5 - above which objects are published
66 |
67 | ### YOLO v2 / v3
68 | 
69 | The `assets` repository has multiple YOLO networks, choose the parameter and model file before launch. _( Default is YOLO-3 on MobileNet-2 )_
70 |
71 | **Publisher**
72 | ```yaml
73 | # Object message
74 | Header header
75 | Rectangle boundingbox # Vector2D position and size
76 | string label
77 | float32 probability
78 | ```
79 | **Params**
80 | * _model_file_ - YOLO network model file
81 | * _param_file_ - YOLO network parameter file
82 | * _probability_threshold_ - default 0.5 - above which objects are published
83 |
84 | ### YOLO v5
85 | 
86 |
87 | **Publisher**
88 | ```yaml
89 | # Object message
90 | Header header
91 | Rectangle boundingbox # Vector2D position and size
92 | string label
93 | float32 probability
94 | ```
95 | **Params**
96 | * _model_file_ - YOLO network model file
97 | * _param_file_ - YOLO network parameter file
98 | * _probability_threshold_ - default 0.5 - above which objects are published
99 |
100 | ### RetinaFace ###
101 | 
102 |
103 | **Publisher**
104 | ```yaml
105 | # FaceObject message
106 | Header header
107 | Rectangle boundingbox # Vector2D position and size
108 | Vector2D[5] landmark # 5x 2x float32
109 | float32 probability
110 | ```
111 | **Params**
112 | * _probability_threshold_ - default 0.5 - above which face objects are published
113 |
114 | ### HopeNet ###
115 | Using RetinaFace as face detector:
116 | 
117 |
118 | **Publisher**
119 | ```yaml
120 | # Euler angles
121 | float32 roll
122 | float32 pitch
123 | float32 yaw
124 | ```
125 |
126 | ### PoseNet ###
127 | 
128 |
129 | ### Faster R-CNN ###
130 | Don't forget to uncompress `ZF_faster_rcnn_final.bin.zip` in assets directory first. _( but again, R-CNN is the past and that's neither a cat nor a bird right there... that's my best friend )_
131 | 
132 |
133 | ## :construction: To do ##
134 |
135 | * General model loader node _( with layer to topic mapping through NDS file )_
136 | * Dynamic reconfiguration for some params _( e.g. probability thresholds )_
137 |
138 | ## :v: Acknowledgements ##
139 | _Special thanks to **[Nihui](https://github.com/nihui)** for her wonderful work._
140 |
--------------------------------------------------------------------------------
/doc/hopenet_graph.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/nilseuropa/ros_ncnn/a7e08d3804ae582f0e6bf726a83d5e4c37d87c56/doc/hopenet_graph.png
--------------------------------------------------------------------------------
/doc/posenet.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/nilseuropa/ros_ncnn/a7e08d3804ae582f0e6bf726a83d5e4c37d87c56/doc/posenet.png
--------------------------------------------------------------------------------
/doc/rcnn.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/nilseuropa/ros_ncnn/a7e08d3804ae582f0e6bf726a83d5e4c37d87c56/doc/rcnn.png
--------------------------------------------------------------------------------
/doc/retinaface.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/nilseuropa/ros_ncnn/a7e08d3804ae582f0e6bf726a83d5e4c37d87c56/doc/retinaface.png
--------------------------------------------------------------------------------
/doc/yolact.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/nilseuropa/ros_ncnn/a7e08d3804ae582f0e6bf726a83d5e4c37d87c56/doc/yolact.png
--------------------------------------------------------------------------------
/doc/yolo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/nilseuropa/ros_ncnn/a7e08d3804ae582f0e6bf726a83d5e4c37d87c56/doc/yolo.png
--------------------------------------------------------------------------------
/doc/yolov5.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/nilseuropa/ros_ncnn/a7e08d3804ae582f0e6bf726a83d5e4c37d87c56/doc/yolov5.png
--------------------------------------------------------------------------------
/include/ros_ncnn/gpu_support.h:
--------------------------------------------------------------------------------
1 | #ifndef _NODE_GPU_SUPPORT_HEADER_
2 | #define _NODE_GPU_SUPPORT_HEADER_
3 |
4 | static ncnn::VulkanDevice* g_vkdev = 0;
5 | static ncnn::VkAllocator* g_blob_vkallocator = 0;
6 | static ncnn::VkAllocator* g_staging_vkallocator = 0;
7 |
8 | // Check GPU info, override selection with 1st discrete device if the selected gpu_device is non-discrete
9 | int selectGPU(int gpu_device){
10 | /*
11 | int gpus = ncnn::get_gpu_count();
12 | ncnn::GpuInfo gpu_info;
13 | int first_discrete = -1;
14 | std::string gpu_type_s, selected_gpu_type_s = "UNKNOWN";
15 | bool selected_discrete = false;
16 | bool has_discrete = false;
17 | for (int g=0; g rect;
13 | int label;
14 | float prob;
15 | };
16 |
17 | static const char* class_names[] = {"background",
18 | "aeroplane", "bicycle", "bird", "boat",
19 | "bottle", "bus", "car", "cat", "chair",
20 | "cow", "diningtable", "dog", "horse",
21 | "motorbike", "person", "pottedplant",
22 | "sheep", "sofa", "train", "tvmonitor"};
23 |
24 | class ncnnFastRcnn
25 | {
26 |
27 | public:
28 |
29 | ncnn::Net neuralnet;
30 |
31 | int detect(const cv::Mat& bgr, std::vector