From 6161f288542a99d222b28c2cbb64d630cc70f85a Mon Sep 17 00:00:00 2001
From: YuzhouPeng <760783896@qq.com>
Date: Thu, 28 Jan 2021 12:59:02 +0800
Subject: [PATCH] add unet code (#380)
* add code
* remove txt
---
unet/CMakeLists.txt | 34 +++
unet/README.md | 64 ++++++
unet/common.hpp | 148 +++++++++++++
unet/gen_wts.py | 36 ++++
unet/logging.h | 503 ++++++++++++++++++++++++++++++++++++++++++++
unet/unet.cpp | 372 ++++++++++++++++++++++++++++++++
6 files changed, 1157 insertions(+)
create mode 100644 unet/CMakeLists.txt
create mode 100644 unet/README.md
create mode 100644 unet/common.hpp
create mode 100644 unet/gen_wts.py
create mode 100644 unet/logging.h
create mode 100644 unet/unet.cpp
diff --git a/unet/CMakeLists.txt b/unet/CMakeLists.txt
new file mode 100644
index 0000000..568e4fc
--- /dev/null
+++ b/unet/CMakeLists.txt
@@ -0,0 +1,34 @@
+cmake_minimum_required(VERSION 2.6)
+
+project(unet)
+
+add_definitions(-std=c++11)
+
+option(CUDA_USE_STATIC_CUDA_RUNTIME OFF)
+set(CMAKE_CXX_STANDARD 11)
+set(CMAKE_BUILD_TYPE Debug)
+
+set(CUDA_NVCC_PLAGS ${CUDA_NVCC_PLAGS};-std=c++11;-g;-G;-gencode;arch=compute_30;code=sm_30)
+
+# cuda directory
+include_directories(${PROJECT_SOURCE_DIR}/include)
+include_directories(/usr/local/cuda-10.2/targets/x86_64-linux/include)
+link_directories(/usr/local/cuda-10.2/targets/x86_64-linux/lib)
+
+# tensorrt
+include_directories(/home/sycv/workplace/pengyuzhou/TensorRT-7.0.0.11/targets/x86_64-linux-gnu/include)
+link_directories(/home/sycv/workplace/pengyuzhou/TensorRT-7.0.0.11/targets/x86_64-linux-gnu/lib)
+
+set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Ofast -Wfatal-errors -D_MWAITXINTRIN_H_INCLUDED")
+
+# link library and add exec file
+add_executable(unet ${PROJECT_SOURCE_DIR}/unet.cpp)
+target_link_libraries(unet nvinfer)
+target_link_libraries(unet cudart)
+
+add_definitions(-O2 -pthread)
+
+# opencv library
+find_package(OpenCV)
+include_directories(OpenCV_INCLUDE_DIRS)
+target_link_libraries(unet ${OpenCV_LIBS})
\ No newline at end of file
diff --git a/unet/README.md b/unet/README.md
new file mode 100644
index 0000000..7cb3d5d
--- /dev/null
+++ b/unet/README.md
@@ -0,0 +1,64 @@
+# tensorrt-unet
+This is a TensorRT version Unet, inspired by [tensorrtx](https://github.com/wang-xinyu/tensorrtx) and [pytorch-unet](https://github.com/milesial/Pytorch-UNet).
+You can generate TensorRT engine file using this script and customize some params and network structure based on network you trained (FP32/16 precision, input size, different conv, activation function...)
+
+# requirements
+
+TensorRT 7.0 (you need to install tensorrt first)
+Cuda 10.2
+Python3.7
+opencv 4.4
+cmake 3.18
+# train .pth file and convert .wts
+
+## create env
+
+```
+pip install -r requirements.txt
+```
+
+## train .pth file
+
+train your dataset by following [pytorch-unet](https://github.com/milesial/Pytorch-UNet) and generate .pth file.
+
+## convert .wts
+
+run gen_wts from utils folder, and move it to project folder
+
+# generate engine file and infer
+
+## create build folder in project folder
+```
+mkdir build
+```
+
+## make file, generate exec file
+```
+cd build
+cmake ..
+make
+```
+
+## generate TensorRT engine file and infer image
+```
+unet -s
+```
+then a unet exec file will generated, you can use unet -d to infer files in a folder
+```
+unet -d ../samples
+```
+
+# efficiency
+the speed of tensorRT engine is much faster
+
+ pytorch | TensorRT FP32 | TensorRT FP16
+ ---- | ----- | ------
+ 816x672 | 816x672 | 816x672
+ 58ms | 43ms (batchsize 8) | 14ms (batchsize 8)
+
+
+# Further development
+
+1. add INT8 calibrator
+2. add custom plugin
+etc
diff --git a/unet/common.hpp b/unet/common.hpp
new file mode 100644
index 0000000..3121f58
--- /dev/null
+++ b/unet/common.hpp
@@ -0,0 +1,148 @@
+#ifndef UNET_COMMON_H_
+#define UNET_COMMON_H_
+
+#include
+#include