Skip to content

armgit / rembg

Public Imported from GitHub
0

Rembg is a tool to remove images background

feat: add birefnet-dynamic session (#868)

Alberto Pasqualetto61fca30e68a3
diff --git a/README.md b/README.md
index 70d4322..3967403 100644
--- a/README.md
+++ b/README.md
@@ -546,6 +546,7 @@ Sample results and benchmark scores for each model are hosted by [withoutBG](htt
- birefnet-hrsod ([download](https://github.com/danielgatis/rembg/releases/download/v0.0.0/BiRefNet-HRSOD_DHU-epoch_115.onnx), [source](https://github.com/ZhengPeng7/BiRefNet), [sample results](https://withoutbg.com/models/birefnet-hrsod?ref=rembg&utm_medium=readme&utm_campaign=models&utm_content=birefnet-hrsod)): A pre-trained model for high-resolution salient object detection (HRSOD).
- birefnet-cod ([download](https://github.com/danielgatis/rembg/releases/download/v0.0.0/BiRefNet-COD-epoch_125.onnx), [source](https://github.com/ZhengPeng7/BiRefNet)): A pre-trained model for concealed object detection (COD).
- birefnet-massive ([download](https://github.com/danielgatis/rembg/releases/download/v0.0.0/BiRefNet-massive-TR_DIS5K_TR_TEs-epoch_420.onnx), [source](https://github.com/ZhengPeng7/BiRefNet), [sample results](https://withoutbg.com/models/birefnet-massive?ref=rembg&utm_medium=readme&utm_campaign=models&utm_content=birefnet-massive)): A pre-trained model with massive dataset.
+- birefnet-dynamic ([download](https://github.com/danielgatis/rembg/releases/download/v0.0.0/BiRefNet_dynamic-1024x1024.onnx), [source](https://huggingface.co/ZhengPeng7/BiRefNet_dynamic)): A general-use model trained on arbitrary input shapes (256x256 to 2304x2304); this export runs at 1024x1024, like birefnet-general.
- bria-rmbg ([download](https://github.com/danielgatis/rembg/releases/download/v0.0.0/bria-rmbg-2.0.onnx), [source](https://huggingface.co/briaai/RMBG-2.0), [sample results](https://withoutbg.com/models/bria-rmbg?ref=rembg&utm_medium=readme&utm_campaign=models&utm_content=bria-rmbg)): **The default.** A state-of-the-art background removal model by BRIA AI. At ~1.02 GB it is the largest local model here and runs at 1024x1024, so it is slower than u2net; pass `-m u2net` for a smaller, faster model with blockier edges. Note that RMBG-2.0 is released under a [BRIA license](https://huggingface.co/briaai/RMBG-2.0) that requires a paid agreement for commercial use. Model weights carry their own licenses, independent of rembg's MIT license — check the linked source before using any model commercially.
- withoutbg ([API](https://withoutbg.com/?ref=rembg&utm_medium=readme&utm_content=models), [sample results](https://withoutbg.com/pro-model-api/results?ref=rembg&utm_medium=readme&utm_content=models)): Cloud API backend. [Get 50 free credits with signup](https://withoutbg.com/signup?ref=rembg&utm_medium=readme&utm_content=models). Pass `api_key` via `-x` / `new_session(...)`, or set `WITHOUTBG_API_KEY`. Images are sent to withoutBG's servers; max upload size is 20 MB.
- atlascloud ([API](https://www.atlascloud.ai)): Cloud API backend running `midjourney/v8.2/remove-background` by default. Pass `api_key` via `-x` / `new_session(...)`, or set `ATLASCLOUD_API_KEY`; change the model with `model` or `ATLASCLOUD_MODEL`. Requests are asynchronous and can take over a minute. Images are sent to Atlas Cloud's servers; max upload size is 20 MB.
diff --git a/rembg/sessions/__init__.py b/rembg/sessions/__init__.py
index e0362a9..0cda66e 100644
--- a/rembg/sessions/__init__.py
+++ b/rembg/sessions/__init__.py
@@ -38,6 +38,10 @@ from .birefnet_massive import BiRefNetSessionMassive
sessions[BiRefNetSessionMassive.name()] = BiRefNetSessionMassive
+from .birefnet_dynamic import BiRefNetSessionDynamic
+
+sessions[BiRefNetSessionDynamic.name()] = BiRefNetSessionDynamic
+
from .dis_anime import DisSession
sessions[DisSession.name()] = DisSession
diff --git a/rembg/sessions/birefnet_dynamic.py b/rembg/sessions/birefnet_dynamic.py
new file mode 100644
index 0000000..85ffe7b
--- /dev/null
+++ b/rembg/sessions/birefnet_dynamic.py
@@ -0,0 +1,56 @@
+import os
+
+import pooch
+
+from . import BiRefNetSessionGeneral
+
+
+class BiRefNetSessionDynamic(BiRefNetSessionGeneral):
+ """
+ This class represents a BiRefNet-Dynamic session, which is a subclass of BiRefNetSessionGeneral.
+ """
+
+ @classmethod
+ def download_models(cls, *args, **kwargs):
+ """
+ Downloads the BiRefNet-Dynamic model file from a specific URL and saves it.
+
+ Parameters:
+ *args: Additional positional arguments.
+ **kwargs: Additional keyword arguments.
+
+ Returns:
+ str: The path to the downloaded model file.
+ """
+ fname = f"{cls.name(*args, **kwargs)}.onnx"
+ existing = cls.resolve_existing(fname, *args, **kwargs)
+ if existing is not None:
+ return existing
+
+ pooch.retrieve(
+ "https://github.com/danielgatis/rembg/releases/download/v0.0.0/BiRefNet_dynamic-1024x1024.onnx",
+ (
+ None
+ if cls.checksum_disabled(*args, **kwargs)
+ else "md5:4f9c4b2d2e16fe1788f98184e7f43d2b"
+ ),
+ fname=fname,
+ path=cls.model_dir(*args, **kwargs),
+ progressbar=True,
+ )
+
+ return os.path.join(cls.model_dir(*args, **kwargs), fname)
+
+ @classmethod
+ def name(cls, *args, **kwargs):
+ """
+ Returns the name of the BiRefNet-Dynamic session.
+
+ Parameters:
+ *args: Additional positional arguments.
+ **kwargs: Additional keyword arguments.
+
+ Returns:
+ str: The name of the session.
+ """
+ return "birefnet-dynamic"
diff --git a/tests/results/anime-girl-1.birefnet-dynamic.png b/tests/results/anime-girl-1.birefnet-dynamic.png
new file mode 100644
index 0000000..4d7f0b1
Binary files /dev/null and b/tests/results/anime-girl-1.birefnet-dynamic.png differ
diff --git a/tests/test_remove.py b/tests/test_remove.py
index b47430e..f4f77d3 100644
--- a/tests/test_remove.py
+++ b/tests/test_remove.py
@@ -36,6 +36,7 @@ def test_remove():
"birefnet-hrsod",
"birefnet-cod",
"birefnet-massive",
+ "birefnet-dynamic",
]:
for picture in ["anime-girl-1"]:
image_path = Path(here / "fixtures" / f"{picture}.jpg")