196个文件已删除
30个文件已添加
1 文件已重命名
2个文件已修改
| | |
| | | cmake_minimum_required(VERSION 3.5) |
| | | project(yoloDetector VERSION 1.0) |
| | | enable_language(CUDA) |
| | | |
| | | # set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden -DEIGEN_MAX_ALIGN_BYTES=32 -DEIGEN_DONT_VECTORIZE -fPIC -march=native -DGPU -DCUDNN -DOPENCV -g -W -O0 -pthread -DS_DEBUG") |
| | | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden -DEIGEN_MAX_ALIGN_BYTES=32 -DEIGEN_DONT_VECTORIZE -fPIC -march=native -DGPU -DCUDNN -DOPENCV -g -W -O0 -pthread -DS_DEBUG -DS_SERVER") |
| | | #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden -DEIGEN_MAX_ALIGN_BYTES=32 -DEIGEN_DONT_VECTORIZE -fPIC -lm -march=native -DGPU -DCUDNN -DOPENCV -g -W -O0 -pthread -Wl,--no-as-needed") |
| | | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden -DGPU -DCUDNN -DOPENCV -g -lm -fPIC -W -O0 -Wl,--no-as-needed -DS_DEBUG -DS_SERVER") # 编译C脚本时调用GPU环境信息 |
| | | #set(CMAKE_C_FLAGS "-fvisibility=hidden") |
| | | #set(CMAKE_CXX_FLAGS "-fvisibility=hidden") |
| | | set(CMAKE_CXX_COMPILIER "/usr/bin/g++") |
| | | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -O3 -Wno-write-strings") |
| | | set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-rpath -Wl,$ORIGIN") |
| | | |
| | | #项目名称/工程名 |
| | | project(baseDetector) |
| | | set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/build) |
| | | #设置c++编译器 |
| | | set(CUDA_Path /usr/local/cuda) |
| | | find_package(OpenCV REQUIRED PATHS "/data/disk2/opt/01_opencv/opencv4.5.2") |
| | | set(TensorRT_DIR /data/disk2/opt/05_tensorRT/TensorRT-7.2.3.4) |
| | | set(LOG_DIR /data/disk2/opt/log4cplus) |
| | | |
| | | set(CMAKE_CXX_STANDARD 14) |
| | | #set(CMAKE_CXX_STANDARD_REQUIRED ON) |
| | | #set(CMAKE_CXX_EXTENSIONS OFF) |
| | | include_directories(${CUDA_Path}/include) |
| | | link_directories(${CUDA_Path}/lib64) |
| | | include_directories(${TensorRT_DIR}/include) |
| | | include_directories(${TensorRT_DIR}/samples/common) |
| | | link_directories(${TensorRT_DIR}/lib) |
| | | link_directories(${LOG_DIR}/lib) |
| | | include_directories(${LOG_DIR}/include) |
| | | |
| | | #set(CMAKE_BUILD_TYPE debug) |
| | | add_compile_options(-fPIC -ldl -lm -g -Wall -Wshadow -Wno-sign-compare -w -pthread) |
| | | #add_definitions(-DDEBUG_ERR -DDEBUG_INFO) |
| | | #add_definitions(-DDEBUG) |
| | | #add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0) |
| | | file(GLOB_RECURSE sources src/* src/core/* src/utils/* src/detecter_tools/* ${TensorRT_DIR}/samples/common/*.h ${TensorRT_DIR}/samples/common/logger.cpp) |
| | | set(LIBS nvinfer nvparsers nvonnxparser nvinfer_plugin stdc++fs jsoncpp log4cplus) |
| | | |
| | | if(NOT DEFINED CMAKE_CUDA_ARCHITECTURES) |
| | | set(CMAKE_CUDA_ARCHITECTURES 61) |
| | | endif(NOT DEFINED CMAKE_CUDA_ARCHITECTURES) |
| | | |
| | | |
| | | file(GLOB_RECURSE CURRENT_HEADERS |
| | | ./*.h |
| | | ./lib/*.h |
| | | ./lib/core/*.h |
| | | ./lib/utils/*.h |
| | | ./lib/detecter_tools/*.h |
| | | ./lib/detecter_tools/extra/*.h |
| | | ./lib/detecter_tools/*.hpp |
| | | ./lib/detecter_tools/*.cuh |
| | | ./lib/detecter_tools/darknet/*.hpp |
| | | ./lib/detecter_tools/darknet/*.cuh |
| | | ) |
| | | # |
| | | |
| | | # 检索查找文件信息 |
| | | file(GLOB sources |
| | | ./*.cpp |
| | | ./lib/*.cpp |
| | | ./lib/core/*.cpp |
| | | ./lib/utils/*.cpp |
| | | ./lib/detecter_tools/*.cpp |
| | | ./lib/detecter_tools/*.cpp |
| | | ./lib/detecter_tools/darknet/*.c |
| | | ./lib/detecter_tools/darknet/*.cpp |
| | | ./lib/detecter_tools/darknet/*.cu |
| | | ) |
| | | |
| | | source_group("Include" FILES ${CURRENT_HEADERS}) |
| | | source_group("Source" FILES ${sources}) |
| | | |
| | | |
| | | link_directories(/usr/local/cuda/lib64) |
| | | link_directories(/usr/local/lib) |
| | | |
| | | # 设置环境的模型查找,如果CUDA版本大于9,则执行CUDA环境的模型查找 |
| | | if(CUDA_VERSION_MAJOR GREATER 9) |
| | | message("-- CUDA ${CUDA_VERSION_MAJOR} detected") |
| | | set( |
| | | CUDA_NVCC_FLAGS |
| | | ${CUDA_NVCC_FLAGS}; |
| | | -gencode arch=compute_61,code=sm_61 -std=c++14 # 不同GPU有不同的算力指数,可查看算力表 |
| | | ) |
| | | set(CUDA_NVCC_FLAGS_RELWITHDEBINFO "--device-debug;-lineinfo") |
| | | #find_package(OpenCV REQUIRED) # 查找系统的默认opencv环境 |
| | | #message(${OpenCV_LIBS}) |
| | | message("-- CUDA ${CUDA_VERSION_MAJOR} detected") |
| | | set( |
| | | CUDA_NVCC_FLAGS |
| | | ${CUDA_NVCC_FLAGS}; |
| | | -gencode arch=compute_61,code=sm_61 -std=c++14# 不同GPU有不同的算力指数,可查看算力表 |
| | | -gencode arch=compute_80,code=sm_80 -std=c++14# 不同GPU有不同的算力指数,可查看算力表 |
| | | -gencode arch=compute_72,code=sm_72 -std=c++14# 不同GPU有不同的算力指数,可查看算力表 |
| | | ) |
| | | set(CUDA_NVCC_FLAGS_RELWITHDEBINFO "--device-debug;-lineinfo") |
| | | #find_package(OpenCV REQUIRED) # 查找系统的默认opencv环境 |
| | | #message(${OpenCV_LIBS}) |
| | | endif() |
| | | |
| | | |
| | | # set(OpenCV_DIR /data/disk2/opt/01_opencv/opencv4.5.1/build) |
| | | FIND_PACKAGE(OpenCV REQUIRED PATHS "/data/disk2/opt/01_opencv/opencv4.5.2") |
| | | include_directories( ${OpenCV_INCLUDE_DIRS} ) |
| | | cuda_add_library(${PROJECT_NAME} SHARED ${sources}) |
| | | target_include_directories(${PROJECT_NAME} PRIVATE ${OpenCV_INCLUDE_DIRS} src/ src/core src/utils/ src/detecter_tools/) |
| | | target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS} ${LIBS}) |
| | | |
| | | SET(LIBS |
| | | gomp |
| | | nvToolsExt |
| | | opencv_core |
| | | opencv_highgui |
| | | opencv_imgcodecs |
| | | opencv_imgproc |
| | | opencv_videoio |
| | | opencv_video |
| | | opencv_calib3d |
| | | opencv_features2d |
| | | opencv_flann |
| | | cublas |
| | | stdc++fs #设置-lstdc++fs 新链接器选项,防止std等gcc库调用不到 |
| | | curand |
| | | cuda |
| | | cudnn |
| | | jsoncpp |
| | | ) |
| | | add_executable(demo demo.cpp) |
| | | target_include_directories(demo PRIVATE src/ src/core src/utils/ src/detecter_tools/) |
| | | target_link_libraries(demo ${PROJECT_NAME}) |
| | | |
| | | # 将源码目录保存到变量中 |
| | | #aux_source_directory(./src DIR_SRCS) # 搜索当前目录下的所有.cpp文件 |
| | | #add_library(demo ${SRC_LIST}) #明确指定包含哪些源文件 |
| | | |
| | | include_directories(".") |
| | | include_directories("./lib") |
| | | include_directories("./lib/core") |
| | | include_directories("./lib/detecter_tools") |
| | | include_directories("./lib/utils") |
| | | #include_directories("/usr/include/x86_64-linux-gnu/") |
| | | #include_directories("/usr/include/json/") |
| | | #include_directories("/usr/include") |
| | | #include_directories("/usr/local/include/") |
| | | |
| | | |
| | | #设置链接库搜索目录,项目中lib路径 |
| | | link_directories(${PYTORCH_DIR}/lib) #动态链接库目录/bazel-bin/tensorflow |
| | | #link_directories(/usr/local/lib) |
| | | link_directories(${OPENCV_DIR}/lib) |
| | | #link_directories(/usr/lib/x86_64-linux-gnu/) |
| | | link_directories(/home/scheaven/anaconda3/lib/) |
| | | |
| | | |
| | | #添加要编译的可执行文件 |
| | | #cuda_add_executable(${PROJECT_NAME} |
| | | |
| | | cuda_add_executable(${PROJECT_NAME} main.cpp ${sources} ${CURRENT_HEADERS}) |
| | | #cuda_add_library(${PROJECT_NAME} SHARED ${sources} ${CURRENT_HEADERS}) |
| | | |
| | | #设置 target 需要链接的库 |
| | | target_link_libraries(${PROJECT_NAME} ${LIBS} ${Opencv_LIBS}) |
| | | # add_executable(demo demo.cpp ${sources}) |
| | | # target_include_directories(demo PRIVATE ${OpenCV_INCLUDE_DIRS} src/ src/core src/utils/ src/detecter_tools/) |
| | | # target_link_libraries(demo ${OpenCV_LIBS} ${LIBS}) |
| | |
| | | {
|
| | | "so_file_path": "/opt/vasystem/libs/yolo/libyolo.so",
|
| | | "runtime": "/opt/vasystem/libs/Yolo:/opt/vasystem/libs/protobuf/lib:/usr/local/cuda-10.0/lib64:/opt/vasystem/libs/opencv2.4.13/lib:/opt/vasystem/libs/libtorch/lib",
|
| | | "so_file_path": "/opt/vasystem/libs/Detect/libdemo.so",
|
| | | "runtime": "/opt/vasystem/libs/Detect:/usr/local/cuda-11.1/lib64:", // 项目所用到的环境
|
| | | "param": {
|
| | | "cfg_path": "/data/disk1/project/model_dump/02_yolo/cfg/tracks.cfg",
|
| | | "weights_path": "/data/disk1/project/model_dump/02_yolo/tracks.weights"
|
| | | // "model_path": "/data/disk1/project/model_dump/02_yolo/baseDetect-kFLOAT-batch16.engine" // para里边自己算法可能用到的参数
|
| | | "model_path": "/data/disk1/project/model_dump/02_yolo/baseDetetor_small.bin", // para里边自己算法可能用到的参数
|
| | | "type":2
|
| | | }
|
| | | }
|
New file |
| | |
| | | ./data\imgs\000000000009.jpg |
| | | ./data\imgs\000000000025.jpg |
| | | ./data\imgs\000000000030.jpg |
| | | ./data\imgs\000000000034.jpg |
| | | ./data\imgs\000000000036.jpg |
| | | ./data\imgs\000000000042.jpg |
| | | ./data\imgs\000000000049.jpg |
| | | ./data\imgs\000000000061.jpg |
| | | ./data\imgs\000000000064.jpg |
| | | ./data\imgs\000000000071.jpg |
| | | ./data\imgs\000000000072.jpg |
| | | ./data\imgs\000000000073.jpg |
| | | ./data\imgs\000000000074.jpg |
| | | ./data\imgs\000000000077.jpg |
| | | ./data\imgs\000000000078.jpg |
| | | ./data\imgs\000000000081.jpg |
| | | ./data\imgs\000000000089.jpg |
| | | ./data\imgs\000000000092.jpg |
| | | ./data\imgs\000000000094.jpg |
| | | ./data\imgs\000000000109.jpg |
| | | ./data\imgs\000000000110.jpg |
| | | ./data\imgs\000000000113.jpg |
| | | ./data\imgs\000000000127.jpg |
| | | ./data\imgs\000000000133.jpg |
| | | ./data\imgs\000000000136.jpg |
| | | ./data\imgs\000000000138.jpg |
| | | ./data\imgs\000000000142.jpg |
| | | ./data\imgs\000000000143.jpg |
| | | ./data\imgs\000000000144.jpg |
| | | ./data\imgs\000000000149.jpg |
| | | ./data\imgs\000000000151.jpg |
| | | ./data\imgs\000000000154.jpg |
| | | ./data\imgs\000000000164.jpg |
| | | ./data\imgs\000000000165.jpg |
| | | ./data\imgs\000000000192.jpg |
| | | ./data\imgs\000000000194.jpg |
| | | ./data\imgs\000000000196.jpg |
| | | ./data\imgs\000000000201.jpg |
| | | ./data\imgs\000000000208.jpg |
| | | ./data\imgs\000000000241.jpg |
| | | ./data\imgs\000000000247.jpg |
| | | ./data\imgs\000000000250.jpg |
| | | ./data\imgs\000000000257.jpg |
| | | ./data\imgs\000000000260.jpg |
| | | ./data\imgs\000000000263.jpg |
| | | ./data\imgs\000000000283.jpg |
| | | ./data\imgs\000000000294.jpg |
| | | ./data\imgs\000000000307.jpg |
| | | ./data\imgs\000000000308.jpg |
| | | ./data\imgs\000000000309.jpg |
| | | ./data\imgs\000000000312.jpg |
| | | ./data\imgs\000000000315.jpg |
| | | ./data\imgs\000000000321.jpg |
| | | ./data\imgs\000000000322.jpg |
| | | ./data\imgs\000000000326.jpg |
| | | ./data\imgs\000000000332.jpg |
| | | ./data\imgs\000000000338.jpg |
| | | ./data\imgs\000000000349.jpg |
| | | ./data\imgs\000000000357.jpg |
| | | ./data\imgs\000000000360.jpg |
| | | ./data\imgs\000000000368.jpg |
| | | ./data\imgs\000000000370.jpg |
| | | ./data\imgs\000000000382.jpg |
| | | ./data\imgs\000000000384.jpg |
| | | ./data\imgs\000000000387.jpg |
| | | ./data\imgs\000000000389.jpg |
| | | ./data\imgs\000000000394.jpg |
| | | ./data\imgs\000000000395.jpg |
| | | ./data\imgs\000000000397.jpg |
| | | ./data\imgs\000000000400.jpg |
| | | ./data\imgs\000000000404.jpg |
| | | ./data\imgs\000000000415.jpg |
| | | ./data\imgs\000000000419.jpg |
| | | ./data\imgs\000000000428.jpg |
| | | ./data\imgs\000000000431.jpg |
| | | ./data\imgs\000000000436.jpg |
| | | ./data\imgs\000000000438.jpg |
| | | ./data\imgs\000000000443.jpg |
| | | ./data\imgs\000000000446.jpg |
| | | ./data\imgs\000000000450.jpg |
| | | ./data\imgs\000000000459.jpg |
| | | ./data\imgs\000000000471.jpg |
| | | ./data\imgs\000000000472.jpg |
| | | ./data\imgs\000000000474.jpg |
| | | ./data\imgs\000000000486.jpg |
| | | ./data\imgs\000000000488.jpg |
| | | ./data\imgs\000000000490.jpg |
| | | ./data\imgs\000000000491.jpg |
| | | ./data\imgs\000000000502.jpg |
| | | ./data\imgs\000000000508.jpg |
| | | ./data\imgs\000000000510.jpg |
| | | ./data\imgs\000000000514.jpg |
| | | ./data\imgs\000000000520.jpg |
| | | ./data\imgs\000000000529.jpg |
| | | ./data\imgs\000000000531.jpg |
| | | ./data\imgs\000000000532.jpg |
| | | ./data\imgs\000000000536.jpg |
| | | ./data\imgs\000000000540.jpg |
| | | ./data\imgs\000000000544.jpg |
| | | ./data\imgs\000000000560.jpg |
| | | ./data\imgs\000000000562.jpg |
| | | ./data\imgs\000000000564.jpg |
| | | ./data\imgs\000000000569.jpg |
| | | ./data\imgs\000000000572.jpg |
| | | ./data\imgs\000000000575.jpg |
| | | ./data\imgs\000000000581.jpg |
| | | ./data\imgs\000000000584.jpg |
| | | ./data\imgs\000000000589.jpg |
| | | ./data\imgs\000000000590.jpg |
| | | ./data\imgs\000000000595.jpg |
| | | ./data\imgs\000000000597.jpg |
| | | ./data\imgs\000000000599.jpg |
| | | ./data\imgs\000000000605.jpg |
| | | ./data\imgs\000000000612.jpg |
| | | ./data\imgs\000000000620.jpg |
| | | ./data\imgs\000000000623.jpg |
| | | ./data\imgs\000000000625.jpg |
| | | ./data\imgs\000000000626.jpg |
| | | ./data\imgs\000000000629.jpg |
| | | ./data\imgs\000000000636.jpg |
| | | ./data\imgs\000000000641.jpg |
| | | ./data\imgs\000000000643.jpg |
| | | ./data\imgs\000000000650.jpg |
| | | ./data\imgs\000000000656.jpg |
| | | ./data\imgs\000000000659.jpg |
| | | ./data\imgs\000000000661.jpg |
| | | ./data\imgs\000000000670.jpg |
| | | ./data\imgs\000000000673.jpg |
| | | ./data\imgs\000000000675.jpg |
| | | ./data\imgs\000000000681.jpg |
| | | ./data\imgs\000000000684.jpg |
| | | ./data\imgs\000000000690.jpg |
| | | ./data\imgs\000000000692.jpg |
| | | ./data\imgs\000000000693.jpg |
| | | ./data\imgs\000000000699.jpg |
| | | ./data\imgs\000000000706.jpg |
| | | ./data\imgs\000000000711.jpg |
| | | ./data\imgs\000000000714.jpg |
| | | ./data\imgs\000000000715.jpg |
| | | ./data\imgs\000000000716.jpg |
| | | ./data\imgs\000000000722.jpg |
| | | ./data\imgs\000000000723.jpg |
| | | ./data\imgs\000000000730.jpg |
| | | ./data\imgs\000000000731.jpg |
| | | ./data\imgs\000000000735.jpg |
| | | ./data\imgs\000000000753.jpg |
| | | ./data\imgs\000000000754.jpg |
| | | ./data\imgs\000000000757.jpg |
| | | ./data\imgs\000000000761.jpg |
| | | ./data\imgs\000000000762.jpg |
| | | ./data\imgs\000000000764.jpg |
| | | ./data\imgs\000000000772.jpg |
| | | ./data\imgs\000000000775.jpg |
| | | ./data\imgs\000000000781.jpg |
| | | ./data\imgs\000000000790.jpg |
| | | ./data\imgs\000000000795.jpg |
| | | ./data\imgs\000000000797.jpg |
| | | ./data\imgs\000000000801.jpg |
| | | ./data\imgs\000000000810.jpg |
| | | ./data\imgs\000000000813.jpg |
| | | ./data\imgs\000000000825.jpg |
| | | ./data\imgs\000000000827.jpg |
| | | ./data\imgs\000000000828.jpg |
| | | ./data\imgs\000000000831.jpg |
| | | ./data\imgs\000000000836.jpg |
| | | ./data\imgs\000000000839.jpg |
| | | ./data\imgs\000000000853.jpg |
| | | ./data\imgs\000000000873.jpg |
| | | ./data\imgs\000000000882.jpg |
| | | ./data\imgs\000000000897.jpg |
| | | ./data\imgs\000000000901.jpg |
| | | ./data\imgs\000000000902.jpg |
| | | ./data\imgs\000000000908.jpg |
| | | ./data\imgs\000000000909.jpg |
| | | ./data\imgs\000000000913.jpg |
| | | ./data\imgs\000000000923.jpg |
| | | ./data\imgs\000000000925.jpg |
| | | ./data\imgs\000000000934.jpg |
| | | ./data\imgs\000000000939.jpg |
| | | ./data\imgs\000000000941.jpg |
| | | ./data\imgs\000000000943.jpg |
| | | ./data\imgs\000000000955.jpg |
| | | ./data\imgs\000000000960.jpg |
| | | ./data\imgs\000000000962.jpg |
| | | ./data\imgs\000000000965.jpg |
| | | ./data\imgs\000000000969.jpg |
| | | ./data\imgs\000000000974.jpg |
| | | ./data\imgs\000000000977.jpg |
| | | ./data\imgs\000000000982.jpg |
| | | ./data\imgs\000000000984.jpg |
| | | ./data\imgs\000000000985.jpg |
| | | ./data\imgs\000000000987.jpg |
| | | ./data\imgs\000000000996.jpg |
| | | ./data\imgs\000000000999.jpg |
| | | ./data\imgs\000000001006.jpg |
| | | ./data\imgs\000000001011.jpg |
| | | ./data\imgs\000000001014.jpg |
| | | ./data\imgs\000000001025.jpg |
| | | ./data\imgs\000000001029.jpg |
| | | ./data\imgs\000000001036.jpg |
| | | ./data\imgs\000000001053.jpg |
| | | ./data\imgs\000000001059.jpg |
| | | ./data\imgs\000000001063.jpg |
| | | ./data\imgs\000000001064.jpg |
| | | ./data\imgs\000000001072.jpg |
| | | ./data\imgs\000000001083.jpg |
| | | ./data\imgs\000000001084.jpg |
| | | ./data\imgs\000000001085.jpg |
| | | ./data\imgs\000000001089.jpg |
| | | ./data\imgs\000000001090.jpg |
| | | ./data\imgs\000000001098.jpg |
| | | ./data\imgs\000000001099.jpg |
| | | ./data\imgs\000000001102.jpg |
| | | ./data\imgs\000000001107.jpg |
| | | ./data\imgs\000000001108.jpg |
| | | ./data\imgs\000000001111.jpg |
| | | ./data\imgs\000000001122.jpg |
| | | ./data\imgs\000000001138.jpg |
| | | ./data\imgs\000000001139.jpg |
| | | ./data\imgs\000000001144.jpg |
| | | ./data\imgs\000000001145.jpg |
| | | ./data\imgs\000000001146.jpg |
| | | ./data\imgs\000000001149.jpg |
| | | ./data\imgs\000000001153.jpg |
| | | ./data\imgs\000000001155.jpg |
| | | ./data\imgs\000000001164.jpg |
| | | ./data\imgs\000000001166.jpg |
| | | ./data\imgs\000000001168.jpg |
| | | ./data\imgs\000000001171.jpg |
| | | ./data\imgs\000000001176.jpg |
| | | ./data\imgs\000000001180.jpg |
| | | ./data\imgs\000000001183.jpg |
| | | ./data\imgs\000000001200.jpg |
| | | ./data\imgs\000000001204.jpg |
| | | ./data\imgs\000000001205.jpg |
| | | ./data\imgs\000000001213.jpg |
| | | ./data\imgs\000000001216.jpg |
| | | ./data\imgs\000000001224.jpg |
| | | ./data\imgs\000000001228.jpg |
| | | ./data\imgs\000000001232.jpg |
| | | ./data\imgs\000000001237.jpg |
| | | ./data\imgs\000000001238.jpg |
| | | ./data\imgs\000000001244.jpg |
| | | ./data\imgs\000000001261.jpg |
| | | ./data\imgs\000000001264.jpg |
| | | ./data\imgs\000000001270.jpg |
| | | ./data\imgs\000000001271.jpg |
| | | ./data\imgs\000000001282.jpg |
| | | ./data\imgs\000000001290.jpg |
| | | ./data\imgs\000000001292.jpg |
| | | ./data\imgs\000000001295.jpg |
| | | ./data\imgs\000000001298.jpg |
| | | ./data\imgs\000000001306.jpg |
| | | ./data\imgs\000000001307.jpg |
| | | ./data\imgs\000000001308.jpg |
| | | ./data\imgs\000000001311.jpg |
| | | ./data\imgs\000000001315.jpg |
| | | ./data\imgs\000000001319.jpg |
| | | ./data\imgs\000000001323.jpg |
| | | ./data\imgs\000000001330.jpg |
| | | ./data\imgs\000000001332.jpg |
| | | ./data\imgs\000000001340.jpg |
| | | ./data\imgs\000000001342.jpg |
| | | ./data\imgs\000000001347.jpg |
| | | ./data\imgs\000000001355.jpg |
| | | ./data\imgs\000000001359.jpg |
| | | ./data\imgs\000000001360.jpg |
| | | ./data\imgs\000000001366.jpg |
| | | ./data\imgs\000000001369.jpg |
| | | ./data\imgs\000000001374.jpg |
New file |
| | |
| | | #配置文件(其它日志级别配置相同 TRACE DEBUG WARN ERROR): |
| | | log4cplus.rootLogger=TRACE, DEBUG_MSGS, ERROR_MSGS, WARN_MSGS |
| | | # |
| | | |
| | | ################################DEBUG#################################### |
| | | |
| | | #设置日志追加到文件尾 |
| | | log4cplus.appender.DEBUG_MSGS=log4cplus::RollingFileAppender |
| | | #log4cplus.appender.DEBUG_MSGS=log4cplus::DailyRollingFileAppender |
| | | log4cplus.appender.DEBUG_MSGS.Schedule=MINUTELY |
| | | |
| | | #设置日志文件大小 |
| | | log4cplus.appender.DEBUG_MSGS.MaxFileSize=1024MB |
| | | |
| | | #设置生成日志最大个数 |
| | | log4cplus.appender.DEBUG_MSGS.MaxBackupIndex=1 |
| | | |
| | | #设置输出日志路径 |
| | | log4cplus.appender.DEBUG_MSGS.File=../logs/trt_debug.log |
| | | log4cplus.appender.DEBUG_MSGS.layout=log4cplus::PatternLayout |
| | | |
| | | #设置日志打印格式 |
| | | log4cplus.appender.DEBUG_MSGS.layout.ConversionPattern=[%D{%Y-%m-%d %H:%M:%S}]%p%m%n |
| | | log4cplus.appender.DEBUG_MSGS.filters.1=log4cplus::spi::LogLevelMatchFilter |
| | | |
| | | #匹配相同日志级别,只有debug日志才输入到该文件中 |
| | | log4cplus.appender.DEBUG_MSGS.filters.1=log4cplus::spi::LogLevelMatchFilter |
| | | log4cplus.appender.DEBUG_MSGS.filters.1.LogLevelToMatch=DEBUG |
| | | log4cplus.appender.DEBUG_MSGS.filters.1.AcceptOnMatch=true |
| | | log4cplus.appender.DEBUG_MSGS.filters.2=log4cplus::spi::DenyAllFilter |
| | | |
| | | ################################ERROR#################################### |
| | | |
| | | #设置日志追加到文件尾 |
| | | log4cplus.appender.ERROR_MSGS=log4cplus::RollingFileAppender |
| | | |
| | | #设置日志文件大小 |
| | | log4cplus.appender.ERROR_MSGS.MaxFileSize=10240MB |
| | | |
| | | #设置生成日志最大个数 |
| | | log4cplus.appender.ERROR_MSGS.MaxBackupIndex=1 |
| | | |
| | | #设置输出日志路径 |
| | | log4cplus.appender.ERROR_MSGS.File=../logs/trt_error.log |
| | | log4cplus.appender.ERROR_MSGS.layout=log4cplus::PatternLayout |
| | | |
| | | #设置日志打印格式 |
| | | log4cplus.appender.ERROR_MSGS.layout.ConversionPattern=[%D{%Y-%m-%d %H:%M:%S}]%p%m%n |
| | | log4cplus.appender.ERROR_MSGS.filters.1=log4cplus::spi::LogLevelMatchFilter |
| | | |
| | | #匹配相同日志级别,只有debug日志才输入到该文件中 |
| | | log4cplus.appender.ERROR_MSGS.filters.1=log4cplus::spi::LogLevelMatchFilter |
| | | log4cplus.appender.ERROR_MSGS.filters.1.LogLevelToMatch=ERROR |
| | | log4cplus.appender.ERROR_MSGS.filters.1.AcceptOnMatch=true |
| | | log4cplus.appender.ERROR_MSGS.filters.2=log4cplus::spi::DenyAllFilter |
| | | |
| | | ################################WARN#################################### |
| | | |
| | | #设置日志追加到文件尾 |
| | | log4cplus.appender.WARN_MSGS=log4cplus::RollingFileAppender |
| | | |
| | | #设置日志文件大小 |
| | | log4cplus.appender.WARN_MSGS.MaxFileSize=10240MB |
| | | |
| | | #设置生成日志最大个数 |
| | | log4cplus.appender.WARN_MSGS.MaxBackupIndex=1 |
| | | |
| | | #设置输出日志路径 |
| | | log4cplus.appender.WARN_MSGS.File=../logs/trt_warn.log |
| | | log4cplus.appender.WARN_MSGS.layout=log4cplus::PatternLayout |
| | | |
| | | #设置日志打印格式 |
| | | log4cplus.appender.WARN_MSGS.layout.ConversionPattern=[%D{%Y-%m-%d %H:%M:%S}]%p%m%n |
| | | log4cplus.appender.WARN_MSGS.filters.1=log4cplus::spi::LogLevelMatchFilter |
| | | |
| | | #匹配相同日志级别,只有debug日志才输入到该文件中 |
| | | log4cplus.appender.WARN_MSGS.filters.1=log4cplus::spi::LogLevelMatchFilter |
| | | log4cplus.appender.WARN_MSGS.filters.1.LogLevelToMatch=WARN |
| | | log4cplus.appender.WARN_MSGS.filters.1.AcceptOnMatch=true |
| | | log4cplus.appender.WARN_MSGS.filters.2=log4cplus::spi::DenyAllFilter |
New file |
| | |
| | | // |
| | | // Created by Scheaven on 2019/11/18. |
| | | // |
| | | #include "src/h_interface.h" |
| | | #include "src/std_target.h" |
| | | #include <opencv2/opencv.hpp> // C++ |
| | | #include "src/utils/draw_util.h" |
| | | |
| | | using namespace std; |
| | | using namespace cv; |
| | | void* handle; |
| | | |
| | | int main(int argc, char *argv[]) |
| | | { |
| | | // //视频流信息 |
| | | VideoCapture cap; |
| | | VideoCapture cap2; |
| | | int cam_id = 0; |
| | | int cam_id2 = 1; |
| | | |
| | | Mat frame, blob; |
| | | Mat frame2, blob2; |
| | | |
| | | const char *conf = "../config.json"; |
| | | int *max_chan; |
| | | int x = 5; |
| | | max_chan=&x; |
| | | handle = create(conf, max_chan); |
| | | |
| | | void *sr; |
| | | void *sr2; |
| | | TImage *img = new TImage(); |
| | | TImage *img2 = new TImage(); |
| | | int i = 0; |
| | | cap = VideoCapture("/data/disk2/01_dataset/03_human/34983128.mp4"); |
| | | |
| | | while(1){ /* code */ |
| | | cap >> frame; |
| | | if(!frame.empty()) |
| | | { |
| | | img->width = frame.cols; |
| | | img->height = frame.rows; |
| | | img->channel = frame.channels(); |
| | | img->data = frame.data; |
| | | sr = get_result(handle, img, 0); |
| | | TResult * t_result = (TResult*) sr; |
| | | cout << "t_result->count==" << t_result->count << endl; |
| | | for (int i=0; i<t_result->count; i++){ |
| | | cout << "confidence:" << t_result->targets[i].confidence << endl; |
| | | draw_SDK_result(frame, t_result->targets[i]); |
| | | } |
| | | }else{ |
| | | cout << "------------over--" << endl; |
| | | release(handle); |
| | | release_result(sr); |
| | | break; |
| | | } |
| | | } |
| | | return 0; |
| | | } |
| | | |
New file |
| | |
| | | //
|
| | | // Created by Scheaven on 2020/3/24.
|
| | | //
|
| | |
|
| | | #ifndef INC_01_CPP_SORT_CONFIG_H
|
| | | #define INC_01_CPP_SORT_CONFIG_H
|
| | | #include "utils/timer_utils.hpp"
|
| | |
|
| | | #include <opencv2/opencv.hpp>
|
| | | #include <algorithm>
|
| | | #include <assert.h>
|
| | | #include <cmath>
|
| | | #include <fstream>
|
| | | #include <iomanip>
|
| | | #include <sstream>
|
| | | #include <sys/stat.h>
|
| | | #include <time.h>
|
| | | #include <cstdlib>
|
| | | #include <iostream>
|
| | | #include <string>
|
| | | #include "argsParser.h"
|
| | | #include "buffers.h"
|
| | | #include "common.h"
|
| | | // #include "parserOnnxConfig.h"
|
| | |
|
| | | #include <map>
|
| | | #include <chrono>
|
| | | #include "logging.h"
|
| | | #include "cuda_runtime_api.h"
|
| | | #include "NvInfer.h"
|
| | |
|
| | | struct bbox_t {
|
| | | unsigned int x, y, w, h; // (x,y) - top-left corner, (w, h) - width & height of bounded box
|
| | | float prob; // confidence - probability that the object was found correctly
|
| | | unsigned int obj_id; // class of object - from range [0, classes-1]
|
| | | unsigned int track_id; // tracking id for video (0 - untracked, 1 - inf - tracked object)
|
| | | unsigned int frames_counter; // counter of frames on which the object was detected
|
| | | float x_3d, y_3d, z_3d; // center of object (in Meters) if ZED 3D Camera is used
|
| | | };
|
| | |
|
| | | typedef struct m_staticStruct
|
| | | {
|
| | | static std::string model_path;
|
| | | static int type;
|
| | | }M_STATICSTRUCT, *P_STATICSTRUCT;
|
| | | #endif //INC_01_CPP_SORT_CONFIG_H
|
| | |
|
| | | #define CLASS_BUM 9
|
New file |
| | |
| | | //
|
| | | // Created by Scheaven on 2020/3/23.
|
| | | //
|
| | |
|
| | | #include "ari_manager.h"
|
| | |
|
| | |
|
| | | AriManager::AriManager()
|
| | | {
|
| | | Config config;
|
| | | config.net_type = COMMON;
|
| | |
|
| | | if(m_staticStruct::type==2)
|
| | | config.net_type = SMALL;
|
| | | else
|
| | | config.net_type = COMMON;
|
| | |
|
| | |
|
| | | this->detector = std::shared_ptr<Detector>(new Detector());
|
| | | this->detector->init(config);
|
| | | }
|
| | |
|
| | | AriManager::~AriManager()
|
| | | {
|
| | |
|
| | | }
|
| | |
|
| | | void AriManager::single_SDK(const int cam_id, const void *img, TResult& t_result)
|
| | | {
|
| | | TImage *frame_img = (TImage*)img;
|
| | | cv::Mat frame(Size(frame_img->width, frame_img->height), CV_8UC3);
|
| | | frame.data = (uchar*)frame_img->data;
|
| | |
|
| | | std::vector<bbox_t> result_vec;
|
| | |
|
| | | std::vector<BatchResult> batch_res;
|
| | | std::vector<cv::Mat> batch_img;
|
| | |
|
| | | // cv::Mat image0 = cv::imread("/data/disk1/project/03_tensorRT/yolo-tensorrt/configs/dog.jpg", cv::IMREAD_UNCHANGED);
|
| | | // cv::Mat image1 = cv::imread("/data/disk1/project/03_tensorRT/yolo-tensorrt/configs/person.jpg", cv::IMREAD_UNCHANGED);
|
| | | batch_img.push_back(frame.clone());
|
| | | // batch_img.push_back(image1.clone());
|
| | | // batch_img.push_back(image1.clone());
|
| | | // batch_img.push_back(image0.clone());
|
| | | // batch_img.push_back(image0.clone());
|
| | | // batch_img.push_back(image0.clone());
|
| | | // batch_img.push_back(image0.clone());
|
| | | // batch_img.push_back(image0.clone());
|
| | | // batch_img.push_back(image0.clone());
|
| | | // batch_img.push_back(image0.clone());
|
| | | // batch_img.push_back(image0.clone());
|
| | | // batch_img.push_back(image0.clone());
|
| | | // batch_img.push_back(image0.clone());
|
| | | // batch_img.push_back(image0.clone());
|
| | | // batch_img.push_back(image0.clone());
|
| | | // batch_img.push_back(image1.clone());
|
| | |
|
| | | // cv::imshow("img",image0);
|
| | | // cv::waitKey(0);
|
| | | Timer timer;
|
| | | timer.reset();
|
| | | this->detector->detect(batch_img, batch_res);
|
| | | timer.out("detect");
|
| | |
|
| | | t_result.targets = (Target*)malloc(sizeof(Target)*batch_res[0].size());
|
| | | // 将算法结果转化为标准的格式(以目标检测为例)
|
| | | int w_count = 0;
|
| | | for (const auto &result_box:batch_res[0])
|
| | | {
|
| | | // if(result_box.id == 0)
|
| | | // {
|
| | | Target target;
|
| | | init_target(&target);
|
| | |
|
| | | target.rect.left = result_box.rect.x;
|
| | | target.rect.top = result_box.rect.y;
|
| | | target.rect.right = result_box.rect.x + result_box.rect.width;
|
| | | target.rect.bottom = result_box.rect.y + result_box.rect.height;
|
| | |
|
| | | target.confidence = result_box.prob*100; // 置信度转化成百分制
|
| | | // target.id = 1; //
|
| | |
|
| | | // target.attribute 可根据实际情况来添加,输出格式要求是json格式
|
| | |
|
| | | //string attribute_json = "{";
|
| | | //attribute_json += "\"smokingScore\":" + to_string(track->smokeScore)+",";
|
| | | //if(attribute_json.length()>2)
|
| | | //{
|
| | | // attribute_json = attribute_json.substr(0, attribute_json.length()-1) +"}";
|
| | | // target.attribute = new char[strlen(attribute_json.c_str())+1];
|
| | | // target.attribute_size = strlen(attribute_json.c_str());
|
| | | // strcpy(target.attribute, attribute_json.c_str());
|
| | | //}
|
| | |
|
| | | t_result.targets[w_count] = target;
|
| | | w_count ++;
|
| | | // }
|
| | | }
|
| | | t_result.count = w_count;
|
| | | }
|
| | |
|
| | | void AriManager::init_target(Target *t){
|
| | | t->attribute = NULL;
|
| | | t->feature = NULL;
|
| | | t->id = 0;
|
| | | t->rect = TRect{0,0,0,0};
|
| | | t->confidence = 0;
|
| | | t->attribute_size = 0;
|
| | | t->feature_size = 0;
|
| | | }
|
New file |
| | |
| | | //
|
| | | // Created by Scheaven on 2020/3/23.
|
| | | //
|
| | |
|
| | | #ifndef ARI_MANAGER_H
|
| | | #define ARI_MANAGER_H
|
| | | #include "std_target.h"
|
| | | #include "config.h"
|
| | | #include "detector.h"
|
| | |
|
| | | using namespace std;
|
| | | using namespace cv;
|
| | |
|
| | | class AriManager{
|
| | | public:
|
| | | std::vector<cv::Rect> boxes;
|
| | | int cam_id; //指定待追踪摄像机编号
|
| | | Mat frame;
|
| | |
|
| | | private:
|
| | | std::shared_ptr<Detector> detector;
|
| | | static void init_target(Target *t);
|
| | |
|
| | | public:
|
| | | AriManager();
|
| | | ~AriManager();
|
| | | void single_SDK(const int cam_id, const void *img, TResult& t_result); // 检测+追踪 接口(SDK封装)
|
| | | };
|
| | |
|
| | | #endif //INC_01_CPP_SORT_TRACKER_MANAGER_H
|
| | |
|
New file |
| | |
| | | #include <cmath> |
| | | #include <stdio.h> |
| | | #include <cassert> |
| | | #include <iostream> |
| | | #include "chunk.h" |
| | | #include <cuda_runtime.h> |
| | | #define NV_CUDA_CHECK(status) \ |
| | | { \ |
| | | if (status != 0) \ |
| | | { \ |
| | | std::cout << "Cuda failure: " << cudaGetErrorString(status) << " in file " << __FILE__ \ |
| | | << " at line " << __LINE__ << std::endl; \ |
| | | abort(); \ |
| | | } \ |
| | | } |
| | | |
| | | |
| | | namespace nvinfer1 |
| | | { |
| | | Chunk::Chunk() |
| | | { |
| | | |
| | | } |
| | | Chunk::Chunk(const void* buffer, size_t size) |
| | | { |
| | | assert(size == sizeof(_n_size_split)); |
| | | _n_size_split = *reinterpret_cast<const int*>(buffer); |
| | | } |
| | | Chunk::~Chunk() |
| | | { |
| | | |
| | | } |
| | | int Chunk::getNbOutputs() const |
| | | { |
| | | return 2; |
| | | } |
| | | |
| | | Dims Chunk::getOutputDimensions(int index, const Dims* inputs, int nbInputDims) |
| | | { |
| | | assert(nbInputDims == 1); |
| | | assert(index == 0 || index == 1); |
| | | return Dims3(inputs[0].d[0] / 2, inputs[0].d[1], inputs[0].d[2]); |
| | | } |
| | | |
| | | int Chunk::initialize() |
| | | { |
| | | return 0; |
| | | } |
| | | |
| | | void Chunk::terminate() |
| | | { |
| | | } |
| | | |
| | | size_t Chunk::getWorkspaceSize(int maxBatchSize) const |
| | | { |
| | | return 0; |
| | | } |
| | | |
| | | int Chunk::enqueue(int batchSize, |
| | | const void* const* inputs, |
| | | void** outputs, |
| | | void* workspace, |
| | | cudaStream_t stream) |
| | | { |
| | | //batch |
| | | for (int b = 0; b < batchSize; ++b) |
| | | { |
| | | NV_CUDA_CHECK(cudaMemcpy((char*)outputs[0] + b * _n_size_split, (char*)inputs[0] + b * 2 * _n_size_split, _n_size_split, cudaMemcpyDeviceToDevice)); |
| | | NV_CUDA_CHECK(cudaMemcpy((char*)outputs[1] + b * _n_size_split, (char*)inputs[0] + b * 2 * _n_size_split + _n_size_split, _n_size_split, cudaMemcpyDeviceToDevice)); |
| | | } |
| | | // NV_CUDA_CHECK(cudaMemcpy(outputs[0], inputs[0], _n_size_split, cudaMemcpyDeviceToDevice)); |
| | | // NV_CUDA_CHECK(cudaMemcpy(outputs[1], (void*)((char*)inputs[0] + _n_size_split), _n_size_split, cudaMemcpyDeviceToDevice)); |
| | | return 0; |
| | | } |
| | | |
| | | size_t Chunk::getSerializationSize() const |
| | | { |
| | | return sizeof(_n_size_split); |
| | | } |
| | | |
| | | void Chunk::serialize(void *buffer)const |
| | | { |
| | | *reinterpret_cast<int*>(buffer) = _n_size_split; |
| | | } |
| | | |
| | | const char* Chunk::getPluginType()const |
| | | { |
| | | return "CHUNK_TRT"; |
| | | } |
| | | const char* Chunk::getPluginVersion() const |
| | | { |
| | | return "1.0"; |
| | | } |
| | | |
| | | void Chunk::destroy() |
| | | { |
| | | delete this; |
| | | } |
| | | |
| | | void Chunk::setPluginNamespace(const char* pluginNamespace) |
| | | { |
| | | _s_plugin_namespace = pluginNamespace; |
| | | } |
| | | |
| | | const char* Chunk::getPluginNamespace() const |
| | | { |
| | | return _s_plugin_namespace.c_str(); |
| | | } |
| | | |
| | | DataType Chunk::getOutputDataType(int index, |
| | | const nvinfer1::DataType* inputTypes, |
| | | int nbInputs) const |
| | | { |
| | | assert(index == 0 || index == 1); |
| | | return DataType::kFLOAT; |
| | | } |
| | | |
| | | bool Chunk::isOutputBroadcastAcrossBatch(int outputIndex, const bool* inputIsBroadcasted, int nbInputs) const |
| | | { |
| | | return false; |
| | | } |
| | | |
| | | bool Chunk::canBroadcastInputAcrossBatch(int inputIndex) const |
| | | { |
| | | return false; |
| | | } |
| | | |
| | | void Chunk::attachToContext(cudnnContext* cudnnContext, cublasContext* cublasContext, IGpuAllocator* gpuAllocator) {} |
| | | |
| | | void Chunk::configurePlugin(const PluginTensorDesc* in, int nbInput, const PluginTensorDesc* out, int nbOutput) |
| | | { |
| | | _n_size_split = in->dims.d[0] / 2 * in->dims.d[1] * in->dims.d[2] *sizeof(float); |
| | | } |
| | | void Chunk::detachFromContext() {} |
| | | |
| | | // Clone the plugin |
| | | IPluginV2IOExt* Chunk::clone() const |
| | | { |
| | | Chunk *p = new Chunk(); |
| | | p->_n_size_split = _n_size_split; |
| | | p->setPluginNamespace(_s_plugin_namespace.c_str()); |
| | | return p; |
| | | } |
| | | |
| | | //---------------------------- |
| | | PluginFieldCollection ChunkPluginCreator::_fc{}; |
| | | std::vector<PluginField> ChunkPluginCreator::_vec_plugin_attributes; |
| | | |
| | | ChunkPluginCreator::ChunkPluginCreator() |
| | | { |
| | | _vec_plugin_attributes.clear(); |
| | | _fc.nbFields = _vec_plugin_attributes.size(); |
| | | _fc.fields = _vec_plugin_attributes.data(); |
| | | } |
| | | |
| | | const char* ChunkPluginCreator::getPluginName() const |
| | | { |
| | | return "CHUNK_TRT"; |
| | | } |
| | | |
| | | const char* ChunkPluginCreator::getPluginVersion() const |
| | | { |
| | | return "1.0"; |
| | | } |
| | | |
| | | const PluginFieldCollection* ChunkPluginCreator::getFieldNames() |
| | | { |
| | | return &_fc; |
| | | } |
| | | |
| | | IPluginV2IOExt* ChunkPluginCreator::createPlugin(const char* name, const PluginFieldCollection* fc) |
| | | { |
| | | Chunk* obj = new Chunk(); |
| | | obj->setPluginNamespace(_s_name_space.c_str()); |
| | | return obj; |
| | | } |
| | | |
| | | IPluginV2IOExt* ChunkPluginCreator::deserializePlugin(const char* name, const void* serialData, size_t serialLength) |
| | | { |
| | | Chunk* obj = new Chunk(serialData,serialLength); |
| | | obj->setPluginNamespace(_s_name_space.c_str()); |
| | | return obj; |
| | | } |
| | | |
| | | void ChunkPluginCreator::setPluginNamespace(const char* libNamespace) |
| | | { |
| | | _s_name_space = libNamespace; |
| | | } |
| | | |
| | | const char* ChunkPluginCreator::getPluginNamespace() const |
| | | { |
| | | return _s_name_space.c_str(); |
| | | } |
| | | }//namespace nvinfer1 |
New file |
| | |
| | | #ifndef CHUNK_H_ |
| | | #define CHUNK_H_ |
| | | |
| | | //#include "NvInfer.h" |
| | | //#include "NvInferPlugin.h" |
| | | //#include "NvInferRuntimeCommon.h" |
| | | //#include <cuda_runtime.h> |
| | | //#include <iostream> |
| | | //#include <memory> |
| | | //#include <sstream> |
| | | //#include <string> |
| | | //#include <cassert> |
| | | //#include <vector> |
| | | |
| | | #include <string> |
| | | #include <vector> |
| | | #include "NvInfer.h" |
| | | |
| | | namespace nvinfer1 |
| | | { |
| | | class Chunk : public IPluginV2IOExt |
| | | { |
| | | public: |
| | | Chunk(); |
| | | Chunk(const void* buffer, size_t length); |
| | | ~Chunk(); |
| | | int getNbOutputs()const override; |
| | | Dims getOutputDimensions(int index, const Dims* inputs, int nbInputDims) override; |
| | | int initialize() override; |
| | | void terminate() override; |
| | | size_t getWorkspaceSize(int maxBatchSize) const override; |
| | | int enqueue(int batchSize, const void* const* inputs, void** outputs, void* workspace, cudaStream_t stream)override; |
| | | size_t getSerializationSize() const override; |
| | | void serialize(void* buffer) const override; |
| | | const char* getPluginType() const override; |
| | | const char* getPluginVersion() const override; |
| | | void destroy() override; |
| | | void setPluginNamespace(const char* pluginNamespace) override; |
| | | const char* getPluginNamespace() const override; |
| | | DataType getOutputDataType(int index, const nvinfer1::DataType* inputTypes, int nbInputs) const override; |
| | | bool isOutputBroadcastAcrossBatch(int outputIndex, const bool* inputIsBroadcasted, int nbInputs) const override; |
| | | bool canBroadcastInputAcrossBatch(int inputIndex) const override; |
| | | void attachToContext( |
| | | cudnnContext* cudnnContext, cublasContext* cublasContext, IGpuAllocator* gpuAllocator) override; |
| | | void configurePlugin(const PluginTensorDesc* in, int nbInput, const PluginTensorDesc* out, int nbOutput) override; |
| | | void detachFromContext() override; |
| | | bool supportsFormatCombination(int pos, const PluginTensorDesc* inOut, int nbInputs, int nbOutputs) const override |
| | | { |
| | | return inOut[pos].format == TensorFormat::kLINEAR && inOut[pos].type == DataType::kFLOAT; |
| | | } |
| | | IPluginV2IOExt* clone() const override; |
| | | private: |
| | | std::string _s_plugin_namespace; |
| | | int _n_size_split; |
| | | }; |
| | | |
| | | class ChunkPluginCreator : public IPluginCreator |
| | | { |
| | | public: |
| | | ChunkPluginCreator(); |
| | | ~ChunkPluginCreator() override = default; |
| | | const char* getPluginName()const override; |
| | | const char* getPluginVersion() const override; |
| | | const PluginFieldCollection* getFieldNames() override; |
| | | IPluginV2IOExt* createPlugin(const char* name, const PluginFieldCollection* fc) override; |
| | | IPluginV2IOExt* deserializePlugin(const char* name, const void* serialData, size_t serialLength) override; |
| | | void setPluginNamespace(const char* libNamespace) override; |
| | | const char* getPluginNamespace() const override; |
| | | private: |
| | | std::string _s_name_space; |
| | | static PluginFieldCollection _fc; |
| | | static std::vector<PluginField> _vec_plugin_attributes; |
| | | }; |
| | | |
| | | }//nampespace nvinfer1 |
| | | |
| | | |
| | | #endif |
| | | |
| | | |
New file |
| | |
| | | #include "detector.h" |
| | | |
| | | |
| | | Detector::Detector() |
| | | { |
| | | |
| | | } |
| | | |
| | | Detector::~Detector() |
| | | { |
| | | |
| | | } |
| | | |
| | | void Detector::init(const Config &config) |
| | | { |
| | | this->_config = config; |
| | | this->set_gpu_id(_config.gpu_id); |
| | | this->parse_config(); |
| | | this->build_net(); |
| | | } |
| | | |
| | | void Detector::detect(const std::vector<cv::Mat> &vec_image, std::vector<BatchResult> &vec_batch_result) |
| | | { |
| | | Timer timer; |
| | | std::vector<DsImage> vec_ds_images; |
| | | vec_batch_result.clear(); |
| | | vec_batch_result.resize(vec_image.size()); |
| | | // std::cout << _p_net->getInputW()<< ":"<< _p_net->getInputH()<< std::endl; |
| | | for (const auto &img:vec_image) |
| | | { |
| | | vec_ds_images.emplace_back(img, _vec_net_type[_config.net_type], _p_net->getInputH(), _p_net->getInputW()); |
| | | } |
| | | cv::Mat trtInput = blobFromDsImages(vec_ds_images, _p_net->getInputH(),_p_net->getInputW()); |
| | | timer.out("pre"); |
| | | |
| | | _p_net->doInference(trtInput.data, vec_ds_images.size()); |
| | | timer.reset(); |
| | | for (uint32_t i = 0; i < vec_ds_images.size(); ++i) |
| | | { |
| | | auto curImage = vec_ds_images.at(i); |
| | | auto binfo = _p_net->decodeDetections(i, curImage.getImageHeight(), curImage.getImageWidth()); |
| | | // exit(0); |
| | | std::cout << binfo.size()<<":binfo"<<std::endl; |
| | | auto remaining = nmsAllClasses(_p_net->getNMSThresh(), |
| | | binfo, |
| | | _p_net->getNumClasses(), |
| | | _vec_net_type[_config.net_type]); |
| | | if (remaining.empty()) |
| | | { |
| | | continue; |
| | | } |
| | | std::vector<Result> vec_result(0); |
| | | for (const auto &b : remaining) |
| | | { |
| | | Result res; |
| | | res.id = b.label; |
| | | res.prob = b.prob; |
| | | // std::cout << "b.prob: " << b.prob << std::endl; |
| | | const int x = b.box.x1; |
| | | const int y = b.box.y1; |
| | | const int w = b.box.x2 - b.box.x1; |
| | | const int h = b.box.y2 - b.box.y1; |
| | | res.rect = cv::Rect(x, y, w, h); |
| | | vec_result.push_back(res); |
| | | } |
| | | vec_batch_result[i] = vec_result; |
| | | } |
| | | timer.out("post"); |
| | | DEBUG("--detect over--" ); |
| | | |
| | | } |
| | | |
| | | void Detector::set_gpu_id(const int id) |
| | | { |
| | | cudaError_t status = cudaSetDevice(id); |
| | | if (status != cudaSuccess) |
| | | { |
| | | /* code */ |
| | | DEBUG((boost::format("gpu id: %d not exist !" )%id).str()); |
| | | assert(0); |
| | | } |
| | | } |
| | | |
| | | void Detector::parse_config() |
| | | { |
| | | _info.precision = _vec_precision[_config.inference_precison]; |
| | | _info.deviceType = "kGPU"; |
| | | _info.inputBlobName = "data"; |
| | | _infer_param.printPerfInfo = false; |
| | | _infer_param.printPredictionInfo = false; |
| | | _infer_param.calibImagesPath = ""; |
| | | _infer_param.probThresh = _config.detect_thresh; |
| | | _infer_param.nmsThresh = 0.5; |
| | | } |
| | | |
| | | void Detector::build_net() |
| | | { |
| | | if(_config.net_type == COMMON) |
| | | _p_net = std::unique_ptr<Detecter>{new Detecter(_info,_infer_param,0)}; |
| | | else{ |
| | | _p_net = std::unique_ptr<Detecter>{new Detecter(_info,_infer_param,1)}; |
| | | } |
| | | } |
New file |
| | |
| | | #ifndef DETECTOR_H |
| | | #define DETECTOR_H |
| | | #include <iostream> |
| | | #include <opencv2/opencv.hpp> |
| | | #include "config.h" |
| | | #include <experimental/filesystem> |
| | | #include <fstream> |
| | | #include <string> |
| | | #include <chrono> |
| | | #include <stdio.h> |
| | | #include "model.h" |
| | | |
| | | |
| | | struct Result |
| | | { |
| | | int id = -1; |
| | | float prob = 0.f; |
| | | cv::Rect rect; |
| | | }; |
| | | |
| | | using BatchResult = std::vector<Result>; |
| | | |
| | | enum ModelType |
| | | { |
| | | COMMON, |
| | | SMALL |
| | | }; |
| | | |
| | | enum Precision |
| | | { |
| | | INT8 = 0, |
| | | FP16, |
| | | FP32 |
| | | }; |
| | | |
| | | struct Config |
| | | { |
| | | float detect_thresh = 0.1; |
| | | |
| | | Precision inference_precison = FP32; |
| | | |
| | | ModelType net_type = COMMON; |
| | | |
| | | int gpu_id = 0; |
| | | }; |
| | | |
| | | class Detector |
| | | { |
| | | private: |
| | | Config _config; |
| | | NetworkInfo _info; |
| | | InferParams _infer_param; |
| | | std::vector<std::string> _vec_net_type{ "common","small"}; |
| | | std::vector<std::string> _vec_precision{ "kINT8","kHALF","kFLOAT" }; |
| | | std::unique_ptr<Detecter> _p_net = nullptr; |
| | | Timer _m_timer; |
| | | |
| | | |
| | | |
| | | public: |
| | | Detector(); |
| | | ~Detector(); |
| | | |
| | | void init(const Config &config); |
| | | void detect(const std::vector<cv::Mat> &vec_image, |
| | | std::vector<BatchResult> &vec_batch_result); |
| | | |
| | | private: |
| | | void set_gpu_id(const int id=0); |
| | | |
| | | void parse_config(); |
| | | |
| | | void build_net(); |
| | | |
| | | // std::vector<BBoxInfo> decodeTensor(const int imageIdx, const int imageH, const int imageW, const TensorInfo& tensor) override; |
| | | }; |
| | | |
| | | #endif |
New file |
| | |
| | | #include "ds_image.h" |
| | | #include <experimental/filesystem> |
| | | |
| | | DsImage::DsImage() : |
| | | m_Height(0), |
| | | m_Width(0), |
| | | m_XOffset(0), |
| | | m_YOffset(0), |
| | | m_ScalingFactor(0.0), |
| | | m_RNG(cv::RNG(unsigned(std::time(0)))), |
| | | m_ImageName() |
| | | { |
| | | } |
| | | |
| | | DsImage::DsImage(const cv::Mat& mat_image_, const std::string &s_net_type_, const int& inputH, const int& inputW) : |
| | | m_Height(0), |
| | | m_Width(0), |
| | | m_XOffset(0), |
| | | m_YOffset(0), |
| | | m_ScalingFactor(0.0), |
| | | m_RNG(cv::RNG(unsigned(std::time(0)))), |
| | | m_ImageName() |
| | | { |
| | | m_OrigImage = mat_image_; |
| | | m_Height = m_OrigImage.rows; |
| | | m_Width = m_OrigImage.cols; |
| | | if (!m_OrigImage.data || m_OrigImage.cols <= 0 || m_OrigImage.rows <= 0) |
| | | { |
| | | std::cout << "empty image !"<< std::endl; |
| | | assert(0); |
| | | } |
| | | if (m_OrigImage.channels() != 3) |
| | | { |
| | | std::cout << "Non RGB images are not supported "<< std::endl; |
| | | assert(0); |
| | | } |
| | | cv::resize(m_OrigImage, m_LetterboxImage, cv::Size(inputW, inputH), 0, 0, cv::INTER_CUBIC); |
| | | // converting to RGB |
| | | //cv::cvtColor(m_LetterboxImage, m_LetterboxImage, cv::COLOR_BGR2RGB); |
| | | |
| | | } |
| | | DsImage::DsImage(const std::string& path, const std::string &s_net_type_, const int& inputH, const int& inputW) : |
| | | m_Height(0), |
| | | m_Width(0), |
| | | m_XOffset(0), |
| | | m_YOffset(0), |
| | | m_ScalingFactor(0.0), |
| | | m_RNG(cv::RNG(unsigned(std::time(0)))), |
| | | m_ImageName() |
| | | { |
| | | m_ImageName = std::experimental::filesystem::path(path).stem().string(); |
| | | m_OrigImage = cv::imread(path, cv::IMREAD_UNCHANGED); |
| | | m_Height = m_OrigImage.rows; |
| | | m_Width = m_OrigImage.cols; |
| | | if (!m_OrigImage.data || m_OrigImage.cols <= 0 || m_OrigImage.rows <= 0) |
| | | { |
| | | std::cout << "Unable to open image : " << path << std::endl; |
| | | assert(0); |
| | | } |
| | | |
| | | if (m_OrigImage.channels() != 3) |
| | | { |
| | | std::cout << "Non RGB images are not supported : " << path << std::endl; |
| | | assert(0); |
| | | } |
| | | |
| | | cv::resize(m_OrigImage, m_LetterboxImage, cv::Size(inputW, inputH), 0, 0, cv::INTER_CUBIC); |
| | | // converting to RGB |
| | | // cv::cvtColor(m_LetterboxImage, m_LetterboxImage, cv::COLOR_BGR2RGB); |
| | | |
| | | } |
| | | |
| | | void DsImage::letterbox(const int& inputH, const int& inputW) |
| | | { |
| | | //m_OrigImage.copyTo(m_MarkedImage); |
| | | m_Height = m_OrigImage.rows; |
| | | m_Width = m_OrigImage.cols; |
| | | |
| | | // resize the DsImage with scale |
| | | float dim = std::max(m_Height, m_Width); |
| | | int resizeH = ((m_Height / dim) * inputH); |
| | | int resizeW = ((m_Width / dim) * inputW); |
| | | m_ScalingFactor = static_cast<float>(resizeH) / static_cast<float>(m_Height); |
| | | float m_ScalingFactorw = static_cast<float>(resizeW) / static_cast<float>(m_Width); |
| | | |
| | | // Additional checks for images with non even dims |
| | | if ((inputW - resizeW) % 2) resizeW--; |
| | | if ((inputH - resizeH) % 2) resizeH--; |
| | | assert((inputW - resizeW) % 2 == 0); |
| | | assert((inputH - resizeH) % 2 == 0); |
| | | |
| | | m_XOffset = (inputW - resizeW) / 2; |
| | | m_YOffset = (inputH - resizeH) / 2; |
| | | |
| | | assert(2 * m_XOffset + resizeW == inputW); |
| | | assert(2 * m_YOffset + resizeH == inputH); |
| | | |
| | | // resizing |
| | | cv::resize(m_OrigImage, m_LetterboxImage, cv::Size(resizeW, resizeH), 0, 0, cv::INTER_CUBIC); |
| | | // letterboxing |
| | | cv::copyMakeBorder(m_LetterboxImage, m_LetterboxImage, m_YOffset, m_YOffset, m_XOffset, |
| | | m_XOffset, cv::BORDER_CONSTANT, cv::Scalar(128, 128, 128)); |
| | | // cv::imwrite("letter.jpg", m_LetterboxImage); |
| | | // converting to RGB |
| | | cv::cvtColor(m_LetterboxImage, m_LetterboxImage, cv::COLOR_BGR2RGB); |
| | | } |
| | | |
| | | |
| | | void DsImage::addBBox(BBoxInfo box, const std::string& labelName) |
| | | { |
| | | m_Bboxes.push_back(box); |
| | | const int x = box.box.x1; |
| | | const int y = box.box.y1; |
| | | const int w = box.box.x2 - box.box.x1; |
| | | const int h = box.box.y2 - box.box.y1; |
| | | const cv::Scalar color |
| | | = cv::Scalar(m_RNG.uniform(0, 255), m_RNG.uniform(0, 255), m_RNG.uniform(0, 255)); |
| | | |
| | | cv::rectangle(m_MarkedImage, cv::Rect(x, y, w, h), color, 1); |
| | | const cv::Size tsize |
| | | = cv::getTextSize(labelName, cv::FONT_HERSHEY_COMPLEX_SMALL, 0.5, 1, nullptr); |
| | | cv::rectangle(m_MarkedImage, cv::Rect(x, y, tsize.width + 3, tsize.height + 4), color, -1); |
| | | cv::putText(m_MarkedImage, labelName.c_str(), cv::Point(x, y + tsize.height), |
| | | cv::FONT_HERSHEY_COMPLEX_SMALL, 0.5, cv::Scalar(255, 255, 255), 1); |
| | | } |
| | | |
| | | void DsImage::showImage() const |
| | | { |
| | | cv::namedWindow(m_ImageName); |
| | | cv::imshow(m_ImageName.c_str(), m_MarkedImage); |
| | | cv::waitKey(0); |
| | | } |
| | | |
| | | void DsImage::saveImageJPEG(const std::string& dirPath) const |
| | | { |
| | | cv::imwrite(dirPath + m_ImageName + ".jpeg", m_MarkedImage); |
| | | } |
| | | std::string DsImage::exportJson() const |
| | | { |
| | | if (m_Bboxes.empty()) return ""; |
| | | std::stringstream json; |
| | | json.precision(2); |
| | | json << std::fixed; |
| | | for (uint32_t i = 0; i < m_Bboxes.size(); ++i) |
| | | { |
| | | json << "\n{\n"; |
| | | json << " \"image_id\" : " << std::stoi(m_ImageName) << ",\n"; |
| | | json << " \"category_id\" : " << m_Bboxes.at(i).classId << ",\n"; |
| | | json << " \"bbox\" : "; |
| | | json << "[" << m_Bboxes.at(i).box.x1 << ", " << m_Bboxes.at(i).box.y1 << ", "; |
| | | json << m_Bboxes.at(i).box.x2 - m_Bboxes.at(i).box.x1 << ", " |
| | | << m_Bboxes.at(i).box.y2 - m_Bboxes.at(i).box.y1 << "],\n"; |
| | | json << " \"score\" : " << m_Bboxes.at(i).prob << "\n"; |
| | | if (i != m_Bboxes.size() - 1) |
| | | json << "},"; |
| | | else |
| | | json << "}"; |
| | | } |
| | | return json.str(); |
| | | } |
New file |
| | |
| | | #ifndef __IMAGE_H__ |
| | | #define __IMAGE_H__ |
| | | |
| | | #include "trt_utils.h" |
| | | |
| | | struct BBoxInfo; |
| | | |
| | | class DsImage |
| | | { |
| | | public: |
| | | DsImage(); |
| | | DsImage(const std::string& path, const std::string &s_net_type_, const int& inputH, const int& inputW); |
| | | DsImage(const cv::Mat& mat_image_, const std::string &s_net_type_, const int& inputH, const int& inputW); |
| | | int getImageHeight() const { return m_Height; } |
| | | int getImageWidth() const { return m_Width; } |
| | | cv::Mat getLetterBoxedImage() const { return m_LetterboxImage; } |
| | | cv::Mat getOriginalImage() const { return m_OrigImage; } |
| | | std::string getImageName() const { return m_ImageName; } |
| | | void addBBox(BBoxInfo box, const std::string& labelName); |
| | | void showImage() const; |
| | | void saveImageJPEG(const std::string& dirPath) const; |
| | | std::string exportJson() const; |
| | | void letterbox(const int& inputH, const int& inputW); |
| | | private: |
| | | int m_Height; |
| | | int m_Width; |
| | | int m_XOffset; |
| | | int m_YOffset; |
| | | float m_ScalingFactor; |
| | | std::string m_ImagePath; |
| | | cv::RNG m_RNG; |
| | | std::string m_ImageName; |
| | | std::vector<BBoxInfo> m_Bboxes; |
| | | |
| | | // unaltered original Image |
| | | cv::Mat m_OrigImage; |
| | | // letterboxed Image given to the network as input |
| | | cv::Mat m_LetterboxImage; |
| | | // final image marked with the bounding boxes |
| | | cv::Mat m_MarkedImage; |
| | | }; |
| | | |
| | | #endif |
New file |
| | |
| | | //sys |
| | | #include <cmath> |
| | | #include <stdio.h> |
| | | #include <cassert> |
| | | #include <iostream> |
| | | #include <cuda_runtime.h> |
| | | #include <cuda.h> |
| | | #include <stdint.h> |
| | | #include <string.h> |
| | | //my |
| | | #include "hardswish.h" |
| | | |
| | | #define NV_CUDA_CHECK(status) \ |
| | | { \ |
| | | if (status != 0) \ |
| | | { \ |
| | | std::cout << "Cuda failure: " << cudaGetErrorString(status) << " in file " << __FILE__ \ |
| | | << " at line " << __LINE__ << std::endl; \ |
| | | abort(); \ |
| | | } \ |
| | | } |
| | | |
| | | namespace nvinfer1 |
| | | { |
| | | Hardswish::Hardswish() |
| | | { |
| | | cudaDeviceProp prop; |
| | | cudaGetDeviceProperties(&prop, 0); |
| | | _n_max_thread_pre_block = prop.maxThreadsPerBlock; |
| | | // printf("Hardswish():%d\n", _n_max_thread_pre_block); |
| | | } |
| | | |
| | | Hardswish::Hardswish(const void* data, size_t length) |
| | | { |
| | | const char *d = reinterpret_cast<const char*>(data), *a = d; |
| | | r(d, _n_max_thread_pre_block); |
| | | r(d, _n_output_size); |
| | | // printf("r:threads:%d,size:%d\n", _n_max_thread_pre_block, _n_output_size); |
| | | assert(d == a + length); |
| | | } |
| | | |
| | | Hardswish::~Hardswish() |
| | | {} |
| | | |
| | | __global__ void kernel_hardswish(const float *input_, float *output_, int n_data_size_) |
| | | { |
| | | int i = threadIdx.x + blockIdx.x * blockDim.x; |
| | | if (i >= n_data_size_)return; |
| | | if (input_[i] >= 3.0f) |
| | | { |
| | | output_[i] = input_[i]; |
| | | } |
| | | else if (input_[i] <= -3.0f) |
| | | { |
| | | output_[i] = 0.0f; |
| | | } |
| | | else |
| | | { |
| | | output_[i] = input_[i] * (input_[i] + 3.0f) / 6.0f; |
| | | } |
| | | } |
| | | |
| | | cudaError_t cuda_hardswish_layer(const void* input_, |
| | | void* output_, |
| | | const int n_batch_size_, |
| | | const int n_output_size_, |
| | | const int threads_, |
| | | cudaStream_t stream_) |
| | | { |
| | | int n_data_size = n_batch_size_ * n_output_size_; |
| | | // printf("cuda_hardswish_layer:%d,size:%d\n", n_batch_size_, n_output_size_); |
| | | kernel_hardswish << <(n_data_size + threads_ -1)/threads_, threads_ >> >( |
| | | reinterpret_cast<const float*>(input_), |
| | | reinterpret_cast<float*>(output_), |
| | | n_data_size); |
| | | return cudaGetLastError(); |
| | | } |
| | | |
| | | int Hardswish::enqueue(int batchSize, const void* const* inputs, void** outputs, void* workspace, |
| | | cudaStream_t stream) |
| | | { |
| | | // printf("batch_size:%d,output_size:%d,threads:%d\n", batchSize, _n_output_size, _n_max_thread_pre_block); |
| | | NV_CUDA_CHECK(cuda_hardswish_layer(inputs[0], outputs[0], batchSize, _n_output_size , _n_max_thread_pre_block,stream)); |
| | | return 0; |
| | | } |
| | | |
| | | size_t Hardswish::getSerializationSize() const |
| | | { |
| | | return sizeof(_n_max_thread_pre_block) +sizeof(_n_output_size); |
| | | } |
| | | |
| | | void Hardswish::serialize(void *buffer) const |
| | | { |
| | | char *d = static_cast<char*>(buffer), *a = d; |
| | | w(d, _n_max_thread_pre_block); |
| | | w(d, _n_output_size); |
| | | // printf("serialize:%d,%d\n", _n_max_thread_pre_block, _n_output_size); |
| | | assert(d == a + getSerializationSize()); |
| | | } |
| | | |
| | | void Hardswish::configurePlugin(const PluginTensorDesc* in, int nbInput, const PluginTensorDesc* out, int nbOutput) |
| | | { |
| | | |
| | | _n_output_size = in->dims.d[0] * in->dims.d[1] * in->dims.d[2]; |
| | | // printf("configurePlugin:%d,%d,%d\n", in->dims.d[0], in->dims.d[1], in->dims.d[2]); |
| | | } |
| | | IPluginV2IOExt* Hardswish::clone() const |
| | | { |
| | | Hardswish *p = new Hardswish(); |
| | | p->setPluginNamespace(_s_plugin_namespace.c_str()); |
| | | p->_n_max_thread_pre_block = _n_max_thread_pre_block; |
| | | p->_n_output_size = _n_output_size; |
| | | // printf("clone:%d,%d\n", _n_max_thread_pre_block, _n_output_size); |
| | | return p; |
| | | } |
| | | |
| | | |
| | | // |
| | | PluginFieldCollection HardswishPluginCreator::_fc{}; |
| | | std::vector<PluginField> HardswishPluginCreator::_vec_plugin_attributes; |
| | | |
| | | HardswishPluginCreator::HardswishPluginCreator() |
| | | { |
| | | _vec_plugin_attributes.clear(); |
| | | _fc.nbFields = _vec_plugin_attributes.size(); |
| | | _fc.fields = _vec_plugin_attributes.data(); |
| | | } |
| | | |
| | | const char* HardswishPluginCreator::getPluginName() const |
| | | { |
| | | return "HARDSWISH_TRT"; |
| | | } |
| | | |
| | | const char* HardswishPluginCreator::getPluginVersion() const |
| | | { |
| | | return "1.0"; |
| | | } |
| | | |
| | | const PluginFieldCollection* HardswishPluginCreator::getFieldNames() |
| | | { |
| | | return &_fc; |
| | | } |
| | | |
| | | IPluginV2IOExt* HardswishPluginCreator::createPlugin(const char* name, const PluginFieldCollection* fc) |
| | | { |
| | | Hardswish* obj = new Hardswish(); |
| | | obj->setPluginNamespace(_s_name_space.c_str()); |
| | | return obj; |
| | | } |
| | | |
| | | IPluginV2IOExt* HardswishPluginCreator::deserializePlugin(const char* name, const void* serialData, size_t serialLength) |
| | | { |
| | | Hardswish* obj = new Hardswish(serialData, serialLength); |
| | | obj->setPluginNamespace(_s_name_space.c_str()); |
| | | return obj; |
| | | } |
| | | |
| | | void HardswishPluginCreator::setPluginNamespace(const char* libNamespace) |
| | | { |
| | | _s_name_space = libNamespace; |
| | | } |
| | | |
| | | const char* HardswishPluginCreator::getPluginNamespace() const |
| | | { |
| | | return _s_name_space.c_str(); |
| | | } |
| | | }//end namespace nvinfer1 |
New file |
| | |
| | | |
| | | #ifndef _HARDSWISH_H_ |
| | | #define _HARDSWISH_H_ |
| | | |
| | | #include <string> |
| | | #include <vector> |
| | | #include "NvInfer.h" |
| | | |
| | | namespace nvinfer1 |
| | | { |
| | | template <typename T> |
| | | void w(char*& buffer, const T& val) |
| | | { |
| | | *reinterpret_cast<T*>(buffer) = val; |
| | | buffer += sizeof(T); |
| | | } |
| | | |
| | | template <typename T> |
| | | void r(const char*& buffer, T& val) |
| | | { |
| | | val = *reinterpret_cast<const T*>(buffer); |
| | | buffer += sizeof(T); |
| | | } |
| | | |
| | | class Hardswish :public IPluginV2IOExt |
| | | { |
| | | public: |
| | | Hardswish(); |
| | | Hardswish(const void* data, size_t length); |
| | | ~Hardswish(); |
| | | int getNbOutputs()const override |
| | | { |
| | | return 1; |
| | | } |
| | | Dims getOutputDimensions(int index, const Dims* inputs, int nbInputDims) override |
| | | { |
| | | return inputs[0]; |
| | | } |
| | | int initialize() override |
| | | { |
| | | return 0; |
| | | } |
| | | void terminate() override |
| | | { |
| | | } |
| | | size_t getWorkspaceSize(int maxBatchSize) const override |
| | | { |
| | | return 0; |
| | | } |
| | | int enqueue(int batchSize, const void* const* inputs, void** outputs, void* workspace, cudaStream_t stream)override; |
| | | size_t getSerializationSize() const override; |
| | | void serialize(void* buffer) const override; |
| | | const char* getPluginType() const override |
| | | { |
| | | return "HARDSWISH_TRT"; |
| | | } |
| | | const char* getPluginVersion() const override |
| | | { |
| | | return "1.0"; |
| | | } |
| | | void destroy() override |
| | | { |
| | | delete this; |
| | | } |
| | | void setPluginNamespace(const char* pluginNamespace) override |
| | | { |
| | | _s_plugin_namespace = pluginNamespace; |
| | | } |
| | | const char* getPluginNamespace() const override |
| | | { |
| | | return _s_plugin_namespace.c_str(); |
| | | } |
| | | DataType getOutputDataType(int index, const nvinfer1::DataType* inputTypes, int nbInputs) const override |
| | | { |
| | | return DataType::kFLOAT; |
| | | } |
| | | bool isOutputBroadcastAcrossBatch(int outputIndex, const bool* inputIsBroadcasted, int nbInputs) const override |
| | | { |
| | | return false; |
| | | } |
| | | bool canBroadcastInputAcrossBatch(int inputIndex) const override |
| | | { |
| | | return false; |
| | | } |
| | | void attachToContext( |
| | | cudnnContext* cudnnContext, cublasContext* cublasContext, IGpuAllocator* gpuAllocator) override |
| | | {} |
| | | void configurePlugin(const PluginTensorDesc* in, int nbInput, const PluginTensorDesc* out, int nbOutput) override; |
| | | void detachFromContext() override |
| | | {} |
| | | bool supportsFormatCombination(int pos, const PluginTensorDesc* inOut, int nbInputs, int nbOutputs) const override |
| | | { |
| | | return inOut[pos].format == TensorFormat::kLINEAR && inOut[pos].type == DataType::kFLOAT; |
| | | } |
| | | IPluginV2IOExt* clone() const override; |
| | | private: |
| | | |
| | | uint32_t _n_max_thread_pre_block; |
| | | uint32_t _n_output_size; |
| | | std::string _s_plugin_namespace; |
| | | }; //end detect |
| | | |
| | | class HardswishPluginCreator : public IPluginCreator |
| | | { |
| | | public: |
| | | HardswishPluginCreator(); |
| | | ~HardswishPluginCreator() override = default; |
| | | const char* getPluginName()const override; |
| | | const char* getPluginVersion() const override; |
| | | const PluginFieldCollection* getFieldNames() override; |
| | | IPluginV2IOExt* createPlugin(const char* name, const PluginFieldCollection* fc) override; |
| | | IPluginV2IOExt* deserializePlugin(const char* name, const void* serialData, size_t serialLength) override; |
| | | void setPluginNamespace(const char* libNamespace) override; |
| | | const char* getPluginNamespace() const override; |
| | | private: |
| | | std::string _s_name_space; |
| | | static PluginFieldCollection _fc; |
| | | static std::vector<PluginField> _vec_plugin_attributes; |
| | | };//end detect creator |
| | | |
| | | }//end namespace nvinfer1 |
| | | |
| | | |
| | | |
| | | #endif |
New file |
| | |
| | | |
| | | #include <cuda.h> |
| | | #include <cuda_runtime.h> |
| | | #include <stdint.h> |
| | | #include <stdio.h> |
| | | #include <string.h> |
| | | |
| | | inline __device__ float sigmoidGPU(const float& x) { return 1.0f / (1.0f + __expf(-x)); } |
| | | |
| | | __global__ void gpuSLayerV3(const float* input, float* output, const uint32_t grid_h_, |
| | | const uint32_t grid_w_, const uint32_t numOutputClasses, |
| | | const uint32_t numBBoxes) |
| | | { |
| | | uint32_t x_id = blockIdx.x * blockDim.x + threadIdx.x; |
| | | uint32_t y_id = blockIdx.y * blockDim.y + threadIdx.y; |
| | | uint32_t z_id = blockIdx.z * blockDim.z + threadIdx.z; |
| | | |
| | | if ((x_id >= grid_w_) || (y_id >= grid_h_) || (z_id >= numBBoxes)) |
| | | { |
| | | return; |
| | | } |
| | | |
| | | const int numGridCells = grid_h_ * grid_w_; |
| | | const int bbindex = y_id * grid_w_ + x_id; |
| | | |
| | | output[bbindex + numGridCells * (z_id * (5 + numOutputClasses) + 0)] |
| | | = sigmoidGPU(input[bbindex + numGridCells * (z_id * (5 + numOutputClasses) + 0)]); |
| | | |
| | | output[bbindex + numGridCells * (z_id * (5 + numOutputClasses) + 1)] |
| | | = sigmoidGPU(input[bbindex + numGridCells * (z_id * (5 + numOutputClasses) + 1)]); |
| | | |
| | | output[bbindex + numGridCells * (z_id * (5 + numOutputClasses) + 2)] |
| | | = __expf(input[bbindex + numGridCells * (z_id * (5 + numOutputClasses) + 2)]); |
| | | |
| | | output[bbindex + numGridCells * (z_id * (5 + numOutputClasses) + 3)] |
| | | = __expf(input[bbindex + numGridCells * (z_id * (5 + numOutputClasses) + 3)]); |
| | | |
| | | output[bbindex + numGridCells * (z_id * (5 + numOutputClasses) + 4)] |
| | | = sigmoidGPU(input[bbindex + numGridCells * (z_id * (5 + numOutputClasses) + 4)]); |
| | | |
| | | for (uint32_t i = 0; i < numOutputClasses; ++i) |
| | | { |
| | | output[bbindex + numGridCells * (z_id * (5 + numOutputClasses) + (5 + i))] |
| | | = sigmoidGPU(input[bbindex + numGridCells * (z_id * (5 + numOutputClasses) + (5 + i))]); |
| | | } |
| | | } |
| | | |
| | | cudaError_t cudaSLayerV3(const void* input, void* output, const uint32_t& batchSize, |
| | | const uint32_t& n_grid_h_,const uint32_t& n_grid_w_, |
| | | const uint32_t& numOutputClasses, const uint32_t& numBBoxes, |
| | | uint64_t outputSize, cudaStream_t stream) |
| | | { |
| | | dim3 threads_per_block(16, 16, 4); |
| | | dim3 number_of_blocks((n_grid_w_ / threads_per_block.x) + 1, |
| | | (n_grid_h_ / threads_per_block.y) + 1, |
| | | (numBBoxes / threads_per_block.z) + 1); |
| | | for (int batch = 0; batch < batchSize; ++batch) |
| | | { |
| | | gpuSLayerV3<<<number_of_blocks, threads_per_block, 0, stream>>>( |
| | | reinterpret_cast<const float*>(input) + (batch * outputSize), |
| | | reinterpret_cast<float*>(output) + (batch * outputSize), n_grid_h_, n_grid_w_, numOutputClasses, |
| | | numBBoxes); |
| | | } |
| | | return cudaGetLastError(); |
| | | } |
New file |
| | |
| | | #include <cmath> |
| | | #include <stdio.h> |
| | | #include <cassert> |
| | | #include <iostream> |
| | | #include "mish.h" |
| | | |
| | | namespace nvinfer1 |
| | | { |
| | | MishPlugin::MishPlugin() |
| | | { |
| | | } |
| | | |
| | | MishPlugin::~MishPlugin() |
| | | { |
| | | } |
| | | |
| | | // create the plugin at runtime from a byte stream |
| | | MishPlugin::MishPlugin(const void* data, size_t length) |
| | | { |
| | | assert(length == sizeof(input_size_)); |
| | | input_size_ = *reinterpret_cast<const int*>(data); |
| | | } |
| | | |
| | | void MishPlugin::serialize(void* buffer) const |
| | | { |
| | | *reinterpret_cast<int*>(buffer) = input_size_; |
| | | } |
| | | |
| | | size_t MishPlugin::getSerializationSize() const |
| | | { |
| | | return sizeof(input_size_); |
| | | } |
| | | |
| | | int MishPlugin::initialize() |
| | | { |
| | | return 0; |
| | | } |
| | | |
| | | Dims MishPlugin::getOutputDimensions(int index, const Dims* inputs, int nbInputDims) |
| | | { |
| | | assert(nbInputDims == 1); |
| | | assert(index == 0); |
| | | input_size_ = inputs[0].d[0] * inputs[0].d[1] * inputs[0].d[2]; |
| | | // Output dimensions |
| | | return Dims3(inputs[0].d[0], inputs[0].d[1], inputs[0].d[2]); |
| | | } |
| | | |
| | | // Set plugin namespace |
| | | void MishPlugin::setPluginNamespace(const char* pluginNamespace) |
| | | { |
| | | mPluginNamespace = pluginNamespace; |
| | | } |
| | | |
| | | const char* MishPlugin::getPluginNamespace() const |
| | | { |
| | | return mPluginNamespace; |
| | | } |
| | | |
| | | // Return the DataType of the plugin output at the requested index |
| | | DataType MishPlugin::getOutputDataType(int index, const nvinfer1::DataType* inputTypes, int nbInputs) const |
| | | { |
| | | return DataType::kFLOAT; |
| | | } |
| | | |
| | | // Return true if output tensor is broadcast across a batch. |
| | | bool MishPlugin::isOutputBroadcastAcrossBatch(int outputIndex, const bool* inputIsBroadcasted, int nbInputs) const |
| | | { |
| | | return false; |
| | | } |
| | | |
| | | // Return true if plugin can use input that is broadcast across batch without replication. |
| | | bool MishPlugin::canBroadcastInputAcrossBatch(int inputIndex) const |
| | | { |
| | | return false; |
| | | } |
| | | |
| | | void MishPlugin::configurePlugin(const PluginTensorDesc* in, int nbInput, const PluginTensorDesc* out, int nbOutput) |
| | | { |
| | | } |
| | | |
| | | // Attach the plugin object to an execution context and grant the plugin the access to some context resource. |
| | | void MishPlugin::attachToContext(cudnnContext* cudnnContext, cublasContext* cublasContext, IGpuAllocator* gpuAllocator) |
| | | { |
| | | } |
| | | |
| | | // Detach the plugin object from its execution context. |
| | | void MishPlugin::detachFromContext() {} |
| | | |
| | | const char* MishPlugin::getPluginType() const |
| | | { |
| | | return "Mish_TRT"; |
| | | } |
| | | |
| | | const char* MishPlugin::getPluginVersion() const |
| | | { |
| | | return "1"; |
| | | } |
| | | |
| | | void MishPlugin::destroy() |
| | | { |
| | | delete this; |
| | | } |
| | | |
| | | // Clone the plugin |
| | | IPluginV2IOExt* MishPlugin::clone() const |
| | | { |
| | | MishPlugin *p = new MishPlugin(); |
| | | p->input_size_ = input_size_; |
| | | p->setPluginNamespace(mPluginNamespace); |
| | | return p; |
| | | } |
| | | |
| | | __device__ float tanh_activate_kernel(float x){return (2/(1 + expf(-2*x)) - 1);} |
| | | |
| | | __device__ float softplus_kernel(float x, float threshold = 20) { |
| | | if (x > threshold) return x; // too large |
| | | else if (x < -threshold) return expf(x); // too small |
| | | return logf(expf(x) + 1); |
| | | } |
| | | |
| | | __global__ void mish_kernel(const float *input, float *output, int num_elem) { |
| | | |
| | | int idx = threadIdx.x + blockDim.x * blockIdx.x; |
| | | if (idx >= num_elem) return; |
| | | |
| | | //float t = exp(input[idx]); |
| | | //if (input[idx] > 20.0) { |
| | | // t *= t; |
| | | // output[idx] = (t - 1.0) / (t + 1.0); |
| | | //} else { |
| | | // float tt = t * t; |
| | | // output[idx] = (tt + 2.0 * t) / (tt + 2.0 * t + 2.0); |
| | | //} |
| | | //output[idx] *= input[idx]; |
| | | output[idx] = input[idx] * tanh_activate_kernel(softplus_kernel(input[idx])); |
| | | } |
| | | |
| | | void MishPlugin::forwardGpu(const float *const * inputs, float* output, cudaStream_t stream, int batchSize) { |
| | | int block_size = thread_count_; |
| | | int grid_size = (input_size_ * batchSize + block_size - 1) / block_size; |
| | | mish_kernel<<<grid_size, block_size>>>(inputs[0], output, input_size_ * batchSize); |
| | | } |
| | | |
| | | int MishPlugin::enqueue(int batchSize, const void*const * inputs, void** outputs, void* workspace, cudaStream_t stream) |
| | | { |
| | | //assert(batchSize == 1); |
| | | //GPU |
| | | //CUDA_CHECK(cudaStreamSynchronize(stream)); |
| | | forwardGpu((const float *const *)inputs, (float*)outputs[0], stream, batchSize); |
| | | return 0; |
| | | } |
| | | |
| | | PluginFieldCollection MishPluginCreator::mFC{}; |
| | | std::vector<PluginField> MishPluginCreator::mPluginAttributes; |
| | | |
| | | MishPluginCreator::MishPluginCreator() |
| | | { |
| | | mPluginAttributes.clear(); |
| | | |
| | | mFC.nbFields = mPluginAttributes.size(); |
| | | mFC.fields = mPluginAttributes.data(); |
| | | } |
| | | |
| | | const char* MishPluginCreator::getPluginName() const |
| | | { |
| | | return "Mish_TRT"; |
| | | } |
| | | |
| | | const char* MishPluginCreator::getPluginVersion() const |
| | | { |
| | | return "1"; |
| | | } |
| | | |
| | | const PluginFieldCollection* MishPluginCreator::getFieldNames() |
| | | { |
| | | return &mFC; |
| | | } |
| | | |
| | | IPluginV2IOExt* MishPluginCreator::createPlugin(const char* name, const PluginFieldCollection* fc) |
| | | { |
| | | MishPlugin* obj = new MishPlugin(); |
| | | obj->setPluginNamespace(mNamespace.c_str()); |
| | | return obj; |
| | | } |
| | | |
| | | IPluginV2IOExt* MishPluginCreator::deserializePlugin(const char* name, const void* serialData, size_t serialLength) |
| | | { |
| | | // This object will be deleted when the network is destroyed, which will |
| | | // call MishPlugin::destroy() |
| | | MishPlugin* obj = new MishPlugin(serialData, serialLength); |
| | | obj->setPluginNamespace(mNamespace.c_str()); |
| | | return obj; |
| | | } |
| | | |
| | | } |
| | | |
New file |
| | |
| | | #ifndef _MISH_PLUGIN_H |
| | | #define _MISH_PLUGIN_H |
| | | |
| | | #include <string> |
| | | #include <vector> |
| | | #include "NvInfer.h" |
| | | |
| | | |
| | | //https://github.com/wang-xinyu/tensorrtx |
| | | namespace nvinfer1 |
| | | { |
| | | class MishPlugin: public IPluginV2IOExt |
| | | { |
| | | public: |
| | | explicit MishPlugin(); |
| | | MishPlugin(const void* data, size_t length); |
| | | |
| | | ~MishPlugin(); |
| | | |
| | | int getNbOutputs() const override |
| | | { |
| | | return 1; |
| | | } |
| | | |
| | | Dims getOutputDimensions(int index, const Dims* inputs, int nbInputDims) override; |
| | | |
| | | int initialize() override; |
| | | |
| | | virtual void terminate() override {} |
| | | |
| | | virtual size_t getWorkspaceSize(int maxBatchSize) const override { return 0;} |
| | | |
| | | virtual int enqueue(int batchSize, const void*const * inputs, void** outputs, void* workspace, cudaStream_t stream) override; |
| | | |
| | | virtual size_t getSerializationSize() const override; |
| | | |
| | | virtual void serialize(void* buffer) const override; |
| | | |
| | | bool supportsFormatCombination(int pos, const PluginTensorDesc* inOut, int nbInputs, int nbOutputs) const override { |
| | | return inOut[pos].format == TensorFormat::kLINEAR && inOut[pos].type == DataType::kFLOAT; |
| | | } |
| | | |
| | | const char* getPluginType() const override; |
| | | |
| | | const char* getPluginVersion() const override; |
| | | |
| | | void destroy() override; |
| | | |
| | | IPluginV2IOExt* clone() const override; |
| | | |
| | | void setPluginNamespace(const char* pluginNamespace) override; |
| | | |
| | | const char* getPluginNamespace() const override; |
| | | |
| | | DataType getOutputDataType(int index, const nvinfer1::DataType* inputTypes, int nbInputs) const override; |
| | | |
| | | bool isOutputBroadcastAcrossBatch(int outputIndex, const bool* inputIsBroadcasted, int nbInputs) const override; |
| | | |
| | | bool canBroadcastInputAcrossBatch(int inputIndex) const override; |
| | | |
| | | void attachToContext( |
| | | cudnnContext* cudnnContext, cublasContext* cublasContext, IGpuAllocator* gpuAllocator) override; |
| | | |
| | | void configurePlugin(const PluginTensorDesc* in, int nbInput, const PluginTensorDesc* out, int nbOutput) override; |
| | | |
| | | void detachFromContext() override; |
| | | |
| | | int input_size_; |
| | | private: |
| | | void forwardGpu(const float *const * inputs, float* output, cudaStream_t stream, int batchSize = 1); |
| | | int thread_count_ = 256; |
| | | const char* mPluginNamespace; |
| | | }; |
| | | |
| | | class MishPluginCreator : public IPluginCreator |
| | | { |
| | | public: |
| | | MishPluginCreator(); |
| | | |
| | | ~MishPluginCreator() override = default; |
| | | |
| | | const char* getPluginName() const override; |
| | | |
| | | const char* getPluginVersion() const override; |
| | | |
| | | const PluginFieldCollection* getFieldNames() override; |
| | | |
| | | IPluginV2IOExt* createPlugin(const char* name, const PluginFieldCollection* fc) override; |
| | | |
| | | IPluginV2IOExt* deserializePlugin(const char* name, const void* serialData, size_t serialLength) override; |
| | | |
| | | void setPluginNamespace(const char* libNamespace) override |
| | | { |
| | | mNamespace = libNamespace; |
| | | } |
| | | |
| | | const char* getPluginNamespace() const override |
| | | { |
| | | return mNamespace.c_str(); |
| | | } |
| | | |
| | | private: |
| | | std::string mNamespace; |
| | | static PluginFieldCollection mFC; |
| | | static std::vector<PluginField> mPluginAttributes; |
| | | }; |
| | | } |
| | | #endif |
New file |
| | |
| | | #include "model.h" |
| | | #include <memory> |
| | | #include <vector> |
| | | #include <fstream> |
| | | #include <iostream> |
| | | #include <sstream> |
| | | #include <iomanip> |
| | | |
| | | using namespace nvinfer1; |
| | | // REGISTER_TENSORRT_PLUGIN(DetectPluginCreator); |
| | | |
| | | |
| | | Detecter::Detecter( const NetworkInfo& networkInfo, const InferParams& inferParams, int type) : |
| | | m_InputBlobName(networkInfo.inputBlobName), |
| | | m_InputH(416), |
| | | m_InputW(416), |
| | | m_InputC(3), |
| | | m_InputSize(m_InputH*m_InputW*m_InputC), |
| | | m_ProbThresh(inferParams.probThresh), |
| | | m_NMSThresh(inferParams.nmsThresh), |
| | | m_Logger(Logger()), |
| | | m_Network(nullptr), |
| | | m_Builder(nullptr), |
| | | m_ModelStream(nullptr), |
| | | m_Engine(nullptr), |
| | | m_Context(nullptr), |
| | | m_InputBindingIndex(-1), |
| | | m_CudaStream(nullptr), |
| | | m_PluginFactory(new PluginFactory) |
| | | { |
| | | setOutput(type); |
| | | m_EnginePath = m_staticStruct::model_path; |
| | | DEBUG((boost::format("m_EnginePath:%s")%m_EnginePath).str()); |
| | | assert(m_PluginFactory != nullptr); |
| | | m_Engine = loadTRTEngine(m_EnginePath, m_PluginFactory, m_Logger); |
| | | assert(m_Engine != nullptr); |
| | | m_Context = m_Engine->createExecutionContext(); |
| | | assert(m_Context != nullptr); |
| | | m_InputBindingIndex = m_Engine->getBindingIndex(m_InputBlobName.c_str()); |
| | | assert(m_InputBindingIndex != -1); |
| | | assert(m_BatchSize <= static_cast<uint32_t>(m_Engine->getMaxBatchSize())); |
| | | allocateBuffers(); |
| | | NV_CUDA_CHECK(cudaStreamCreate(&m_CudaStream)); |
| | | assert(verifyEngine()); |
| | | } |
| | | |
| | | Detecter::~Detecter() |
| | | { |
| | | for (auto& tensor : m_OutputTensors) NV_CUDA_CHECK(cudaFreeHost(tensor.hostBuffer)); |
| | | for (auto& deviceBuffer : m_DeviceBuffers) NV_CUDA_CHECK(cudaFree(deviceBuffer)); |
| | | NV_CUDA_CHECK(cudaStreamDestroy(m_CudaStream)); |
| | | if (m_Context) |
| | | { |
| | | m_Context->destroy(); |
| | | m_Context = nullptr; |
| | | } |
| | | |
| | | if (m_Engine) |
| | | { |
| | | m_Engine->destroy(); |
| | | m_Engine = nullptr; |
| | | } |
| | | |
| | | if (m_PluginFactory) |
| | | { |
| | | m_PluginFactory->destroy(); |
| | | m_PluginFactory = nullptr; |
| | | } |
| | | |
| | | // m_TinyMaxpoolPaddingFormula.reset(); |
| | | } |
| | | |
| | | void Detecter::doInference(const unsigned char* input, const uint32_t batchSize) |
| | | { |
| | | Timer timer; |
| | | // std::vector<int64_t> bufferSize; |
| | | // int nbBindings = m_Engine->getNbBindings(); |
| | | // bufferSize.resize(nbBindings); |
| | | |
| | | // for (int i = 0; i < nbBindings; ++i) |
| | | // { |
| | | // nvinfer1::Dims dims = m_Engine->getBindingDimensions(i); |
| | | // nvinfer1::DataType dtype = m_Engine->getBindingDataType(i); |
| | | // int64_t totalSize = volume(dims) * 1 * getElementSize(dtype); |
| | | // bufferSize[i] = totalSize; |
| | | // std::cout << "binding" << i << ": " << totalSize << std::endl; |
| | | // cudaMalloc(&s_buffers[i], totalSize); |
| | | // } |
| | | |
| | | // CHECK(cudaMalloc(&s_buffers[0], batchSize *416 * 416 * 3 * sizeof(float))); |
| | | // CHECK(cudaMalloc(&s_buffers[1], batchSize * 3 * sizeof(float))); |
| | | // CHECK(cudaMalloc(&s_buffers[2], batchSize * 3 * sizeof(float))); |
| | | // CHECK(cudaMalloc(&s_buffers[3], batchSize * 3 * sizeof(float))); |
| | | |
| | | assert(batchSize <= m_BatchSize && "Image batch size exceeds TRT engines batch size"); |
| | | // cudaMemcpyAsync(s_buffers[0], input, |
| | | // batchSize * m_InputSize * sizeof(float), cudaMemcpyHostToDevice, |
| | | // m_CudaStream); |
| | | |
| | | // m_Context->enqueue(batchSize, s_buffers, m_CudaStream, nullptr); |
| | | NV_CUDA_CHECK(cudaMemcpyAsync(m_DeviceBuffers.at(m_InputBindingIndex), input, |
| | | batchSize * m_InputSize * sizeof(float), cudaMemcpyHostToDevice, |
| | | m_CudaStream)); |
| | | |
| | | m_Context->enqueue(batchSize, m_DeviceBuffers.data(), m_CudaStream, nullptr); |
| | | for (auto& tensor : m_OutputTensors) |
| | | { |
| | | NV_CUDA_CHECK(cudaMemcpyAsync(tensor.hostBuffer, m_DeviceBuffers.at(tensor.bindingIndex), |
| | | batchSize * tensor.volume * sizeof(float), |
| | | cudaMemcpyDeviceToHost, m_CudaStream)); |
| | | } |
| | | |
| | | |
| | | // int outSize1 = bufferSize[1] / sizeof(float) / BATCH_SIZE; |
| | | // int outSize2 = bufferSize[2] / sizeof(float) / BATCH_SIZE; |
| | | // int outSize3 = bufferSize[3] / sizeof(float) / BATCH_SIZE; |
| | | // std::cout << bufferSize[1] << std::endl; |
| | | // std::cout << bufferSize[2] << std::endl; |
| | | // std::cout << bufferSize[3] << std::endl; |
| | | |
| | | // auto *out1 = new float[batchSize * 3]; |
| | | // auto *out2 = new float[batchSize * 3]; |
| | | // auto *out3 = new float[batchSize * 3]; |
| | | // cudaMemcpyAsync(out1, s_buffers[1], bufferSize[1], cudaMemcpyDeviceToHost, m_CudaStream); |
| | | // cudaMemcpyAsync(out2, s_buffers[2], bufferSize[2], cudaMemcpyDeviceToHost, m_CudaStream); |
| | | // cudaMemcpyAsync(out3, s_buffers[3], bufferSize[3], cudaMemcpyDeviceToHost, m_CudaStream); |
| | | // CHECK(cudaMemcpyAsync(out1, s_buffers[1], batchSize * 3 * sizeof(float), cudaMemcpyDeviceToHost, m_CudaStream)); |
| | | // CHECK(cudaMemcpyAsync(out2, s_buffers[2], batchSize * 3 * sizeof(float), cudaMemcpyDeviceToHost, m_CudaStream)); |
| | | // CHECK(cudaMemcpyAsync(out3, s_buffers[3], batchSize * 3 * sizeof(float), cudaMemcpyDeviceToHost, m_CudaStream)); |
| | | // exit(0); |
| | | |
| | | |
| | | cudaStreamSynchronize(m_CudaStream); |
| | | timer.out("inference"); |
| | | } |
| | | |
| | | std::vector<BBoxInfo> Detecter::decodeDetections(const int& imageIdx, |
| | | const int& imageH, |
| | | const int& imageW) |
| | | { |
| | | Timer timer; |
| | | std::vector<BBoxInfo> binfo; |
| | | for (auto& tensor : m_OutputTensors) |
| | | { |
| | | std::vector<BBoxInfo> curBInfo = decodeTensor(imageIdx, imageH, imageW, tensor); |
| | | binfo.insert(binfo.end(), curBInfo.begin(), curBInfo.end()); |
| | | } |
| | | timer.out("decodeDetections"); |
| | | return binfo; |
| | | } |
| | | |
| | | |
| | | void Detecter::allocateBuffers() |
| | | { |
| | | m_DeviceBuffers.resize(m_Engine->getNbBindings(), nullptr); |
| | | assert(m_InputBindingIndex != -1 && "Invalid input binding index"); |
| | | NV_CUDA_CHECK(cudaMalloc(&m_DeviceBuffers.at(m_InputBindingIndex), |
| | | m_BatchSize * m_InputSize * sizeof(float))); |
| | | |
| | | for (auto& tensor : m_OutputTensors) |
| | | { |
| | | tensor.bindingIndex = m_Engine->getBindingIndex(tensor.blobName.c_str()); |
| | | assert((tensor.bindingIndex != -1) && "Invalid output binding index"); |
| | | NV_CUDA_CHECK(cudaMalloc(&m_DeviceBuffers.at(tensor.bindingIndex), |
| | | m_BatchSize * tensor.volume * sizeof(float))); |
| | | NV_CUDA_CHECK( |
| | | cudaMallocHost(&tensor.hostBuffer, tensor.volume * m_BatchSize * sizeof(float))); |
| | | } |
| | | } |
| | | |
| | | bool Detecter::verifyEngine() |
| | | { |
| | | assert((m_Engine->getNbBindings() == (1 + m_OutputTensors.size()) |
| | | && "Binding info doesn't match between cfg and engine file \n")); |
| | | |
| | | for (auto tensor : m_OutputTensors) |
| | | { |
| | | assert(!strcmp(m_Engine->getBindingName(tensor.bindingIndex), tensor.blobName.c_str()) |
| | | && "Blobs names dont match between cfg and engine file \n"); |
| | | assert(get3DTensorVolume(m_Engine->getBindingDimensions(tensor.bindingIndex)) |
| | | == tensor.volume |
| | | && "Tensor volumes dont match between cfg and engine file \n"); |
| | | } |
| | | |
| | | assert(m_Engine->bindingIsInput(m_InputBindingIndex) && "Incorrect input binding index \n"); |
| | | assert(m_Engine->getBindingName(m_InputBindingIndex) == m_InputBlobName |
| | | && "Input blob name doesn't match between config and engine file"); |
| | | assert(get3DTensorVolume(m_Engine->getBindingDimensions(m_InputBindingIndex)) == m_InputSize); |
| | | return true; |
| | | } |
| | | |
| | | void Detecter::destroyNetworkUtils(std::vector<nvinfer1::Weights>& trtWeights) |
| | | { |
| | | if (m_Network) m_Network->destroy(); |
| | | if (m_Engine) m_Engine->destroy(); |
| | | if (m_Builder) m_Builder->destroy(); |
| | | if (m_ModelStream) m_ModelStream->destroy(); |
| | | |
| | | // deallocate the weights |
| | | for (auto & trtWeight : trtWeights) |
| | | { |
| | | if (trtWeight.count > 0) free(const_cast<void*>(trtWeight.values)); |
| | | } |
| | | } |
| | | |
| | | std::vector<BBoxInfo> Detecter::decodeTensor(const int imageIdx, const int imageH, const int imageW, const TensorInfo& tensor) |
| | | { |
| | | float scale_h = 1.f; |
| | | float scale_w = 1.f; |
| | | int xOffset = 0; |
| | | int yOffset = 0; |
| | | |
| | | const float* detections = &tensor.hostBuffer[imageIdx * tensor.volume]; |
| | | |
| | | std::vector<BBoxInfo> binfo; |
| | | for (uint32_t y = 0; y < tensor.grid_h; ++y) |
| | | { |
| | | for (uint32_t x = 0; x < tensor.grid_w; ++x) |
| | | { |
| | | for (uint32_t b = 0; b < tensor.numBBoxes; ++b) |
| | | { |
| | | const float pw = tensor.anchors[tensor.masks[b] * 2]; |
| | | const float ph = tensor.anchors[tensor.masks[b] * 2 + 1]; |
| | | |
| | | const int numGridCells = tensor.grid_h * tensor.grid_w; |
| | | const int bbindex = y * tensor.grid_w + x; |
| | | const float bx |
| | | = x + detections[bbindex + numGridCells * (b * (5 + tensor.numClasses) + 0)]; |
| | | const float by |
| | | = y + detections[bbindex + numGridCells * (b * (5 + tensor.numClasses) + 1)]; |
| | | const float bw |
| | | = pw * detections[bbindex + numGridCells * (b * (5 + tensor.numClasses) + 2)]; |
| | | const float bh |
| | | = ph * detections[bbindex + numGridCells * (b * (5 + tensor.numClasses) + 3)]; |
| | | |
| | | const float objectness |
| | | = detections[bbindex + numGridCells * (b * (5 + tensor.numClasses) + 4)]; |
| | | |
| | | float maxProb = 0.0f; |
| | | int maxIndex = -1; |
| | | |
| | | for (uint32_t i = 0; i < tensor.numClasses; ++i) |
| | | { |
| | | float prob |
| | | = (detections[bbindex |
| | | + numGridCells * (b * (5 + tensor.numClasses) + (5 + i))]); |
| | | |
| | | if (prob > maxProb) |
| | | { |
| | | maxProb = prob; |
| | | maxIndex = i; |
| | | } |
| | | } |
| | | maxProb = objectness * maxProb; |
| | | // printf("-=-=-=-=-=-%f\n",maxProb); |
| | | |
| | | if (maxProb > m_ProbThresh) |
| | | { |
| | | add_bbox_proposal(bx, by, bw, bh, tensor.stride_h, tensor.stride_w, scale_h, scale_w, xOffset, yOffset, maxIndex, maxProb, imageW, imageH, binfo); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | return binfo; |
| | | } |
| | | |
| | | |
| | | void Detecter::setOutput(int type) |
| | | { |
| | | m_OutputTensors.clear(); |
| | | if(type==0) |
| | | for (int i = 0; i < 3; ++i) |
| | | { |
| | | TensorInfo outputTensor; |
| | | outputTensor.numClasses = CLASS_BUM; |
| | | outputTensor.blobName = "yolo_" + std::to_string(i); |
| | | outputTensor.gridSize = (m_InputH / 32) * pow(2, 2-i); |
| | | outputTensor.grid_h = (m_InputH / 32) * pow(2, 2-i); |
| | | outputTensor.grid_w = (m_InputW / 32) * pow(2, 2-i); |
| | | outputTensor.stride = m_InputH / outputTensor.gridSize; |
| | | outputTensor.stride_h = m_InputH / outputTensor.grid_h; |
| | | outputTensor.stride_w = m_InputW / outputTensor.grid_w; |
| | | outputTensor.numBBoxes = 3; |
| | | outputTensor.volume = outputTensor.grid_h* outputTensor.grid_w |
| | | *(outputTensor.numBBoxes*(5 + outputTensor.numClasses)); |
| | | if(i==0) |
| | | { |
| | | outputTensor.masks.push_back(0); |
| | | outputTensor.masks.push_back(1); |
| | | outputTensor.masks.push_back(2); |
| | | } |
| | | if(i==1) |
| | | { |
| | | outputTensor.masks.push_back(3); |
| | | outputTensor.masks.push_back(4); |
| | | outputTensor.masks.push_back(5); |
| | | } |
| | | if(i==2) |
| | | { |
| | | outputTensor.masks.push_back(6); |
| | | outputTensor.masks.push_back(7); |
| | | outputTensor.masks.push_back(8); |
| | | } |
| | | outputTensor.anchors.push_back(12); |
| | | outputTensor.anchors.push_back(16); |
| | | outputTensor.anchors.push_back(19); |
| | | outputTensor.anchors.push_back(36); |
| | | outputTensor.anchors.push_back(40); |
| | | outputTensor.anchors.push_back(28); |
| | | outputTensor.anchors.push_back(36); |
| | | outputTensor.anchors.push_back(75); |
| | | outputTensor.anchors.push_back(76); |
| | | outputTensor.anchors.push_back(55); |
| | | outputTensor.anchors.push_back(72); |
| | | outputTensor.anchors.push_back(146); |
| | | outputTensor.anchors.push_back(142); |
| | | outputTensor.anchors.push_back(110); |
| | | outputTensor.anchors.push_back(192); |
| | | outputTensor.anchors.push_back(243); |
| | | outputTensor.anchors.push_back(459); |
| | | outputTensor.anchors.push_back(401); |
| | | |
| | | if (m_ClassNames.empty()) |
| | | { |
| | | for (uint32_t j=0;j< outputTensor.numClasses;++j) |
| | | { |
| | | m_ClassNames.push_back(std::to_string(j)); |
| | | } |
| | | } |
| | | m_OutputTensors.push_back(outputTensor); |
| | | } |
| | | else |
| | | for (int i = 0; i < 2; ++i) |
| | | { |
| | | |
| | | TensorInfo outputTensor; |
| | | outputTensor.numClasses = CLASS_BUM; |
| | | outputTensor.blobName = "yolo_" + std::to_string(i); |
| | | outputTensor.gridSize = (m_InputH / 32) * pow(2, i); |
| | | outputTensor.grid_h = (m_InputH / 32) * pow(2, i); |
| | | outputTensor.grid_w = (m_InputW / 32) * pow(2, i); |
| | | outputTensor.stride = m_InputH / outputTensor.gridSize; |
| | | outputTensor.stride_h = m_InputH / outputTensor.grid_h; |
| | | outputTensor.stride_w = m_InputW / outputTensor.grid_w; |
| | | outputTensor.numBBoxes = 3; |
| | | outputTensor.volume = outputTensor.grid_h* outputTensor.grid_w |
| | | *(outputTensor.numBBoxes*(5 + outputTensor.numClasses)); |
| | | if(i==1) |
| | | { |
| | | outputTensor.masks.push_back(1); |
| | | outputTensor.masks.push_back(2); |
| | | outputTensor.masks.push_back(3); |
| | | } |
| | | if(i==0) |
| | | { |
| | | outputTensor.masks.push_back(3); |
| | | outputTensor.masks.push_back(4); |
| | | outputTensor.masks.push_back(5); |
| | | } |
| | | outputTensor.anchors.push_back(10); |
| | | outputTensor.anchors.push_back(14); |
| | | outputTensor.anchors.push_back(23); |
| | | outputTensor.anchors.push_back(27); |
| | | outputTensor.anchors.push_back(37); |
| | | outputTensor.anchors.push_back(58); |
| | | outputTensor.anchors.push_back(81); |
| | | outputTensor.anchors.push_back(82); |
| | | outputTensor.anchors.push_back(135); |
| | | outputTensor.anchors.push_back(169); |
| | | outputTensor.anchors.push_back(344); |
| | | outputTensor.anchors.push_back(319); |
| | | |
| | | if (m_ClassNames.empty()) |
| | | { |
| | | for (uint32_t j=0;j< outputTensor.numClasses;++j) |
| | | { |
| | | m_ClassNames.push_back(std::to_string(j)); |
| | | } |
| | | } |
| | | m_OutputTensors.push_back(outputTensor); |
| | | } |
| | | } |
New file |
| | |
| | | #ifndef _MODEL_H_ |
| | | #define _MODEL_H_ |
| | | |
| | | #include "plugin_factory.h" |
| | | #include "trt_utils.h" |
| | | |
| | | #include "NvInfer.h" |
| | | #include "NvInferPlugin.h" |
| | | #include "NvInferRuntimeCommon.h" |
| | | #include "cuda_runtime_api.h" |
| | | #include <algorithm> |
| | | #include <cmath> |
| | | #include <stdint.h> |
| | | #include <string> |
| | | #include <vector> |
| | | #include "../utils/timer_utils.hpp" |
| | | #include "../config.h" |
| | | #include "opencv2/opencv.hpp" |
| | | |
| | | struct NetworkInfo |
| | | { |
| | | std::string networkType; |
| | | std::string labelsFilePath; |
| | | std::string precision; |
| | | std::string deviceType; |
| | | std::string calibrationTablePath; |
| | | std::string enginePath; |
| | | std::string inputBlobName; |
| | | std::string data_path; |
| | | }; |
| | | |
| | | struct InferParams |
| | | { |
| | | bool printPerfInfo; |
| | | bool printPredictionInfo; |
| | | std::string calibImages; |
| | | std::string calibImagesPath; |
| | | float probThresh; |
| | | float nmsThresh; |
| | | }; |
| | | |
| | | struct TensorInfo |
| | | { |
| | | std::string blobName; |
| | | uint32_t stride{0}; |
| | | uint32_t stride_h{0}; |
| | | uint32_t stride_w{0}; |
| | | uint32_t gridSize{0}; |
| | | uint32_t grid_h{ 0 }; |
| | | uint32_t grid_w{ 0 }; |
| | | uint32_t numClasses{0}; |
| | | uint32_t numBBoxes{0}; |
| | | uint64_t volume{0}; |
| | | std::vector<uint32_t> masks; |
| | | std::vector<float> anchors; |
| | | int bindingIndex{-1}; |
| | | float* hostBuffer{nullptr}; |
| | | }; |
| | | |
| | | |
| | | class Detecter |
| | | { |
| | | public: |
| | | float getNMSThresh() const { return m_NMSThresh; } |
| | | std::string getClassName(const int& label) const { return m_ClassNames.at(label); } |
| | | int getClassId(const int& label) const { return m_ClassIds.at(label); } |
| | | uint32_t getInputH() const { return m_InputH; } |
| | | uint32_t getInputW() const { return m_InputW; } |
| | | uint32_t getNumClasses() const { return static_cast<uint32_t>(m_ClassNames.size()); } |
| | | void doInference(const unsigned char* input, const uint32_t batchSize); |
| | | std::vector<BBoxInfo> decodeDetections(const int& imageIdx, |
| | | const int& imageH, |
| | | const int& imageW); |
| | | Detecter(const NetworkInfo& networkInfo, const InferParams& inferParams, int type); |
| | | ~Detecter(); |
| | | |
| | | std::string m_EnginePath; |
| | | const std::string m_DeviceType; |
| | | const std::string m_InputBlobName; |
| | | std::vector<TensorInfo> m_OutputTensors; |
| | | uint32_t m_InputH; |
| | | uint32_t m_InputW; |
| | | uint32_t m_InputC; |
| | | uint64_t m_InputSize; |
| | | uint32_t _n_classes = 0; |
| | | float _f_depth_multiple = 0; |
| | | float _f_width_multiple = 0; |
| | | const float m_ProbThresh; |
| | | const float m_NMSThresh; |
| | | std::vector<std::string> m_ClassNames; |
| | | const std::vector<int> m_ClassIds{ |
| | | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| | | 22, 23, 24, 25, 27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, |
| | | 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, |
| | | 67, 70, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 84, 85, 86, 87, 88, 89, 90}; |
| | | uint32_t m_BatchSize = 10; |
| | | nvinfer1::INetworkDefinition* m_Network; |
| | | nvinfer1::IBuilder* m_Builder ; |
| | | nvinfer1::IHostMemory* m_ModelStream; |
| | | nvinfer1::ICudaEngine* m_Engine; |
| | | nvinfer1::IExecutionContext* m_Context; |
| | | std::vector<void*> m_DeviceBuffers; |
| | | int m_InputBindingIndex; |
| | | cudaStream_t m_CudaStream; |
| | | PluginFactory* m_PluginFactory; |
| | | |
| | | std::vector<BBoxInfo> decodeTensor(const int imageIdx, const int imageH, |
| | | const int imageW, const TensorInfo& tensor); |
| | | |
| | | BBox convert_bbox_res(const float& bx, const float& by, const float& bw, const float& bh, |
| | | const uint32_t& stride_h_, const uint32_t& stride_w_, const uint32_t& netW, const uint32_t& netH) |
| | | { |
| | | BBox b; |
| | | float x = bx * stride_w_; |
| | | float y = by * stride_h_; |
| | | |
| | | b.x1 = x - bw / 2; |
| | | b.x2 = x + bw / 2; |
| | | |
| | | b.y1 = y - bh / 2; |
| | | b.y2 = y + bh / 2; |
| | | |
| | | b.x1 = clamp(b.x1, 0, netW); |
| | | b.x2 = clamp(b.x2, 0, netW); |
| | | b.y1 = clamp(b.y1, 0, netH); |
| | | b.y2 = clamp(b.y2, 0, netH); |
| | | |
| | | return b; |
| | | } |
| | | |
| | | |
| | | inline void add_bbox_proposal(const float bx, const float by, const float bw, const float bh, |
| | | const uint32_t stride_h_, const uint32_t stride_w_, const float scaleH, const float scaleW, const float xoffset_, const float yoffset, const int maxIndex, const float maxProb, |
| | | const uint32_t image_w, const uint32_t image_h, |
| | | std::vector<BBoxInfo>& binfo) |
| | | { |
| | | BBoxInfo bbi; |
| | | bbi.box = convert_bbox_res(bx, by, bw, bh, stride_h_, stride_w_, m_InputW, m_InputH); |
| | | if ((bbi.box.x1 > bbi.box.x2) || (bbi.box.y1 > bbi.box.y2)) |
| | | { |
| | | return; |
| | | } |
| | | bbi.box.x1 = ((float)bbi.box.x1 / (float)m_InputW)*(float)image_w; |
| | | bbi.box.y1 = ((float)bbi.box.y1 / (float)m_InputH)*(float)image_h; |
| | | bbi.box.x2 = ((float)bbi.box.x2 / (float)m_InputW)*(float)image_w; |
| | | bbi.box.y2 = ((float)bbi.box.y2 / (float)m_InputH)*(float)image_h; |
| | | |
| | | bbi.label = maxIndex; |
| | | bbi.prob = maxProb; |
| | | bbi.classId = getClassId(maxIndex); |
| | | binfo.push_back(bbi); |
| | | }; |
| | | |
| | | inline int64_t volume(const nvinfer1::Dims& d) |
| | | { |
| | | return std::accumulate(d.d,d.d+d.nbDims,1,std::multiplies<int64_t>()); |
| | | } |
| | | inline unsigned int getElementSize(nvinfer1::DataType t) |
| | | { |
| | | switch (t) |
| | | { |
| | | case nvinfer1::DataType::kINT32: return 4; |
| | | case nvinfer1::DataType::kFLOAT: return 4; |
| | | case nvinfer1::DataType::kHALF: return 2; |
| | | case nvinfer1::DataType::kBOOL: |
| | | case nvinfer1::DataType::kINT8: return 1; |
| | | } |
| | | throw std::runtime_error("Invalid DataType."); |
| | | return 0; |
| | | } |
| | | |
| | | void setOutput(int type); |
| | | private: |
| | | Logger m_Logger; |
| | | void allocateBuffers(); |
| | | bool verifyEngine(); |
| | | void destroyNetworkUtils(std::vector<nvinfer1::Weights>& trtWeights); |
| | | |
| | | private: |
| | | Timer _timer; |
| | | }; |
| | | |
| | | #endif |
New file |
| | |
| | | #include "plugin_factory.h" |
| | | #include "trt_utils.h" |
| | | |
| | | PluginFactory::PluginFactory() : m_ReorgLayer{nullptr}, m_RegionLayer{nullptr} |
| | | { |
| | | for (int i = 0; i < m_MaxLeakyLayers; ++i) m_LeakyReLULayers[i] = nullptr; |
| | | } |
| | | |
| | | nvinfer1::IPlugin* PluginFactory::createPlugin(const char* layerName, const void* serialData, |
| | | size_t serialLength) |
| | | { |
| | | assert(isPlugin(layerName)); |
| | | if (std::string(layerName).find("leaky") != std::string::npos) |
| | | { |
| | | assert(m_LeakyReLUCount >= 0 && m_LeakyReLUCount <= m_MaxLeakyLayers); |
| | | assert(m_LeakyReLULayers[m_LeakyReLUCount] == nullptr); |
| | | ++m_LeakyReLUCount; |
| | | return m_LeakyReLULayers[m_LeakyReLUCount - 1].get(); |
| | | } |
| | | else if (std::string(layerName).find("reorg") != std::string::npos) |
| | | { |
| | | assert(m_ReorgLayer == nullptr); |
| | | return m_ReorgLayer.get(); |
| | | } |
| | | else if (std::string(layerName).find("region") != std::string::npos) |
| | | { |
| | | assert(m_RegionLayer == nullptr); |
| | | return m_RegionLayer.get(); |
| | | } |
| | | else if (std::string(layerName).find("yolo") != std::string::npos) |
| | | { |
| | | assert(m_SLayerCount >= 0 && m_SLayerCount < m_MaxSLayers); |
| | | assert(m_SLayers[m_SLayerCount] == nullptr); |
| | | m_SLayers[m_SLayerCount] |
| | | = unique_ptr_IPlugin(new SLayerV3(serialData, serialLength)); |
| | | ++m_SLayerCount; |
| | | return m_SLayers[m_SLayerCount - 1].get(); |
| | | } |
| | | else |
| | | { |
| | | std::cerr << "ERROR: Unrecognised layer : " << layerName << std::endl; |
| | | assert(0); |
| | | return nullptr; |
| | | } |
| | | } |
| | | |
| | | bool PluginFactory::isPlugin(const char* name) |
| | | { |
| | | return ((std::string(name).find("leaky") != std::string::npos) |
| | | || (std::string(name).find("reorg") != std::string::npos) |
| | | || (std::string(name).find("region") != std::string::npos) |
| | | || (std::string(name).find("yolo") != std::string::npos)); |
| | | } |
| | | |
| | | void PluginFactory::destroy() |
| | | { |
| | | m_ReorgLayer.reset(); |
| | | m_RegionLayer.reset(); |
| | | |
| | | for (int i = 0; i < m_MaxLeakyLayers; ++i) |
| | | { |
| | | m_LeakyReLULayers[i].reset(); |
| | | } |
| | | |
| | | for (int i = 0; i < m_MaxSLayers; ++i) |
| | | { |
| | | m_SLayers[i].reset(); |
| | | } |
| | | |
| | | m_LeakyReLUCount = 0; |
| | | m_SLayerCount = 0; |
| | | } |
| | | |
| | | /******* Layer V3 *******/ |
| | | /*****************************/ |
| | | SLayerV3::SLayerV3(const void* data, size_t length) |
| | | { |
| | | const char *d = static_cast<const char*>(data), *a = d; |
| | | read(d, m_NumBoxes); |
| | | read(d, m_NumClasses); |
| | | read(d,_n_grid_h); |
| | | read(d,_n_grid_w); |
| | | read(d, m_OutputSize); |
| | | assert(d = a + length); |
| | | } |
| | | |
| | | SLayerV3::SLayerV3(const uint32_t& numBoxes, const uint32_t& numClasses, const uint32_t& grid_h_,const uint32_t &grid_w_): |
| | | m_NumBoxes(numBoxes), |
| | | m_NumClasses(numClasses), |
| | | _n_grid_h(grid_h_), |
| | | _n_grid_w(grid_w_) |
| | | { |
| | | assert(m_NumBoxes > 0); |
| | | assert(m_NumClasses > 0); |
| | | assert(_n_grid_h > 0); |
| | | assert(_n_grid_w > 0); |
| | | m_OutputSize = _n_grid_h * _n_grid_w * (m_NumBoxes * (4 + 1 + m_NumClasses)); |
| | | } |
| | | |
| | | int SLayerV3::getNbOutputs() const { return 1; } |
| | | |
| | | nvinfer1::Dims SLayerV3::getOutputDimensions(int index, const nvinfer1::Dims* inputs, |
| | | int nbInputDims) |
| | | { |
| | | assert(index == 0); |
| | | assert(nbInputDims == 1); |
| | | return inputs[0]; |
| | | } |
| | | |
| | | void SLayerV3::configure(const nvinfer1::Dims* inputDims, int nbInputs, |
| | | const nvinfer1::Dims* outputDims, int nbOutputs, int maxBatchSize) |
| | | { |
| | | assert(nbInputs == 1); |
| | | assert(inputDims != nullptr); |
| | | } |
| | | |
| | | int SLayerV3::initialize() { return 0; } |
| | | |
| | | void SLayerV3::terminate() {} |
| | | |
| | | size_t SLayerV3::getWorkspaceSize(int maxBatchSize) const { return 0; } |
| | | |
| | | int SLayerV3::enqueue(int batchSize, const void* const* inputs, void** outputs, void* workspace, |
| | | cudaStream_t stream) |
| | | { |
| | | NV_CUDA_CHECK(cudaSLayerV3(inputs[0], outputs[0], batchSize,_n_grid_h,_n_grid_w, m_NumClasses, |
| | | m_NumBoxes, m_OutputSize, stream)); |
| | | return 0; |
| | | } |
| | | |
| | | size_t SLayerV3::getSerializationSize() |
| | | { |
| | | return sizeof(m_NumBoxes) + sizeof(m_NumClasses) + sizeof(_n_grid_w)+sizeof(_n_grid_h) + sizeof(m_OutputSize); |
| | | } |
| | | |
| | | void SLayerV3::serialize(void* buffer) |
| | | { |
| | | char *d = static_cast<char*>(buffer), *a = d; |
| | | write(d, m_NumBoxes); |
| | | write(d, m_NumClasses); |
| | | write(d,_n_grid_h); |
| | | write(d,_n_grid_w); |
| | | write(d, m_OutputSize); |
| | | assert(d == a + getSerializationSize()); |
| | | } |
New file |
| | |
| | | #ifndef __PLUGIN_LAYER_H__ |
| | | #define __PLUGIN_LAYER_H__ |
| | | |
| | | #include <cassert> |
| | | #include <cstring> |
| | | #include <cudnn.h> |
| | | #include <iostream> |
| | | #include <memory> |
| | | |
| | | #include "NvInferPlugin.h" |
| | | |
| | | #define NV_CUDA_CHECK(status) \ |
| | | { \ |
| | | if (status != 0) \ |
| | | { \ |
| | | std::cout << "Cuda failure: " << cudaGetErrorString(status) << " in file " << __FILE__ \ |
| | | << " at line " << __LINE__ << std::endl; \ |
| | | abort(); \ |
| | | } \ |
| | | } |
| | | |
| | | cudaError_t cudaSLayerV3(const void* input, void* output, const uint32_t& batchSize, |
| | | const uint32_t& n_grid_h_, const uint32_t& n_grid_w_, |
| | | const uint32_t& numOutputClasses, const uint32_t& numBBoxes, |
| | | uint64_t outputSize, cudaStream_t stream); |
| | | |
| | | class PluginFactory : public nvinfer1::IPluginFactory |
| | | { |
| | | |
| | | public: |
| | | PluginFactory(); |
| | | nvinfer1::IPlugin* createPlugin(const char* layerName, const void* serialData, |
| | | size_t serialLength) override; |
| | | bool isPlugin(const char* name); |
| | | void destroy(); |
| | | |
| | | private: |
| | | static const int m_MaxLeakyLayers = 72; |
| | | static const int m_ReorgStride = 2; |
| | | static constexpr float m_LeakyNegSlope = 0.1f; |
| | | static const int m_NumBoxes = 5; |
| | | static const int m_NumCoords = 4; |
| | | static const int m_NumClasses = 80; |
| | | static const int m_MaxSLayers = 3; |
| | | int m_LeakyReLUCount = 0; |
| | | int m_SLayerCount = 0; |
| | | nvinfer1::plugin::RegionParameters m_RegionParameters{m_NumBoxes, m_NumCoords, m_NumClasses, |
| | | nullptr}; |
| | | |
| | | struct INvPluginDeleter |
| | | { |
| | | void operator()(nvinfer1::plugin::INvPlugin* ptr) |
| | | { |
| | | if (ptr) |
| | | { |
| | | ptr->destroy(); |
| | | } |
| | | } |
| | | }; |
| | | struct IPluginDeleter |
| | | { |
| | | void operator()(nvinfer1::IPlugin* ptr) |
| | | { |
| | | if (ptr) |
| | | { |
| | | ptr->terminate(); |
| | | } |
| | | } |
| | | }; |
| | | typedef std::unique_ptr<nvinfer1::plugin::INvPlugin, INvPluginDeleter> unique_ptr_INvPlugin; |
| | | typedef std::unique_ptr<nvinfer1::IPlugin, IPluginDeleter> unique_ptr_IPlugin; |
| | | |
| | | unique_ptr_INvPlugin m_ReorgLayer; |
| | | unique_ptr_INvPlugin m_RegionLayer; |
| | | unique_ptr_INvPlugin m_LeakyReLULayers[m_MaxLeakyLayers]; |
| | | unique_ptr_IPlugin m_SLayers[m_MaxSLayers]; |
| | | }; |
| | | |
| | | class SLayerV3 : public nvinfer1::IPlugin |
| | | { |
| | | public: |
| | | SLayerV3(const void* data, size_t length); |
| | | SLayerV3(const uint32_t& numBoxes, const uint32_t& numClasses, const uint32_t& grid_h_,const uint32_t &grid_w_); |
| | | int getNbOutputs() const override; |
| | | nvinfer1::Dims getOutputDimensions(int index, const nvinfer1::Dims* inputs, |
| | | int nbInputDims) override; |
| | | void configure(const nvinfer1::Dims* inputDims, int nbInputs, const nvinfer1::Dims* outputDims, |
| | | int nbOutputs, int maxBatchSize) override; |
| | | int initialize() override; |
| | | void terminate() override; |
| | | size_t getWorkspaceSize(int maxBatchSize) const override; |
| | | int enqueue(int batchSize, const void* const* intputs, void** outputs, void* workspace, |
| | | cudaStream_t stream) override; |
| | | size_t getSerializationSize() override; |
| | | void serialize(void* buffer) override; |
| | | |
| | | private: |
| | | template <typename T> |
| | | void write(char*& buffer, const T& val) |
| | | { |
| | | *reinterpret_cast<T*>(buffer) = val; |
| | | buffer += sizeof(T); |
| | | } |
| | | |
| | | template <typename T> |
| | | void read(const char*& buffer, T& val) |
| | | { |
| | | val = *reinterpret_cast<const T*>(buffer); |
| | | buffer += sizeof(T); |
| | | } |
| | | uint32_t m_NumBoxes; |
| | | uint32_t m_NumClasses; |
| | | uint32_t m_GridSize; |
| | | uint64_t m_OutputSize; |
| | | uint32_t _n_grid_h; |
| | | uint32_t _n_grid_w; |
| | | }; |
| | | |
| | | #endif // __PLUGIN_LAYER_H__ |
New file |
| | |
| | | #include "trt_utils.h" |
| | | #include <NvInferRuntimeCommon.h> |
| | | #include <experimental/filesystem> |
| | | #include <fstream> |
| | | #include <iomanip> |
| | | using namespace nvinfer1; |
| | | REGISTER_TENSORRT_PLUGIN(MishPluginCreator); |
| | | REGISTER_TENSORRT_PLUGIN(ChunkPluginCreator); |
| | | REGISTER_TENSORRT_PLUGIN(HardswishPluginCreator); |
| | | |
| | | cv::Mat blobFromDsImages(const std::vector<DsImage>& inputImages, |
| | | const int& inputH, |
| | | const int& inputW) |
| | | { |
| | | std::vector<cv::Mat> letterboxStack(inputImages.size()); |
| | | for (uint32_t i = 0; i < inputImages.size(); ++i) |
| | | { |
| | | inputImages.at(i).getLetterBoxedImage().copyTo(letterboxStack.at(i)); |
| | | } |
| | | return cv::dnn::blobFromImages(letterboxStack, 1.0, cv::Size(inputW, inputH), |
| | | cv::Scalar(0.0, 0.0, 0.0),true); |
| | | } |
| | | |
| | | // static void leftTrim(std::string& s) |
| | | // { |
| | | // s.erase(s.begin(), find_if(s.begin(), s.end(), [](int ch) { return !isspace(ch); })); |
| | | // } |
| | | |
| | | // static void rightTrim(std::string& s) |
| | | // { |
| | | // s.erase(find_if(s.rbegin(), s.rend(), [](int ch) { return !isspace(ch); }).base(), s.end()); |
| | | // } |
| | | |
| | | // std::string trim(std::string s) |
| | | // { |
| | | // leftTrim(s); |
| | | // rightTrim(s); |
| | | // return s; |
| | | // } |
| | | |
| | | // std::string triml(std::string s,const char* t) |
| | | // { |
| | | // s.erase(0, s.find_first_not_of(t)); |
| | | // return s; |
| | | // } |
| | | |
| | | // std::string trimr(std::string s, const char* t) |
| | | // { |
| | | // s.erase(s.find_last_not_of(t) + 1); |
| | | // return s; |
| | | // } |
| | | |
| | | float clamp(const float val, const float minVal, const float maxVal) |
| | | { |
| | | assert(minVal <= maxVal); |
| | | return std::min(maxVal, std::max(minVal, val)); |
| | | } |
| | | |
| | | bool fileExists(const std::string fileName, bool verbose) |
| | | { |
| | | if (!std::experimental::filesystem::exists(std::experimental::filesystem::path(fileName))) |
| | | { |
| | | if (verbose) std::cout << "File does not exist : " << fileName << std::endl; |
| | | return false; |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | // BBox convertBBoxNetRes(const float& bx, const float& by, const float& bw, const float& bh, |
| | | // const uint32_t& stride, const uint32_t& netW, const uint32_t& netH) |
| | | // { |
| | | // BBox b; |
| | | // // Restore coordinates to network input resolution |
| | | // float x = bx * stride; |
| | | // float y = by * stride; |
| | | |
| | | // b.x1 = x - bw / 2; |
| | | // b.x2 = x + bw / 2; |
| | | |
| | | // b.y1 = y - bh / 2; |
| | | // b.y2 = y + bh / 2; |
| | | |
| | | // b.x1 = clamp(b.x1, 0, netW); |
| | | // b.x2 = clamp(b.x2, 0, netW); |
| | | // b.y1 = clamp(b.y1, 0, netH); |
| | | // b.y2 = clamp(b.y2, 0, netH); |
| | | |
| | | // return b; |
| | | // } |
| | | |
| | | // void convertBBoxImgRes(const float scalingFactor, |
| | | // const float xOffset, |
| | | // const float yOffset, |
| | | // BBox& bbox) |
| | | // { |
| | | // //// Undo Letterbox |
| | | // bbox.x1 -= xOffset; |
| | | // bbox.x2 -= xOffset; |
| | | // bbox.y1 -= yOffset; |
| | | // bbox.y2 -= yOffset; |
| | | // //// Restore to input resolution |
| | | // bbox.x1 /= scalingFactor; |
| | | // bbox.x2 /= scalingFactor; |
| | | // bbox.y1 /= scalingFactor; |
| | | // bbox.y2 /= scalingFactor; |
| | | // std::cout << "convertBBoxImgRes" << std::endl; |
| | | |
| | | |
| | | // } |
| | | |
| | | // void printPredictions(const BBoxInfo& b, const std::string& className) |
| | | // { |
| | | // std::cout << " label:" << b.label << "(" << className << ")" |
| | | // << " confidence:" << b.prob << " xmin:" << b.box.x1 << " ymin:" << b.box.y1 |
| | | // << " xmax:" << b.box.x2 << " ymax:" << b.box.y2 << std::endl; |
| | | // } |
| | | // |
| | | uint64_t get3DTensorVolume(nvinfer1::Dims inputDims) |
| | | { |
| | | assert(inputDims.nbDims == 3); |
| | | return inputDims.d[0] * inputDims.d[1] * inputDims.d[2]; |
| | | } |
| | | |
| | | |
| | | nvinfer1::ICudaEngine* loadTRTEngine(const std::string planFilePath, PluginFactory* pluginFactory, |
| | | Logger& logger) |
| | | { |
| | | // reading the model in memory |
| | | std::cout << "Loading TRT Engine..." << std::endl; |
| | | assert(fileExists(planFilePath)); |
| | | std::stringstream trtModelStream; |
| | | trtModelStream.seekg(0, trtModelStream.beg); |
| | | std::ifstream cache(planFilePath,std::ios::binary | std::ios::in); |
| | | assert(cache.good()); |
| | | trtModelStream << cache.rdbuf(); |
| | | cache.close(); |
| | | |
| | | // calculating model size |
| | | trtModelStream.seekg(0, std::ios::end); |
| | | const auto modelSize = trtModelStream.tellg(); |
| | | trtModelStream.seekg(0, std::ios::beg); |
| | | void* modelMem = malloc(modelSize); |
| | | trtModelStream.read((char*) modelMem, modelSize); |
| | | |
| | | nvinfer1::IRuntime* runtime = nvinfer1::createInferRuntime(logger); |
| | | std::cout << "test................................" << std::endl; |
| | | nvinfer1::ICudaEngine* engine |
| | | = runtime->deserializeCudaEngine(modelMem, modelSize, pluginFactory); |
| | | free(modelMem); |
| | | runtime->destroy(); |
| | | std::cout << "Loading Complete!" << std::endl; |
| | | |
| | | return engine; |
| | | } |
| | | |
| | | std::vector<BBoxInfo> nmsAllClasses(const float nmsThresh, |
| | | std::vector<BBoxInfo>& binfo, |
| | | const uint32_t numClasses, |
| | | const std::string &model_type) |
| | | { |
| | | std::vector<BBoxInfo> result; |
| | | std::vector<std::vector<BBoxInfo>> splitBoxes(numClasses); |
| | | for (auto& box : binfo) |
| | | { |
| | | splitBoxes.at(box.label).push_back(box); |
| | | } |
| | | |
| | | for (auto& boxes : splitBoxes) |
| | | { |
| | | boxes = nonMaximumSuppression(nmsThresh, boxes); |
| | | result.insert(result.end(), boxes.begin(), boxes.end()); |
| | | } |
| | | |
| | | return result; |
| | | } |
| | | |
| | | std::vector<BBoxInfo> nonMaximumSuppression(const float nmsThresh, std::vector<BBoxInfo> binfo) |
| | | { |
| | | auto overlap1D = [](float x1min, float x1max, float x2min, float x2max) -> float |
| | | { |
| | | if (x1min > x2min) |
| | | { |
| | | std::swap(x1min, x2min); |
| | | std::swap(x1max, x2max); |
| | | } |
| | | return x1max < x2min ? 0 : std::min(x1max, x2max) - x2min; |
| | | }; |
| | | auto computeIoU = [&overlap1D](BBox& bbox1, BBox& bbox2) -> float |
| | | { |
| | | float overlapX = overlap1D(bbox1.x1, bbox1.x2, bbox2.x1, bbox2.x2); |
| | | float overlapY = overlap1D(bbox1.y1, bbox1.y2, bbox2.y1, bbox2.y2); |
| | | float area1 = (bbox1.x2 - bbox1.x1) * (bbox1.y2 - bbox1.y1); |
| | | float area2 = (bbox2.x2 - bbox2.x1) * (bbox2.y2 - bbox2.y1); |
| | | float overlap2D = overlapX * overlapY; |
| | | float u = area1 + area2 - overlap2D; |
| | | return u == 0 ? 0 : overlap2D / u; |
| | | }; |
| | | |
| | | std::stable_sort(binfo.begin(), binfo.end(), |
| | | [](const BBoxInfo& b1, const BBoxInfo& b2) { return b1.prob > b2.prob; }); |
| | | std::vector<BBoxInfo> out; |
| | | for (auto& i : binfo) |
| | | { |
| | | bool keep = true; |
| | | for (auto& j : out) |
| | | { |
| | | if (keep) |
| | | { |
| | | float overlap = computeIoU(i.box, j.box); |
| | | keep = overlap <= nmsThresh; |
| | | } |
| | | else |
| | | break; |
| | | } |
| | | if (keep) out.push_back(i); |
| | | } |
| | | return out; |
| | | } |
New file |
| | |
| | | #ifndef __TRT_UTILS_H__ |
| | | #define __TRT_UTILS_H__ |
| | | |
| | | /* OpenCV headers */ |
| | | //#include <opencv/cv.h> |
| | | #include <opencv2/opencv.hpp> |
| | | #include <opencv2/core/core.hpp> |
| | | #include <opencv2/dnn/dnn.hpp> |
| | | #include <opencv2/highgui/highgui.hpp> |
| | | #include <opencv2/imgproc/imgproc.hpp> |
| | | |
| | | #include "mish.h" |
| | | #include "chunk.h" |
| | | #include "hardswish.h" |
| | | #include <set> |
| | | #include <math.h> |
| | | #include <algorithm> |
| | | #include "NvInfer.h" |
| | | |
| | | #include "ds_image.h" |
| | | #include "plugin_factory.h" |
| | | //#include "logging.h" |
| | | class DsImage; |
| | | struct BBox |
| | | { |
| | | float x1, y1, x2, y2; |
| | | }; |
| | | |
| | | struct BBoxInfo |
| | | { |
| | | BBox box; |
| | | int label; |
| | | int classId; // For coco benchmarking |
| | | float prob; |
| | | }; |
| | | |
| | | class Logger : public nvinfer1::ILogger |
| | | { |
| | | public: |
| | | Logger(Severity severity = Severity::kWARNING) |
| | | { |
| | | |
| | | } |
| | | |
| | | ~Logger() |
| | | { |
| | | |
| | | } |
| | | nvinfer1::ILogger& getTRTLogger() |
| | | { |
| | | return *this; |
| | | } |
| | | |
| | | void log(nvinfer1::ILogger::Severity severity, const char* msg) override |
| | | { |
| | | // suppress info-level messages |
| | | if (severity == Severity::kINFO) return; |
| | | |
| | | switch (severity) |
| | | { |
| | | case Severity::kINTERNAL_ERROR: std::cerr << "INTERNAL_ERROR: " << msg << std::endl; break; |
| | | case Severity::kERROR: std::cerr << "ERROR: " << msg << std::endl; break; |
| | | case Severity::kWARNING: std::cerr << "WARNING: " << msg << std::endl; break; |
| | | case Severity::kINFO: std::cerr << "INFO: " << msg << std::endl; break; |
| | | case Severity::kVERBOSE: break; |
| | | // default: std::cerr <<"UNKNOW:"<< msg << std::endl;break; |
| | | } |
| | | } |
| | | }; |
| | | float clamp(const float val, const float minVal, const float maxVal); |
| | | // Common helper functions |
| | | cv::Mat blobFromDsImages(const std::vector<DsImage>& inputImages, const int& inputH, |
| | | const int& inputW); |
| | | |
| | | bool fileExists(const std::string fileName, bool verbose = true); |
| | | |
| | | nvinfer1::ICudaEngine* loadTRTEngine(const std::string planFilePath, PluginFactory* pluginFactory, |
| | | Logger& logger); |
| | | uint64_t get3DTensorVolume(nvinfer1::Dims inputDims); |
| | | |
| | | std::vector<BBoxInfo> nmsAllClasses(const float nmsThresh, std::vector<BBoxInfo>& binfo, |
| | | const uint32_t numClasses, const std::string &model_type); |
| | | |
| | | std::vector<BBoxInfo> nonMaximumSuppression(const float nmsThresh, std::vector<BBoxInfo> binfo); |
| | | #endif |
New file |
| | |
| | | //
|
| | | // Created by Scheaven on 2020/4/26.
|
| | | //
|
| | | #include "h_interface.h"
|
| | | #include <iostream>
|
| | | #include <fstream>
|
| | | #include <json/json.h>
|
| | | using namespace std;
|
| | |
|
| | | string m_staticStruct::model_path = "path";
|
| | | int m_staticStruct::type = 1;
|
| | |
|
| | | // 创建
|
| | | API void* create(const char *conf, int *max_chan)
|
| | | {
|
| | | CLog::Initialize("../config/log4cplus.properties");
|
| | | ReadJsonFromFile(conf);
|
| | | AriManager *handle = new AriManager();
|
| | | return handle;
|
| | | }
|
| | |
|
| | | // 原查找结果接口
|
| | | API void* get_result(void *handle, const void *img, const int chan)
|
| | | {
|
| | | AriManager *h = (AriManager*)handle;
|
| | | TResult *t_result = (TResult*)malloc(sizeof(TResult));
|
| | | init_TResult(t_result);
|
| | | h->single_SDK(chan, img, *t_result);
|
| | | return t_result;
|
| | | }
|
| | |
|
| | | API void release_result(void *res)
|
| | | {
|
| | | if (!res) return;
|
| | | TResult *cres = (TResult*)res;
|
| | | // delete cres;
|
| | | for (int i = 0; i < cres->count; i++){
|
| | | Target t = cres->targets[i];
|
| | | if (t.feature) free(t.feature);
|
| | | if (t.attribute) free(t.attribute);
|
| | | }
|
| | |
|
| | | free(cres->targets);
|
| | | free(cres);
|
| | | }
|
| | |
|
| | | API void release(void* handle)
|
| | | {
|
| | | AriManager *h = (AriManager*)handle;
|
| | | delete h;
|
| | | h = NULL;
|
| | | }
|
| | |
|
| | | // 读取Json文件
|
| | | void ReadJsonFromFile(const char* filename)
|
| | | {
|
| | | Json::Reader reader;
|
| | | Json::Value root;
|
| | | int max_cam_num;
|
| | | int wander_time;
|
| | | float mv_velocity;
|
| | | float fall_rate;
|
| | | string tmp = filename;
|
| | |
|
| | | ifstream in(filename, ios::binary);
|
| | | if( !in.is_open() )
|
| | | {
|
| | | cout << "Error opening json config file\n";
|
| | | return;
|
| | | }
|
| | | if(reader.parse(in,root))
|
| | | {
|
| | | std::string model_path = root["param"]["model_path"].asString();
|
| | | int type = root["param"]["type"].asInt();
|
| | | m_staticStruct::model_path = model_path;
|
| | | m_staticStruct::type = type;
|
| | | }
|
| | | in.close();
|
| | | }
|
| | |
|
| | | void init_TResult(TResult *t)
|
| | | {
|
| | | t->count = 0;
|
| | | t->targets = nullptr;
|
| | | }
|
New file |
| | |
| | | //
|
| | | // Created by Scheaven on 2020/4/26.
|
| | | //
|
| | |
|
| | | #ifndef H_INTERFACE_H
|
| | | #define H_INTERFACE_H
|
| | | #include "./core/ari_manager.h"
|
| | | #include "config.h"
|
| | | #ifdef __cplusplus
|
| | | extern "C"{
|
| | | #endif
|
| | |
|
| | | #define API extern "C" __attribute__((visibility ("default")))
|
| | |
|
| | | API void* create(const char *conf, int *max_chan);
|
| | | API void release(void* handle);
|
| | | API void* get_result(void *handle, const void *img, const int chan);
|
| | | API void release_result(void *res);
|
| | |
|
| | | void ReadJsonFromFile(const char* filename); // 读取json文件
|
| | | int setCamConfig(const char* data);
|
| | | int ReadJsonStr(void* handle, const char* data);
|
| | |
|
| | | void init_TResult(TResult *t);
|
| | |
|
| | | #ifdef __cplusplus
|
| | | }
|
| | | #endif
|
| | | #endif
|
New file |
| | |
| | | //
|
| | | // Created by Scheaven on 2020/4/23.
|
| | | //
|
| | |
|
| | | #include "draw_util.h"
|
| | | #include <sys/types.h>
|
| | | #include <sys/stat.h>
|
| | | #include <stdlib.h>
|
| | | #include <iostream>
|
| | | #include <unistd.h>
|
| | | #include <stdio.h>
|
| | | #include <dirent.h>
|
| | | #include <string.h>
|
| | | #include "log_util.h"
|
| | | #include <opencv2/opencv.hpp> // C++
|
| | |
|
| | | using namespace cv;
|
| | | using namespace std;
|
| | |
|
| | |
|
| | | void create_foldert(const char *path)
|
| | | {
|
| | | // if(access(path, 0) == -1)
|
| | | // {
|
| | | // int flag = mkdir(path,0777);
|
| | | // if(flag==0)
|
| | | // {
|
| | | // DEBUG( ":::::::::create folder!::::::");
|
| | | // }else
|
| | | // {
|
| | | // DEBUG( ":::::::::create folder! error!::::::");
|
| | | // }
|
| | | // }
|
| | | DEBUG("=2==create_path:")
|
| | | char DirName[256];
|
| | | strcpy(DirName,path);
|
| | | int i,len = strlen(DirName);
|
| | | if(DirName[len-1]!='/')
|
| | | strcat(DirName,"/");
|
| | | len = strlen(DirName);
|
| | | for(i=1;i<len;i++)
|
| | | {
|
| | | if(DirName[i]=='/')
|
| | | {
|
| | | // DirName[i] = 0;
|
| | | // if(access(DirName,NULL) != 0)
|
| | | // {
|
| | | // if(mkdir(DirName,0777) == -1)
|
| | | // {
|
| | | // perror("mkdir error");
|
| | | // }
|
| | | // }
|
| | | DEBUG("=1==create_path:")
|
| | | int a = access(DirName, F_OK);
|
| | | if(a ==-1)
|
| | | {
|
| | | mkdir(DirName,0755);
|
| | | }
|
| | | DirName[i] = '/';
|
| | | }
|
| | | }
|
| | | }
|
| | |
|
| | | void save_human_img(cv::Mat aaaa, string path, int type, string name)
|
| | | {
|
| | | // aaaa.convertTo(aaaa, CV_8UC3, 255.0f);
|
| | | // cv::cvtColor(aaaa, aaaa, cv::COLOR_RGB2BGR); // 颜色空间需要确认一下Z
|
| | | string file_path ="./02_humanID/"+ path +"/";
|
| | | create_foldert(file_path.c_str());
|
| | | cv::imwrite(file_path + name + "_" + to_string(type) + ".jpg", aaaa);
|
| | | }
|
| | |
|
| | | void draw_SDK_result(cv::Mat mat_img, Target& target)
|
| | | {
|
| | | cv::Rect tmp_rect = cv::Rect(target.rect.left,target.rect.top,target.rect.right-target.rect.left,target.rect.bottom-target.rect.top);
|
| | | cv::rectangle(mat_img, tmp_rect , cv::Scalar(50, 200, 50), 2);
|
| | | // cv::resize(mat_img, mat_img, frameSize);
|
| | | // cv::imwrite("1111.jpg", mat_img);
|
| | | cv::imshow(to_string(1), mat_img);
|
| | | cv::waitKey(0);
|
| | | }
|
New file |
| | |
| | | //
|
| | | // Created by Scheaven on 2020/4/23.
|
| | | //
|
| | | #ifndef DRAW_UTIL_H
|
| | | #define DRAW_UTIL_H
|
| | | #include "../config.h"
|
| | | #include "std_target.h"
|
| | | #include "unistd.h"
|
| | |
|
| | | void save_human_img(cv::Mat aaaa, std::string path, int type, std::string name);
|
| | |
|
| | | void create_foldert(const char *path);
|
| | |
|
| | | void draw_SDK_result(cv::Mat mat_img, Target& target);
|
| | |
|
| | | #endif //DRAW_UTIL_H
|
New file |
| | |
| | | #include "log_util.h" |
| | | #include <sstream> |
| | | #include <memory> |
| | | #include <fstream> |
| | | #include <iomanip> |
| | | #include <sstream> |
| | | #include <cstdlib> |
| | | #include <iostream> |
| | | |
| | | #include <log4cplus/logger.h> |
| | | #include <log4cplus/loggingmacros.h> |
| | | #include <log4cplus/fileappender.h> |
| | | #include <log4cplus/configurator.h> |
| | | |
| | | using namespace std; |
| | | using namespace log4cplus; |
| | | |
| | | static Logger global_pLogger; |
| | | CLog* CLog::pInstance = NULL; |
| | | |
| | | CLog* CLog::Initialize(const string& strLogFile) |
| | | { |
| | | if (NULL == pInstance) |
| | | { |
| | | pInstance = new CLog(strLogFile); |
| | | } |
| | | return pInstance; |
| | | } |
| | | |
| | | CLog::CLog(const string& strLogFile) |
| | | { |
| | | PropertyConfigurator::doConfigure(LOG4CPLUS_TEXT(strLogFile)); |
| | | global_pLogger = Logger::getRoot(); |
| | | } |
| | | |
| | | void DEBUG_LOG(const string& strInfo, const string& strFile, int iLine) |
| | | { |
| | | stringstream ssLogData; |
| | | ssLogData << "[" << strFile << ":" << iLine << "] " << strInfo; |
| | | std::cout << ssLogData.str() << std::endl; |
| | | |
| | | |
| | | LOG4CPLUS_DEBUG(global_pLogger, ssLogData.str()); |
| | | } |
| | | |
| | | void ERROR_LOG(const string& strInfo, const string& strFile, int iLine) |
| | | { |
| | | stringstream ssLogData; |
| | | ssLogData << "[" << strFile << ":" << iLine << "] " << strInfo; |
| | | std::cout << ssLogData.str() << std::endl; |
| | | LOG4CPLUS_ERROR(global_pLogger, ssLogData.str()); |
| | | } |
| | | |
| | | void WARN_LOG(const string& strInfo, const string& strFile, int iLine) |
| | | { |
| | | stringstream ssLogData; |
| | | ssLogData << "[" << strFile << ":" << iLine << "] " << strInfo; |
| | | std::cout << ssLogData.str() << std::endl; |
| | | LOG4CPLUS_WARN(global_pLogger, ssLogData.str()); |
| | | } |
| | | |
| | | |
New file |
| | |
| | | #ifndef _LOG_UTIL_H_ |
| | | #define _LOG_UTIL_H_ |
| | | |
| | | #include <string> |
| | | #include <iostream> |
| | | |
| | | #define DEBUG(input) DEBUG_LOG((input), (__FILE__), (__LINE__)); |
| | | #define ERROR(input) ERROR_LOG((input), (__FILE__), (__LINE__)); |
| | | #define WARN(input) WARN_LOG((input), (__FILE__), (__LINE__)); |
| | | |
| | | using namespace std; |
| | | // using namespace log4cplus; |
| | | |
| | | void DEBUG_LOG(const std::string& strInfo, const std::string& strFile, int iLine); |
| | | void ERROR_LOG(const std::string& strInfo, const std::string& strFile, int iLine); |
| | | void WARN_LOG(const std::string& strInfo, const std::string& strFile, int iLine); |
| | | |
| | | class CLog |
| | | { |
| | | public: |
| | | static CLog* Initialize(const std::string& strLogFile); |
| | | |
| | | private: |
| | | CLog(const std::string& strLogFile); |
| | | static CLog* pInstance; |
| | | }; |
| | | |
| | | #endif |
New file |
| | |
| | | #pragma once |
| | | |
| | | #include <istream> |
| | | #include <string> |
| | | #include <chrono> |
| | | #include "log_util.h" |
| | | #include <boost/format.hpp> |
| | | |
| | | class Timer |
| | | { |
| | | public: |
| | | Timer():beg_(clock_::now()){} |
| | | void reset() |
| | | { |
| | | beg_ = clock_::now(); |
| | | } |
| | | double elapsed() const |
| | | { |
| | | return std::chrono::duration_cast<second_>(clock_::now() - beg_).count(); |
| | | } |
| | | void out(std::string message="") |
| | | { |
| | | double t = elapsed(); |
| | | // std::cout << message << " elasped time:" << t << "ms" << std::endl; |
| | | DEBUG((boost::format("%e lasped time:%f ms")%message %t).str()); |
| | | reset(); |
| | | } |
| | | // DEBUG((boost::format("nhao%d")%1).str()); |
| | | |
| | | double get_duration() const |
| | | { |
| | | return elapsed(); |
| | | } |
| | | private: |
| | | using clock_ = std::chrono::high_resolution_clock; |
| | | using second_=std::chrono::duration<double,std::milli>; |
| | | std::chrono::time_point<clock_>beg_; |
| | | }; |