iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🔥

Darknet_ros_FP16 Report (1.3x Faster)

に公開
2021/09/02

Hello everyone. In this article, I would like to provide a brief explanation of darknet_ros + ROS2 Foxy + cuDNN and report on the benchmark results.

If you are reading this article, many of you probably already know darknet_ros (or darknet).

Yes, it's the program that comes to mind first when you want to perform AI-based object detection with robots! It is a high-speed open-source object detection program developed by Joseph Redmon, characterized by being written in C from scratch without depending on TensorFlow or Caffe.

The impact of this paper and program was immense, with its GitHub page having 20.9k stars and 17.4k forks.

Since then, derivatives such as YOLO v4 (by Alexey Bochkovskiy), YOLO R/Scaled-YOLO v4 (by Kin-Yiu, Wong), and YOLO v5 (by Glenn Jocher) have been developed and ported to various languages and libraries like MATLAB and OpenCV.

darknet_ros is being developed to run that high-speed object detection algorithm on ROS. Many articles have been written about it as a staple for ROS + object detection due to its compatibility with embedded computers like Jetson.

That was a brief explanation of darknet and darknet_ros.

No YOLO v4 in darknet_ros!

darknet (YOLO v4) is a project primarily developed by Alexey Bochkovskiy, as mentioned earlier, and it has garnered 16.5k stars, rivaling the original. It has progressed toward C++ integration and supports OpenCV4 and cuDNN, making it an extremely user-friendly program.

However, as of June 2021, the upstream of darknet_ros is still the original version, and the situation where YOLO v4 cannot be used continues. (The developer apparently wants to implement it eventually...)

https://github.com/leggedrobotics/darknet_ros/issues/243#issuecomment-816974717

This is somewhat unavoidable.

Darknet itself had its first paper published in 2015, and because it used quite old libraries, it may not run in current default environments. (Furthermore, development of Joseph Redmon's Darknet ended in 2018...)

Example of handling version-related bugs: https://github.com/AlexeyAB/darknet/issues/932

While darknet_ros is also progressing with support for OpenCV4 and newer versions of ROS, and new pull requests are still being submitted, supporting YOLO v4 (i.e., switching the submodule to AlexeyAB's version) requires very significant changes.

By the way, there are several sites and repositories that support YOLO v4 with darknet_ros on ROS1.

https://github.com/Tossy0423/yolov4-for-darknet_ros

https://www.programmersought.com/article/32007282885/


However, I really wanted darknet_ros to work on ROS2...

So, in late April, I decided I wanted to run YOLO v4 on darknet_ros and implemented it around May.

https://twitter.com/Ray255Ar/status/1391335986412482564

I have been sharing bits and pieces about this in various articles, created a repository on GitHub to submit PRs, and announced results on Twitter.

https://github.com/Ar-Ray-code/darknet_ros_fp16

https://qiita.com/Ar-Ray/items/cda557711bda87979ef5

https://ar-ray.hatenablog.com/entry/2021/06/02/180000

https://ar-ray.hatenablog.com/entry/2021/05/25/180000

I have also gratefully received several reports of it working.

A month and a half since then, I have achieved further speedups and would like to report, with results, that this repository has reached a speed close to the original implementation within the darknet_ros ecosystem.

What are cuDNN and FP16?

To achieve high speed, it is necessary to provide support for cuDNN and FP16 (CUDNN_HALF), which are supported in AlexeyAB/darknet, on the darknet_ros side as well.

Here, I will briefly explain cuDNN and FP16.

In NVIDIA's CUDA libraries, there is a library specialized for deep learning called "cuDNN." This is an external library that can further accelerate the dnn module included as standard in CUDA.

cuDNN is a library that optimizes deep learning modules that cannot be fully supported by CUDA alone. Since it can be enabled by adding it to a Makefile, it can be said that cuDNN partially overwrites the original CUDA libraries.

FP16 is one of the features within cuDNN, and it is very powerful. Standard CUDA calculations handle single-precision floating point. This is almost the same element as the float used in CPUs and can handle a wide range of numbers. However, since the input colors only have 256 levels to begin with, there isn't much point in having such a wide range of values. By using FP16 to halve the representation and precision, it is possible to achieve nearly double the operations with the same memory usage. This is said to increase speed by about 1.3 to 1.5 times.

According to: https://github.com/AlexeyAB/darknet#geforce-rtx-2080-ti

However, FP16 requires hardware support, necessitating certain Volta GPUs or Turing and later GPUs. These GPUs commonly include Tensor Cores, so choosing based on this criterion should be reliable.

Compatible GPUs (those exceeding cm_70):

  • GeForce RTX series (including Laptop)
  • Quadro RTX series
  • Jetson Xavier (including NX?)
  • TITAN V, NVIDIA TITAN RTX
  • NVIDIA V100, T4, A-series

Brief explanation of cuDNN integration

From here, I will provide a brief explanation of CMakeLists.txt for the introduction of cuDNN. I struggled quite a bit with enabling cuDNN, so I hope that by showing excerpts, I can clearly convey the necessary changes to those who need them.

↓ The GitHub repository for darknet_ros with cuDNN support is available here:

https://github.com/Ar-Ray-code/darknet_ros_FP16

When writing CMakeLists.txt, it is necessary to use Find and if commands for conditional branching to ensure compatibility with any environment.

Since hints for finding cuDNN are not provided from the start, you need to refer to FindCUDNN.cmake from AlexeyAB/darknet using set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../darknet/cmake/Modules/" ${CMAKE_MODULE_PATH}). If you are creating an original package, you will need to prepare this yourself.

Then, once found, you link it according to those conditions.

#--- Omitted ---

## For benchmarking
set(CUDA_ENABLE ON)
set(CUDNN_ENABLE ON)
set(FP16_ENABLE ON)

#--- Omitted ---
## Search for FindCUDNN.cmake
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/../darknet/cmake/Modules/" ${CMAKE_MODULE_PATH})

if(CUDNN_ENABLE)
  find_package(CUDNN) ## Search for cuDNN
  message(STATUS "cuDNN Version: ${CUDNN_VERSION_STRINGS}")
  message(STATUS "cuDNN Libararies: ${CUDNN_LIBRARIES}")
endif()

## If cuDNN is found
if(CUDNN_FOUND)
    set(ADDITIONAL_CXX_FLAGS "${ADDITIONAL_CXX_FLAGS} -DCUDNN") # <- Enable cuDNN
endif()
#--- Omitted ---
  
  ## When using cuDNN
  if(CUDNN_FOUND)
  	## When also using FP16 from cuDNN
    if(ENABLE_CUDNN_HALF)
      target_link_libraries(${PROJECT_NAME}_lib
        cuda
        cudart
        cublas
        curand
        CuDNN::CuDNN ## <-- Enable cuDNN
      )
      target_compile_definitions(${PROJECT_NAME}_lib PRIVATE
        -DCUDNN    ## <-- Enable cuDNN
        -DCUDNN_HALF  ## <-- Enable FP16
      )
    ## When not using FP16 from cuDNN
    else()
      target_link_libraries(${PROJECT_NAME}_lib
        cuda
        cudart
        cublas
        curand
        CuDNN::CuDNN  ## <-- Enable cuDNN
      )
      target_compile_definitions(${PROJECT_NAME}_lib PRIVATE
        -DCUDNN       ## <-- Enable cuDNN
      )
    endif()
  ## When not using cuDNN --omitted--
  endif()

Determining CUDNN_HALF (FP16) by Architecture

Regarding the method for enabling CUDNN_HALF, while checking the GPU and setting it manually is the most accurate, many users may attempt to compile without being aware of this. Therefore, I wrote a shell script within CMakeLists.txt to automatically filter out unsupported modules. The system uses the lspci command to display GPUs of the target architecture and filters them out if they are not found. Consequently, GPUs such as the Jetson AGX Xavier are filtered out by default. In such cases, please uncomment # set(CMAKE_CUDA_ARCHITECTURES 72) on line 17.

execute_process (
    COMMAND bash -c "lspci | grep NVIDIA | grep -e TU106 -e TU104 -e TU102 -e GA106 -e GA104 -e GA102"
    OUTPUT_VARIABLE outVar
)
message ( STATUS " GPU (FP16): " ${outVar} )
if(outVar AND FP16_ENABLE)
  set(CMAKE_CUDA_ARCHITECTURES 75)
endif()

if ( 70 IN_LIST CMAKE_CUDA_ARCHITECTURES OR
     72 IN_LIST CMAKE_CUDA_ARCHITECTURES OR
     75 IN_LIST CMAKE_CUDA_ARCHITECTURES OR
     80 IN_LIST CMAKE_CUDA_ARCHITECTURES OR
     86 IN_LIST CMAKE_CUDA_ARCHITECTURES)
  set(ENABLE_CUDNN_HALF "TRUE" CACHE BOOL "Enable CUDNN Half precision" FORCE)
  message(STATUS "Your setup supports half precision (CUDA_ARCHITECTURES >= 70)")
else()
  set(ENABLE_CUDNN_HALF "FALSE" CACHE BOOL "Enable CUDNN Half precision" FORCE)
  message(STATUS "Your setup does not support half precision (it requires CUDA_ARCHITECTURES >= 70)")
endif()

For Speed Verification

It is possible to enable or disable CUDA, CUDNN, and CUDNN_HALF (FP16). Currently, setting CUDA=OFF will result in an error. Additionally, CUDNN will not be enabled if CUDA is OFF, and CUDNN_HALF will not be enabled if CUDNN is not enabled.

set(CUDA_ENABLE ON)
set(CUDNN_ENABLE ON)
set(FP16_ENABLE ON)

Installation

The dependencies are shown below.

Required

  • ROS2 Dashing or later
  • OpenCV 3.2 or later
  • CUDA 10.0 or later (CUDA 11.0 or later for Ampere or later)
  • GPU with 4GB VRAM capable of running NVIDIA CUDA environment

Optional / As Needed

  • cuDNN 7 or later (Please match your CUDA version)
  • GPU with 6GB VRAM capable of running NVIDIA CUDA environment
  • GPU capable of FP16 calculation

Assuming the above installation is complete, enter the following commands.

source /opt/ros/foxy/setup.bash
mkdir -p ~/ros2_ws/src
cd ~/ros2_ws/src
git clone --recursive https://github.com/Ar-Ray-code/darknet_ros_fp16.git
darknet_ros_yolov4/darknet_ros/rm_darknet_CMakeLists.sh
cd ~/ros2_ws
colcon build --symlink-install

With a webcam connected, enter and execute the following command.

source /opt/ros/foxy/setup.bash
source ~/ros2_ws/install/local_setup.bash
ros2 launch darknet_ros demo-v4-tiny.launch.py

It is successful if cudnn_half=1 is shown during loading.

If the image is displayed after execution, it is successful. Yay! 🎉🎉🎉

To quit, stop it with Ctrl+C.

Benchmark (The Main Topic)

Now, what was the main topic again? Ah, right. "How much faster did it get with FP16?"

It's probably faster to just look at the graphs.


I tested in the following environment. Also, benchmarks were taken based on the following premises:

  • This is not intended to compare the speed ratio between the RTX 2080 Ti and Jetson AGX Xavier. It is strictly to confirm the speed ratio within the same model and environment.
  • Models were used as downloaded, and no processing such as TensorRT was performed.
  • Regarding speed, since the values fluctuate significantly and bottlenecks occur depending on the calculation, maximum and minimum FPS were recorded instead of calculating the average FPS. Based on visual observation, the mode seems close to (Max FPS - Min FPS) x 0.7 + Min FPS.
  • Optimization settings such as -gencode arch=compute_75,code=[sm_75,compute_75] were performed manually.
  • Regarding speed, the maximum and minimum FPS immediately after the start were not taken. The speed reflected after leaving it for one minute was recorded as the result.
  • This branch specifically for benchmarking was used.

The following four weight files were used:

  • YOLOv4x-mish.weights (640)
  • YOLO v4.weights (608)
  • YOLOv4-scp.weights (512)
  • YOLOv4-tiny (416)

Environment 1: RTX 2080 Ti

Topics Spec
CPU Ryzen7 2700X (@3.7GHz x 16)
RAM 16GB DDR4
GPU NVIDIA GeForce RTX 2080 Ti (GDDR6 11GB)
Driver 465.19.01
Topics Version
Ubuntu 20.04 LTS
ROS Foxy
CUDA 11.3
cuDNN 8.2.1
OpenCV 4.2

YOLOv4-tiny with 167 FPS! 🔥🔥🔥🔥🔥🔥🔥🔥

Environment 2: Jetson AGX Xavier (MAXN)

Topics Spec
CPU 8-core ARM v8.2 64-bit CPU, 8MB L2 + 4MB L3
RAM 32 GB 256-Bit LPDDR4x |137 GB/s
GPU 512-core Volta GPU with Tensor Cores
Driver JetPack 4.4.1
Topics Version
Ubuntu 18.04 LTS
ROS Dashing
CUDA 10.2
cuDNN 8.0
OpenCV 4.1
  • Note: YOLOv4.weights and YOLOv4x-mish could not be executed on Jetson AGX Xavier.

YOLOv4-tiny with 98 FPS! 🔥🔥🔥🔥🔥🔥🔥🔥

Discussion and Conclusion

The following three things can be said for both the RTX 2080 Ti and Jetson AGX Xavier:

  • FPS increases particularly with FP16.
  • The heavier the file, the greater the gain in instantaneous FPS.
  • Overall, the speed of cuDNN (FP16) is about 1.3 times faster than when cuDNN is not used.

However, for the Jetson AGX Xavier, it was found that using cuDNN doesn't mean much unless FP16 is enabled.

Recently, many automation applications using object detection have been developed, and AI products are becoming more familiar. With the rapid evolution of various libraries and the spread of new conventions like ROS2, porting of various modules is progressing. I personally speculate that "porting capability" is becoming a highly necessary skill.

I hope this is helpful for those who want to use YOLO v4 models in ROS.

With this document, I would like to conclude my major improvements for now, but if you have any questions or anything you don't understand, please feel free to ask on GitHub or Twitter, and I will answer as much as possible.

https://github.com/Ar-Ray-code/darknet_ros_fp16

https://twitter.com/Ray255Ar

References

https://github.com/pjreddie/darknet

https://github.com/AlexeyAB/darknet

https://github.com/leggedrobotics/darknet_ros

https://developer.nvidia.com/cuda-gpus

https://developer.nvidia.com/cudnn

Discussion