Skip to content

Implement BiRefNet ONNX inference for .NET#1

Draft
Copilot wants to merge 5 commits intomainfrom
copilot/add-onnx-inference-implementation
Draft

Implement BiRefNet ONNX inference for .NET#1
Copilot wants to merge 5 commits intomainfrom
copilot/add-onnx-inference-implementation

Conversation

Copy link

Copilot AI commented Dec 24, 2025

Complete ONNX Runtime implementation for BiRefNet image segmentation and background removal in .NET.

Core Implementation

  • BiRefNetModel: ONNX inference with sync/async APIs, mask application, proper resource disposal
  • ImagePreprocessor: Resize to 1024x1024, ImageNet normalization (RGB → NCHW tensor)
  • ImagePostprocessor: Sigmoid activation, configurable threshold/smoothing, handles non-square tensors
  • BiRefNetHelper: High-level utilities for background removal and mask extraction

Usage

using BiRefNetSharp;

// Load model once, reuse for multiple images
using var model = new BiRefNetModel("model.onnx");

// Simple background removal
BiRefNetHelper.RemoveBackground(model, "input.jpg", "output.png");

// Or manual control
using var image = Image.Load<Rgb24>("input.jpg");
using var mask = model.Predict(image);
using var smoothed = ImagePostprocessor.SmoothMask(mask, sigma: 2.0f);
using var result = BiRefNetModel.ApplyMask(image, smoothed);
result.SaveAsPng("output.png");

Package & Tooling

  • NuGet package metadata with XML documentation
  • Cross-platform CI/CD (Ubuntu, Windows, macOS)
  • Sample console application demonstrating usage
  • Comprehensive documentation (README, EXAMPLES, CONTRIBUTING)

Dependencies

  • Microsoft.ML.OnnxRuntime 1.19.2
  • SixLabors.ImageSharp 3.1.12 (latest, no CVEs)
Original prompt

BiRefNet's ONNX inference implementation under .NET


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 4 commits December 24, 2025 03:57
- Add BiRefNetSharp class library with ONNX Runtime integration
- Implement core inference model (BiRefNetModel)
- Add image preprocessing with ImageNet normalization
- Add image postprocessing for mask generation
- Create helper utilities for common operations
- Add sample console application
- Update documentation with usage examples
- Add MIT license

Co-authored-by: dogvane <3345678+dogvane@users.noreply.github.com>
- Add validation for square output tensors
- Add overload for explicit width/height dimensions
- Improve error messages for invalid inputs
- Better documentation of assumptions

Co-authored-by: dogvane <3345678+dogvane@users.noreply.github.com>
- Enable XML documentation generation
- Add comprehensive examples in EXAMPLES.md
- Add CHANGELOG.md for version tracking
- Add .editorconfig for consistent code style
- Add NuGet package metadata to project file

Co-authored-by: dogvane <3345678+dogvane@users.noreply.github.com>
- Add GitHub Actions workflow for multi-platform builds
- Add CONTRIBUTING.md with guidelines for contributors
- Enable README in NuGet package
- Configure NuGet package creation with proper metadata

Co-authored-by: dogvane <3345678+dogvane@users.noreply.github.com>
Copilot AI changed the title [WIP] Add ONNX inference implementation for BiRefNet in .NET Implement BiRefNet ONNX inference for .NET Dec 24, 2025
Copilot AI requested a review from dogvane December 24, 2025 04:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants