Add BiRefNet (Bilateral Reference Network) Support #3301
+2,629
−3
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds support for BiRefNet (Bilateral Reference Network), a state-of-the-art model for high-resolution dichotomous image segmentation, commonly used for background removal tasks.
This PR depends on the
deform_conv2doperation which is currently pending in PR #3292 (The development work of this PR has been completed, and the data verification has passed and is waiting to be merged).The following dependencies in
Cargo.tomlare temporarily using a fork:Once PR #3292 is merged, these dependencies will be reverted to local paths.
Changes
New Files
candle-transformers/src/models/birefnet/
mod.rs- Module exports and documentationconfig.rs- Model configuration with support for different backbone typesbirefnet.rs- Main BiRefNet model implementationswin.rs- Swin Transformer backbone (SwinTransformerBlock, BasicLayer, PatchEmbed, PatchMerging)decoder.rs- Decoder with multi-scale feature fusionaspp.rs- ASPP (Atrous Spatial Pyramid Pooling) with Deformable Convolution supportblocks.rs- Basic building blocks (BasicDecBlk, BasicLatBlk, SimpleConvs)candle-examples/examples/birefnet/
main.rs- Example application for background removalREADME.md- Usage documentationModified Files
candle-transformers/src/models/mod.rs- Added birefnet module exportCargo.toml- Added temporary fork dependencies for deform_conv2d supportFeatures
Example Usage
Platform Support
--features cuda)--features metal)--cpuflag)Test Results
Input Image
Python Reference Output (Official BiRefNet)
Rust Candle Output
Cuda Result
Metal Result
References