License Notice: This package uses the RMBG-2.0 model by BRIA AI under CC BY-NC 4.0. Free for non-commercial use. Commercial use requires a license from BRIA AI.
Swift Package for high-quality background removal using BRIA AI's RMBG-2.0 model, optimized for Apple Neural Engine.
- Simple, high-level API for background removal
- Automatic model download from HuggingFace
- Two model variants: INT8 quantized (233 MB) or FP32 full precision (461 MB)
- Optimized for Apple Neural Engine (ANE) with automatic fallback to GPU
- Support for macOS 13+ and iOS 16+
- Progress tracking during model download
- Batch processing support
Add RMBG2Swift to your project using Xcode:
- File → Add Package Dependencies
- Enter:
https://github.com/VincentGourbin/RMBG2Swift - Select version and add to your target
Or add to your Package.swift:
dependencies: [
.package(url: "https://github.com/VincentGourbin/RMBG2Swift", from: "1.0.0")
]import RMBG2Swift
// Create instance (downloads model on first use)
let rmbg = try await RMBG2()
// Remove background
let result = try await rmbg.removeBackground(from: image)
// Use the result
let outputImage = result.image // Image with transparent background
let mask = result.mask // Grayscale segmentation mask
let time = result.inferenceTime // Inference durationlet rmbg = try await RMBG2 { progress, status in
print("\(status): \(Int(progress * 100))%")
}Two model variants are available:
| Variant | Size | Description |
|---|---|---|
.quantized (default) |
233 MB | INT8 quantized, ANE optimized, recommended |
.full |
461 MB | FP32 full precision |
// INT8 quantized model (default, recommended)
let rmbg = try await RMBG2(configuration: .int8)
// Full precision FP32 model
let rmbg = try await RMBG2(configuration: .fullPrecision)// Model variants
RMBG2Configuration.default // INT8 + ANE (recommended)
RMBG2Configuration.int8 // INT8 + ANE (explicit)
RMBG2Configuration.fullPrecision // FP32 + ANE
// Compute unit variants
RMBG2Configuration.cpuAndGPU // INT8 + CPU/GPU (no ANE)
RMBG2Configuration.cpuOnly // INT8 + CPU onlyimport RMBG2Swift
// Full control
let config = RMBG2Configuration(
modelVariant: .quantized, // .quantized (INT8) or .full (FP32)
computeUnits: .all, // .cpuOnly, .cpuAndGPU, .all (ANE)
modelURL: customModelURL, // Optional: use local model
cacheDirectory: customDir // Optional: custom cache location
)
let rmbg = try await RMBG2(configuration: config)Note: If ANE loading fails, the library automatically falls back to CPU+GPU.
let mask = try await rmbg.generateMask(from: image)let outputImage = rmbg.applyMask(customMask, to: image)let results = try await rmbg.removeBackground(from: [image1, image2, image3])// macOS
let result = try await rmbg.removeBackground(from: nsImage)
// iOS
let result = try await rmbg.removeBackground(from: uiImage)The package includes a command-line tool:
# Build
swift build -c release
# Remove background from image
.build/release/rmbg2-cli photo.jpg output.png
# Cache management
.build/release/rmbg2-cli cache info
.build/release/rmbg2-cli cache clear| Property | Value |
|---|---|
| Architecture | BiRefNet (RMBG-2.0) |
| Input Size | 1024 x 1024 |
| Format | CoreML ML Program |
| Compute Units | CPU, GPU, ANE |
| Minimum OS | macOS 13+ / iOS 16+ |
| Variant | File | Size | Quantization |
|---|---|---|---|
| INT8 (default) | RMBG-2-native-int8.mlpackage |
233 MB | INT8 symmetric |
| Full Precision | RMBG-2-native.mlpackage |
461 MB | FP32 |
| Device | Compute Units | Inference Time |
|---|---|---|
| M1 Pro | .all (ANE) | ~5s |
| M1 Pro | .cpuAndGPU | ~3s |
Note: Performance varies by device and model variant.
The model is automatically downloaded and cached at:
- macOS/iOS:
~/Library/Caches/models/VincentGOURBIN/RMBG-2-CoreML/
// Initialization
init(progress: DownloadProgressHandler?) async throws
init(configuration: RMBG2Configuration, progress: DownloadProgressHandler?) async throws
// Main API
func removeBackground(from image: CGImage) async throws -> RMBG2Result
func generateMask(from image: CGImage) async throws -> CGImage
func applyMask(_ mask: CGImage, to image: CGImage) -> CGImage?
// Batch processing
func removeBackground(from images: [CGImage]) async throws -> [RMBG2Result]struct RMBG2Result {
let image: CGImage // Image with transparent background
let mask: CGImage // Grayscale segmentation mask
let inferenceTime: TimeInterval
}enum ModelVariant {
case quantized // INT8, 233 MB (default, recommended)
case full // FP32, 461 MB
}struct RMBG2Configuration {
let modelVariant: ModelVariant // .quantized or .full
let computeUnits: MLComputeUnits // .all, .cpuAndGPU, .cpuOnly
let modelURL: URL? // Custom model path
let cacheDirectory: URL? // Custom cache directory
// Static configurations
static let `default`: RMBG2Configuration // INT8 + ANE
static let int8: RMBG2Configuration // INT8 + ANE
static let fullPrecision: RMBG2Configuration // FP32 + ANE
static let cpuAndGPU: RMBG2Configuration // INT8 + CPU/GPU
static let cpuOnly: RMBG2Configuration // INT8 + CPU
}This package uses the RMBG-2.0 model by BRIA AI.
- License: CC BY-NC 4.0
- Free for: Research, personal projects, education
- Commercial use: Requires separate license from BRIA AI
RMBG2Swift/
├── Package.swift
├── Sources/RMBG2Swift/
│ ├── RMBG2.swift # Main public API
│ ├── RMBG2Configuration.swift # Configuration options
│ ├── RMBG2Error.swift # Error types
│ ├── ModelDownloader.swift # HuggingFace download
│ ├── ImageProcessing.swift # CGImage ↔ MLMultiArray
│ └── Internal/
│ └── Constants.swift # Model URLs, sizes, etc.
├── Tests/RMBG2SwiftTests/
│ └── RMBG2Tests.swift
└── Examples/CLI/
└── main.swift # CLI tool
The native conversion scripts are preserved for reproducibility:
convert_native.py- Main conversion scriptnative_deform_conv.py- Native deformable convolution implementation
To reconvert the model:
# Create environment
python3 -m venv venv
source venv/bin/activate
pip install torch torchvision transformers coremltools pillow numpy
# Run conversion
python convert_native.pyContributions are welcome! Please ensure any changes maintain compatibility with both macOS and iOS platforms.