Update
Browse files- .pre-commit-config.yaml +59 -35
- .style.yapf +0 -5
- .vscode/settings.json +30 -0
- README.md +1 -1
- app.py +54 -70
- model.py +25 -35
- requirements.txt +6 -5
- style.css +6 -6
.pre-commit-config.yaml
CHANGED
@@ -1,36 +1,60 @@
|
|
1 |
-
exclude: ^stylegan3
|
2 |
repos:
|
3 |
-
- repo: https://github.com/pre-commit/pre-commit-hooks
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
- repo: https://github.com/pre-commit/mirrors-mypy
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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: red
|
5 |
colorTo: blue
|
6 |
sdk: gradio
|
7 |
-
sdk_version:
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
---
|
|
|
4 |
colorFrom: red
|
5 |
colorTo: blue
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 4.20.0
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
---
|
app.py
CHANGED
@@ -7,99 +7,83 @@ import numpy as np
|
|
7 |
|
8 |
from model import Model
|
9 |
|
10 |
-
|
11 |
-
DESCRIPTION = '''# StyleGAN3
|
12 |
-
|
13 |
-
This is an unofficial demo for [https://github.com/NVlabs/stylegan3](https://github.com/NVlabs/stylegan3).
|
14 |
-
'''
|
15 |
|
16 |
|
17 |
def get_sample_image_url(name: str) -> str:
|
18 |
-
sample_image_dir =
|
19 |
-
return f
|
20 |
|
21 |
|
22 |
def get_sample_image_markdown(name: str) -> str:
|
23 |
url = get_sample_image_url(name)
|
24 |
-
size = 512 if name ==
|
25 |
-
seed =
|
26 |
-
return f
|
27 |
- size: {size}x{size}
|
28 |
- seed: {seed}
|
29 |
- truncation: 0.7
|
30 |
-

|
31 |
|
32 |
|
33 |
model = Model()
|
34 |
|
35 |
-
with gr.Blocks(css=
|
36 |
gr.Markdown(DESCRIPTION)
|
37 |
|
38 |
with gr.Tabs():
|
39 |
-
with gr.TabItem(
|
40 |
with gr.Row():
|
41 |
with gr.Column():
|
42 |
-
model_name = gr.Dropdown(
|
43 |
-
model.MODEL_NAME_DICT.keys()),
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
psi = gr.Slider(0,
|
52 |
-
2,
|
53 |
-
step=0.05,
|
54 |
-
value=0.7,
|
55 |
-
label='Truncation psi')
|
56 |
-
tx = gr.Slider(-1,
|
57 |
-
1,
|
58 |
-
step=0.05,
|
59 |
-
value=0,
|
60 |
-
label='Translate X')
|
61 |
-
ty = gr.Slider(-1,
|
62 |
-
1,
|
63 |
-
step=0.05,
|
64 |
-
value=0,
|
65 |
-
label='Translate Y')
|
66 |
-
angle = gr.Slider(-180,
|
67 |
-
180,
|
68 |
-
step=5,
|
69 |
-
value=0,
|
70 |
-
label='Angle')
|
71 |
-
run_button = gr.Button('Run')
|
72 |
with gr.Column():
|
73 |
-
result = gr.Image(label=
|
74 |
|
75 |
-
with gr.TabItem(
|
76 |
with gr.Row():
|
77 |
-
model_name2 = gr.Dropdown(
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
|
|
|
|
86 |
with gr.Row():
|
87 |
text = get_sample_image_markdown(model_name2.value)
|
88 |
sample_images = gr.Markdown(text)
|
89 |
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
from model import Model
|
9 |
|
10 |
+
DESCRIPTION = "# [StyleGAN3](https://github.com/NVlabs/stylegan3)"
|
|
|
|
|
|
|
|
|
11 |
|
12 |
|
13 |
def get_sample_image_url(name: str) -> str:
|
14 |
+
sample_image_dir = "https://huggingface.co/spaces/hysts/StyleGAN3/resolve/main/samples"
|
15 |
+
return f"{sample_image_dir}/{name}.jpg"
|
16 |
|
17 |
|
18 |
def get_sample_image_markdown(name: str) -> str:
|
19 |
url = get_sample_image_url(name)
|
20 |
+
size = 512 if name == "afhqv2" else 1024
|
21 |
+
seed = "0-99"
|
22 |
+
return f"""
|
23 |
- size: {size}x{size}
|
24 |
- seed: {seed}
|
25 |
- truncation: 0.7
|
26 |
+
"""
|
27 |
|
28 |
|
29 |
model = Model()
|
30 |
|
31 |
+
with gr.Blocks(css="style.css") as demo:
|
32 |
gr.Markdown(DESCRIPTION)
|
33 |
|
34 |
with gr.Tabs():
|
35 |
+
with gr.TabItem("App"):
|
36 |
with gr.Row():
|
37 |
with gr.Column():
|
38 |
+
model_name = gr.Dropdown(
|
39 |
+
label="Model", choices=list(model.MODEL_NAME_DICT.keys()), value="FFHQ-1024-R"
|
40 |
+
)
|
41 |
+
seed = gr.Slider(label="Seed", minimum=0, maximum=np.iinfo(np.uint32).max, step=1, value=0)
|
42 |
+
psi = gr.Slider(label="Truncation psi", minimum=0, maximum=2, step=0.05, value=0.7)
|
43 |
+
tx = gr.Slider(label="Translate X", minimum=-1, maximum=1, step=0.05, value=0)
|
44 |
+
ty = gr.Slider(label="Translate Y", minimum=-1, maximum=1, step=0.05, value=0)
|
45 |
+
angle = gr.Slider(label="Angle", minimum=-180, maximum=180, step=5, value=0)
|
46 |
+
run_button = gr.Button()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
with gr.Column():
|
48 |
+
result = gr.Image(label="Result", elem_id="result")
|
49 |
|
50 |
+
with gr.TabItem("Sample Images"):
|
51 |
with gr.Row():
|
52 |
+
model_name2 = gr.Dropdown(
|
53 |
+
[
|
54 |
+
"afhqv2",
|
55 |
+
"ffhq",
|
56 |
+
"ffhq-u",
|
57 |
+
"metfaces",
|
58 |
+
"metfaces-u",
|
59 |
+
],
|
60 |
+
value="afhqv2",
|
61 |
+
label="Model",
|
62 |
+
)
|
63 |
with gr.Row():
|
64 |
text = get_sample_image_markdown(model_name2.value)
|
65 |
sample_images = gr.Markdown(text)
|
66 |
|
67 |
+
run_button.click(
|
68 |
+
fn=model.set_model_and_generate_image,
|
69 |
+
inputs=[
|
70 |
+
model_name,
|
71 |
+
seed,
|
72 |
+
psi,
|
73 |
+
tx,
|
74 |
+
ty,
|
75 |
+
angle,
|
76 |
+
],
|
77 |
+
outputs=result,
|
78 |
+
api_name="run",
|
79 |
+
)
|
80 |
+
model_name2.change(
|
81 |
+
fn=get_sample_image_markdown,
|
82 |
+
inputs=model_name2,
|
83 |
+
outputs=sample_images,
|
84 |
+
queue=False,
|
85 |
+
api_name=False,
|
86 |
+
)
|
87 |
+
|
88 |
+
if __name__ == "__main__":
|
89 |
+
demo.queue(max_size=20).launch()
|
model.py
CHANGED
@@ -1,6 +1,5 @@
|
|
1 |
from __future__ import annotations
|
2 |
|
3 |
-
import os
|
4 |
import pathlib
|
5 |
import pickle
|
6 |
import sys
|
@@ -11,42 +10,37 @@ import torch.nn as nn
|
|
11 |
from huggingface_hub import hf_hub_download
|
12 |
|
13 |
current_dir = pathlib.Path(__file__).parent
|
14 |
-
submodule_dir = current_dir /
|
15 |
sys.path.insert(0, submodule_dir.as_posix())
|
16 |
|
17 |
-
HF_TOKEN = os.getenv('HF_TOKEN')
|
18 |
-
|
19 |
|
20 |
class Model:
|
21 |
MODEL_NAME_DICT = {
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
}
|
35 |
|
36 |
def __init__(self):
|
37 |
-
self.device = torch.device(
|
38 |
-
'cuda:0' if torch.cuda.is_available() else 'cpu')
|
39 |
self._download_all_models()
|
40 |
-
self.model_name =
|
41 |
self.model = self._load_model(self.model_name)
|
42 |
|
43 |
def _load_model(self, model_name: str) -> nn.Module:
|
44 |
file_name = self.MODEL_NAME_DICT[model_name]
|
45 |
-
path = hf_hub_download(
|
46 |
-
|
47 |
-
|
48 |
-
with open(path, 'rb') as f:
|
49 |
-
model = pickle.load(f)['G_ema']
|
50 |
model.eval()
|
51 |
model.to(self.device)
|
52 |
return model
|
@@ -62,8 +56,7 @@ class Model:
|
|
62 |
self._load_model(name)
|
63 |
|
64 |
@staticmethod
|
65 |
-
def make_transform(translate: tuple[float, float],
|
66 |
-
angle: float) -> np.ndarray:
|
67 |
mat = np.eye(3)
|
68 |
sin = np.sin(angle / 360 * np.pi * 2)
|
69 |
cos = np.cos(angle / 360 * np.pi * 2)
|
@@ -81,8 +74,7 @@ class Model:
|
|
81 |
return torch.from_numpy(z).float().to(self.device)
|
82 |
|
83 |
def postprocess(self, tensor: torch.Tensor) -> np.ndarray:
|
84 |
-
tensor = (tensor.permute(0, 2, 3, 1) * 127.5 + 128).clamp(0, 255).to(
|
85 |
-
torch.uint8)
|
86 |
return tensor.cpu().numpy()
|
87 |
|
88 |
def set_transform(self, tx: float, ty: float, angle: float) -> None:
|
@@ -91,12 +83,10 @@ class Model:
|
|
91 |
self.model.synthesis.input.transform.copy_(torch.from_numpy(mat))
|
92 |
|
93 |
@torch.inference_mode()
|
94 |
-
def generate(self, z: torch.Tensor, label: torch.Tensor,
|
95 |
-
truncation_psi: float) -> torch.Tensor:
|
96 |
return self.model(z, label, truncation_psi=truncation_psi)
|
97 |
|
98 |
-
def generate_image(self, seed: int, truncation_psi: float, tx: float,
|
99 |
-
ty: float, angle: float) -> np.ndarray:
|
100 |
self.set_transform(tx, ty, angle)
|
101 |
|
102 |
z = self.generate_z(seed)
|
@@ -106,8 +96,8 @@ class Model:
|
|
106 |
out = self.postprocess(out)
|
107 |
return out[0]
|
108 |
|
109 |
-
def set_model_and_generate_image(
|
110 |
-
|
111 |
-
|
112 |
self.set_model(model_name)
|
113 |
return self.generate_image(seed, truncation_psi, tx, ty, angle)
|
|
|
1 |
from __future__ import annotations
|
2 |
|
|
|
3 |
import pathlib
|
4 |
import pickle
|
5 |
import sys
|
|
|
10 |
from huggingface_hub import hf_hub_download
|
11 |
|
12 |
current_dir = pathlib.Path(__file__).parent
|
13 |
+
submodule_dir = current_dir / "stylegan3"
|
14 |
sys.path.insert(0, submodule_dir.as_posix())
|
15 |
|
|
|
|
|
16 |
|
17 |
class Model:
|
18 |
MODEL_NAME_DICT = {
|
19 |
+
"AFHQv2-512-R": "stylegan3-r-afhqv2-512x512.pkl",
|
20 |
+
"FFHQ-1024-R": "stylegan3-r-ffhq-1024x1024.pkl",
|
21 |
+
"FFHQ-U-256-R": "stylegan3-r-ffhqu-256x256.pkl",
|
22 |
+
"FFHQ-U-1024-R": "stylegan3-r-ffhqu-1024x1024.pkl",
|
23 |
+
"MetFaces-1024-R": "stylegan3-r-metfaces-1024x1024.pkl",
|
24 |
+
"MetFaces-U-1024-R": "stylegan3-r-metfacesu-1024x1024.pkl",
|
25 |
+
"AFHQv2-512-T": "stylegan3-t-afhqv2-512x512.pkl",
|
26 |
+
"FFHQ-1024-T": "stylegan3-t-ffhq-1024x1024.pkl",
|
27 |
+
"FFHQ-U-256-T": "stylegan3-t-ffhqu-256x256.pkl",
|
28 |
+
"FFHQ-U-1024-T": "stylegan3-t-ffhqu-1024x1024.pkl",
|
29 |
+
"MetFaces-1024-T": "stylegan3-t-metfaces-1024x1024.pkl",
|
30 |
+
"MetFaces-U-1024-T": "stylegan3-t-metfacesu-1024x1024.pkl",
|
31 |
}
|
32 |
|
33 |
def __init__(self):
|
34 |
+
self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
|
|
35 |
self._download_all_models()
|
36 |
+
self.model_name = "FFHQ-1024-R"
|
37 |
self.model = self._load_model(self.model_name)
|
38 |
|
39 |
def _load_model(self, model_name: str) -> nn.Module:
|
40 |
file_name = self.MODEL_NAME_DICT[model_name]
|
41 |
+
path = hf_hub_download("hysts/StyleGAN3", f"models/{file_name}")
|
42 |
+
with open(path, "rb") as f:
|
43 |
+
model = pickle.load(f)["G_ema"]
|
|
|
|
|
44 |
model.eval()
|
45 |
model.to(self.device)
|
46 |
return model
|
|
|
56 |
self._load_model(name)
|
57 |
|
58 |
@staticmethod
|
59 |
+
def make_transform(translate: tuple[float, float], angle: float) -> np.ndarray:
|
|
|
60 |
mat = np.eye(3)
|
61 |
sin = np.sin(angle / 360 * np.pi * 2)
|
62 |
cos = np.cos(angle / 360 * np.pi * 2)
|
|
|
74 |
return torch.from_numpy(z).float().to(self.device)
|
75 |
|
76 |
def postprocess(self, tensor: torch.Tensor) -> np.ndarray:
|
77 |
+
tensor = (tensor.permute(0, 2, 3, 1) * 127.5 + 128).clamp(0, 255).to(torch.uint8)
|
|
|
78 |
return tensor.cpu().numpy()
|
79 |
|
80 |
def set_transform(self, tx: float, ty: float, angle: float) -> None:
|
|
|
83 |
self.model.synthesis.input.transform.copy_(torch.from_numpy(mat))
|
84 |
|
85 |
@torch.inference_mode()
|
86 |
+
def generate(self, z: torch.Tensor, label: torch.Tensor, truncation_psi: float) -> torch.Tensor:
|
|
|
87 |
return self.model(z, label, truncation_psi=truncation_psi)
|
88 |
|
89 |
+
def generate_image(self, seed: int, truncation_psi: float, tx: float, ty: float, angle: float) -> np.ndarray:
|
|
|
90 |
self.set_transform(tx, ty, angle)
|
91 |
|
92 |
z = self.generate_z(seed)
|
|
|
96 |
out = self.postprocess(out)
|
97 |
return out[0]
|
98 |
|
99 |
+
def set_model_and_generate_image(
|
100 |
+
self, model_name: str, seed: int, truncation_psi: float, tx: float, ty: float, angle: float
|
101 |
+
) -> np.ndarray:
|
102 |
self.set_model(model_name)
|
103 |
return self.generate_image(seed, truncation_psi, tx, ty, angle)
|
requirements.txt
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
|
|
|
1 |
+
gradio==4.20.0
|
2 |
+
numpy==1.26.4
|
3 |
+
Pillow==10.2.0
|
4 |
+
scipy==1.12.0
|
5 |
+
torch==2.0.1
|
6 |
+
torchvision==0.15.2
|
style.css
CHANGED
@@ -1,11 +1,11 @@
|
|
1 |
h1 {
|
2 |
text-align: center;
|
3 |
-
}
|
4 |
-
div#result {
|
5 |
-
max-width: 600px;
|
6 |
-
max-height: 600px;
|
7 |
-
}
|
8 |
-
img#visitor-badge {
|
9 |
display: block;
|
|
|
|
|
|
|
10 |
margin: auto;
|
|
|
|
|
|
|
11 |
}
|
|
|
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 |
}
|