Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- README.md +1 -1
- hf_space.py +88 -0
README.md
CHANGED
|
@@ -17,7 +17,7 @@ colorFrom: red
|
|
| 17 |
colorTo: red
|
| 18 |
sdk: gradio
|
| 19 |
sdk_version: 4.29.0
|
| 20 |
-
app_file:
|
| 21 |
pinned: false
|
| 22 |
---
|
| 23 |
|
|
|
|
| 17 |
colorTo: red
|
| 18 |
sdk: gradio
|
| 19 |
sdk_version: 4.29.0
|
| 20 |
+
app_file: hf_space.py
|
| 21 |
pinned: false
|
| 22 |
---
|
| 23 |
|
hf_space.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import spaces
|
| 2 |
+
import numpy as np
|
| 3 |
+
import torch
|
| 4 |
+
import torch.nn.functional as F
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from ormbg.models.ormbg import ORMBG
|
| 7 |
+
from PIL import Image
|
| 8 |
+
|
| 9 |
+
model_path = "models/ormbg.pth"
|
| 10 |
+
|
| 11 |
+
# Load the model globally but don't send to device yet
|
| 12 |
+
net = ORMBG()
|
| 13 |
+
net.load_state_dict(torch.load(model_path, map_location="cpu"))
|
| 14 |
+
net.eval()
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def resize_image(image):
|
| 18 |
+
image = image.convert("RGB")
|
| 19 |
+
model_input_size = (1024, 1024)
|
| 20 |
+
image = image.resize(model_input_size, Image.BILINEAR)
|
| 21 |
+
return image
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
@spaces.GPU
|
| 25 |
+
@torch.inference_mode()
|
| 26 |
+
def inference(image):
|
| 27 |
+
# Check for CUDA and set the device inside inference
|
| 28 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 29 |
+
net.to(device)
|
| 30 |
+
|
| 31 |
+
# Prepare input
|
| 32 |
+
orig_image = Image.fromarray(image)
|
| 33 |
+
w, h = orig_image.size
|
| 34 |
+
image = resize_image(orig_image)
|
| 35 |
+
im_np = np.array(image)
|
| 36 |
+
im_tensor = torch.tensor(im_np, dtype=torch.float32).permute(2, 0, 1)
|
| 37 |
+
im_tensor = torch.unsqueeze(im_tensor, 0)
|
| 38 |
+
im_tensor = torch.divide(im_tensor, 255.0)
|
| 39 |
+
|
| 40 |
+
if torch.cuda.is_available():
|
| 41 |
+
im_tensor = im_tensor.to(device)
|
| 42 |
+
|
| 43 |
+
# Inference
|
| 44 |
+
result = net(im_tensor)
|
| 45 |
+
# Post process
|
| 46 |
+
result = torch.squeeze(F.interpolate(result[0][0], size=(h, w), mode="bilinear"), 0)
|
| 47 |
+
ma = torch.max(result)
|
| 48 |
+
mi = torch.min(result)
|
| 49 |
+
result = (result - mi) / (ma - mi)
|
| 50 |
+
# Image to PIL
|
| 51 |
+
im_array = (result * 255).cpu().data.numpy().astype(np.uint8)
|
| 52 |
+
pil_im = Image.fromarray(np.squeeze(im_array))
|
| 53 |
+
# Paste the mask on the original image
|
| 54 |
+
new_im = Image.new("RGBA", pil_im.size, (0, 0, 0, 0))
|
| 55 |
+
new_im.paste(orig_image, mask=pil_im)
|
| 56 |
+
|
| 57 |
+
return new_im
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
# Gradio interface setup
|
| 61 |
+
title = "Open Remove Background Model (ormbg)"
|
| 62 |
+
description = r"""
|
| 63 |
+
This model is a <strong>fully open-source background remover</strong> optimized for images with humans. It is based on [Highly Accurate Dichotomous Image Segmentation research](https://github.com/xuebinqin/DIS). The model was trained with the synthetic <a href="https://huggingface.co/datasets/schirrmacher/humans">Human Segmentation Dataset</a>, <a href="https://paperswithcode.com/dataset/p3m-10k">P3M-10k</a> and <a href="https://paperswithcode.com/dataset/aim-500">AIM-500</a>.
|
| 64 |
+
|
| 65 |
+
If you identify cases where the model fails, <a href='https://huggingface.co/schirrmacher/ormbg/discussions' target='_blank'>upload your examples</a>!
|
| 66 |
+
|
| 67 |
+
- <a href='https://huggingface.co/schirrmacher/ormbg' target='_blank'>Model card</a>: find inference code, training information, tutorials
|
| 68 |
+
- <a href='https://huggingface.co/schirrmacher/ormbg' target='_blank'>Dataset</a>: see training images, segmentation data, backgrounds
|
| 69 |
+
- <a href='https://huggingface.co/schirrmacher/ormbg\#research' target='_blank'>Research</a>: see current approach for improvements
|
| 70 |
+
"""
|
| 71 |
+
|
| 72 |
+
examples = [
|
| 73 |
+
"./examples/image/example1.jpeg",
|
| 74 |
+
"./examples/image/example2.jpeg",
|
| 75 |
+
"./examples/image/example3.jpeg",
|
| 76 |
+
]
|
| 77 |
+
|
| 78 |
+
demo = gr.Interface(
|
| 79 |
+
fn=inference,
|
| 80 |
+
inputs="image",
|
| 81 |
+
outputs="image",
|
| 82 |
+
examples=examples,
|
| 83 |
+
title=title,
|
| 84 |
+
description=description,
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
if __name__ == "__main__":
|
| 88 |
+
demo.launch(share=False, allowed_paths=["ormbg", "models", "examples"])
|