Spaces:
Sleeping
Sleeping
Add files
Browse files- .gitattributes +2 -0
- .pre-commit-config.yaml +50 -0
- .vscode/settings.json +21 -0
- LICENSE +21 -0
- README.md +8 -0
- app.py +152 -0
- assets/LICENSE +21 -0
- assets/README.md +4 -0
- assets/bulb_rgb.png +3 -0
- assets/bulb_trimap.png +3 -0
- assets/new_bg.jpg +3 -0
- assets/retriever_rgb.png +3 -0
- assets/retriever_trimap.png +3 -0
- requirements.txt +4 -0
- style.css +10 -0
.gitattributes
CHANGED
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
*.png filter=lfs diff=lfs merge=lfs -text
|
37 |
+
*.jpg filter=lfs diff=lfs merge=lfs -text
|
.pre-commit-config.yaml
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
repos:
|
2 |
+
- repo: https://github.com/pre-commit/pre-commit-hooks
|
3 |
+
rev: v4.4.0
|
4 |
+
hooks:
|
5 |
+
- id: check-executables-have-shebangs
|
6 |
+
- id: check-json
|
7 |
+
- id: check-merge-conflict
|
8 |
+
- id: check-shebang-scripts-are-executable
|
9 |
+
- id: check-toml
|
10 |
+
- id: check-yaml
|
11 |
+
- id: end-of-file-fixer
|
12 |
+
- id: mixed-line-ending
|
13 |
+
args: ["--fix=lf"]
|
14 |
+
- id: requirements-txt-fixer
|
15 |
+
- id: trailing-whitespace
|
16 |
+
- repo: https://github.com/myint/docformatter
|
17 |
+
rev: v1.7.5
|
18 |
+
hooks:
|
19 |
+
- id: docformatter
|
20 |
+
args: ["--in-place"]
|
21 |
+
- repo: https://github.com/pycqa/isort
|
22 |
+
rev: 5.12.0
|
23 |
+
hooks:
|
24 |
+
- id: isort
|
25 |
+
args: ["--profile", "black"]
|
26 |
+
- repo: https://github.com/pre-commit/mirrors-mypy
|
27 |
+
rev: v1.5.1
|
28 |
+
hooks:
|
29 |
+
- id: mypy
|
30 |
+
args: ["--ignore-missing-imports"]
|
31 |
+
additional_dependencies: ["types-python-slugify", "types-requests", "types-PyYAML"]
|
32 |
+
- repo: https://github.com/psf/black
|
33 |
+
rev: 23.9.1
|
34 |
+
hooks:
|
35 |
+
- id: black
|
36 |
+
language_version: python3.10
|
37 |
+
args: ["--line-length", "119"]
|
38 |
+
- repo: https://github.com/kynan/nbstripout
|
39 |
+
rev: 0.6.1
|
40 |
+
hooks:
|
41 |
+
- id: nbstripout
|
42 |
+
args: ["--extra-keys", "metadata.interpreter metadata.kernelspec cell.metadata.pycharm"]
|
43 |
+
- repo: https://github.com/nbQA-dev/nbQA
|
44 |
+
rev: 1.7.0
|
45 |
+
hooks:
|
46 |
+
- id: nbqa-black
|
47 |
+
- id: nbqa-pyupgrade
|
48 |
+
args: ["--py37-plus"]
|
49 |
+
- id: nbqa-isort
|
50 |
+
args: ["--float-to-top"]
|
.vscode/settings.json
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"[python]": {
|
3 |
+
"editor.defaultFormatter": "ms-python.black-formatter",
|
4 |
+
"editor.formatOnType": true,
|
5 |
+
"editor.codeActionsOnSave": {
|
6 |
+
"source.organizeImports": true
|
7 |
+
}
|
8 |
+
},
|
9 |
+
"black-formatter.args": [
|
10 |
+
"--line-length=119"
|
11 |
+
],
|
12 |
+
"isort.args": ["--profile", "black"],
|
13 |
+
"flake8.args": [
|
14 |
+
"--max-line-length=119"
|
15 |
+
],
|
16 |
+
"ruff.args": [
|
17 |
+
"--line-length=119"
|
18 |
+
],
|
19 |
+
"editor.formatOnSave": true,
|
20 |
+
"files.insertFinalNewline": true
|
21 |
+
}
|
LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
MIT License
|
2 |
+
|
3 |
+
Copyright (c) 2023 hysts
|
4 |
+
|
5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6 |
+
of this software and associated documentation files (the "Software"), to deal
|
7 |
+
in the Software without restriction, including without limitation the rights
|
8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9 |
+
copies of the Software, and to permit persons to whom the Software is
|
10 |
+
furnished to do so, subject to the following conditions:
|
11 |
+
|
12 |
+
The above copyright notice and this permission notice shall be included in all
|
13 |
+
copies or substantial portions of the Software.
|
14 |
+
|
15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21 |
+
SOFTWARE.
|
README.md
CHANGED
@@ -5,8 +5,16 @@ colorFrom: indigo
|
|
5 |
colorTo: blue
|
6 |
sdk: gradio
|
7 |
sdk_version: 3.44.4
|
|
|
8 |
app_file: app.py
|
9 |
pinned: false
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
---
|
11 |
|
12 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
5 |
colorTo: blue
|
6 |
sdk: gradio
|
7 |
sdk_version: 3.44.4
|
8 |
+
python_version: 3.10.12
|
9 |
app_file: app.py
|
10 |
pinned: false
|
11 |
+
license: mit
|
12 |
+
suggested_hardware: t4-small
|
13 |
+
models:
|
14 |
+
- hustvl/vitmatte-small-distinctions-646
|
15 |
+
- hustvl/vitmatte-base-distinctions-646
|
16 |
+
- hustvl/vitmatte-small-composition-1k
|
17 |
+
- hustvl/vitmatte-base-composition-1k
|
18 |
---
|
19 |
|
20 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,152 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
|
3 |
+
import os
|
4 |
+
|
5 |
+
import gradio as gr
|
6 |
+
import numpy as np
|
7 |
+
import PIL.Image
|
8 |
+
import spaces
|
9 |
+
import torch
|
10 |
+
from transformers import VitMatteForImageMatting, VitMatteImageProcessor
|
11 |
+
|
12 |
+
DESCRIPTION = "# [ViTMatte](https://github.com/hustvl/ViTMatte)"
|
13 |
+
|
14 |
+
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
15 |
+
|
16 |
+
MAX_IMAGE_SIZE = int(os.getenv("MAX_IMAGE_SIZE", "1500"))
|
17 |
+
MODEL_ID = os.getenv("MODEL_ID", "hustvl/vitmatte-small-distinctions-646")
|
18 |
+
|
19 |
+
processor = VitMatteImageProcessor.from_pretrained(MODEL_ID)
|
20 |
+
model = VitMatteForImageMatting.from_pretrained(MODEL_ID).to(device)
|
21 |
+
|
22 |
+
|
23 |
+
def check_image_size(image: PIL.Image.Image) -> None:
|
24 |
+
if max(image.size) > MAX_IMAGE_SIZE:
|
25 |
+
raise gr.Error(f"Image size is too large. Max image size is {MAX_IMAGE_SIZE} pixels.")
|
26 |
+
|
27 |
+
|
28 |
+
def binarize_mask(mask: np.ndarray) -> np.ndarray:
|
29 |
+
mask[mask < 128] = 0
|
30 |
+
mask[mask > 0] = 1
|
31 |
+
return mask
|
32 |
+
|
33 |
+
|
34 |
+
def update_trimap(foreground_mask: dict[str, np.ndarray], unknown_mask: dict[str, np.ndarray]) -> np.ndarray:
|
35 |
+
foreground = foreground_mask["mask"]
|
36 |
+
foreground = binarize_mask(foreground)
|
37 |
+
|
38 |
+
unknown = unknown_mask["mask"]
|
39 |
+
unknown = binarize_mask(unknown)
|
40 |
+
|
41 |
+
trimap = np.zeros_like(foreground)
|
42 |
+
trimap[unknown > 0] = 128
|
43 |
+
trimap[foreground > 0] = 255
|
44 |
+
return trimap
|
45 |
+
|
46 |
+
|
47 |
+
@spaces.GPU
|
48 |
+
@torch.inference_mode()
|
49 |
+
def run(image: PIL.Image.Image, trimap: PIL.Image.Image) -> tuple[PIL.Image.Image, PIL.Image.Image]:
|
50 |
+
if image.size != trimap.size:
|
51 |
+
raise gr.Error("Image and trimap must have the same size.")
|
52 |
+
if max(image.size) > MAX_IMAGE_SIZE:
|
53 |
+
raise gr.Error(f"Image size is too large. Max image size is {MAX_IMAGE_SIZE} pixels.")
|
54 |
+
if image.mode != "RGB":
|
55 |
+
raise gr.Error("Image must be RGB.")
|
56 |
+
if trimap.mode != "L":
|
57 |
+
raise gr.Error("Trimap must be grayscale.")
|
58 |
+
|
59 |
+
pixel_values = processor(images=image, trimaps=trimap, return_tensors="pt").to(device).pixel_values
|
60 |
+
out = model(pixel_values=pixel_values)
|
61 |
+
alpha = out.alphas[0, 0].to("cpu").numpy()
|
62 |
+
|
63 |
+
w, h = image.size
|
64 |
+
alpha = alpha[:h, :w]
|
65 |
+
|
66 |
+
foreground = np.array(image).astype(float) / 255 * alpha[:, :, None] + (1 - alpha[:, :, None])
|
67 |
+
foreground = (foreground * 255).astype(np.uint8)
|
68 |
+
foreground = PIL.Image.fromarray(foreground)
|
69 |
+
|
70 |
+
return alpha, foreground
|
71 |
+
|
72 |
+
|
73 |
+
with gr.Blocks(css="style.css") as demo:
|
74 |
+
gr.Markdown(DESCRIPTION)
|
75 |
+
gr.DuplicateButton(
|
76 |
+
value="Duplicate Space for private use",
|
77 |
+
elem_id="duplicate-button",
|
78 |
+
visible=os.getenv("SHOW_DUPLICATE_BUTTON") == "1",
|
79 |
+
)
|
80 |
+
|
81 |
+
with gr.Row():
|
82 |
+
with gr.Column():
|
83 |
+
with gr.Box():
|
84 |
+
image = gr.Image(label="Input image", type="pil", height=500)
|
85 |
+
with gr.Tabs():
|
86 |
+
with gr.Tab(label="Trimap"):
|
87 |
+
trimap = gr.Image(label="Trimap", type="pil", image_mode="L", height=500)
|
88 |
+
with gr.Tab(label="Draw trimap"):
|
89 |
+
load_image_button = gr.Button("Load image")
|
90 |
+
foreground_mask = gr.Image(
|
91 |
+
label="Foreground",
|
92 |
+
tool="sketch",
|
93 |
+
type="numpy",
|
94 |
+
brush_color="green",
|
95 |
+
mask_opacity=0.7,
|
96 |
+
height=500,
|
97 |
+
)
|
98 |
+
unknown_mask = gr.Image(
|
99 |
+
label="Unkown",
|
100 |
+
tool="sketch",
|
101 |
+
type="numpy",
|
102 |
+
brush_color="green",
|
103 |
+
mask_opacity=0.7,
|
104 |
+
height=500,
|
105 |
+
)
|
106 |
+
set_trimap_button = gr.Button("Set trimap")
|
107 |
+
run_button = gr.Button("Run")
|
108 |
+
with gr.Column():
|
109 |
+
with gr.Box():
|
110 |
+
out_alpha = gr.Image(label="Alpha", height=500)
|
111 |
+
out_foreground = gr.Image(label="Foreground", height=500)
|
112 |
+
|
113 |
+
gr.Examples(
|
114 |
+
examples=[
|
115 |
+
["assets/bulb_rgb.png", "assets/bulb_trimap.png"],
|
116 |
+
["assets/retriever_rgb.png", "assets/retriever_trimap.png"],
|
117 |
+
],
|
118 |
+
inputs=[image, trimap],
|
119 |
+
outputs=[out_alpha, out_foreground],
|
120 |
+
fn=run,
|
121 |
+
cache_examples=os.getenv("CACHE_EXAMPLES") == "1",
|
122 |
+
)
|
123 |
+
|
124 |
+
image.change(
|
125 |
+
fn=check_image_size,
|
126 |
+
inputs=image,
|
127 |
+
queue=False,
|
128 |
+
api_name=False,
|
129 |
+
)
|
130 |
+
load_image_button.click(
|
131 |
+
fn=lambda image: (image, image),
|
132 |
+
inputs=image,
|
133 |
+
outputs=[foreground_mask, unknown_mask],
|
134 |
+
queue=False,
|
135 |
+
api_name=False,
|
136 |
+
)
|
137 |
+
set_trimap_button.click(
|
138 |
+
fn=update_trimap,
|
139 |
+
inputs=[foreground_mask, unknown_mask],
|
140 |
+
outputs=trimap,
|
141 |
+
queue=False,
|
142 |
+
api_name=False,
|
143 |
+
)
|
144 |
+
run_button.click(
|
145 |
+
fn=run,
|
146 |
+
inputs=[image, trimap],
|
147 |
+
outputs=[out_alpha, out_foreground],
|
148 |
+
api_name="run",
|
149 |
+
)
|
150 |
+
|
151 |
+
if __name__ == "__main__":
|
152 |
+
demo.queue(max_size=20).launch()
|
assets/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
MIT License
|
2 |
+
|
3 |
+
Copyright (c) 2023 Hust Vision Lab
|
4 |
+
|
5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6 |
+
of this software and associated documentation files (the "Software"), to deal
|
7 |
+
in the Software without restriction, including without limitation the rights
|
8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9 |
+
copies of the Software, and to permit persons to whom the Software is
|
10 |
+
furnished to do so, subject to the following conditions:
|
11 |
+
|
12 |
+
The above copyright notice and this permission notice shall be included in all
|
13 |
+
copies or substantial portions of the Software.
|
14 |
+
|
15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21 |
+
SOFTWARE.
|
assets/README.md
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# About these images
|
2 |
+
|
3 |
+
These images were obtained from [this link](https://github.com/hustvl/ViTMatte/tree/f48208ceb0d172ab0c1c528bba33cc1159596407/demo).
|
4 |
+
The original repository is licensed under the MIT license.
|
assets/bulb_rgb.png
ADDED
Git LFS Details
|
assets/bulb_trimap.png
ADDED
Git LFS Details
|
assets/new_bg.jpg
ADDED
Git LFS Details
|
assets/retriever_rgb.png
ADDED
Git LFS Details
|
assets/retriever_trimap.png
ADDED
Git LFS Details
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
git+https://github.com/huggingface/transformers@8e3980a
|
2 |
+
Pillow==10.0.1
|
3 |
+
torch==2.0.0
|
4 |
+
torchvision==0.15.1
|
style.css
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
h1 {
|
2 |
+
text-align: center;
|
3 |
+
}
|
4 |
+
|
5 |
+
#duplicate-button {
|
6 |
+
margin: auto;
|
7 |
+
color: #fff;
|
8 |
+
background: #1565c0;
|
9 |
+
border-radius: 100vh;
|
10 |
+
}
|