From f4c384d1b1b4cb854ecc1d311c6f1476db8e42d1 Mon Sep 17 00:00:00 2001
From: makaveli <39617050+makaveli10@users.noreply.github.com>
Date: Thu, 6 May 2021 08:35:20 +0530
Subject: [PATCH] add: Scaled yolov4 (#524)
* add: mish, yololayer, layers
* update: CMake
* fix: compile
* add: yolov4-csp net def
* update: cuda kernel for scaled_yolov4
* increase nms thresh
* update: README
* add: gen_wts
* update: CMake
* fix memory leak
* Update README.md
* Update README.md
* Update README.md
* Update README.md
---
scaled-yolov4/CMakeLists.txt | 37 +++
scaled-yolov4/README.md | 56 ++++
scaled-yolov4/common.hpp | 196 +++++++++++++
scaled-yolov4/gen_wts.py | 23 ++
scaled-yolov4/logging.h | 507 ++++++++++++++++++++++++++++++++++
scaled-yolov4/mish.cu | 196 +++++++++++++
scaled-yolov4/mish.h | 108 ++++++++
scaled-yolov4/utils.h | 39 +++
scaled-yolov4/yololayer.cu | 274 +++++++++++++++++++
scaled-yolov4/yololayer.h | 154 +++++++++++
scaled-yolov4/yolov4_csp.cpp | 516 +++++++++++++++++++++++++++++++++++
11 files changed, 2106 insertions(+)
create mode 100644 scaled-yolov4/CMakeLists.txt
create mode 100644 scaled-yolov4/README.md
create mode 100644 scaled-yolov4/common.hpp
create mode 100644 scaled-yolov4/gen_wts.py
create mode 100644 scaled-yolov4/logging.h
create mode 100644 scaled-yolov4/mish.cu
create mode 100644 scaled-yolov4/mish.h
create mode 100644 scaled-yolov4/utils.h
create mode 100644 scaled-yolov4/yololayer.cu
create mode 100644 scaled-yolov4/yololayer.h
create mode 100644 scaled-yolov4/yolov4_csp.cpp
diff --git a/scaled-yolov4/CMakeLists.txt b/scaled-yolov4/CMakeLists.txt
new file mode 100644
index 0000000..110560d
--- /dev/null
+++ b/scaled-yolov4/CMakeLists.txt
@@ -0,0 +1,37 @@
+cmake_minimum_required(VERSION 2.6)
+
+project(yolov4)
+
+add_definitions(-std=c++11)
+
+option(CUDA_USE_STATIC_CUDA_RUNTIME OFF)
+set(CMAKE_CXX_STANDARD 11)
+set(CMAKE_BUILD_TYPE Debug)
+
+find_package(CUDA REQUIRED)
+
+include_directories(${PROJECT_SOURCE_DIR}/include)
+# include and link dirs of cuda and tensorrt, you need adapt them if yours are different
+# cuda
+include_directories(/usr/local/cuda/include)
+link_directories(/usr/local/cuda/lib64)
+# tensorrt
+include_directories(/usr/include/x86_64-linux-gnu/)
+link_directories(/usr/lib/x86_64-linux-gnu/)
+
+set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Ofast -Wfatal-errors -D_MWAITXINTRIN_H_INCLUDED")
+
+cuda_add_library(myplugins SHARED ${PROJECT_SOURCE_DIR}/yololayer.cu ${PROJECT_SOURCE_DIR}/mish.cu)
+target_link_libraries(myplugins nvinfer cudart)
+
+find_package(OpenCV)
+include_directories(${OpenCV_INCLUDE_DIRS})
+
+add_executable(yolov4csp ${PROJECT_SOURCE_DIR}/yolov4_csp.cpp)
+target_link_libraries(yolov4csp nvinfer)
+target_link_libraries(yolov4csp cudart)
+target_link_libraries(yolov4csp myplugins)
+target_link_libraries(yolov4csp ${OpenCV_LIBS})
+
+add_definitions(-O2 -pthread)
+
diff --git a/scaled-yolov4/README.md b/scaled-yolov4/README.md
new file mode 100644
index 0000000..dc94e07
--- /dev/null
+++ b/scaled-yolov4/README.md
@@ -0,0 +1,56 @@
+# scaled-yolov4
+
+The Pytorch implementation is from [WongKinYiu/ScaledYOLOv4 yolov4-csp branch](https://github.com/WongKinYiu/ScaledYOLOv4/tree/yolov4-csp). It can load yolov4-csp.cfg and yolov4-csp.weights(from AlexeyAB/darknet).
+
+Note: There is a slight difference in yolov4-csp.cfg for darknet and pytorch. Use the one given in the above repo.
+
+## Config
+
+- Input shape `INPUT_H`, `INPUT_W` defined in yololayer.h
+- Number of classes `CLASS_NUM` defined in yololayer.h
+- FP16/FP32 can be selected by the macro `USE_FP16` in yolov4_csp.cpp
+- GPU id can be selected by the macro `DEVICE` in yolov4_csp.cpp
+- NMS thresh `NMS_THRESH` in yolov4_csp.cpp
+- bbox confidence threshold `BBOX_CONF_THRESH` in yolov4_csp.cpp
+- `BATCH_SIZE` in yolov4_csp.cpp
+
+## How to run
+
+1. generate yolov4_csp.wts from pytorch implementation with yolov4-csp.cfg and yolov4-csp.weights.
+
+```
+git clone https://github.com/wang-xinyu/tensorrtx.git
+git clone -b yolov4-csp https://github.com/WongKinYiu/ScaledYOLOv4.git
+// download yolov4-csp.weights from https://github.com/WongKinYiu/ScaledYOLOv4/tree/yolov4-csp#yolov4-csp
+cp {tensorrtx}/scaled-yolov4/gen_wts.py {ScaledYOLOv4/}
+cd {ScaledYOLOv4/}
+python gen_wts.py yolov4-csp.weights
+// a file 'yolov4_csp.wts' will be generated.
+```
+
+2. put yolov4_csp.wts into {tensorrtx}/scaled-yolov4, build and run
+
+```
+mv yolov4_csp.wts {tensorrtx}/scaled-yolov4/
+cd {tensorrtx}/scaled-yolov4
+mkdir build
+cd build
+cmake ..
+make
+sudo ./yolov4csp -s // serialize model to plan file i.e. 'yolov4csp.engine'
+sudo ./yolov4csp -d ../../yolov3-spp/samples // deserialize plan file and run inference, the images in samples will be processed.
+```
+
+3. check the images generated, as follows. _zidane.jpg and _bus.jpg
+
+
+
+
+
+
+
+
+
+## More Information
+
+See the readme in [home page.](https://github.com/wang-xinyu/tensorrtx)
diff --git a/scaled-yolov4/common.hpp b/scaled-yolov4/common.hpp
new file mode 100644
index 0000000..5e41a93
--- /dev/null
+++ b/scaled-yolov4/common.hpp
@@ -0,0 +1,196 @@
+#include
+#include