Spaces:
Running
on
Zero
Running
on
Zero
Update
Browse files- .pre-commit-config.yaml +59 -34
- .vscode/settings.json +30 -0
- README.md +1 -1
- app.py +39 -43
- requirements.txt +2 -2
- 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 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
- repo: https://github.com/pre-commit/mirrors-mypy
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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"]
|
.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: gray
|
5 |
colorTo: red
|
6 |
sdk: gradio
|
7 |
-
sdk_version:
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
---
|
|
|
4 |
colorFrom: gray
|
5 |
colorTo: red
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 4.19.2
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
---
|
app.py
CHANGED
@@ -15,54 +15,51 @@ import numpy as np
|
|
15 |
import PIL.Image
|
16 |
import torch
|
17 |
|
18 |
-
sys.path.insert(0,
|
19 |
|
20 |
from models.yolo import Model
|
21 |
from utils.datasets import letterbox
|
22 |
from utils.general import non_max_suppression, scale_coords
|
23 |
|
24 |
-
DESCRIPTION =
|
25 |
|
26 |
-
MODEL_REPO =
|
27 |
|
28 |
|
29 |
def load_sample_image_paths() -> list[pathlib.Path]:
|
30 |
-
image_dir = pathlib.Path(
|
31 |
if not image_dir.exists():
|
32 |
-
dataset_repo =
|
33 |
-
path = huggingface_hub.hf_hub_download(dataset_repo,
|
34 |
-
'images.tar.gz',
|
35 |
-
repo_type='dataset')
|
36 |
with tarfile.open(path) as f:
|
37 |
f.extractall()
|
38 |
-
return sorted(image_dir.glob(
|
39 |
|
40 |
|
41 |
def load_model(device: torch.device) -> torch.nn.Module:
|
42 |
torch.set_grad_enabled(False)
|
43 |
-
model_path = huggingface_hub.hf_hub_download(MODEL_REPO,
|
44 |
-
|
45 |
-
config_path = huggingface_hub.hf_hub_download(MODEL_REPO, 'yolov5x.yaml')
|
46 |
state_dict = torch.load(model_path)
|
47 |
model = Model(cfg=config_path)
|
48 |
model.load_state_dict(state_dict)
|
49 |
model.to(device)
|
50 |
-
if device.type !=
|
51 |
model.half()
|
52 |
model.eval()
|
53 |
return model
|
54 |
|
55 |
|
56 |
@torch.inference_mode()
|
57 |
-
def predict(
|
58 |
-
|
59 |
-
|
60 |
orig_image = np.asarray(image)
|
61 |
|
62 |
image = letterbox(orig_image, new_shape=640)[0]
|
63 |
data = torch.from_numpy(image.transpose(2, 0, 1)).float() / 255
|
64 |
data = data.to(device).unsqueeze(0)
|
65 |
-
if device.type !=
|
66 |
data = data.half()
|
67 |
|
68 |
preds = model(data)[0]
|
@@ -71,12 +68,10 @@ def predict(image: PIL.Image.Image, score_threshold: float,
|
|
71 |
detections = []
|
72 |
for pred in preds:
|
73 |
if pred is not None and len(pred) > 0:
|
74 |
-
pred[:, :4] = scale_coords(data.shape[2:], pred[:, :4],
|
75 |
-
orig_image.shape).round()
|
76 |
# (x0, y0, x1, y0, conf, class)
|
77 |
detections.append(pred.cpu().numpy())
|
78 |
-
detections = np.concatenate(detections) if detections else np.empty(
|
79 |
-
shape=(0, 6))
|
80 |
|
81 |
res = orig_image.copy()
|
82 |
for det in detections:
|
@@ -88,34 +83,35 @@ def predict(image: PIL.Image.Image, score_threshold: float,
|
|
88 |
image_paths = load_sample_image_paths()
|
89 |
examples = [[path.as_posix(), 0.4, 0.5] for path in image_paths]
|
90 |
|
91 |
-
device = torch.device(
|
92 |
model = load_model(device)
|
93 |
fn = functools.partial(predict, device=device, model=model)
|
94 |
|
95 |
-
with gr.Blocks(css=
|
96 |
gr.Markdown(DESCRIPTION)
|
97 |
with gr.Row():
|
98 |
with gr.Column():
|
99 |
-
image = gr.Image(label=
|
100 |
-
score_threshold = gr.Slider(label=
|
101 |
-
|
102 |
-
|
103 |
-
step=0.05,
|
104 |
-
value=0.4)
|
105 |
-
iou_threshold = gr.Slider(label='IoU Threshold',
|
106 |
-
minimum=0,
|
107 |
-
maximum=1,
|
108 |
-
step=0.05,
|
109 |
-
value=0.5)
|
110 |
-
run_button = gr.Button('Run')
|
111 |
with gr.Column():
|
112 |
-
result = gr.Image(label=
|
113 |
|
114 |
inputs = [image, score_threshold, iou_threshold]
|
115 |
-
gr.Examples(
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
import PIL.Image
|
16 |
import torch
|
17 |
|
18 |
+
sys.path.insert(0, "yolov5_anime")
|
19 |
|
20 |
from models.yolo import Model
|
21 |
from utils.datasets import letterbox
|
22 |
from utils.general import non_max_suppression, scale_coords
|
23 |
|
24 |
+
DESCRIPTION = "# [zymk9/yolov5_anime](https://github.com/zymk9/yolov5_anime)"
|
25 |
|
26 |
+
MODEL_REPO = "public-data/yolov5_anime"
|
27 |
|
28 |
|
29 |
def load_sample_image_paths() -> list[pathlib.Path]:
|
30 |
+
image_dir = pathlib.Path("images")
|
31 |
if not image_dir.exists():
|
32 |
+
dataset_repo = "hysts/sample-images-TADNE"
|
33 |
+
path = huggingface_hub.hf_hub_download(dataset_repo, "images.tar.gz", repo_type="dataset")
|
|
|
|
|
34 |
with tarfile.open(path) as f:
|
35 |
f.extractall()
|
36 |
+
return sorted(image_dir.glob("*"))
|
37 |
|
38 |
|
39 |
def load_model(device: torch.device) -> torch.nn.Module:
|
40 |
torch.set_grad_enabled(False)
|
41 |
+
model_path = huggingface_hub.hf_hub_download(MODEL_REPO, "yolov5x_anime.pth")
|
42 |
+
config_path = huggingface_hub.hf_hub_download(MODEL_REPO, "yolov5x.yaml")
|
|
|
43 |
state_dict = torch.load(model_path)
|
44 |
model = Model(cfg=config_path)
|
45 |
model.load_state_dict(state_dict)
|
46 |
model.to(device)
|
47 |
+
if device.type != "cpu":
|
48 |
model.half()
|
49 |
model.eval()
|
50 |
return model
|
51 |
|
52 |
|
53 |
@torch.inference_mode()
|
54 |
+
def predict(
|
55 |
+
image: PIL.Image.Image, score_threshold: float, iou_threshold: float, device: torch.device, model: torch.nn.Module
|
56 |
+
) -> np.ndarray:
|
57 |
orig_image = np.asarray(image)
|
58 |
|
59 |
image = letterbox(orig_image, new_shape=640)[0]
|
60 |
data = torch.from_numpy(image.transpose(2, 0, 1)).float() / 255
|
61 |
data = data.to(device).unsqueeze(0)
|
62 |
+
if device.type != "cpu":
|
63 |
data = data.half()
|
64 |
|
65 |
preds = model(data)[0]
|
|
|
68 |
detections = []
|
69 |
for pred in preds:
|
70 |
if pred is not None and len(pred) > 0:
|
71 |
+
pred[:, :4] = scale_coords(data.shape[2:], pred[:, :4], orig_image.shape).round()
|
|
|
72 |
# (x0, y0, x1, y0, conf, class)
|
73 |
detections.append(pred.cpu().numpy())
|
74 |
+
detections = np.concatenate(detections) if detections else np.empty(shape=(0, 6))
|
|
|
75 |
|
76 |
res = orig_image.copy()
|
77 |
for det in detections:
|
|
|
83 |
image_paths = load_sample_image_paths()
|
84 |
examples = [[path.as_posix(), 0.4, 0.5] for path in image_paths]
|
85 |
|
86 |
+
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
87 |
model = load_model(device)
|
88 |
fn = functools.partial(predict, device=device, model=model)
|
89 |
|
90 |
+
with gr.Blocks(css="style.css") as demo:
|
91 |
gr.Markdown(DESCRIPTION)
|
92 |
with gr.Row():
|
93 |
with gr.Column():
|
94 |
+
image = gr.Image(label="Input", type="pil")
|
95 |
+
score_threshold = gr.Slider(label="Score Threshold", minimum=0, maximum=1, step=0.05, value=0.4)
|
96 |
+
iou_threshold = gr.Slider(label="IoU Threshold", minimum=0, maximum=1, step=0.05, value=0.5)
|
97 |
+
run_button = gr.Button("Run")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
with gr.Column():
|
99 |
+
result = gr.Image(label="Output")
|
100 |
|
101 |
inputs = [image, score_threshold, iou_threshold]
|
102 |
+
gr.Examples(
|
103 |
+
examples=examples,
|
104 |
+
inputs=inputs,
|
105 |
+
outputs=result,
|
106 |
+
fn=fn,
|
107 |
+
cache_examples=os.getenv("CACHE_EXAMPLES") == "1",
|
108 |
+
)
|
109 |
+
run_button.click(
|
110 |
+
fn=fn,
|
111 |
+
inputs=inputs,
|
112 |
+
outputs=result,
|
113 |
+
api_name="predict",
|
114 |
+
)
|
115 |
+
|
116 |
+
if __name__ == "__main__":
|
117 |
+
demo.queue(max_size=15).launch()
|
requirements.txt
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
opencv-python-headless==4.
|
2 |
-
scipy==1.
|
3 |
torch==2.0.1
|
4 |
torchvision==0.15.2
|
|
|
1 |
+
opencv-python-headless==4.9.0.80
|
2 |
+
scipy==1.12.0
|
3 |
torch==2.0.1
|
4 |
torchvision==0.15.2
|
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 |
}
|