|
1 | | -# BiRefNet-Cpp-TensorRT |
2 | | -A high-performance C++ implementation of the Bilateral Reference Network (**BiRefNet**) leveraging **TensorRT** and **CUDA**, optimized for real-time, high-resolution dichotomous image segmentation. |
| 1 | +# BiRefNet C++ TENSORRT |
| 2 | +A high-performance C++ implementation of the Bilateral Reference Network (**BiRefNet**) leveraging **TensorRT** and **CUDA**, optimized for real-time, high-resolution dichotomous image segmentation. |
| 3 | + |
| 4 | +<img src="asset/BiRefNet-Cpp-TensorRT.JPG" alt="BiRefNet Banner" width="800"/> |
| 5 | + |
| 6 | +<a href="https://github.com/hamdiboukamcha/BiRefNet-Cpp-TensorRT" style="margin: 0 2px;"> |
| 7 | + <img src="https://img.shields.io/badge/GitHub-Repo-blue?style=flat&logo=GitHub" alt="GitHub"> |
| 8 | +</a> |
| 9 | +<a href="https://github.com/hamdiboukamcha/BiRefNet-Cpp-TensorRT?tab=License" style="margin: 0 2px;"> |
| 10 | + <img src="https://img.shields.io/badge/License-MIT-lightgrey?style=flat&logo=license" alt="License"> |
| 11 | +</a> |
| 12 | + |
| 13 | +--- |
| 14 | + |
| 15 | +## 🌐 Overview |
| 16 | + |
| 17 | +**BiRefNet C++ TENSORRT** is designed to efficiently run bilateral reference segmentation tasks on the GPU. By harnessing TensorRT’s optimizations and CUDA kernels, it aims to deliver state-of-the-art performance with minimal latency. |
| 18 | + |
| 19 | +### Key Features |
| 20 | + |
| 21 | +- **TensorRT Acceleration**: Speed up inference for segmentation tasks using serialized TRT engines. |
| 22 | +- **CUDA Integration**: Comprehensive GPU-based preprocessing, postprocessing, and memory handling. |
| 23 | +- **High-Resolution Support**: Out-of-the-box ability to process high-resolution images (e.g., 1024x1024). |
| 24 | +- **Easy Integration**: C++17 codebase for easy deployment into existing pipelines. |
| 25 | + |
| 26 | +--- |
| 27 | + |
| 28 | +## 📢 What's New |
| 29 | + |
| 30 | +- **Enhanced Bilateral Reference**: Improves dichotomous segmentation outputs by leveraging dual reference guidance. |
| 31 | +- **Improved Memory Footprint**: Optimized GPU allocation for large-batch or high-resolution workloads. |
| 32 | +- **Configurable Precision**: Support for **FP16** or **FP32** modes (requires GPU with half-precision support for FP16). |
| 33 | +- **Flexible I/O**: Easily integrate your own data loaders or pipeline steps thanks to modular design. |
| 34 | + |
| 35 | +--- |
| 36 | + |
| 37 | +## 📂 Project Structure |
| 38 | + |
| 39 | + BiRefNet/ ├── include |
| 40 | + │ └── birefnet.h # Main BiRefNet class definition |
| 41 | + ├── src |
| 42 | + │ └── birefnet.cpp # Implementation of the BiRefNet class |
| 43 | + ├── CMakeLists.txt # CMake configuration |
| 44 | + └── main.cpp # Demo application |
| 45 | + |
| 46 | + |
| 47 | +- **include/birefnet.h** |
| 48 | + Header file defining the `BiRefNet` class, which manages TensorRT engine creation, execution, and memory buffers. |
| 49 | + |
| 50 | +- **src/birefnet.cpp** |
| 51 | + Source implementation for loading serialized engines, running inference, and handling output postprocessing. |
| 52 | + |
| 53 | +- **CMakeLists.txt** |
| 54 | + Configuration for building the project using CMake. Adjust paths to TensorRT, CUDA, and OpenCV as needed. |
| 55 | + |
| 56 | +- **main.cpp** |
| 57 | + A minimal example demonstrating how to load the model, run inference on images or videos, and save the results. |
| 58 | + |
| 59 | +--- |
| 60 | + |
| 61 | +## 🚀 Installation |
| 62 | + |
| 63 | +1. **Clone the Repository** |
| 64 | + |
| 65 | + git clone https://github.com/hamdiboukamcha/BiRefNet-Cpp-TensorRT.git |
| 66 | + cd BiRefNet-Cpp-TensorRT |
| 67 | + mkdir build && cd build |
| 68 | + cmake .. |
| 69 | + make -j$(nproc) |
| 70 | + |
| 71 | + |
| 72 | +## 📦 Dependencies |
| 73 | +CUDA |
| 74 | +Required for GPU acceleration and kernel launches (e.g., CUDA 11.x or later). |
| 75 | + |
| 76 | +TensorRT |
| 77 | +High-performance deep learning inference library (v8.x or later recommended). |
| 78 | + |
| 79 | +OpenCV |
| 80 | +Needed for image loading, preprocessing, and basic visualization. |
| 81 | + |
| 82 | +C++17 |
| 83 | +This project uses modern C++ features. Ensure your compiler supports C++17 or above. |
| 84 | + |
| 85 | +## 🔍 Code Overview |
| 86 | +Main Components |
| 87 | +BiRefNet Class |
| 88 | + |
| 89 | +Initializes a TensorRT engine from a given engine/model path. |
| 90 | +Handles preprocessing (image resizing, mean/std normalization, etc.). |
| 91 | +Runs inference and postprocesses outputs into segmentation maps. |
| 92 | +Manages CUDA resources and streams. |
| 93 | +Logger Class (in main.cpp) |
| 94 | + |
| 95 | +Implements TensorRT’s ILogger interface for custom logging. |
| 96 | +Notable Functions |
| 97 | +BiRefNet::BiRefNet(...) |
| 98 | + |
| 99 | +Constructor that loads a .trt (serialized TensorRT) engine into memory. |
| 100 | +BiRefNet::predict(cv::Mat& image) |
| 101 | + |
| 102 | +Main function to run inference: takes an OpenCV cv::Mat as input, returns the segmented result as cv::Mat. |
| 103 | +BiRefNet::preprocess(...) |
| 104 | + |
| 105 | +Converts an image into normalized floats (mean subtraction, division by std, etc.). |
| 106 | +BiRefNet::postprocess(...) |
| 107 | + |
| 108 | +Reshapes the raw output into meaningful image data, typically an 8-bit or 32-bit matrix for segmentation. |
| 109 | + |
| 110 | +## 🎬 Usage |
| 111 | +Prepare Your Engine |
| 112 | + |
| 113 | +Convert your model to ONNX, then build a TensorRT engine (e.g., using trtexec or a custom builder). |
| 114 | + trtexec --onnx=birefnet_model.onnx --saveEngine=BiRefNet-tiny.engine --fp16 |
| 115 | + |
| 116 | +## 📞 Contact |
| 117 | +For further inquiries or advanced usage discussions: |
| 118 | + |
| 119 | +Email: your.email@example.com |
| 120 | +LinkedIn: Your Name |
| 121 | + |
| 122 | +## 📜 Citation |
| 123 | +If you use BiRefNet C++ TENSORRT in your academic work or research, please cite: |
| 124 | + |
| 125 | +@misc{Boukamcha2025BiRefNet, |
| 126 | + author = {Hamdi Boukamcha}, |
| 127 | + title = {BiRefNet C++ TENSORRT}, |
| 128 | + year = {2025}, |
| 129 | + publisher = {GitHub}, |
| 130 | + howpublished = {\url{https://github.com/hamdiboukamcha/BiRefNet-Cpp-TensorRT}} |
| 131 | +} |
0 commit comments