Update README.md
Browse files
README.md
CHANGED
|
@@ -28,8 +28,61 @@ The model is designed to produce **pixel-perfect alpha mattes**, handling fine d
|
|
| 28 |
- **Input**: RGB image tensor `(B, 3, H, W)`
|
| 29 |
- **Output**: `(semantic, detail, matte)` predictions
|
| 30 |
|
| 31 |
-
|
| 32 |
|
| 33 |
```python
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
---
|
|
|
|
| 28 |
- **Input**: RGB image tensor `(B, 3, H, W)`
|
| 29 |
- **Output**: `(semantic, detail, matte)` predictions
|
| 30 |
|
| 31 |
+
## How to Load the Model
|
| 32 |
|
| 33 |
```python
|
| 34 |
+
from transformers import AutoModel
|
| 35 |
+
|
| 36 |
+
model = AutoModel.from_pretrained(
|
| 37 |
+
"boopathiraj/MODNet",
|
| 38 |
+
trust_remote_code=True
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
model.eval()
|
| 42 |
+
```
|
| 43 |
+
|
| 44 |
+
## Example Inference
|
| 45 |
+
|
| 46 |
+
```python
|
| 47 |
+
|
| 48 |
+
import torch
|
| 49 |
+
import cv2
|
| 50 |
+
import numpy as np
|
| 51 |
+
from PIL import Image
|
| 52 |
+
from torchvision import transforms
|
| 53 |
+
|
| 54 |
+
# Preprocess
|
| 55 |
+
def preprocess(image_path, ref_size=512):
|
| 56 |
+
image = Image.open(image_path).convert("RGB")
|
| 57 |
+
w, h = image.size
|
| 58 |
+
|
| 59 |
+
scale = ref_size / max(h, w)
|
| 60 |
+
new_w, new_h = int(w * scale), int(h * scale)
|
| 61 |
+
image_resized = image.resize((new_w, new_h), Image.BILINEAR)
|
| 62 |
+
|
| 63 |
+
transform = transforms.Compose([
|
| 64 |
+
transforms.ToTensor(),
|
| 65 |
+
transforms.Normalize(mean=[0.5, 0.5, 0.5],
|
| 66 |
+
std=[0.5, 0.5, 0.5])
|
| 67 |
+
])
|
| 68 |
+
|
| 69 |
+
tensor = transform(image_resized).unsqueeze(0)
|
| 70 |
+
return tensor, (w, h)
|
| 71 |
+
|
| 72 |
+
# Run inference
|
| 73 |
+
inp, original_size = preprocess("input.jpg")
|
| 74 |
+
|
| 75 |
+
with torch.no_grad():
|
| 76 |
+
semantic, detail, matte = model(inp, True)
|
| 77 |
+
|
| 78 |
+
matte = matte[0, 0].cpu().numpy()
|
| 79 |
+
matte = cv2.resize(matte, original_size)
|
| 80 |
+
|
| 81 |
+
# Save alpha matte
|
| 82 |
+
matte = (matte * 255).astype(np.uint8)
|
| 83 |
+
Image.fromarray(matte).save("alpha_matte.png")
|
| 84 |
+
|
| 85 |
+
```
|
| 86 |
+
|
| 87 |
+
|
| 88 |
---
|