Update README.md
Browse files
README.md
CHANGED
@@ -100,16 +100,57 @@ wget https://huggingface.co/briaai/RMBG-1.4/resolve/main/requirements.txt && pip
|
|
100 |
|
101 |
## Usage
|
102 |
|
103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
```python
|
105 |
from transformers import AutoModelForImageSegmentation
|
106 |
model = AutoModelForImageSegmentation.from_pretrained("briaai/RMBG-1.4",trust_remote_code=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
```
|
108 |
|
109 |
-
or load the pipeline
|
110 |
-
```python
|
111 |
-
from transformers import pipeline
|
112 |
-
pipe = pipeline("image-segmentation", model="briaai/RMBG-1.4", trust_remote_code=True)
|
113 |
-
pillow_mask = pipe("img_path",return_mask = True) # outputs a pillow mask
|
114 |
-
pillow_image = pipe("image_path") # applies mask on input and returns a pillow image
|
115 |
-
```
|
|
|
100 |
|
101 |
## Usage
|
102 |
|
103 |
+
Either load the pipeline
|
104 |
+
```python
|
105 |
+
from transformers import pipeline
|
106 |
+
image_path = "https://farm5.staticflickr.com/4007/4322154488_997e69e4cf_z.jpg"
|
107 |
+
pipe = pipeline("image-segmentation", model="briaai/RMBG-1.4", trust_remote_code=True)
|
108 |
+
pillow_mask = pipe(img_path, return_mask = True) # outputs a pillow mask
|
109 |
+
pillow_image = pipe(image_path) # applies mask on input and returns a pillow image
|
110 |
+
```
|
111 |
+
|
112 |
+
Or load the model
|
113 |
```python
|
114 |
from transformers import AutoModelForImageSegmentation
|
115 |
model = AutoModelForImageSegmentation.from_pretrained("briaai/RMBG-1.4",trust_remote_code=True)
|
116 |
+
def preprocess_image(im: np.ndarray, model_input_size: list) -> torch.Tensor:
|
117 |
+
if len(im.shape) < 3:
|
118 |
+
im = im[:, :, np.newaxis]
|
119 |
+
# orig_im_size=im.shape[0:2]
|
120 |
+
im_tensor = torch.tensor(im, dtype=torch.float32).permute(2,0,1)
|
121 |
+
im_tensor = F.interpolate(torch.unsqueeze(im_tensor,0), size=model_input_size, mode='bilinear')
|
122 |
+
image = torch.divide(im_tensor,255.0)
|
123 |
+
image = normalize(image,[0.5,0.5,0.5],[1.0,1.0,1.0])
|
124 |
+
return image
|
125 |
+
|
126 |
+
def postprocess_image(result: torch.Tensor, im_size: list)-> np.ndarray:
|
127 |
+
result = torch.squeeze(F.interpolate(result, size=im_size, mode='bilinear') ,0)
|
128 |
+
ma = torch.max(result)
|
129 |
+
mi = torch.min(result)
|
130 |
+
result = (result-mi)/(ma-mi)
|
131 |
+
im_array = (result*255).permute(1,2,0).cpu().data.numpy().astype(np.uint8)
|
132 |
+
im_array = np.squeeze(im_array)
|
133 |
+
return im_array
|
134 |
+
|
135 |
+
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
136 |
+
model.to(device)
|
137 |
+
|
138 |
+
# prepare input
|
139 |
+
image_path = "https://farm5.staticflickr.com/4007/4322154488_997e69e4cf_z.jpg"
|
140 |
+
orig_im = io.imread(im_path)
|
141 |
+
orig_im_size = orig_im.shape[0:2]
|
142 |
+
image = preprocess_image(orig_im, model_input_size).to(device)
|
143 |
+
|
144 |
+
# inference
|
145 |
+
result=model(image)
|
146 |
+
|
147 |
+
# post process
|
148 |
+
result_image = postprocess_image(result[0][0], orig_im_size)
|
149 |
+
|
150 |
+
# save result
|
151 |
+
pil_im = Image.fromarray(result_image)
|
152 |
+
no_bg_image = Image.new("RGBA", pil_im.size, (0,0,0,0))
|
153 |
+
orig_image = Image.open(im_path)
|
154 |
+
no_bg_image.paste(orig_image, mask=pil_im)
|
155 |
```
|
156 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|