Update
Browse files- .pre-commit-config.yaml +35 -0
- .style.yapf +5 -0
- README.md +1 -29
- app.py +33 -69
- requirements.txt +3 -2
.pre-commit-config.yaml
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
repos:
|
2 |
+
- repo: https://github.com/pre-commit/pre-commit-hooks
|
3 |
+
rev: v4.2.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: double-quote-string-fixer
|
12 |
+
- id: end-of-file-fixer
|
13 |
+
- id: mixed-line-ending
|
14 |
+
args: ['--fix=lf']
|
15 |
+
- id: requirements-txt-fixer
|
16 |
+
- id: trailing-whitespace
|
17 |
+
- repo: https://github.com/myint/docformatter
|
18 |
+
rev: v1.4
|
19 |
+
hooks:
|
20 |
+
- id: docformatter
|
21 |
+
args: ['--in-place']
|
22 |
+
- repo: https://github.com/pycqa/isort
|
23 |
+
rev: 5.12.0
|
24 |
+
hooks:
|
25 |
+
- id: isort
|
26 |
+
- repo: https://github.com/pre-commit/mirrors-mypy
|
27 |
+
rev: v0.991
|
28 |
+
hooks:
|
29 |
+
- id: mypy
|
30 |
+
args: ['--ignore-missing-imports']
|
31 |
+
- repo: https://github.com/google/yapf
|
32 |
+
rev: v0.32.0
|
33 |
+
hooks:
|
34 |
+
- id: yapf
|
35 |
+
args: ['--parallel', '--in-place']
|
.style.yapf
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
[style]
|
2 |
+
based_on_style = pep8
|
3 |
+
blank_line_before_nested_class_or_def = false
|
4 |
+
spaces_before_comment = 2
|
5 |
+
split_before_logical_operator = true
|
README.md
CHANGED
@@ -4,35 +4,7 @@ emoji: 🐢
|
|
4 |
colorFrom: blue
|
5 |
colorTo: indigo
|
6 |
sdk: gradio
|
7 |
-
sdk_version: 3.
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
---
|
11 |
-
|
12 |
-
# Configuration
|
13 |
-
|
14 |
-
`title`: _string_
|
15 |
-
Display title for the Space
|
16 |
-
|
17 |
-
`emoji`: _string_
|
18 |
-
Space emoji (emoji-only character allowed)
|
19 |
-
|
20 |
-
`colorFrom`: _string_
|
21 |
-
Color for Thumbnail gradient (red, yellow, green, blue, indigo, purple, pink, gray)
|
22 |
-
|
23 |
-
`colorTo`: _string_
|
24 |
-
Color for Thumbnail gradient (red, yellow, green, blue, indigo, purple, pink, gray)
|
25 |
-
|
26 |
-
`sdk`: _string_
|
27 |
-
Can be either `gradio`, `streamlit`, or `static`
|
28 |
-
|
29 |
-
`sdk_version` : _string_
|
30 |
-
Only applicable for `streamlit` SDK.
|
31 |
-
See [doc](https://hf.co/docs/hub/spaces) for more info on supported versions.
|
32 |
-
|
33 |
-
`app_file`: _string_
|
34 |
-
Path to your main application file (which contains either `gradio` or `streamlit` Python code, or `static` html code).
|
35 |
-
Path is relative to the root of the repository.
|
36 |
-
|
37 |
-
`pinned`: _boolean_
|
38 |
-
Whether the Space stays on top of your list.
|
|
|
4 |
colorFrom: blue
|
5 |
colorTo: indigo
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 3.19.1
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.py
CHANGED
@@ -2,7 +2,6 @@
|
|
2 |
|
3 |
from __future__ import annotations
|
4 |
|
5 |
-
import argparse
|
6 |
import functools
|
7 |
import os
|
8 |
import pathlib
|
@@ -25,29 +24,12 @@ from _util.twodee_v0 import I as ImageWrapper
|
|
25 |
|
26 |
TITLE = 'ShuhongChen/bizarre-pose-estimator (segmenter)'
|
27 |
DESCRIPTION = 'This is an unofficial demo for https://github.com/ShuhongChen/bizarre-pose-estimator.'
|
28 |
-
ARTICLE = '<center><img src="https://visitor-badge.glitch.me/badge?page_id=hysts.bizarre-pose-estimator-segmenter" alt="visitor badge"/></center>'
|
29 |
|
30 |
-
|
31 |
MODEL_REPO = 'hysts/bizarre-pose-estimator-models'
|
32 |
MODEL_FILENAME = 'segmenter.pth'
|
33 |
|
34 |
|
35 |
-
def parse_args() -> argparse.Namespace:
|
36 |
-
parser = argparse.ArgumentParser()
|
37 |
-
parser.add_argument('--device', type=str, default='cpu')
|
38 |
-
parser.add_argument('--score-slider-step', type=float, default=0.05)
|
39 |
-
parser.add_argument('--score-threshold', type=float, default=0.5)
|
40 |
-
parser.add_argument('--theme', type=str)
|
41 |
-
parser.add_argument('--live', action='store_true')
|
42 |
-
parser.add_argument('--share', action='store_true')
|
43 |
-
parser.add_argument('--port', type=int)
|
44 |
-
parser.add_argument('--disable-queue',
|
45 |
-
dest='enable_queue',
|
46 |
-
action='store_false')
|
47 |
-
parser.add_argument('--allow-flagging', type=str, default='never')
|
48 |
-
return parser.parse_args()
|
49 |
-
|
50 |
-
|
51 |
def load_sample_image_paths() -> list[pathlib.Path]:
|
52 |
image_dir = pathlib.Path('images')
|
53 |
if not image_dir.exists():
|
@@ -55,7 +37,7 @@ def load_sample_image_paths() -> list[pathlib.Path]:
|
|
55 |
path = huggingface_hub.hf_hub_download(dataset_repo,
|
56 |
'images.tar.gz',
|
57 |
repo_type='dataset',
|
58 |
-
use_auth_token=
|
59 |
with tarfile.open(path) as f:
|
60 |
f.extractall()
|
61 |
return sorted(image_dir.glob('*'))
|
@@ -65,7 +47,7 @@ def load_model(
|
|
65 |
device: torch.device) -> tuple[torch.nn.Module, torch.nn.Module]:
|
66 |
path = huggingface_hub.hf_hub_download(MODEL_REPO,
|
67 |
MODEL_FILENAME,
|
68 |
-
use_auth_token=
|
69 |
ckpt = torch.load(path)
|
70 |
|
71 |
model = torchvision.models.segmentation.deeplabv3_resnet101()
|
@@ -115,59 +97,41 @@ def predict(image: PIL.Image.Image, score_threshold: float,
|
|
115 |
probs = probs[1] # foreground
|
116 |
probs = PIL.Image.fromarray(probs.cpu().numpy()).resize(image.size)
|
117 |
|
118 |
-
mask = np.asarray(probs)
|
119 |
mask[mask < score_threshold] = 0
|
120 |
mask[mask > 0] = 1
|
121 |
mask = mask.astype(bool)
|
122 |
|
123 |
-
res = np.asarray(image)
|
124 |
res[~mask] = 255
|
125 |
return res
|
126 |
|
127 |
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
],
|
157 |
-
gr.outputs.Image(label='Masked'),
|
158 |
-
examples=examples,
|
159 |
-
title=TITLE,
|
160 |
-
description=DESCRIPTION,
|
161 |
-
article=ARTICLE,
|
162 |
-
theme=args.theme,
|
163 |
-
allow_flagging=args.allow_flagging,
|
164 |
-
live=args.live,
|
165 |
-
).launch(
|
166 |
-
enable_queue=args.enable_queue,
|
167 |
-
server_port=args.port,
|
168 |
-
share=args.share,
|
169 |
-
)
|
170 |
-
|
171 |
-
|
172 |
-
if __name__ == '__main__':
|
173 |
-
main()
|
|
|
2 |
|
3 |
from __future__ import annotations
|
4 |
|
|
|
5 |
import functools
|
6 |
import os
|
7 |
import pathlib
|
|
|
24 |
|
25 |
TITLE = 'ShuhongChen/bizarre-pose-estimator (segmenter)'
|
26 |
DESCRIPTION = 'This is an unofficial demo for https://github.com/ShuhongChen/bizarre-pose-estimator.'
|
|
|
27 |
|
28 |
+
HF_TOKEN = os.getenv('HF_TOKEN')
|
29 |
MODEL_REPO = 'hysts/bizarre-pose-estimator-models'
|
30 |
MODEL_FILENAME = 'segmenter.pth'
|
31 |
|
32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
def load_sample_image_paths() -> list[pathlib.Path]:
|
34 |
image_dir = pathlib.Path('images')
|
35 |
if not image_dir.exists():
|
|
|
37 |
path = huggingface_hub.hf_hub_download(dataset_repo,
|
38 |
'images.tar.gz',
|
39 |
repo_type='dataset',
|
40 |
+
use_auth_token=HF_TOKEN)
|
41 |
with tarfile.open(path) as f:
|
42 |
f.extractall()
|
43 |
return sorted(image_dir.glob('*'))
|
|
|
47 |
device: torch.device) -> tuple[torch.nn.Module, torch.nn.Module]:
|
48 |
path = huggingface_hub.hf_hub_download(MODEL_REPO,
|
49 |
MODEL_FILENAME,
|
50 |
+
use_auth_token=HF_TOKEN)
|
51 |
ckpt = torch.load(path)
|
52 |
|
53 |
model = torchvision.models.segmentation.deeplabv3_resnet101()
|
|
|
97 |
probs = probs[1] # foreground
|
98 |
probs = PIL.Image.fromarray(probs.cpu().numpy()).resize(image.size)
|
99 |
|
100 |
+
mask = np.asarray(probs).copy()
|
101 |
mask[mask < score_threshold] = 0
|
102 |
mask[mask > 0] = 1
|
103 |
mask = mask.astype(bool)
|
104 |
|
105 |
+
res = np.asarray(image).copy()
|
106 |
res[~mask] = 255
|
107 |
return res
|
108 |
|
109 |
|
110 |
+
image_paths = load_sample_image_paths()
|
111 |
+
examples = [[path.as_posix(), 0.5] for path in image_paths]
|
112 |
+
|
113 |
+
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
|
114 |
+
model, final_head = load_model(device)
|
115 |
+
transform = T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
116 |
+
|
117 |
+
func = functools.partial(predict,
|
118 |
+
transform=transform,
|
119 |
+
device=device,
|
120 |
+
model=model,
|
121 |
+
final_head=final_head)
|
122 |
+
|
123 |
+
gr.Interface(
|
124 |
+
fn=func,
|
125 |
+
inputs=[
|
126 |
+
gr.Image(type='pil', label='Input'),
|
127 |
+
gr.Slider(label='Score Threshold',
|
128 |
+
minimum=0,
|
129 |
+
maximum=1,
|
130 |
+
step=0.05,
|
131 |
+
value=0.5),
|
132 |
+
],
|
133 |
+
outputs=gr.Image(label='Masked'),
|
134 |
+
examples=examples,
|
135 |
+
title=TITLE,
|
136 |
+
description=DESCRIPTION,
|
137 |
+
).queue().launch(show_api=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
@@ -1,2 +1,3 @@
|
|
1 |
-
|
2 |
-
|
|
|
|
1 |
+
numpy==1.23.5
|
2 |
+
torch==1.13.1
|
3 |
+
torchvision==0.14.1
|