hysts HF staff commited on
Commit
8aae359
1 Parent(s): 09b70ad
Files changed (6) hide show
  1. .pre-commit-config.yaml +59 -34
  2. .style.yapf +0 -5
  3. .vscode/settings.json +30 -0
  4. README.md +1 -1
  5. app.py +54 -46
  6. style.css +8 -0
.pre-commit-config.yaml CHANGED
@@ -1,35 +1,60 @@
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']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  repos:
2
+ - repo: https://github.com/pre-commit/pre-commit-hooks
3
+ rev: v4.5.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.13.2
23
+ hooks:
24
+ - id: isort
25
+ args: ["--profile", "black"]
26
+ - repo: https://github.com/pre-commit/mirrors-mypy
27
+ rev: v1.8.0
28
+ hooks:
29
+ - id: mypy
30
+ args: ["--ignore-missing-imports"]
31
+ additional_dependencies:
32
+ [
33
+ "types-python-slugify",
34
+ "types-requests",
35
+ "types-PyYAML",
36
+ "types-pytz",
37
+ ]
38
+ - repo: https://github.com/psf/black
39
+ rev: 24.2.0
40
+ hooks:
41
+ - id: black
42
+ language_version: python3.10
43
+ args: ["--line-length", "119"]
44
+ - repo: https://github.com/kynan/nbstripout
45
+ rev: 0.7.1
46
+ hooks:
47
+ - id: nbstripout
48
+ args:
49
+ [
50
+ "--extra-keys",
51
+ "metadata.interpreter metadata.kernelspec cell.metadata.pycharm",
52
+ ]
53
+ - repo: https://github.com/nbQA-dev/nbQA
54
+ rev: 1.7.1
55
+ hooks:
56
+ - id: nbqa-black
57
+ - id: nbqa-pyupgrade
58
+ args: ["--py37-plus"]
59
+ - id: nbqa-isort
60
+ args: ["--float-to-top"]
.style.yapf DELETED
@@ -1,5 +0,0 @@
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
 
 
 
 
 
 
.vscode/settings.json ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "editor.formatOnSave": true,
3
+ "files.insertFinalNewline": false,
4
+ "[python]": {
5
+ "editor.defaultFormatter": "ms-python.black-formatter",
6
+ "editor.formatOnType": true,
7
+ "editor.codeActionsOnSave": {
8
+ "source.organizeImports": "explicit"
9
+ }
10
+ },
11
+ "[jupyter]": {
12
+ "files.insertFinalNewline": false
13
+ },
14
+ "black-formatter.args": [
15
+ "--line-length=119"
16
+ ],
17
+ "isort.args": ["--profile", "black"],
18
+ "flake8.args": [
19
+ "--max-line-length=119"
20
+ ],
21
+ "ruff.lint.args": [
22
+ "--line-length=119"
23
+ ],
24
+ "notebook.output.scrolling": true,
25
+ "notebook.formatOnCellExecution": true,
26
+ "notebook.formatOnSave.enabled": true,
27
+ "notebook.codeActionsOnSave": {
28
+ "source.organizeImports": "explicit"
29
+ }
30
+ }
README.md CHANGED
@@ -4,7 +4,7 @@ emoji: 🐢
4
  colorFrom: blue
5
  colorTo: indigo
6
  sdk: gradio
7
- sdk_version: 3.36.1
8
  app_file: app.py
9
  pinned: false
10
  ---
 
4
  colorFrom: blue
5
  colorTo: indigo
6
  sdk: gradio
7
+ sdk_version: 4.19.2
8
  app_file: app.py
9
  pinned: false
10
  ---
app.py CHANGED
@@ -18,29 +18,27 @@ import torch.nn as nn
18
  import torchvision
19
  import torchvision.transforms as T
20
 
21
- sys.path.insert(0, 'bizarre-pose-estimator')
22
 
23
  from _util.twodee_v0 import I as ImageWrapper
24
 
25
- DESCRIPTION = '# [ShuhongChen/bizarre-pose-estimator (segmenter)](https://github.com/ShuhongChen/bizarre-pose-estimator)'
 
 
26
 
27
 
28
  def load_sample_image_paths() -> list[pathlib.Path]:
29
- image_dir = pathlib.Path('images')
30
  if not image_dir.exists():
31
- dataset_repo = 'hysts/sample-images-TADNE'
32
- path = huggingface_hub.hf_hub_download(dataset_repo,
33
- 'images.tar.gz',
34
- repo_type='dataset')
35
  with tarfile.open(path) as f:
36
  f.extractall()
37
- return sorted(image_dir.glob('*'))
38
 
39
 
40
- def load_model(
41
- device: torch.device) -> tuple[torch.nn.Module, torch.nn.Module]:
42
- path = huggingface_hub.hf_hub_download(
43
- 'public-data/bizarre-pose-estimator-models', 'segmenter.pth')
44
  ckpt = torch.load(path)
45
 
46
  model = torchvision.models.segmentation.deeplabv3_resnet101()
@@ -62,8 +60,8 @@ def load_model(
62
  nn.LeakyReLU(),
63
  nn.Conv2d(8, 2, kernel_size=1, stride=1),
64
  )
65
- model.load_state_dict(ckpt['model'])
66
- final_head.load_state_dict(ckpt['final_head'])
67
  model.to(device)
68
  model.eval()
69
  final_head.to(device)
@@ -72,20 +70,29 @@ def load_model(
72
 
73
 
74
  @torch.inference_mode()
75
- def predict(image: PIL.Image.Image, score_threshold: float,
76
- transform: Callable, device: torch.device, model: torch.nn.Module,
77
- final_head: torch.nn.Module) -> np.ndarray:
78
- data = ImageWrapper(image).resize_min(256).convert('RGBA').alpha_bg(
79
- 1).convert('RGB').pil()
 
 
 
 
80
  data = torchvision.transforms.functional.to_tensor(data)
81
  data = transform(data)
82
  data = data.to(device).unsqueeze(0)
83
 
84
- out = model(data)['out']
85
- out_fin = final_head(torch.cat([
86
- out,
87
- data,
88
- ], dim=1))
 
 
 
 
 
89
  probs = torch.softmax(out_fin, dim=1)[0]
90
  probs = probs[1] # foreground
91
  probs = PIL.Image.fromarray(probs.cpu().numpy()).resize(image.size)
@@ -103,35 +110,36 @@ def predict(image: PIL.Image.Image, score_threshold: float,
103
  image_paths = load_sample_image_paths()
104
  examples = [[path.as_posix(), 0.5] for path in image_paths]
105
 
106
- device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
107
  model, final_head = load_model(device)
108
  transform = T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
109
 
110
- fn = functools.partial(predict,
111
- transform=transform,
112
- device=device,
113
- model=model,
114
- final_head=final_head)
115
 
116
- with gr.Blocks(css='style.css') as demo:
117
  gr.Markdown(DESCRIPTION)
118
  with gr.Row():
119
  with gr.Column():
120
- image = gr.Image(label='Input', type='pil')
121
- threshold = gr.Slider(label='Score Threshold',
122
- minimum=0,
123
- maximum=1,
124
- step=0.05,
125
- value=0.5)
126
- run_button = gr.Button('Run')
127
  with gr.Column():
128
- result = gr.Image(label='Masked')
129
 
130
  inputs = [image, threshold]
131
- gr.Examples(examples=examples,
132
- inputs=inputs,
133
- outputs=result,
134
- fn=fn,
135
- cache_examples=os.getenv('CACHE_EXAMPLES') == '1')
136
- run_button.click(fn=fn, inputs=inputs, outputs=result, api_name='predict')
137
- demo.queue(max_size=15).launch()
 
 
 
 
 
 
 
 
 
 
18
  import torchvision
19
  import torchvision.transforms as T
20
 
21
+ sys.path.insert(0, "bizarre-pose-estimator")
22
 
23
  from _util.twodee_v0 import I as ImageWrapper
24
 
25
+ DESCRIPTION = (
26
+ "# [ShuhongChen/bizarre-pose-estimator (segmenter)](https://github.com/ShuhongChen/bizarre-pose-estimator)"
27
+ )
28
 
29
 
30
  def load_sample_image_paths() -> list[pathlib.Path]:
31
+ image_dir = pathlib.Path("images")
32
  if not image_dir.exists():
33
+ dataset_repo = "hysts/sample-images-TADNE"
34
+ path = huggingface_hub.hf_hub_download(dataset_repo, "images.tar.gz", repo_type="dataset")
 
 
35
  with tarfile.open(path) as f:
36
  f.extractall()
37
+ return sorted(image_dir.glob("*"))
38
 
39
 
40
+ def load_model(device: torch.device) -> tuple[torch.nn.Module, torch.nn.Module]:
41
+ path = huggingface_hub.hf_hub_download("public-data/bizarre-pose-estimator-models", "segmenter.pth")
 
 
42
  ckpt = torch.load(path)
43
 
44
  model = torchvision.models.segmentation.deeplabv3_resnet101()
 
60
  nn.LeakyReLU(),
61
  nn.Conv2d(8, 2, kernel_size=1, stride=1),
62
  )
63
+ model.load_state_dict(ckpt["model"])
64
+ final_head.load_state_dict(ckpt["final_head"])
65
  model.to(device)
66
  model.eval()
67
  final_head.to(device)
 
70
 
71
 
72
  @torch.inference_mode()
73
+ def predict(
74
+ image: PIL.Image.Image,
75
+ score_threshold: float,
76
+ transform: Callable,
77
+ device: torch.device,
78
+ model: torch.nn.Module,
79
+ final_head: torch.nn.Module,
80
+ ) -> np.ndarray:
81
+ data = ImageWrapper(image).resize_min(256).convert("RGBA").alpha_bg(1).convert("RGB").pil()
82
  data = torchvision.transforms.functional.to_tensor(data)
83
  data = transform(data)
84
  data = data.to(device).unsqueeze(0)
85
 
86
+ out = model(data)["out"]
87
+ out_fin = final_head(
88
+ torch.cat(
89
+ [
90
+ out,
91
+ data,
92
+ ],
93
+ dim=1,
94
+ )
95
+ )
96
  probs = torch.softmax(out_fin, dim=1)[0]
97
  probs = probs[1] # foreground
98
  probs = PIL.Image.fromarray(probs.cpu().numpy()).resize(image.size)
 
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
+ fn = functools.partial(predict, transform=transform, device=device, model=model, final_head=final_head)
 
 
 
 
118
 
119
+ with gr.Blocks(css="style.css") as demo:
120
  gr.Markdown(DESCRIPTION)
121
  with gr.Row():
122
  with gr.Column():
123
+ image = gr.Image(label="Input", type="pil")
124
+ threshold = gr.Slider(label="Score Threshold", minimum=0, maximum=1, step=0.05, value=0.5)
125
+ run_button = gr.Button("Run")
 
 
 
 
126
  with gr.Column():
127
+ result = gr.Image(label="Masked")
128
 
129
  inputs = [image, threshold]
130
+ gr.Examples(
131
+ examples=examples,
132
+ inputs=inputs,
133
+ outputs=result,
134
+ fn=fn,
135
+ cache_examples=os.getenv("CACHE_EXAMPLES") == "1",
136
+ )
137
+ run_button.click(
138
+ fn=fn,
139
+ inputs=inputs,
140
+ outputs=result,
141
+ api_name="predict",
142
+ )
143
+
144
+ if __name__ == "__main__":
145
+ demo.queue(max_size=15).launch()
style.css CHANGED
@@ -1,3 +1,11 @@
1
  h1 {
2
  text-align: center;
 
 
 
 
 
 
 
 
3
  }
 
1
  h1 {
2
  text-align: center;
3
+ display: block;
4
+ }
5
+
6
+ #duplicate-button {
7
+ margin: auto;
8
+ color: #fff;
9
+ background: #1565c0;
10
+ border-radius: 100vh;
11
  }