├── .gitattributes ├── SOLOv2-TensorRT ├── image │ ├── 1.jpg │ ├── 2.jpg │ ├── 3.jpg │ ├── result.jpg │ ├── result1.jpg │ ├── result2.jpg │ └── result3.jpg ├── build │ ├── result1.jpg │ ├── convertModel │ ├── SOLOv2-TensorRT │ ├── custom.names │ └── Makefile ├── include │ ├── logging.h │ └── segmentation_trt.h └── src │ ├── demo.cpp │ └── segmentation_trt.cpp └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /SOLOv2-TensorRT/image/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Broad-sky/common-image-segmentation-algorithm/HEAD/SOLOv2-TensorRT/image/1.jpg -------------------------------------------------------------------------------- /SOLOv2-TensorRT/image/2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Broad-sky/common-image-segmentation-algorithm/HEAD/SOLOv2-TensorRT/image/2.jpg -------------------------------------------------------------------------------- /SOLOv2-TensorRT/image/3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Broad-sky/common-image-segmentation-algorithm/HEAD/SOLOv2-TensorRT/image/3.jpg -------------------------------------------------------------------------------- /SOLOv2-TensorRT/build/result1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Broad-sky/common-image-segmentation-algorithm/HEAD/SOLOv2-TensorRT/build/result1.jpg -------------------------------------------------------------------------------- /SOLOv2-TensorRT/image/result.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Broad-sky/common-image-segmentation-algorithm/HEAD/SOLOv2-TensorRT/image/result.jpg -------------------------------------------------------------------------------- /SOLOv2-TensorRT/image/result1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Broad-sky/common-image-segmentation-algorithm/HEAD/SOLOv2-TensorRT/image/result1.jpg -------------------------------------------------------------------------------- /SOLOv2-TensorRT/image/result2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Broad-sky/common-image-segmentation-algorithm/HEAD/SOLOv2-TensorRT/image/result2.jpg -------------------------------------------------------------------------------- /SOLOv2-TensorRT/image/result3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Broad-sky/common-image-segmentation-algorithm/HEAD/SOLOv2-TensorRT/image/result3.jpg -------------------------------------------------------------------------------- /SOLOv2-TensorRT/include/logging.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Broad-sky/common-image-segmentation-algorithm/HEAD/SOLOv2-TensorRT/include/logging.h -------------------------------------------------------------------------------- /SOLOv2-TensorRT/build/convertModel: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Broad-sky/common-image-segmentation-algorithm/HEAD/SOLOv2-TensorRT/build/convertModel -------------------------------------------------------------------------------- /SOLOv2-TensorRT/build/SOLOv2-TensorRT: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Broad-sky/common-image-segmentation-algorithm/HEAD/SOLOv2-TensorRT/build/SOLOv2-TensorRT -------------------------------------------------------------------------------- /SOLOv2-TensorRT/build/custom.names: -------------------------------------------------------------------------------- 1 | person 2 | bicycle 3 | car 4 | motorbike 5 | aeroplane 6 | bus 7 | train 8 | truck 9 | boat 10 | traffic light 11 | fire hydrant 12 | stop sign 13 | parking meter 14 | bench 15 | bird 16 | cat 17 | dog 18 | horse 19 | sheep 20 | cow 21 | elephant 22 | bear 23 | zebra 24 | giraffe 25 | backpack 26 | umbrella 27 | handbag 28 | tie 29 | suitcase 30 | frisbee 31 | skis 32 | snowboard 33 | sports ball 34 | kite 35 | baseball bat 36 | baseball glove 37 | skateboard 38 | surfboard 39 | tennis racket 40 | bottle 41 | wine glass 42 | cup 43 | fork 44 | knife 45 | spoon 46 | bowl 47 | banana 48 | apple 49 | sandwich 50 | orange 51 | broccoli 52 | carrot 53 | hot dog 54 | pizza 55 | donut 56 | cake 57 | chair 58 | sofa 59 | pottedplant 60 | bed 61 | diningtable 62 | toilet 63 | tvmonitor 64 | laptop 65 | mouse 66 | remote 67 | keyboard 68 | cell phone 69 | microwave 70 | oven 71 | toaster 72 | sink 73 | refrigerator 74 | book 75 | clock 76 | vase 77 | scissors 78 | teddy bear 79 | hair drier 80 | toothbrush 81 | -------------------------------------------------------------------------------- /SOLOv2-TensorRT/src/demo.cpp: -------------------------------------------------------------------------------- 1 | //!Time : 2021年03月04日 14时42分 2 | //!GitHub : https://github.com/Broad-sky 3 | //!Author : Shengping Shen 4 | //!File : segmentation_trt.h 5 | //!About : Practice is perfect in one, formed in thought destoryed 6 | 7 | #include 8 | #include 9 | #include"segmentation_trt.h" 10 | //..\include\ 11 | 12 | int main(int argc, char **argv) 13 | { 14 | if (argc == 5 && std::string(argv[1]) == "-image_path" && std::string(argv[3]) == "-save_path") 15 | { 16 | const char *image_path = argv[2]; // input image path 17 | const char *save_path = argv[4]; // save result image path 18 | 19 | segTRTSigma* seg = new segTRTSigma(); 20 | if (seg->initialModel()) // init model 21 | { 22 | cv::Mat srcimg = cv::imread(image_path); 23 | std::vector segobj; 24 | if (!srcimg.empty()) 25 | { 26 | clock_t start, finish; 27 | start = clock(); 28 | segobj = seg->runModel(srcimg, false); // run model 29 | cv::imwrite(save_path, segobj[0].segMat); 30 | finish = clock(); 31 | double subtime = (float)(finish - start) / CLOCKS_PER_SEC; 32 | printf("%f seconds\n", subtime); 33 | } 34 | } 35 | else 36 | { 37 | std::cerr << "initial model error" << std::endl; 38 | } 39 | } 40 | else 41 | { 42 | std::cerr << "-->arguments not right!" << std::endl; 43 | std::cerr << "--> ./SOLOv2-TensorRT -image_path ./1.jpg -save_path ./demo1.jpg" << std::endl; 44 | return -1; 45 | } 46 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # common image segmentation algorithms 2 | 3 | 🚀The project will release some accelerated image segmentation algorithms, including: instance segmentation, semantic segmentation, panoramic segmentation, etc., such as: SOLOv2, Deeplab, Panoptic SegFormer, mainly using TensorRT acceleration library, all implemented in C++. 4 | 5 | ## Instance segmentation part 6 | 7 | #### Introduction 8 | 9 | ​ This Part is based on SOLOv2 instance segmentation, and the model is deployed with tensorRT, aiming to attract jade. 10 | 11 | #### Show result 12 | 13 | result1 14 | 15 | #### Get model 16 | 17 | Download link: https://pan.baidu.com/s/1lX6n44CtXlqdIV0NiS8jXA 18 | 19 | Code: fw8f 20 | 21 | #### Run example 22 | 23 | **1. go to the build directory.** 24 | 25 | ```powershell 26 | SOLOv2-TensorRT$ cd build 27 | ``` 28 | 29 | **2. run command.** 30 | 31 | ```powershell 32 | ./convertModel ./solov2_r50_fpn_8gpu_3x.onnx -g ./seg_coco_permute.bin 33 | ``` 34 | 35 | **3. run command.** 36 | 37 | ```powershell 38 | SOLOv2-TensorRT/build$ make clean 39 | ``` 40 | 41 | **4. Compile the project.** 42 | 43 | ```powershell 44 | SOLOv2-TensorRT/build$ make 45 | 46 | -- The C compiler identification is GNU 7.5.0 47 | -- The CXX compiler identification is GNU 7.5.0 48 | -- Check for working C compiler: /usr/bin/cc 49 | -- Check for working C compiler: /usr/bin/cc -- works 50 | -- Detecting C compiler ABI info 51 | -- Detecting C compiler ABI info - done 52 | -- Detecting C compile features 53 | -- Detecting C compile features - done 54 | -- Check for working CXX compiler: /usr/bin/c++ 55 | -- Check for working CXX compiler: /usr/bin/c++ -- works 56 | -- Detecting CXX compiler ABI info 57 | -- Detecting CXX compiler ABI info - done 58 | -- Detecting CXX compile features 59 | -- Detecting CXX compile features - done 60 | -- Found CUDA: /usr/local/cuda-11.0 (found version "11.0") 61 | -- Found OpenCV: /usr/local/opencv3 (found version "3.4.10") 62 | -- Configuring done 63 | -- Generating done 64 | -- Build files have been written to: ../SOLOv2-TensorRT/build 65 | Scanning dependencies of target SOLOv2-TensorRT 66 | [ 33%] Building CXX object CMakeFiles/SOLOv2-TensorRT.dir/src/segmentation_trt.cpp.o 67 | [ 66%] Building CXX object CMakeFiles/SOLOv2-TensorRT.dir/src/demo.cpp.o 68 | [100%] Linking CXX executable SOLOv2-TensorRT 69 | [100%] Built target SOLOv2-TensorRT 70 | ``` 71 | 72 | **5. run the demo program.** 73 | 74 | ```powershell 75 | SOLOv2-TensorRT/build$ ./SOLOv2-TensorRT -image_path ../image/1.jpg -save_path ./demo1.jpg 76 | 77 | curr opencv version 3.4.10 78 | num of classes: 80 79 | [04/19/2022-16:58:56] [W] [TRT] TensorRT was linked against cuDNN 8.0.5 but loaded cuDNN 8.0.2 80 | [04/19/2022-16:58:56] [W] [TRT] TensorRT was linked against cuBLAS/cuBLAS LT 11.2.0 but loaded cuBLAS/cuBLAS LT 11.1.0 81 | [04/19/2022-16:58:56] [W] [TRT] TensorRT was linked against cuDNN 8.0.5 but loaded cuDNN 8.0.2 82 | [04/19/2022-16:58:56] [W] [TRT] TensorRT was linked against cuBLAS/cuBLAS LT 11.2.0 but loaded cuBLAS/cuBLAS LT 11.1.0 83 | person 0.851572 84 | person 0.763859 85 | snowboard 0.460805 86 | 0.683118 seconds 87 | ``` 88 | 89 | ## Semantic segmentation part 90 | 91 | coming soon... 92 | 93 | ## Panoramic segmentation part 94 | 95 | coming soon... 96 | -------------------------------------------------------------------------------- /SOLOv2-TensorRT/include/segmentation_trt.h: -------------------------------------------------------------------------------- 1 | //!Time : 2021年03月04日 14时23分 2 | //!GitHub : https://github.com/Broad-sky 3 | //!Author : Shengping Shen 4 | //!File : segmentation_trt.h 5 | //!About : Practice is perfect in one, formed in thought destoryed 6 | 7 | #ifndef _SEGMENTATION_TRT_H_ 8 | #define _SEGMENTATION_TRT_H_ 9 | #include 10 | #include 11 | #include 12 | #include "NvInferPlugin.h" 13 | #include "cuda_runtime.h" 14 | #include "cublas_v2.h" 15 | #include 16 | #include 17 | #include "cuda_runtime_api.h" 18 | #include"logging.h" 19 | #include 20 | #include 21 | #include 22 | #include"opencv2/opencv.hpp" 23 | 24 | struct segParams 25 | { 26 | int32_t batchSize; 27 | int32_t dlaCore; 28 | bool int8; 29 | bool fp16; 30 | bool seria; 31 | std::vector dataDirs; 32 | std::vector inputTensorNames; 33 | std::vector outputTensorNames; 34 | 35 | }; 36 | 37 | struct segTRTParams : public segParams 38 | { 39 | std::string trtFileName; 40 | }; 41 | 42 | struct segOnnxParams : public segParams 43 | { 44 | std::string onnxFileName; 45 | }; 46 | 47 | struct InferDeleter 48 | { 49 | template 50 | void operator()(T* obj) const 51 | { 52 | if (obj) 53 | { 54 | obj->destroy(); 55 | } 56 | } 57 | 58 | }; 59 | 60 | struct Object { 61 | int fea_w; 62 | int fea_h; 63 | cv::Rect_ rect; 64 | int cx; 65 | int cy; 66 | int label; 67 | float prob; 68 | cv::Mat mask; 69 | cv::Mat seg_preds; 70 | }; 71 | 72 | struct segObject { 73 | std::string message; 74 | int target_num; 75 | cv::Mat segMat; 76 | cv::Mat gammMat; 77 | int ret; 78 | }; 79 | 80 | 81 | class segTRTSigma 82 | { 83 | template 84 | using segUniquePtr = std::unique_ptr; 85 | 86 | public: 87 | segTRTSigma(); 88 | ~segTRTSigma(); 89 | 90 | bool initialModel(); 91 | std::vector runModel(cv::Mat& srcimg_gray, bool is_gamma); 92 | 93 | private: 94 | segTRTParams initial_params(); 95 | bool build_model(); 96 | size_t do_inference(cv::Mat & srcimg_gray, std::vector & segobj, bool is_gamma); 97 | 98 | private: 99 | segTRTParams sParams; 100 | std::shared_ptr< nvinfer1::ICudaEngine> sEngine; 101 | std::shared_ptr< nvinfer1::IExecutionContext> context; 102 | 103 | float * imnormalize(cv::Mat &srcimg); 104 | size_t get_seg(cv::Mat& srcimg, float* feature_pred_output, 105 | float* kernel_pred1_output, float* kernel_pred2_output, float* kernel_pred3_output, 106 | float* kernel_pred4_output, float* kernel_pred5_output, 107 | float* cate_pred1_output, float* cate_pred2_output, float* cate_pred3_output, 108 | float* cate_pred4_output, float* cate_pred5_output, std::vector &segobj); 109 | 110 | std::vector nms_sorted_seg(const std::vector& objects, std::vector& temp_picked, std::string kernel, float sigma); 111 | void dynamic_conv(float * kernel_pred, float * feature_pred, std::vector> &objects 112 | , std::vector &all_picked_kernel); 113 | void get_class_names(); 114 | void show_result_seg(const cv::Mat &bgr, const std::vector &objects, std::vector & segobj); 115 | private: 116 | float nms_thresh; 117 | size_t keep_top_k; 118 | 119 | int target_size_w; 120 | int target_size_h; 121 | 122 | int ins_w; 123 | int ins_h; 124 | 125 | int nms_pre; 126 | float score_thr; 127 | float mask_thr; 128 | float update_thr; 129 | std::string kernel; 130 | float sigma; 131 | int max_per_img; 132 | int num_class; 133 | 134 | std::string model_path; 135 | std::string save_path; 136 | 137 | 138 | std::vector class_names; 139 | std::string names_path; 140 | }; 141 | 142 | #endif // !_SEGMENTATION_TRT_H_ 143 | 144 | 145 | -------------------------------------------------------------------------------- /SOLOv2-TensorRT/build/Makefile: -------------------------------------------------------------------------------- 1 | # CMAKE generated file: DO NOT EDIT! 2 | # Generated by "Unix Makefiles" Generator, CMake Version 3.10 3 | 4 | # Default target executed when no arguments are given to make. 5 | default_target: all 6 | 7 | .PHONY : default_target 8 | 9 | # Allow only one "make -f Makefile2" at a time, but pass parallelism. 10 | .NOTPARALLEL: 11 | 12 | 13 | #============================================================================= 14 | # Special targets provided by cmake. 15 | 16 | # Disable implicit rules so canonical targets will work. 17 | .SUFFIXES: 18 | 19 | 20 | # Remove some rules from gmake that .SUFFIXES does not remove. 21 | SUFFIXES = 22 | 23 | .SUFFIXES: .hpux_make_needs_suffix_list 24 | 25 | 26 | # Suppress display of executed commands. 27 | $(VERBOSE).SILENT: 28 | 29 | 30 | # A target that is always out of date. 31 | cmake_force: 32 | 33 | .PHONY : cmake_force 34 | 35 | #============================================================================= 36 | # Set environment variables for the build. 37 | 38 | # The shell in which to execute make rules. 39 | SHELL = /bin/sh 40 | 41 | # The CMake executable. 42 | CMAKE_COMMAND = /usr/bin/cmake 43 | 44 | # The command to remove a file. 45 | RM = /usr/bin/cmake -E remove -f 46 | 47 | # Escaping for special characters. 48 | EQUALS = = 49 | 50 | # The top-level source directory on which CMake was run. 51 | CMAKE_SOURCE_DIR = "../SOLOv2-TensorRT" 52 | 53 | # The top-level build directory on which CMake was run. 54 | CMAKE_BINARY_DIR = "../SOLOv2-TensorRT/build" 55 | 56 | #============================================================================= 57 | # Targets provided globally by CMake. 58 | 59 | # Special rule for the target rebuild_cache 60 | rebuild_cache: 61 | @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake to regenerate build system..." 62 | /usr/bin/cmake -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) 63 | .PHONY : rebuild_cache 64 | 65 | # Special rule for the target rebuild_cache 66 | rebuild_cache/fast: rebuild_cache 67 | 68 | .PHONY : rebuild_cache/fast 69 | 70 | # Special rule for the target edit_cache 71 | edit_cache: 72 | @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "No interactive CMake dialog available..." 73 | /usr/bin/cmake -E echo No\ interactive\ CMake\ dialog\ available. 74 | .PHONY : edit_cache 75 | 76 | # Special rule for the target edit_cache 77 | edit_cache/fast: edit_cache 78 | 79 | .PHONY : edit_cache/fast 80 | 81 | # The main all target 82 | all: cmake_check_build_system 83 | $(CMAKE_COMMAND) -E cmake_progress_start "../SOLOv2-TensorRT/build/CMakeFiles" "../SOLOv2-TensorRT/build/CMakeFiles/progress.marks" 84 | $(MAKE) -f CMakeFiles/Makefile2 all 85 | $(CMAKE_COMMAND) -E cmake_progress_start "../SOLOv2-TensorRT/build/CMakeFiles" 0 86 | .PHONY : all 87 | 88 | # The main clean target 89 | clean: 90 | $(MAKE) -f CMakeFiles/Makefile2 clean 91 | .PHONY : clean 92 | 93 | # The main clean target 94 | clean/fast: clean 95 | 96 | .PHONY : clean/fast 97 | 98 | # Prepare targets for installation. 99 | preinstall: all 100 | $(MAKE) -f CMakeFiles/Makefile2 preinstall 101 | .PHONY : preinstall 102 | 103 | # Prepare targets for installation. 104 | preinstall/fast: 105 | $(MAKE) -f CMakeFiles/Makefile2 preinstall 106 | .PHONY : preinstall/fast 107 | 108 | # clear depends 109 | depend: 110 | $(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 1 111 | .PHONY : depend 112 | 113 | #============================================================================= 114 | # Target rules for targets named SOLOv2-TensorRT 115 | 116 | # Build rule for target. 117 | SOLOv2-TensorRT: cmake_check_build_system 118 | $(MAKE) -f CMakeFiles/Makefile2 SOLOv2-TensorRT 119 | .PHONY : SOLOv2-TensorRT 120 | 121 | # fast build rule for target. 122 | SOLOv2-TensorRT/fast: 123 | $(MAKE) -f CMakeFiles/SOLOv2-TensorRT.dir/build.make CMakeFiles/SOLOv2-TensorRT.dir/build 124 | .PHONY : SOLOv2-TensorRT/fast 125 | 126 | src/demo.o: src/demo.cpp.o 127 | 128 | .PHONY : src/demo.o 129 | 130 | # target to build an object file 131 | src/demo.cpp.o: 132 | $(MAKE) -f CMakeFiles/SOLOv2-TensorRT.dir/build.make CMakeFiles/SOLOv2-TensorRT.dir/src/demo.cpp.o 133 | .PHONY : src/demo.cpp.o 134 | 135 | src/demo.i: src/demo.cpp.i 136 | 137 | .PHONY : src/demo.i 138 | 139 | # target to preprocess a source file 140 | src/demo.cpp.i: 141 | $(MAKE) -f CMakeFiles/SOLOv2-TensorRT.dir/build.make CMakeFiles/SOLOv2-TensorRT.dir/src/demo.cpp.i 142 | .PHONY : src/demo.cpp.i 143 | 144 | src/demo.s: src/demo.cpp.s 145 | 146 | .PHONY : src/demo.s 147 | 148 | # target to generate assembly for a file 149 | src/demo.cpp.s: 150 | $(MAKE) -f CMakeFiles/SOLOv2-TensorRT.dir/build.make CMakeFiles/SOLOv2-TensorRT.dir/src/demo.cpp.s 151 | .PHONY : src/demo.cpp.s 152 | 153 | src/segmentation_trt.o: src/segmentation_trt.cpp.o 154 | 155 | .PHONY : src/segmentation_trt.o 156 | 157 | # target to build an object file 158 | src/segmentation_trt.cpp.o: 159 | $(MAKE) -f CMakeFiles/SOLOv2-TensorRT.dir/build.make CMakeFiles/SOLOv2-TensorRT.dir/src/segmentation_trt.cpp.o 160 | .PHONY : src/segmentation_trt.cpp.o 161 | 162 | src/segmentation_trt.i: src/segmentation_trt.cpp.i 163 | 164 | .PHONY : src/segmentation_trt.i 165 | 166 | # target to preprocess a source file 167 | src/segmentation_trt.cpp.i: 168 | $(MAKE) -f CMakeFiles/SOLOv2-TensorRT.dir/build.make CMakeFiles/SOLOv2-TensorRT.dir/src/segmentation_trt.cpp.i 169 | .PHONY : src/segmentation_trt.cpp.i 170 | 171 | src/segmentation_trt.s: src/segmentation_trt.cpp.s 172 | 173 | .PHONY : src/segmentation_trt.s 174 | 175 | # target to generate assembly for a file 176 | src/segmentation_trt.cpp.s: 177 | $(MAKE) -f CMakeFiles/SOLOv2-TensorRT.dir/build.make CMakeFiles/SOLOv2-TensorRT.dir/src/segmentation_trt.cpp.s 178 | .PHONY : src/segmentation_trt.cpp.s 179 | 180 | # Help Target 181 | help: 182 | @echo "The following are some of the valid targets for this Makefile:" 183 | @echo "... all (the default if no target is provided)" 184 | @echo "... clean" 185 | @echo "... depend" 186 | @echo "... rebuild_cache" 187 | @echo "... SOLOv2-TensorRT" 188 | @echo "... edit_cache" 189 | @echo "... src/demo.o" 190 | @echo "... src/demo.i" 191 | @echo "... src/demo.s" 192 | @echo "... src/segmentation_trt.o" 193 | @echo "... src/segmentation_trt.i" 194 | @echo "... src/segmentation_trt.s" 195 | .PHONY : help 196 | 197 | 198 | 199 | #============================================================================= 200 | # Special targets to cleanup operation of make. 201 | 202 | # Special rule to run CMake to check the build system integrity. 203 | # No rule that depends on this can have commands that come from listfiles 204 | # because they might be regenerated. 205 | cmake_check_build_system: 206 | $(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 0 207 | .PHONY : cmake_check_build_system 208 | 209 | -------------------------------------------------------------------------------- /SOLOv2-TensorRT/src/segmentation_trt.cpp: -------------------------------------------------------------------------------- 1 | //!Time : 2021年03月04日 14时38分 2 | //!GitHub : https://github.com/Broad-sky 3 | //!Author : Shengping Shen 4 | //!File : segmentation_trt.h 5 | //!About : Practice is perfect in one, formed in thought destoryed 6 | 7 | #include"segmentation_trt.h" 8 | 9 | #ifdef __cplusplus 10 | #define PUT_IN_REGISTER 11 | #else 12 | #define PUT_IN_REGISTER register 13 | #endif 14 | 15 | #define CV_VERSION_ID CVAUX_STR(CV_MAJOR_VERSION) CVAUX_STR(CV_MINOR_VERSION) CVAUX_STR(CV_SUBMINOR_VERSION) 16 | #define CHECK(status) \ 17 | do\ 18 | {\ 19 | auto ret = (status);\ 20 | if (ret != 0)\ 21 | {\ 22 | std::cerr << "Cuda failure: " << ret << std::endl;\ 23 | abort();\ 24 | }\ 25 | } while (0) 26 | 27 | static Logger gLogger; 28 | 29 | segTRTParams segTRTSigma::initial_params() 30 | { 31 | std::cout<<"curr opencv version " << CVAUX_STR(CV_MAJOR_VERSION) << "." << CVAUX_STR(CV_MINOR_VERSION) 32 | <<"." << CVAUX_STR(CV_SUBMINOR_VERSION) << std::endl;//2 33 | nms_thresh = 0.6; 34 | target_size_w = 992; 35 | target_size_h = 1024; 36 | nms_pre = 500; 37 | score_thr = 0.1; 38 | mask_thr = 0.5; 39 | kernel = "gaussian"; 40 | sigma = 2.0; 41 | max_per_img = 100; 42 | num_class = 80; 43 | ins_w = int(target_size_w / 4); 44 | ins_h = int(target_size_h / 4); 45 | std::vector dataDirs; 46 | segTRTParams params; 47 | params.dataDirs = dataDirs; 48 | model_path = "seg_coco_permute.bin"; 49 | names_path = "./custom.names"; 50 | save_path = "./demo.jpg"; 51 | params.inputTensorNames.push_back("dummy_input"); 52 | params.inputTensorNames.push_back("dummy_coord32"); 53 | params.inputTensorNames.push_back("dummy_coord16"); 54 | params.inputTensorNames.push_back("dummy_coord8"); 55 | params.outputTensorNames.push_back("feature_pred"); 56 | params.outputTensorNames.push_back("kernel_pred1"); 57 | params.outputTensorNames.push_back("kernel_pred2"); 58 | params.outputTensorNames.push_back("kernel_pred3"); 59 | params.outputTensorNames.push_back("kernel_pred4"); 60 | params.outputTensorNames.push_back("kernel_pred5"); 61 | params.outputTensorNames.push_back("cate_pred1"); 62 | params.outputTensorNames.push_back("cate_pred2"); 63 | params.outputTensorNames.push_back("cate_pred3"); 64 | params.outputTensorNames.push_back("cate_pred4"); 65 | params.outputTensorNames.push_back("cate_pred5"); 66 | params.dlaCore = -1; 67 | params.int8 = false; 68 | params.fp16 = false; 69 | params.batchSize = 1; 70 | params.seria = false; 71 | return params; 72 | } 73 | 74 | void segTRTSigma::get_class_names() 75 | { 76 | std::fstream fst; 77 | fst.open(names_path, std::ifstream::in); 78 | if (fst.is_open()) 79 | { 80 | std::string buff{""}; 81 | while (std::getline(fst, buff)) 82 | { 83 | class_names.push_back(buff); 84 | } 85 | for (auto vec: class_names ) 86 | { 87 | std::cout<(1.f / (1.f + exp(-x))); 105 | } 106 | 107 | static inline float intersection_area(const Object &a, const Object &b, int img_w, int img_h) { 108 | float area = 0.f; 109 | for (int y = 0; y < img_h; y = y + 4) { 110 | for (int x = 0; x < img_w; x = x + 4) { 111 | const uchar *mp1 = a.mask.ptr(y); 112 | const uchar *mp2 = b.mask.ptr(y); 113 | if (mp1[x] == 255 && mp2[x] == 255) area += 1.f; 114 | } 115 | } 116 | return area; 117 | } 118 | 119 | static inline float area(const Object &a, int img_w, int img_h) { 120 | float area = 0.f; 121 | for (int y = 0; y < img_h; y = y + 4) { 122 | for (int x = 0; x < img_w; x = x + 4) { 123 | const uchar *mp = a.mask.ptr(y); 124 | if (mp[x] == 255) area += 1.f; 125 | } 126 | } 127 | return area; 128 | } 129 | 130 | static void qsort_descent_inplace(std::vector& objects, int left, int right) 131 | { 132 | int i = left; 133 | int j = right; 134 | float p = objects[(left + right) / 2].prob; 135 | 136 | while (i <= j) 137 | { 138 | while (objects[i].prob > p) 139 | i++; 140 | 141 | while (objects[j].prob < p) 142 | j--; 143 | 144 | if (i <= j) 145 | { 146 | // swap 147 | std::swap(objects[i], objects[j]); 148 | 149 | i++; 150 | j--; 151 | } 152 | } 153 | 154 | #pragma omp parallel sections 155 | { 156 | #pragma omp section 157 | { 158 | if (left < j) qsort_descent_inplace(objects, left, j); 159 | } 160 | #pragma omp section 161 | { 162 | if (i < right) qsort_descent_inplace(objects, i, right); 163 | } 164 | } 165 | } 166 | 167 | static void qsort_descent_inplace(std::vector& objects) 168 | { 169 | if (objects.empty()) 170 | return; 171 | 172 | qsort_descent_inplace(objects, 0, objects.size() - 1); 173 | } 174 | 175 | 176 | std::vector segTRTSigma::nms_sorted_seg(const std::vector& objects, std::vector& nms_picked, std::string kernel , 177 | float sigma) 178 | { 179 | nms_picked.clear(); 180 | const int n = objects.size(); 181 | std::vector areas(n); 182 | 183 | for (int i = 0; i < n; i++) 184 | { 185 | areas[i] = area(objects[i], ins_w, ins_h); 186 | } 187 | for (int i = 0; i < n; i++) 188 | { 189 | const Object& a = objects[i]; 190 | int keep = 1; 191 | for (int j = 0; j < (int)nms_picked.size(); j++) 192 | { 193 | const Object& b = objects[nms_picked[j]]; 194 | // intersection over union 195 | float inter_area = intersection_area(a, b, ins_w, ins_h); 196 | float union_area = areas[i] + areas[nms_picked[j]] - inter_area; 197 | float cur_iou = inter_area / union_area; 198 | if (cur_iou > 0.5) 199 | { 200 | keep = 0; 201 | } 202 | } 203 | if (keep) 204 | { 205 | nms_picked.push_back(i); 206 | } 207 | } 208 | return objects; 209 | } 210 | 211 | void calloc_error() 212 | { 213 | fprintf(stderr, "Calloc error - possibly out of CPU RAM \n"); 214 | exit(EXIT_FAILURE); 215 | } 216 | 217 | void *xcalloc(size_t nmemb, size_t size) { 218 | void *ptr = calloc(nmemb, size); 219 | if (!ptr) { 220 | calloc_error(); 221 | } 222 | memset(ptr, 0, nmemb * size); 223 | return ptr; 224 | } 225 | 226 | void gemm_nt(int M, int N, int K, float ALPHA, 227 | float *A, int lda, 228 | float *B, int ldb, 229 | float *C, int ldc) 230 | { 231 | int i, j, k; 232 | for (i = 0; i < M; ++i) { 233 | for (j = 0; j < N; ++j) { 234 | PUT_IN_REGISTER float sum = 0; 235 | for (k = 0; k < K; ++k) { 236 | sum += ALPHA*A[i*lda + k] * B[j*ldb + k]; 237 | } 238 | C[i*ldc + j] += sum; 239 | } 240 | } 241 | } 242 | 243 | void gemm_tn(int M, int N, int K, float ALPHA, 244 | float *A, int lda, 245 | float *B, int ldb, 246 | float *C, int ldc) 247 | { 248 | int i, j, k; 249 | for (i = 0; i < M; ++i) { 250 | for (k = 0; k < K; ++k) { 251 | PUT_IN_REGISTER float A_PART = ALPHA * A[k * lda + i]; 252 | for (j = 0; j < N; ++j) { 253 | C[i*ldc + j] += A_PART*B[k*ldb + j]; 254 | } 255 | } 256 | } 257 | } 258 | 259 | void gemm_tt(int M, int N, int K, float ALPHA, 260 | float *A, int lda, 261 | float *B, int ldb, 262 | float *C, int ldc) 263 | { 264 | int i, j, k; 265 | for (i = 0; i < M; ++i) { 266 | for (j = 0; j < N; ++j) { 267 | PUT_IN_REGISTER float sum = 0; 268 | for (k = 0; k < K; ++k) { 269 | sum += ALPHA*A[i + k*lda] * B[k + j*ldb]; 270 | } 271 | C[i*ldc + j] += sum; 272 | } 273 | } 274 | } 275 | 276 | void gemm_nn(int M, int N, int K, float ALPHA, 277 | float *A, int lda, 278 | float *B, int ldb, 279 | float *C, int ldc) 280 | { 281 | int i, j, k; 282 | #pragma omp parallel for 283 | for (i = 0; i < M; ++i) { 284 | for (k = 0; k < K; ++k) { 285 | register float A_PART = ALPHA*A[i*lda + k]; 286 | for (j = 0; j < N; ++j) { 287 | C[i*ldc + j] += A_PART*B[k*ldb + j]; 288 | } 289 | } 290 | } 291 | } 292 | 293 | void gemm_cpu(int TA, int TB, int M, int N, int K, float ALPHA, 294 | float *A, int lda, 295 | float *B, int ldb, 296 | float BETA, 297 | float *C, int ldc) 298 | { 299 | if (BETA != 1) { 300 | int i, j; 301 | for (i = 0; i < M; ++i) { 302 | for (j = 0; j < N; ++j) { 303 | C[i*ldc + j] *= BETA; 304 | } 305 | } 306 | } 307 | int t; 308 | #pragma omp parallel for 309 | for (t = 0; t < M; ++t) { 310 | if (!TA && !TB) 311 | gemm_nn(1, N, K, ALPHA, A + t*lda, lda, B, ldb, C + t*ldc, ldc); 312 | else if (TA && !TB) 313 | gemm_tn(1, N, K, ALPHA, A + t, lda, B, ldb, C + t*ldc, ldc); 314 | else if (!TA && TB) 315 | gemm_nt(1, N, K, ALPHA, A + t*lda, lda, B, ldb, C + t*ldc, ldc); 316 | else 317 | gemm_tt(1, N, K, ALPHA, A + t, lda, B, ldb, C + t*ldc, ldc); 318 | } 319 | } 320 | 321 | void gemm(int TA, int TB, int M, int N, int K, float ALPHA, 322 | float *A, int lda, 323 | float *B, int ldb, 324 | float BETA, 325 | float *C, int ldc) 326 | { 327 | gemm_cpu(TA, TB, M, N, K, ALPHA, A, lda, B, ldb, BETA, C, ldc); 328 | } 329 | 330 | void fill_cpu(int N, float ALPHA, float *X, int INCX) 331 | { 332 | int i; 333 | if (INCX == 1 && ALPHA == 0) { 334 | memset(X, 0, N * sizeof(float)); 335 | } 336 | else { 337 | for (i = 0; i < N; ++i) X[i*INCX] = ALPHA; 338 | } 339 | } 340 | 341 | 342 | static void filter_ins(const float *cate_pred, std::vector &picked, std::vector &all_picked, 343 | int num_class, float cate_thresh, int fea_w, int fea_h) 344 | { 345 | int count_ins = 0; 346 | int num_anchors = fea_w*fea_h; 347 | 348 | for (int i = 0; i < fea_h; i++) 349 | { 350 | for (int j = 0; j < fea_w; j++) 351 | { 352 | float global_prob = 0; 353 | int global_cls_idx = 1; 354 | for (int cls_idx = 0; cls_idx < num_class; cls_idx++) 355 | { 356 | if (cate_pred == nullptr) 357 | { 358 | std::cout << "cate_pred is null" << std::endl; 359 | } 360 | float prob_x = cate_pred[i*fea_w*num_class + num_class * j + cls_idx]; 361 | if (prob_x > global_prob) 362 | { 363 | global_prob = prob_x; 364 | global_cls_idx = cls_idx; 365 | } 366 | } 367 | if (global_prob < cate_thresh) 368 | { 369 | continue; 370 | } 371 | else 372 | { 373 | Object obj; 374 | obj.fea_w = fea_w; 375 | obj.fea_h = fea_h; 376 | obj.rect.x = (int)j; 377 | obj.rect.y = (int)i; 378 | obj.prob = global_prob; 379 | obj.label = global_cls_idx; 380 | picked.push_back(obj); 381 | all_picked.push_back(obj); 382 | } 383 | } 384 | } 385 | } 386 | 387 | void segTRTSigma::dynamic_conv(float * kernel_pred, float * feature_pred, std::vector> &objects 388 | , std::vector &all_picked_kernel) 389 | { 390 | int c_out = all_picked_kernel.size(); 391 | int output_size{ 0 }; 392 | int stride = 0; 393 | if (c_out > 0) { 394 | float * a = kernel_pred; 395 | float * b = feature_pred; 396 | int n = ins_h * ins_w; 397 | output_size = c_out * n; 398 | float * output = (float*)xcalloc(output_size, sizeof(float)); 399 | fill_cpu(output_size, 0, output, 1); 400 | int m = c_out; 401 | int k = 256; 402 | float * c = output; 403 | 404 | // #ifdef GPU 405 | cublasStatus_t status; 406 | 407 | float *d_w, *d_f, *d_o; 408 | cudaMalloc((void**)&d_w, sizeof(float)*m*k); 409 | cudaMalloc((void**)&d_f, sizeof(float)*k*n); 410 | cudaMalloc((void**)&d_o, sizeof(float)*m*n); 411 | 412 | cublasHandle_t handle; 413 | cublasCreate(&handle); 414 | cudaMemcpy(d_w, a, sizeof(float)*m*k, cudaMemcpyHostToDevice); 415 | cudaMemcpy(d_f, b, sizeof(float)*k*n, cudaMemcpyHostToDevice); 416 | 417 | float alpha = 1, beta = 0; 418 | cublasSgemm( 419 | handle, 420 | CUBLAS_OP_N, 421 | CUBLAS_OP_N, 422 | n, 423 | m, 424 | k, 425 | &alpha, 426 | d_f, 427 | n, 428 | d_w, 429 | k, 430 | &beta, 431 | d_o, 432 | n 433 | ); 434 | 435 | cudaMemcpy(c, d_o, sizeof(float)*m*n, cudaMemcpyDeviceToHost); 436 | // #elif 437 | // gemm(0, 0, m, n, k, 1, a, k, b, n, 0, c, n); // [9, 256] x [256, 200*200] => [9, 200, 200], 即 mk x kn => mn 438 | // #endif //GPU 439 | 440 | int count_out_channel1 = 0; 441 | //40, 36, 24, 16, 12 442 | for (auto iter = all_picked_kernel.cbegin(); iter != all_picked_kernel.cend(); iter++) 443 | { 444 | if ((*iter).fea_w==40 || (*iter).fea_w == 36) 445 | { 446 | stride = 8; 447 | } 448 | if ((*iter).fea_w == 24) 449 | { 450 | stride = 16; 451 | } 452 | if ((*iter).fea_w == 16 || (*iter).fea_w == 12) 453 | { 454 | stride = 32; 455 | } 456 | 457 | Object obj; 458 | // mask float uchar 459 | obj.seg_preds = cv::Mat(ins_h, ins_w, CV_32FC1); 460 | obj.mask = cv::Mat(ins_h, ins_w, CV_8UC1); 461 | float seg_scores = 0.f; 462 | int sum_masks = 0; 463 | 464 | obj.seg_preds = cv::Scalar(0.f); 465 | obj.mask = cv::Scalar(0); 466 | for (int ih = 0; ih < ins_h; ih++) 467 | { 468 | float *temp_seg_preds = obj.seg_preds.ptr(ih); 469 | uchar *temp_seg_masks = obj.mask.ptr(ih); 470 | for (int iw = 0; iw < ins_w; iw++) 471 | { 472 | float seg_pred = sigmoid((output + count_out_channel1*ins_w*ins_h)[ih*ins_w + iw]); 473 | if (seg_pred > mask_thr) 474 | { 475 | temp_seg_preds[iw] = seg_pred; 476 | temp_seg_masks[iw] = 255; 477 | seg_scores += seg_pred; 478 | sum_masks++; 479 | } 480 | } 481 | } 482 | count_out_channel1++; 483 | if (sum_masks < stride) { 484 | std::cout << "now count_mask().swap(all_picked_kernel); 500 | } 501 | 502 | void segTRTSigma::show_result_seg(const cv::Mat &bgr, const std::vector &objects, std::vector &segobj) { 503 | unsigned char colors[80][3] = { 504 | { 255, 0, 255 }, 505 | { 153, 122, 23 }, 506 | { 85, 162, 199 }, 507 | { 1, 114, 210 }, 508 | { 255, 226, 0 }, 509 | { 0, 18, 255 }, 510 | { 255, 151, 0 }, 511 | { 170, 0, 255 }, 512 | { 0, 255, 56 }, 513 | { 255, 0, 75 }, 514 | { 0, 75, 255 }, 515 | { 0, 255, 169 }, 516 | { 255, 0, 207 }, 517 | { 75, 255, 0 }, 518 | { 207, 0, 255 }, 519 | { 37, 0, 255 }, 520 | { 0, 207, 255 }, 521 | { 94, 0, 255 }, 522 | { 0, 255, 113 }, 523 | { 255, 18, 0 }, 524 | { 255, 0, 56 }, 525 | { 18, 0, 255 }, 526 | { 0, 255, 226 }, 527 | { 170, 255, 0 }, 528 | { 255, 0, 245 }, 529 | { 151, 255, 0 }, 530 | { 132, 255, 0 }, 531 | { 75, 0, 255 }, 532 | { 151, 0, 255 }, 533 | { 0, 151, 255 }, 534 | { 132, 0, 255 }, 535 | { 0, 255, 245 }, 536 | { 255, 132, 0 }, 537 | { 226, 0, 255 }, 538 | { 255, 37, 0 }, 539 | { 207, 255, 0 }, 540 | { 0, 255, 207 }, 541 | { 94, 255, 0 }, 542 | { 0, 226, 255 }, 543 | { 56, 255, 0 }, 544 | { 255, 94, 0 }, 545 | { 255, 113, 0 }, 546 | { 0, 132, 255 }, 547 | { 255, 0, 132 }, 548 | { 255, 170, 0 }, 549 | { 255, 0, 188 }, 550 | { 113, 255, 0 }, 551 | { 245, 0, 255 }, 552 | { 113, 0, 255 }, 553 | { 255, 188, 0 }, 554 | { 0, 113, 255 }, 555 | { 255, 0, 0 }, 556 | { 0, 56, 255 }, 557 | { 255, 0, 113 }, 558 | { 0, 255, 188 }, 559 | { 255, 0, 94 }, 560 | { 255, 0, 18 }, 561 | { 18, 255, 0 }, 562 | { 0, 255, 132 }, 563 | { 0, 188, 255 }, 564 | { 0, 245, 255 }, 565 | { 0, 169, 255 }, 566 | { 37, 255, 0 }, 567 | { 255, 0, 151 }, 568 | { 188, 0, 255 }, 569 | { 0, 255, 37 }, 570 | { 0, 255, 0 }, 571 | { 255, 0, 170 }, 572 | { 255, 0, 37 }, 573 | { 255, 75, 0 }, 574 | { 0, 0, 255 }, 575 | { 255, 207, 0 }, 576 | { 255, 0, 226 }, 577 | { 255, 245, 0 }, 578 | { 188, 255, 0 }, 579 | { 0, 255, 18 }, 580 | { 0, 255, 75 }, 581 | { 0, 255, 151 }, 582 | { 255, 56, 0 }, 583 | { 245, 255, 0 } 584 | }; 585 | cv::Mat image = bgr.clone(); 586 | cv::Mat bgr1 = bgr.clone(); 587 | std::string message(""); 588 | for (size_t i = 0; i < objects.size(); i++) { 589 | const Object &obj = objects[i]; 590 | if (obj.prob < 0.3) 591 | continue; 592 | int img_h = bgr.rows; 593 | int img_w = bgr.cols; 594 | cv::Mat seg_preds2; // rescale 595 | cv::resize(obj.seg_preds, seg_preds2, cv::Size(img_w, img_h), cv::INTER_LINEAR); 596 | 597 | cv::Mat final_mask = cv::Mat(img_h, img_w, CV_8UC1); 598 | float sum_mask_y = 0.0f; 599 | float sum_mask_x = 0.0f; 600 | int area = 0; 601 | { 602 | final_mask = cv::Scalar(0); 603 | for (int y = 0; y < img_h; y++) 604 | { 605 | float *seg_preds_p = seg_preds2.ptr(y); 606 | uchar *mask_p = final_mask.ptr(y); 607 | for (int x = 0; x < img_w; x++) 608 | { 609 | if (seg_preds_p[x]>0.5){ 610 | mask_p[x] = 255; 611 | sum_mask_y += float(y); 612 | sum_mask_x += float(x); 613 | area++; 614 | } 615 | else{ 616 | mask_p[x] = 0; 617 | } 618 | } 619 | } 620 | } 621 | if (area < 81) continue; 622 | int cx = int(sum_mask_x / area); 623 | int cy = int(sum_mask_y / area); 624 | const unsigned char *color = colors[i%80]; 625 | std::string text = class_names[obj.label]; 626 | std::string text_sub =text.substr(0, text.size()-1); 627 | std::cout<(bgr1_p[0] * 0.5 + color[0] * 0.5); 637 | image_p[1] = cv::saturate_cast(bgr1_p[1] * 0.5 + color[1] * 0.5); 638 | image_p[2] = cv::saturate_cast(bgr1_p[2] * 0.5 + color[2] * 0.5); 639 | } 640 | image_p += 3; 641 | bgr1_p += 3; 642 | } 643 | } 644 | cv::putText(image, text_sub, cv::Point(x, y), 645 | cv::FONT_HERSHEY_COMPLEX, 0.3, cv::Scalar(0, 0, 0), 1); 646 | std::string temp_name = text_sub; 647 | message = message + temp_name + std::to_string(obj.prob); 648 | } 649 | segobj[0].message = message; 650 | segobj[0].segMat = image; 651 | segobj[0].target_num = int(objects.size()); 652 | } 653 | 654 | static float * create_picked_kernel(const float * kernel_pred, std::vector &kernel_picked) { 655 | int c_out = kernel_picked.size(); 656 | int count_out_channel = 0; 657 | int kernel_size = c_out * 256; 658 | float * weights = new float[kernel_size]; 659 | for (auto iter = kernel_picked.cbegin(); iter != kernel_picked.cend(); iter++) 660 | { 661 | for (int i = 0; i < (*iter).fea_h; i++) 662 | { 663 | for (int j = 0; j < (*iter).fea_w; j++) 664 | { 665 | if ((*iter).rect.x == j && (*iter).rect.y == i) 666 | { 667 | memcpy(weights + count_out_channel * 256, kernel_pred + (i*(*iter).fea_w + j) * 256, 256 * sizeof(float)); 668 | } 669 | } 670 | } 671 | count_out_channel++; 672 | } 673 | return weights; 674 | } 675 | 676 | 677 | static void combine_array(float src[], float src1[], float src2[], float src3[], float src4[], float des[], 678 | int src_num, int src_num1, int src_num2, int src_num3, int src_num4, int des_num){ 679 | 680 | int s1 = src_num + src_num1; 681 | int s2 = src_num + src_num1 + src_num2; 682 | int s3 = src_num + src_num1 + src_num2 + src_num3; 683 | int s4 = src_num + src_num1 + src_num2 + src_num3 + src_num4; 684 | 685 | for (int i = 0; i < des_num; i++) 686 | { 687 | if (i &segobj) 719 | { 720 | int fea_sacles[5] = { 40,36,24,16,12 }; 721 | std::vector filtered_inses, filtered_ins1, filtered_ins2, filtered_ins3, filtered_ins4, filtered_ins5; 722 | filter_ins(cate_pred1_output, filtered_ins1, filtered_inses, num_class, score_thr, fea_sacles[0], fea_sacles[0]); 723 | filter_ins(cate_pred2_output, filtered_ins2, filtered_inses, num_class, score_thr, fea_sacles[1], fea_sacles[1]); 724 | filter_ins(cate_pred3_output, filtered_ins3, filtered_inses, num_class, score_thr, fea_sacles[2], fea_sacles[2]); 725 | filter_ins(cate_pred4_output, filtered_ins4, filtered_inses, num_class, score_thr, fea_sacles[3], fea_sacles[3]); 726 | filter_ins(cate_pred5_output, filtered_ins5, filtered_inses, num_class, score_thr, fea_sacles[4], fea_sacles[4]); 727 | std::vector> temp_objects; 728 | temp_objects.resize(num_class); 729 | 730 | float *picked_kernel_pred1 = nullptr; 731 | float *picked_kernel_pred2 = nullptr; 732 | float *picked_kernel_pred3 = nullptr; 733 | float *picked_kernel_pred4 = nullptr; 734 | float *picked_kernel_pred5 = nullptr; 735 | 736 | picked_kernel_pred1 = create_picked_kernel(kernel_pred1_output, filtered_ins1); 737 | picked_kernel_pred2 = create_picked_kernel(kernel_pred2_output, filtered_ins2); 738 | picked_kernel_pred3 = create_picked_kernel(kernel_pred3_output, filtered_ins3); 739 | picked_kernel_pred4 = create_picked_kernel(kernel_pred4_output, filtered_ins4); 740 | picked_kernel_pred5 = create_picked_kernel(kernel_pred5_output, filtered_ins5); 741 | 742 | int dynamic_out_channels = (int)filtered_inses.size(); 743 | int total_num_weights = dynamic_out_channels * 256; 744 | float * kernel_preds = new float[total_num_weights]; 745 | int filtered_ins1_size = filtered_ins1.size(); 746 | int filtered_ins2_size = filtered_ins2.size(); 747 | int filtered_ins3_size = filtered_ins3.size(); 748 | int filtered_ins4_size = filtered_ins4.size(); 749 | int filtered_ins5_size = filtered_ins5.size(); 750 | 751 | combine_array(picked_kernel_pred1, picked_kernel_pred2, picked_kernel_pred3, picked_kernel_pred4, picked_kernel_pred5, kernel_preds, 752 | filtered_ins1_size * 256, filtered_ins2_size * 256, filtered_ins3_size * 256, filtered_ins4_size * 256, filtered_ins5_size * 256, 753 | total_num_weights); 754 | dynamic_conv(kernel_preds, feature_pred_output, temp_objects, filtered_inses); 755 | 756 | delete []picked_kernel_pred1; 757 | delete []picked_kernel_pred2; 758 | delete []picked_kernel_pred3; 759 | delete []picked_kernel_pred4; 760 | delete []picked_kernel_pred5; 761 | delete[]kernel_preds; 762 | picked_kernel_pred1 = nullptr; 763 | picked_kernel_pred2 = nullptr; 764 | picked_kernel_pred3 = nullptr; 765 | picked_kernel_pred4 = nullptr; 766 | picked_kernel_pred5 = nullptr; 767 | kernel_preds = nullptr; 768 | 769 | std::vector final_objects; 770 | for (int i = 0; i < (int)temp_objects.size(); i++) 771 | { 772 | std::vector one_object = temp_objects[i]; 773 | qsort_descent_inplace(one_object); 774 | std::vector nms_picked; 775 | std::vector nms_proposals; 776 | if (one_object.size()>0) 777 | { 778 | nms_sorted_seg(one_object, nms_picked,"gaussian", 2.0); 779 | for (int j = 0; j < (int)nms_picked.size(); j++) 780 | { 781 | int z = nms_picked[j]; 782 | final_objects.push_back(one_object[z]); 783 | } 784 | } 785 | } 786 | show_result_seg(srcimg, final_objects, segobj); 787 | return 0; 788 | } 789 | 790 | float * segTRTSigma::imnormalize(cv::Mat& img) 791 | { 792 | const float means[3] = { 123.675f, 116.28f, 103.53f }; 793 | const float stds[3] = { 58.395f, 57.12f, 57.375f }; 794 | float * blob = new float[img.total() * 3]; 795 | int channels = 3; 796 | int img_h = target_size_h; 797 | int img_w = target_size_w; 798 | for (size_t c = 0; c < channels; c++) 799 | { 800 | for (size_t h = 0; h < img_h; h++) 801 | { 802 | for (size_t w = 0; w < img_w; w++) 803 | { 804 | blob[c * img_w * img_h + h * img_w + w] = 805 | (((float)img.at(h, w)[c]) - means[c]) / stds[c]; 806 | } 807 | } 808 | } 809 | return blob; 810 | } 811 | 812 | 813 | bool segTRTSigma::build_model() 814 | { 815 | initLibNvInferPlugins(&gLogger.getTRTLogger(), ""); 816 | nvinfer1::IRuntime* runtime = nvinfer1::createInferRuntime(gLogger); 817 | if (!runtime) 818 | { 819 | return false; 820 | } 821 | char* trtDeSerBuffer{nullptr}; 822 | const std::string engine_file_path(model_path); 823 | std::ifstream ifs; 824 | int serLength; 825 | ifs.open(engine_file_path.c_str(), std::ios::in | std::ios::binary); // open input file 826 | if (ifs.is_open()) 827 | { 828 | ifs.seekg(0, std::ios::end); // go to the end 829 | serLength = ifs.tellg(); // report location (this is the length) 830 | ifs.seekg(0, std::ios::beg); // go back to the beginning 831 | trtDeSerBuffer = new char[serLength]; // allocate memory for a buffer of appropriate dimension 832 | ifs.read(trtDeSerBuffer, serLength); // read the whole file into the buffer 833 | ifs.close(); 834 | } 835 | else 836 | { 837 | return false; 838 | } 839 | sEngine = std::shared_ptr(runtime->deserializeCudaEngine(trtDeSerBuffer, serLength, nullptr), InferDeleter()); 840 | if (!sEngine) 841 | { 842 | std::cout << "load engine failed!" << std::endl; 843 | return false; 844 | } 845 | delete[] trtDeSerBuffer; 846 | 847 | context = std::shared_ptr(sEngine->createExecutionContext(),InferDeleter()); 848 | if (!context) 849 | { 850 | return false; 851 | } 852 | return true; 853 | } 854 | 855 | 856 | float * prepare_coord_input(int target_size_w,int target_size_h, int stride) 857 | { 858 | int pw = int(target_size_w / stride); 859 | int ph = int(target_size_h / stride); 860 | float step_w = 2.f / (pw - 1); 861 | float step_h = 2.f / (ph - 1); 862 | float * channel_coord3 = new float[pw*ph * 2]; 863 | for (int h = 0; h < ph; h++) 864 | { 865 | for (int w = 0; w < pw; w++) 866 | { 867 | channel_coord3[h*pw + w] = -1.f + step_w*(float)w; 868 | channel_coord3[pw*ph + h*pw + w] = -1.f + step_h*(float)h; 869 | } 870 | } 871 | return channel_coord3; 872 | } 873 | 874 | 875 | size_t segTRTSigma::do_inference(cv::Mat& srcimg, std::vector & segobj, bool is_gamma) 876 | { 877 | if (srcimg.empty()) 878 | { 879 | return 102; 880 | } 881 | cv::Mat img_resize, src_img; 882 | cv::resize(srcimg, img_resize, cv::Size(target_size_w, target_size_h)); 883 | cv::cvtColor(img_resize, src_img, cv::COLOR_BGR2RGB); 884 | segObject segt; 885 | segt.gammMat = srcimg; 886 | segobj.push_back(segt); 887 | float* blob{ nullptr }; 888 | blob = imnormalize(src_img); 889 | if (blob == nullptr) 890 | { 891 | return 102; 892 | } 893 | float * channel_coord323 = nullptr; 894 | channel_coord323 = prepare_coord_input(target_size_w, target_size_h, 32); 895 | float * channel_coord163 = nullptr; 896 | channel_coord163 = prepare_coord_input(target_size_w, target_size_h, 16); 897 | float * channel_coord83 = nullptr; 898 | channel_coord83 = prepare_coord_input(target_size_w, target_size_h, 8); 899 | assert(sParams.inputTensorNames.size() == 2); 900 | int mBatchSize = sEngine->getMaxBatchSize(); 901 | int dummy_inputIndex = sEngine->getBindingIndex(sParams.inputTensorNames[0].c_str()); 902 | assert(sEngine->getBindingDataType(dummy_inputIndex) == nvinfer1::DataType::kFLOAT); 903 | int dummy_coord32_Index = sEngine->getBindingIndex(sParams.inputTensorNames[1].c_str()); 904 | assert(sEngine->getBindingDataType(dummy_coord32_Index) == nvinfer1::DataType::kFLOAT); 905 | int dummy_coord16_Index = sEngine->getBindingIndex(sParams.inputTensorNames[2].c_str()); 906 | assert(sEngine->getBindingDataType(dummy_coord16_Index) == nvinfer1::DataType::kFLOAT); 907 | int dummy_coord8_Index = sEngine->getBindingIndex(sParams.inputTensorNames[3].c_str()); 908 | assert(sEngine->getBindingDataType(dummy_coord8_Index) == nvinfer1::DataType::kFLOAT); 909 | const int feature_pred_outputIndex = sEngine->getBindingIndex(sParams.outputTensorNames[0].c_str()); 910 | assert(engine.getBindingDataType(feature_pred_outputIndex) == nvinfer1::DataType::kFLOAT); 911 | const int kernel_pred1_outputIndex = sEngine->getBindingIndex(sParams.outputTensorNames[1].c_str()); 912 | assert(engine.getBindingDataType(kernel_pred1_outputIndex) == nvinfer1::DataType::kFLOAT); 913 | const int kernel_pred2_outputIndex = sEngine->getBindingIndex(sParams.outputTensorNames[2].c_str()); 914 | assert(engine.getBindingDataType(kernel_pred2_outputIndex) == nvinfer1::DataType::kFLOAT); 915 | const int kernel_pred3_outputIndex = sEngine->getBindingIndex(sParams.outputTensorNames[3].c_str()); 916 | assert(engine.getBindingDataType(kernel_pred3_outputIndex) == nvinfer1::DataType::kFLOAT); 917 | const int kernel_pred4_outputIndex = sEngine->getBindingIndex(sParams.outputTensorNames[4].c_str()); 918 | assert(engine.getBindingDataType(kernel_pred3_outputIndex) == nvinfer1::DataType::kFLOAT); 919 | const int kernel_pred5_outputIndex = sEngine->getBindingIndex(sParams.outputTensorNames[5].c_str()); 920 | assert(engine.getBindingDataType(kernel_pred5_outputIndex) == nvinfer1::DataType::kFLOAT); 921 | const int cate_pred1_outputIndex = sEngine->getBindingIndex(sParams.outputTensorNames[6].c_str()); 922 | assert(engine.getBindingDataType(cate_pred1_outputIndex) == nvinfer1::DataType::kFLOAT); 923 | const int cate_pred2_outputIndex = sEngine->getBindingIndex(sParams.outputTensorNames[7].c_str()); 924 | assert(engine.getBindingDataType(cate_pred2_outputIndex) == nvinfer1::DataType::kFLOAT); 925 | const int cate_pred3_outputIndex = sEngine->getBindingIndex(sParams.outputTensorNames[8].c_str()); 926 | assert(engine.getBindingDataType(cate_pred3_outputIndex) == nvinfer1::DataType::kFLOAT); 927 | const int cate_pred4_outputIndex = sEngine->getBindingIndex(sParams.outputTensorNames[9].c_str()); 928 | assert(engine.getBindingDataType(cate_pred4_outputIndex) == nvinfer1::DataType::kFLOAT); 929 | const int cate_pred5_outputIndex = sEngine->getBindingIndex(sParams.outputTensorNames[10].c_str()); 930 | assert(engine.getBindingDataType(cate_pred5_outputIndex) == nvinfer1::DataType::kFLOAT); 931 | int dummy_input_size = 1; 932 | auto dummy_input_dims = sEngine->getBindingDimensions(dummy_inputIndex); 933 | for (int i = 0; i < dummy_input_dims.nbDims; i++) 934 | { 935 | dummy_input_size *= dummy_input_dims.d[i]; 936 | } 937 | int dummy_coord32_input_size = 1; 938 | auto dummy_coord32_input_dims = sEngine->getBindingDimensions(dummy_coord32_Index); 939 | for (int i = 0; i < dummy_coord32_input_dims.nbDims; i++) 940 | { 941 | dummy_coord32_input_size *= dummy_coord32_input_dims.d[i]; 942 | } 943 | int dummy_coord16_input_size = 1; 944 | auto dummy_coord16_input_dims = sEngine->getBindingDimensions(dummy_coord16_Index); 945 | for (int i = 0; i < dummy_coord16_input_dims.nbDims; i++) 946 | { 947 | dummy_coord16_input_size *= dummy_coord16_input_dims.d[i]; 948 | } 949 | int dummy_coord8_input_size = 1; 950 | auto dummy_coord8_input_dims = sEngine->getBindingDimensions(dummy_coord8_Index); 951 | for (int i = 0; i < dummy_coord8_input_dims.nbDims; i++) 952 | { 953 | dummy_coord8_input_size *= dummy_coord8_input_dims.d[i]; 954 | } 955 | int feature_pred_size = 1; 956 | auto feature_pred_dims = sEngine->getBindingDimensions(feature_pred_outputIndex); 957 | for (int i = 0; i < feature_pred_dims.nbDims; i++) 958 | { 959 | feature_pred_size *= feature_pred_dims.d[i]; 960 | } 961 | int kernel_pred1_size = 1; 962 | auto kernel_pred1_dims = sEngine->getBindingDimensions(kernel_pred1_outputIndex); 963 | for (int i = 0; i < kernel_pred1_dims.nbDims; i++) 964 | { 965 | kernel_pred1_size *= kernel_pred1_dims.d[i]; 966 | } 967 | int kernel_pred2_size = 1; 968 | auto kernel_pred2_dims = sEngine->getBindingDimensions(kernel_pred2_outputIndex); 969 | for (int i = 0; i < kernel_pred2_dims.nbDims; i++) 970 | { 971 | kernel_pred2_size *= kernel_pred2_dims.d[i]; 972 | } 973 | int kernel_pred3_size = 1; 974 | auto kernel_pred3_dims = sEngine->getBindingDimensions(kernel_pred3_outputIndex); 975 | for (int i = 0; i < kernel_pred3_dims.nbDims; i++) 976 | { 977 | kernel_pred3_size *= kernel_pred3_dims.d[i]; 978 | } 979 | int kernel_pred4_size = 1; 980 | auto kernel_pred4_dims = sEngine->getBindingDimensions(kernel_pred4_outputIndex); 981 | for (int i = 0; i < kernel_pred4_dims.nbDims; i++) 982 | { 983 | kernel_pred4_size *= kernel_pred4_dims.d[i]; 984 | } 985 | int kernel_pred5_size = 1; 986 | auto kernel_pred5_dims = sEngine->getBindingDimensions(kernel_pred5_outputIndex); 987 | for (int i = 0; i < kernel_pred5_dims.nbDims; i++) 988 | { 989 | kernel_pred5_size *= kernel_pred5_dims.d[i]; 990 | } 991 | int cate_pred1_size = 1; 992 | auto cate_pred1_dims = sEngine->getBindingDimensions(cate_pred1_outputIndex); 993 | for (int i = 0; i < cate_pred1_dims.nbDims; i++) 994 | { 995 | cate_pred1_size *= cate_pred1_dims.d[i]; 996 | } 997 | int cate_pred2_size = 1; 998 | auto cate_pred2_dims = sEngine->getBindingDimensions(cate_pred2_outputIndex); 999 | for (int i = 0; i < cate_pred2_dims.nbDims; i++) 1000 | { 1001 | cate_pred2_size *= cate_pred2_dims.d[i]; 1002 | } 1003 | int cate_pred3_size = 1; 1004 | auto cate_pred3_dims = sEngine->getBindingDimensions(cate_pred3_outputIndex); 1005 | for (int i = 0; i < cate_pred3_dims.nbDims; i++) 1006 | { 1007 | cate_pred3_size *= cate_pred3_dims.d[i]; 1008 | } 1009 | int cate_pred4_size = 1; 1010 | auto cate_pred4_dims = sEngine->getBindingDimensions(cate_pred4_outputIndex); 1011 | for (int i = 0; i < cate_pred4_dims.nbDims; i++) 1012 | { 1013 | cate_pred4_size *= cate_pred4_dims.d[i]; 1014 | } 1015 | int cate_pred5_size = 1; 1016 | auto cate_pred5_dims = sEngine->getBindingDimensions(cate_pred5_outputIndex); 1017 | for (int i = 0; i < cate_pred5_dims.nbDims; i++) 1018 | { 1019 | cate_pred5_size *= cate_pred5_dims.d[i]; 1020 | } 1021 | float * feature_pred_output = new float[feature_pred_size]; 1022 | float * kernel_pred1_output = new float[kernel_pred1_size]; 1023 | float * kernel_pred2_output = new float[kernel_pred2_size]; 1024 | float * kernel_pred3_output = new float[kernel_pred3_size]; 1025 | float * kernel_pred4_output = new float[kernel_pred4_size]; 1026 | float * kernel_pred5_output = new float[kernel_pred5_size]; 1027 | float * cate_pred1_output = new float[cate_pred1_size]; 1028 | float * cate_pred2_output = new float[cate_pred2_size]; 1029 | float * cate_pred3_output = new float[cate_pred3_size]; 1030 | float * cate_pred4_output = new float[cate_pred4_size]; 1031 | float * cate_pred5_output = new float[cate_pred5_size]; 1032 | 1033 | void* buffers[15]; 1034 | assert(3 * target_size_w * target_size_h == dummy_input_size); 1035 | CHECK(cudaMalloc(&buffers[dummy_inputIndex], dummy_input_size * sizeof(float))); 1036 | CHECK(cudaMalloc(&buffers[dummy_coord32_Index], dummy_coord32_input_size * sizeof(float))); 1037 | CHECK(cudaMalloc(&buffers[dummy_coord16_Index], dummy_coord16_input_size * sizeof(float))); 1038 | CHECK(cudaMalloc(&buffers[dummy_coord8_Index], dummy_coord8_input_size * sizeof(float))); 1039 | 1040 | CHECK(cudaMalloc(&buffers[feature_pred_outputIndex], feature_pred_size * sizeof(float))); 1041 | 1042 | CHECK(cudaMalloc(&buffers[kernel_pred1_outputIndex], kernel_pred1_size * sizeof(float))); 1043 | CHECK(cudaMalloc(&buffers[kernel_pred2_outputIndex], kernel_pred2_size * sizeof(float))); 1044 | CHECK(cudaMalloc(&buffers[kernel_pred3_outputIndex], kernel_pred3_size * sizeof(float))); 1045 | CHECK(cudaMalloc(&buffers[kernel_pred4_outputIndex], kernel_pred4_size * sizeof(float))); 1046 | CHECK(cudaMalloc(&buffers[kernel_pred5_outputIndex], kernel_pred5_size * sizeof(float))); 1047 | 1048 | CHECK(cudaMalloc(&buffers[cate_pred1_outputIndex], cate_pred1_size * sizeof(float))); 1049 | CHECK(cudaMalloc(&buffers[cate_pred2_outputIndex], cate_pred2_size * sizeof(float))); 1050 | CHECK(cudaMalloc(&buffers[cate_pred3_outputIndex], cate_pred3_size * sizeof(float))); 1051 | CHECK(cudaMalloc(&buffers[cate_pred4_outputIndex], cate_pred4_size * sizeof(float))); 1052 | CHECK(cudaMalloc(&buffers[cate_pred5_outputIndex], cate_pred5_size * sizeof(float))); 1053 | 1054 | cudaStream_t stream; 1055 | CHECK(cudaStreamCreate(&stream)); 1056 | CHECK(cudaMemcpyAsync(buffers[dummy_inputIndex], blob, dummy_input_size * sizeof(float), cudaMemcpyHostToDevice, stream)); 1057 | CHECK(cudaMemcpyAsync(buffers[dummy_coord32_Index], channel_coord323, dummy_coord32_input_size * sizeof(float), cudaMemcpyHostToDevice, stream)); 1058 | CHECK(cudaMemcpyAsync(buffers[dummy_coord16_Index], channel_coord163, dummy_coord16_input_size * sizeof(float), cudaMemcpyHostToDevice, stream)); 1059 | CHECK(cudaMemcpyAsync(buffers[dummy_coord8_Index], channel_coord83, dummy_coord8_input_size * sizeof(float), cudaMemcpyHostToDevice, stream)); 1060 | 1061 | bool status = context->enqueue(mBatchSize,buffers, stream,nullptr); 1062 | if (!status) 1063 | { 1064 | std::cout << "execute ifer error! " << std::endl; 1065 | return 101; 1066 | } 1067 | CHECK(cudaMemcpyAsync(feature_pred_output, buffers[feature_pred_outputIndex], feature_pred_size * sizeof(float), cudaMemcpyDeviceToHost, stream)); 1068 | CHECK(cudaMemcpyAsync(kernel_pred1_output, buffers[kernel_pred1_outputIndex], kernel_pred1_size * sizeof(float), cudaMemcpyDeviceToHost, stream)); 1069 | CHECK(cudaMemcpyAsync(kernel_pred2_output, buffers[kernel_pred2_outputIndex], kernel_pred2_size * sizeof(float), cudaMemcpyDeviceToHost, stream)); 1070 | CHECK(cudaMemcpyAsync(kernel_pred3_output, buffers[kernel_pred3_outputIndex], kernel_pred3_size * sizeof(float), cudaMemcpyDeviceToHost, stream)); 1071 | CHECK(cudaMemcpyAsync(kernel_pred4_output, buffers[kernel_pred4_outputIndex], kernel_pred4_size * sizeof(float), cudaMemcpyDeviceToHost, stream)); 1072 | CHECK(cudaMemcpyAsync(kernel_pred5_output, buffers[kernel_pred5_outputIndex], kernel_pred5_size * sizeof(float), cudaMemcpyDeviceToHost, stream)); 1073 | 1074 | CHECK(cudaMemcpyAsync(cate_pred1_output, buffers[cate_pred1_outputIndex], cate_pred1_size * sizeof(float), cudaMemcpyDeviceToHost, stream)); 1075 | CHECK(cudaMemcpyAsync(cate_pred2_output, buffers[cate_pred2_outputIndex], cate_pred2_size * sizeof(float), cudaMemcpyDeviceToHost, stream)); 1076 | CHECK(cudaMemcpyAsync(cate_pred3_output, buffers[cate_pred3_outputIndex], cate_pred3_size * sizeof(float), cudaMemcpyDeviceToHost, stream)); 1077 | CHECK(cudaMemcpyAsync(cate_pred4_output, buffers[cate_pred4_outputIndex], cate_pred4_size * sizeof(float), cudaMemcpyDeviceToHost, stream)); 1078 | CHECK(cudaMemcpyAsync(cate_pred5_output, buffers[cate_pred5_outputIndex], cate_pred5_size * sizeof(float), cudaMemcpyDeviceToHost, stream)); 1079 | cudaStreamDestroy(stream); 1080 | CHECK(cudaFree(buffers[dummy_inputIndex])); 1081 | CHECK(cudaFree(buffers[dummy_coord32_Index])); 1082 | CHECK(cudaFree(buffers[dummy_coord16_Index])); 1083 | CHECK(cudaFree(buffers[dummy_coord8_Index])); 1084 | 1085 | CHECK(cudaFree(buffers[feature_pred_outputIndex])); 1086 | 1087 | CHECK(cudaFree(buffers[kernel_pred1_outputIndex])); 1088 | CHECK(cudaFree(buffers[kernel_pred2_outputIndex])); 1089 | CHECK(cudaFree(buffers[kernel_pred3_outputIndex])); 1090 | CHECK(cudaFree(buffers[kernel_pred4_outputIndex])); 1091 | CHECK(cudaFree(buffers[kernel_pred5_outputIndex])); 1092 | 1093 | CHECK(cudaFree(buffers[cate_pred1_outputIndex])); 1094 | CHECK(cudaFree(buffers[cate_pred2_outputIndex])); 1095 | CHECK(cudaFree(buffers[cate_pred3_outputIndex])); 1096 | CHECK(cudaFree(buffers[cate_pred4_outputIndex])); 1097 | CHECK(cudaFree(buffers[cate_pred5_outputIndex])); 1098 | 1099 | delete[]channel_coord323; 1100 | delete[]channel_coord163; 1101 | delete[]channel_coord83; 1102 | channel_coord323 = nullptr; 1103 | channel_coord163 = nullptr; 1104 | channel_coord83 = nullptr; 1105 | 1106 | size_t ret = get_seg(srcimg, feature_pred_output, 1107 | kernel_pred1_output, kernel_pred2_output, kernel_pred3_output, kernel_pred4_output, kernel_pred5_output, 1108 | cate_pred1_output,cate_pred2_output, cate_pred3_output, cate_pred4_output, cate_pred5_output, segobj); 1109 | delete[] blob; 1110 | delete[] feature_pred_output; 1111 | 1112 | delete[] kernel_pred1_output; 1113 | delete[] kernel_pred2_output; 1114 | delete[] kernel_pred3_output; 1115 | delete[] kernel_pred4_output; 1116 | delete[] kernel_pred5_output; 1117 | 1118 | delete[] cate_pred1_output; 1119 | delete[] cate_pred2_output; 1120 | delete[] cate_pred3_output; 1121 | delete[] cate_pred4_output; 1122 | delete[] cate_pred5_output; 1123 | blob = nullptr; 1124 | feature_pred_output = nullptr; 1125 | kernel_pred1_output = nullptr; 1126 | kernel_pred2_output = nullptr; 1127 | kernel_pred3_output = nullptr; 1128 | kernel_pred4_output = nullptr; 1129 | kernel_pred5_output = nullptr; 1130 | cate_pred1_output = nullptr; 1131 | cate_pred2_output = nullptr; 1132 | cate_pred3_output = nullptr; 1133 | cate_pred4_output = nullptr; 1134 | cate_pred5_output = nullptr; 1135 | return ret; 1136 | } 1137 | 1138 | bool segTRTSigma::initialModel() 1139 | { 1140 | bool ret = build_model(); 1141 | return ret; 1142 | } 1143 | 1144 | std::vector segTRTSigma::runModel(cv::Mat& srcimg_gray, bool is_gamma) 1145 | { 1146 | std::vector segobj; 1147 | size_t ret = do_inference(srcimg_gray, segobj, is_gamma); 1148 | return segobj; 1149 | } 1150 | 1151 | segTRTSigma::~segTRTSigma() 1152 | { 1153 | } --------------------------------------------------------------------------------