Update
Browse files- .pre-commit-config.yaml +59 -35
- .vscode/settings.json +30 -0
- README.md +1 -1
- app.py +108 -106
- model.py +27 -36
- 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"]
|
.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: purple
|
5 |
colorTo: gray
|
6 |
sdk: gradio
|
7 |
-
sdk_version:
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
---
|
|
|
4 |
colorFrom: purple
|
5 |
colorTo: gray
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 4.20.0
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
---
|
app.py
CHANGED
@@ -9,68 +9,65 @@ import numpy as np
|
|
9 |
|
10 |
from model import Model
|
11 |
|
12 |
-
DESCRIPTION =
|
13 |
-
|
14 |
-
This is an unofficial demo for [https://github.com/NVlabs/stylegan3](https://github.com/NVlabs/stylegan3).
|
15 |
-
'''
|
16 |
|
17 |
|
18 |
def update_class_index(name: str) -> dict:
|
19 |
-
if name ==
|
20 |
-
return gr.Slider
|
21 |
else:
|
22 |
-
return gr.Slider
|
23 |
|
24 |
|
25 |
def get_sample_image_url(name: str) -> str:
|
26 |
-
sample_image_dir =
|
27 |
-
return f
|
28 |
|
29 |
|
30 |
def get_sample_image_markdown(name: str) -> str:
|
31 |
url = get_sample_image_url(name)
|
32 |
-
if name ==
|
33 |
size = 32
|
34 |
-
class_index =
|
35 |
-
seed =
|
36 |
else:
|
37 |
-
class_index =
|
38 |
-
seed =
|
39 |
-
if name ==
|
40 |
size = 512
|
41 |
-
elif name ==
|
42 |
size = 512
|
43 |
-
elif name ==
|
44 |
size = 512
|
45 |
-
elif name ==
|
46 |
size = 512
|
47 |
-
elif name ==
|
48 |
size = 256
|
49 |
-
elif name ==
|
50 |
size = 1024
|
51 |
-
elif name ==
|
52 |
size = 1024
|
53 |
-
elif name ==
|
54 |
size = 1024
|
55 |
-
elif name ==
|
56 |
size = 256
|
57 |
-
elif name ==
|
58 |
size = 1024
|
59 |
-
elif name ==
|
60 |
size = 1024
|
61 |
else:
|
62 |
raise ValueError
|
63 |
|
64 |
-
return f
|
65 |
- size: {size}x{size}
|
66 |
- class_index: {class_index}
|
67 |
- seed: {seed}
|
68 |
- truncation: 0.7
|
69 |
-

|
70 |
|
71 |
|
72 |
def load_class_names(name: str) -> list[str]:
|
73 |
-
with open(f
|
74 |
names = json.load(f)
|
75 |
return names
|
76 |
|
@@ -80,103 +77,108 @@ def get_class_name_df(name: str) -> list:
|
|
80 |
return list(map(list, enumerate(names))) # type: ignore
|
81 |
|
82 |
|
83 |
-
CIFAR10_NAMES = load_class_names(
|
84 |
|
85 |
|
86 |
def update_class_name(model_name: str, index: int) -> dict:
|
87 |
-
if model_name ==
|
88 |
value = CIFAR10_NAMES[index]
|
89 |
-
return gr.Textbox
|
90 |
else:
|
91 |
-
return gr.Textbox
|
92 |
|
93 |
|
94 |
model = Model()
|
95 |
|
96 |
-
with gr.Blocks(css=
|
97 |
gr.Markdown(DESCRIPTION)
|
98 |
|
99 |
with gr.Tabs():
|
100 |
-
with gr.TabItem(
|
101 |
with gr.Row():
|
102 |
with gr.Column():
|
103 |
-
model_name = gr.Dropdown(
|
104 |
-
model.MODEL_NAME_DICT.keys()),
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
step=1,
|
110 |
-
value=0,
|
111 |
-
label='Seed')
|
112 |
-
psi = gr.Slider(0,
|
113 |
-
2,
|
114 |
-
step=0.05,
|
115 |
-
value=0.7,
|
116 |
-
label='Truncation psi')
|
117 |
-
class_index = gr.Slider(0,
|
118 |
-
9,
|
119 |
-
step=1,
|
120 |
-
value=0,
|
121 |
-
label='Class Index',
|
122 |
-
visible=False)
|
123 |
class_name = gr.Textbox(
|
124 |
-
value=CIFAR10_NAMES[class_index.value],
|
125 |
-
|
126 |
-
|
127 |
-
visible=False)
|
128 |
-
run_button = gr.Button('Run')
|
129 |
with gr.Column():
|
130 |
-
result = gr.Image(label=
|
131 |
|
132 |
-
with gr.TabItem(
|
133 |
with gr.Row():
|
134 |
-
model_name2 = gr.Dropdown(
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
|
|
|
|
150 |
with gr.Row():
|
151 |
text = get_sample_image_markdown(model_name2.value)
|
152 |
sample_images = gr.Markdown(text)
|
153 |
|
154 |
-
model_name.change(
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
from model import Model
|
11 |
|
12 |
+
DESCRIPTION = "# [StyleGAN2](https://github.com/NVlabs/stylegan3)"
|
|
|
|
|
|
|
13 |
|
14 |
|
15 |
def update_class_index(name: str) -> dict:
|
16 |
+
if name == "CIFAR-10":
|
17 |
+
return gr.Slider(maximum=9, visible=True)
|
18 |
else:
|
19 |
+
return gr.Slider(visible=False)
|
20 |
|
21 |
|
22 |
def get_sample_image_url(name: str) -> str:
|
23 |
+
sample_image_dir = "https://huggingface.co/spaces/hysts/StyleGAN2/resolve/main/samples"
|
24 |
+
return f"{sample_image_dir}/{name}.jpg"
|
25 |
|
26 |
|
27 |
def get_sample_image_markdown(name: str) -> str:
|
28 |
url = get_sample_image_url(name)
|
29 |
+
if name == "cifar10":
|
30 |
size = 32
|
31 |
+
class_index = "0-9"
|
32 |
+
seed = "0-9"
|
33 |
else:
|
34 |
+
class_index = "N/A"
|
35 |
+
seed = "0-99"
|
36 |
+
if name == "afhq-cat":
|
37 |
size = 512
|
38 |
+
elif name == "afhq-dog":
|
39 |
size = 512
|
40 |
+
elif name == "afhq-wild":
|
41 |
size = 512
|
42 |
+
elif name == "afhqv2":
|
43 |
size = 512
|
44 |
+
elif name == "brecahad":
|
45 |
size = 256
|
46 |
+
elif name == "celebahq":
|
47 |
size = 1024
|
48 |
+
elif name == "ffhq":
|
49 |
size = 1024
|
50 |
+
elif name == "ffhq-u":
|
51 |
size = 1024
|
52 |
+
elif name == "lsun-dog":
|
53 |
size = 256
|
54 |
+
elif name == "metfaces":
|
55 |
size = 1024
|
56 |
+
elif name == "metfaces-u":
|
57 |
size = 1024
|
58 |
else:
|
59 |
raise ValueError
|
60 |
|
61 |
+
return f"""
|
62 |
- size: {size}x{size}
|
63 |
- class_index: {class_index}
|
64 |
- seed: {seed}
|
65 |
- truncation: 0.7
|
66 |
+
"""
|
67 |
|
68 |
|
69 |
def load_class_names(name: str) -> list[str]:
|
70 |
+
with open(f"labels/{name}_classes.json") as f:
|
71 |
names = json.load(f)
|
72 |
return names
|
73 |
|
|
|
77 |
return list(map(list, enumerate(names))) # type: ignore
|
78 |
|
79 |
|
80 |
+
CIFAR10_NAMES = load_class_names("cifar10")
|
81 |
|
82 |
|
83 |
def update_class_name(model_name: str, index: int) -> dict:
|
84 |
+
if model_name == "CIFAR-10":
|
85 |
value = CIFAR10_NAMES[index]
|
86 |
+
return gr.Textbox(value=value, visible=True)
|
87 |
else:
|
88 |
+
return gr.Textbox(visible=False)
|
89 |
|
90 |
|
91 |
model = Model()
|
92 |
|
93 |
+
with gr.Blocks(css="style.css") as demo:
|
94 |
gr.Markdown(DESCRIPTION)
|
95 |
|
96 |
with gr.Tabs():
|
97 |
+
with gr.TabItem("App"):
|
98 |
with gr.Row():
|
99 |
with gr.Column():
|
100 |
+
model_name = gr.Dropdown(
|
101 |
+
label="Model", choices=list(model.MODEL_NAME_DICT.keys()), value="FFHQ-1024"
|
102 |
+
)
|
103 |
+
seed = gr.Slider(label="Seed", minimum=0, maximum=np.iinfo(np.uint32).max, step=1, value=0)
|
104 |
+
psi = gr.Slider(label="Truncation psi", minimum=0, maximum=2, step=0.05, value=0.7)
|
105 |
+
class_index = gr.Slider(label="Class Index", minimum=0, maximum=9, step=1, value=0, visible=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
class_name = gr.Textbox(
|
107 |
+
label="Class Label", value=CIFAR10_NAMES[class_index.value], interactive=False, visible=False
|
108 |
+
)
|
109 |
+
run_button = gr.Button()
|
|
|
|
|
110 |
with gr.Column():
|
111 |
+
result = gr.Image(label="Result")
|
112 |
|
113 |
+
with gr.TabItem("Sample Images"):
|
114 |
with gr.Row():
|
115 |
+
model_name2 = gr.Dropdown(
|
116 |
+
label="Model",
|
117 |
+
choices=[
|
118 |
+
"afhq-cat",
|
119 |
+
"afhq-dog",
|
120 |
+
"afhq-wild",
|
121 |
+
"afhqv2",
|
122 |
+
"brecahad",
|
123 |
+
"celebahq",
|
124 |
+
"cifar10",
|
125 |
+
"ffhq",
|
126 |
+
"ffhq-u",
|
127 |
+
"lsun-dog",
|
128 |
+
"metfaces",
|
129 |
+
"metfaces-u",
|
130 |
+
],
|
131 |
+
value="afhq-cat",
|
132 |
+
)
|
133 |
with gr.Row():
|
134 |
text = get_sample_image_markdown(model_name2.value)
|
135 |
sample_images = gr.Markdown(text)
|
136 |
|
137 |
+
model_name.change(
|
138 |
+
fn=update_class_index,
|
139 |
+
inputs=model_name,
|
140 |
+
outputs=class_index,
|
141 |
+
queue=False,
|
142 |
+
api_name=False,
|
143 |
+
)
|
144 |
+
model_name.change(
|
145 |
+
fn=update_class_name,
|
146 |
+
inputs=[
|
147 |
+
model_name,
|
148 |
+
class_index,
|
149 |
+
],
|
150 |
+
outputs=class_name,
|
151 |
+
queue=False,
|
152 |
+
api_name=False,
|
153 |
+
)
|
154 |
+
class_index.change(
|
155 |
+
fn=update_class_name,
|
156 |
+
inputs=[
|
157 |
+
model_name,
|
158 |
+
class_index,
|
159 |
+
],
|
160 |
+
outputs=class_name,
|
161 |
+
queue=False,
|
162 |
+
api_name=False,
|
163 |
+
)
|
164 |
+
run_button.click(
|
165 |
+
fn=model.set_model_and_generate_image,
|
166 |
+
inputs=[
|
167 |
+
model_name,
|
168 |
+
seed,
|
169 |
+
psi,
|
170 |
+
class_index,
|
171 |
+
],
|
172 |
+
outputs=result,
|
173 |
+
api_name="run",
|
174 |
+
)
|
175 |
+
model_name2.change(
|
176 |
+
fn=get_sample_image_markdown,
|
177 |
+
inputs=model_name2,
|
178 |
+
outputs=sample_images,
|
179 |
+
queue=False,
|
180 |
+
api_name=False,
|
181 |
+
)
|
182 |
+
|
183 |
+
if __name__ == "__main__":
|
184 |
+
demo.queue(max_size=10).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,45 +10,40 @@ 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 |
-
|
37 |
}
|
38 |
|
39 |
def __init__(self):
|
40 |
-
self.device = torch.device(
|
41 |
-
'cuda:0' if torch.cuda.is_available() else 'cpu')
|
42 |
self._download_all_models()
|
43 |
-
self.model_name =
|
44 |
self.model = self._load_model(self.model_name)
|
45 |
|
46 |
def _load_model(self, model_name: str) -> nn.Module:
|
47 |
file_name = self.MODEL_NAME_DICT[model_name]
|
48 |
-
path = hf_hub_download(
|
49 |
-
|
50 |
-
|
51 |
-
with open(path, 'rb') as f:
|
52 |
-
model = pickle.load(f)['G_ema']
|
53 |
model.eval()
|
54 |
model.to(self.device)
|
55 |
return model
|
@@ -70,8 +64,7 @@ class Model:
|
|
70 |
return torch.from_numpy(z).float().to(self.device)
|
71 |
|
72 |
def postprocess(self, tensor: torch.Tensor) -> np.ndarray:
|
73 |
-
tensor = (tensor.permute(0, 2, 3, 1) * 127.5 + 128).clamp(0, 255).to(
|
74 |
-
torch.uint8)
|
75 |
return tensor.cpu().numpy()
|
76 |
|
77 |
def make_label_tensor(self, class_index: int) -> torch.Tensor:
|
@@ -85,12 +78,10 @@ class Model:
|
|
85 |
return label
|
86 |
|
87 |
@torch.inference_mode()
|
88 |
-
def generate(self, z: torch.Tensor, label: torch.Tensor,
|
89 |
-
truncation_psi: float) -> torch.Tensor:
|
90 |
return self.model(z, label, truncation_psi=truncation_psi)
|
91 |
|
92 |
-
def generate_image(self, seed: int, truncation_psi: float,
|
93 |
-
class_index: int) -> np.ndarray:
|
94 |
z = self.generate_z(seed)
|
95 |
label = self.make_label_tensor(class_index)
|
96 |
|
@@ -98,8 +89,8 @@ class Model:
|
|
98 |
out = self.postprocess(out)
|
99 |
return out[0]
|
100 |
|
101 |
-
def set_model_and_generate_image(
|
102 |
-
|
103 |
-
|
104 |
self.set_model(model_name)
|
105 |
return self.generate_image(seed, truncation_psi, class_index)
|
|
|
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 |
+
"AFHQ-Cat-512": "stylegan2-afhqcat-512x512.pkl",
|
20 |
+
"AFHQ-Dog-512": "stylegan2-afhqdog-512x512.pkl",
|
21 |
+
"AFHQv2-512": "stylegan2-afhqv2-512x512.pkl",
|
22 |
+
"AFHQ-Wild-512": "stylegan2-afhqwild-512x512.pkl",
|
23 |
+
"BreCaHAD-512": "stylegan2-brecahad-512x512.pkl",
|
24 |
+
"CelebA-HQ-256": "stylegan2-celebahq-256x256.pkl",
|
25 |
+
"CIFAR-10": "stylegan2-cifar10-32x32.pkl",
|
26 |
+
"FFHQ-256": "stylegan2-ffhq-256x256.pkl",
|
27 |
+
"FFHQ-512": "stylegan2-ffhq-512x512.pkl",
|
28 |
+
"FFHQ-1024": "stylegan2-ffhq-1024x1024.pkl",
|
29 |
+
"FFHQ-U-256": "stylegan2-ffhqu-256x256.pkl",
|
30 |
+
"FFHQ-U-1024": "stylegan2-ffhqu-1024x1024.pkl",
|
31 |
+
"LSUN-Dog-256": "stylegan2-lsundog-256x256.pkl",
|
32 |
+
"MetFaces-1024": "stylegan2-metfaces-1024x1024.pkl",
|
33 |
+
"MetFaces-U-1024": "stylegan2-metfacesu-1024x1024.pkl",
|
34 |
}
|
35 |
|
36 |
def __init__(self):
|
37 |
+
self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
|
|
38 |
self._download_all_models()
|
39 |
+
self.model_name = "FFHQ-1024"
|
40 |
self.model = self._load_model(self.model_name)
|
41 |
|
42 |
def _load_model(self, model_name: str) -> nn.Module:
|
43 |
file_name = self.MODEL_NAME_DICT[model_name]
|
44 |
+
path = hf_hub_download("hysts/StyleGAN2", f"models/{file_name}")
|
45 |
+
with open(path, "rb") as f:
|
46 |
+
model = pickle.load(f)["G_ema"]
|
|
|
|
|
47 |
model.eval()
|
48 |
model.to(self.device)
|
49 |
return model
|
|
|
64 |
return torch.from_numpy(z).float().to(self.device)
|
65 |
|
66 |
def postprocess(self, tensor: torch.Tensor) -> np.ndarray:
|
67 |
+
tensor = (tensor.permute(0, 2, 3, 1) * 127.5 + 128).clamp(0, 255).to(torch.uint8)
|
|
|
68 |
return tensor.cpu().numpy()
|
69 |
|
70 |
def make_label_tensor(self, class_index: int) -> torch.Tensor:
|
|
|
78 |
return label
|
79 |
|
80 |
@torch.inference_mode()
|
81 |
+
def generate(self, z: torch.Tensor, label: torch.Tensor, truncation_psi: float) -> torch.Tensor:
|
|
|
82 |
return self.model(z, label, truncation_psi=truncation_psi)
|
83 |
|
84 |
+
def generate_image(self, seed: int, truncation_psi: float, class_index: int) -> np.ndarray:
|
|
|
85 |
z = self.generate_z(seed)
|
86 |
label = self.make_label_tensor(class_index)
|
87 |
|
|
|
89 |
out = self.postprocess(out)
|
90 |
return out[0]
|
91 |
|
92 |
+
def set_model_and_generate_image(
|
93 |
+
self, model_name: str, seed: int, truncation_psi: float, class_index: int
|
94 |
+
) -> np.ndarray:
|
95 |
self.set_model(model_name)
|
96 |
return self.generate_image(seed, truncation_psi, class_index)
|
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 |
}
|