Image Segmentation
BiRefNet
Safetensors
Transformers
background-removal
mask-generation
Dichotomous Image Segmentation
Camouflaged Object Detection
Salient Object Detection
pytorch_model_hub_mixin
model_hub_mixin
custom_code
Instructions to use ZhengPeng7/BiRefNet with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- BiRefNet
How to use ZhengPeng7/BiRefNet with BiRefNet:
# Option 1: use with transformers from transformers import AutoModelForImageSegmentation birefnet = AutoModelForImageSegmentation.from_pretrained("ZhengPeng7/BiRefNet", trust_remote_code=True)# Option 2: use with BiRefNet # Install from https://github.com/ZhengPeng7/BiRefNet from models.birefnet import BiRefNet model = BiRefNet.from_pretrained("ZhengPeng7/BiRefNet") - Transformers
How to use ZhengPeng7/BiRefNet with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-segmentation", model="ZhengPeng7/BiRefNet", trust_remote_code=True)# Load model directly from transformers import AutoModelForImageSegmentation model = AutoModelForImageSegmentation.from_pretrained("ZhengPeng7/BiRefNet", trust_remote_code=True, dtype="auto") - Notebooks
- Google Colab
- Kaggle
Update README.md
Browse files
README.md
CHANGED
|
@@ -52,6 +52,15 @@ Visit our GitHub repo: [https://github.com/ZhengPeng7/BiRefNet](https://github.c
|
|
| 52 |
|
| 53 |
### 1. Load BiRefNet:
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
#### Use codes from GitHub + weights from HuggingFace
|
| 56 |
> Only use the weights on HuggingFace -- Pro: codes are always latest; Con: Need to clone the BiRefNet repo from my GitHub.
|
| 57 |
|
|
@@ -64,16 +73,19 @@ cd BiRefNet
|
|
| 64 |
```python
|
| 65 |
# Load weights
|
| 66 |
from models.birefnet import BiRefNet
|
|
|
|
|
|
|
| 67 |
birefnet = BiRefNet.from_pretrained('zhengpeng7/birefnet')
|
| 68 |
-
```
|
| 69 |
|
| 70 |
-
#
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
-
```python
|
| 74 |
-
# Load BiRefNet with weights
|
| 75 |
-
from transformers import AutoModelForImageSegmentation
|
| 76 |
-
birefnet = AutoModelForImageSegmentation.from_pretrained('zhengpeng7/birefnet', trust_remote_code=True)
|
| 77 |
```
|
| 78 |
|
| 79 |
#### Use the loaded BiRefNet for inference
|
|
|
|
| 52 |
|
| 53 |
### 1. Load BiRefNet:
|
| 54 |
|
| 55 |
+
#### Use codes + weights from HuggingFace
|
| 56 |
+
> Only use the weights on HuggingFace -- Pro: No need to download BiRefNet codes manually; Con: Codes on HuggingFace might not be latest version (I'll try to keep them always latest).
|
| 57 |
+
|
| 58 |
+
```python
|
| 59 |
+
# Load BiRefNet with weights
|
| 60 |
+
from transformers import AutoModelForImageSegmentation
|
| 61 |
+
birefnet = AutoModelForImageSegmentation.from_pretrained('zhengpeng7/birefnet', trust_remote_code=True)
|
| 62 |
+
```
|
| 63 |
+
|
| 64 |
#### Use codes from GitHub + weights from HuggingFace
|
| 65 |
> Only use the weights on HuggingFace -- Pro: codes are always latest; Con: Need to clone the BiRefNet repo from my GitHub.
|
| 66 |
|
|
|
|
| 73 |
```python
|
| 74 |
# Load weights
|
| 75 |
from models.birefnet import BiRefNet
|
| 76 |
+
|
| 77 |
+
# Option-1: From Hugging Face Models
|
| 78 |
birefnet = BiRefNet.from_pretrained('zhengpeng7/birefnet')
|
|
|
|
| 79 |
|
| 80 |
+
# Option-2: From local disk
|
| 81 |
+
import torch
|
| 82 |
+
from utils import check_state_dict
|
| 83 |
+
|
| 84 |
+
birefnet = BiRefNet(bb_pretrained=False)
|
| 85 |
+
state_dict = torch.load(PATH_TO_WEIGHT, map_location='cpu')
|
| 86 |
+
state_dict = check_state_dict(state_dict)
|
| 87 |
+
birefnet.load_state_dict(state_dict)
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
```
|
| 90 |
|
| 91 |
#### Use the loaded BiRefNet for inference
|