Spaces:
Running
Running
Update
Browse files- .pre-commit-config.yaml +59 -36
- .style.yapf +0 -5
- .vscode/settings.json +30 -0
- README.md +1 -1
- app.py +126 -129
- model.py +22 -29
- requirements.txt +5 -4
- style.css +6 -6
.pre-commit-config.yaml
CHANGED
@@ -1,37 +1,60 @@
|
|
1 |
-
exclude: ^stylegan_xl
|
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 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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: purple
|
6 |
sdk: gradio
|
7 |
-
sdk_version:
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
suggested_hardware: t4-small
|
|
|
4 |
colorFrom: red
|
5 |
colorTo: purple
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 4.20.0
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
suggested_hardware: t4-small
|
app.py
CHANGED
@@ -9,54 +9,54 @@ import numpy as np
|
|
9 |
|
10 |
from model import Model
|
11 |
|
12 |
-
DESCRIPTION =
|
13 |
|
14 |
|
15 |
def update_class_index(name: str) -> dict:
|
16 |
-
if
|
17 |
-
return gr.Slider
|
18 |
-
elif
|
19 |
-
return gr.Slider
|
20 |
else:
|
21 |
-
return gr.Slider
|
22 |
|
23 |
|
24 |
def get_sample_image_url(name: str) -> str:
|
25 |
-
sample_image_dir =
|
26 |
-
return f
|
27 |
|
28 |
|
29 |
def get_sample_image_markdown(name: str) -> str:
|
30 |
url = get_sample_image_url(name)
|
31 |
-
if name ==
|
32 |
size = 128
|
33 |
-
class_index =
|
34 |
-
seed =
|
35 |
-
elif name ==
|
36 |
size = 32
|
37 |
-
class_index =
|
38 |
-
seed =
|
39 |
-
elif name ==
|
40 |
size = 256
|
41 |
-
class_index =
|
42 |
-
seed =
|
43 |
-
elif name ==
|
44 |
size = 256
|
45 |
-
class_index =
|
46 |
-
seed =
|
47 |
else:
|
48 |
raise ValueError
|
49 |
|
50 |
-
return f
|
51 |
- size: {size}x{size}
|
52 |
- class_index: {class_index}
|
53 |
- seed: {seed}
|
54 |
- truncation: 0.7
|
55 |
-
![sample images]({url})
|
56 |
|
57 |
|
58 |
def load_class_names(name: str) -> list[str]:
|
59 |
-
with open(f
|
60 |
names = json.load(f)
|
61 |
return names
|
62 |
|
@@ -66,139 +66,136 @@ def get_class_name_df(name: str) -> list:
|
|
66 |
return list(map(list, enumerate(names))) # type: ignore
|
67 |
|
68 |
|
69 |
-
IMAGENET_NAMES = load_class_names(
|
70 |
-
CIFAR10_NAMES = load_class_names(
|
71 |
|
72 |
|
73 |
def update_class_name(model_name: str, index: int) -> dict:
|
74 |
-
if
|
75 |
if index < len(IMAGENET_NAMES):
|
76 |
value = IMAGENET_NAMES[index]
|
77 |
else:
|
78 |
-
value =
|
79 |
-
return gr.Textbox
|
80 |
-
elif
|
81 |
if index < len(CIFAR10_NAMES):
|
82 |
value = CIFAR10_NAMES[index]
|
83 |
else:
|
84 |
-
value =
|
85 |
-
return gr.Textbox
|
86 |
else:
|
87 |
-
return gr.Textbox
|
88 |
|
89 |
|
90 |
model = Model()
|
91 |
|
92 |
-
with gr.Blocks(css=
|
93 |
gr.Markdown(DESCRIPTION)
|
94 |
|
95 |
with gr.Tabs():
|
96 |
-
with gr.TabItem(
|
97 |
with gr.Row():
|
98 |
with gr.Column():
|
99 |
with gr.Group():
|
100 |
-
model_name = gr.Dropdown(model.MODEL_NAMES,
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
np.iinfo(np.uint32).max,
|
105 |
-
step=1,
|
106 |
-
value=0,
|
107 |
-
label='Seed')
|
108 |
-
psi = gr.Slider(0,
|
109 |
-
2,
|
110 |
-
step=0.05,
|
111 |
-
value=0.7,
|
112 |
-
label='Truncation psi')
|
113 |
-
class_index = gr.Slider(0,
|
114 |
-
999,
|
115 |
-
step=1,
|
116 |
-
value=83,
|
117 |
-
label='Class Index')
|
118 |
class_name = gr.Textbox(
|
119 |
-
value=IMAGENET_NAMES[class_index.value],
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
value=0,
|
126 |
-
label='Translate X')
|
127 |
-
ty = gr.Slider(-1,
|
128 |
-
1,
|
129 |
-
step=0.05,
|
130 |
-
value=0,
|
131 |
-
label='Translate Y')
|
132 |
-
angle = gr.Slider(-180,
|
133 |
-
180,
|
134 |
-
step=5,
|
135 |
-
value=0,
|
136 |
-
label='Angle')
|
137 |
-
run_button = gr.Button('Run')
|
138 |
with gr.Column():
|
139 |
-
result = gr.Image(label=
|
140 |
|
141 |
-
with gr.TabItem(
|
142 |
with gr.Row():
|
143 |
-
model_name2 = gr.Dropdown(
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
|
|
|
|
151 |
with gr.Row():
|
152 |
text = get_sample_image_markdown(model_name2.value)
|
153 |
sample_images = gr.Markdown(text)
|
154 |
|
155 |
-
with gr.TabItem(
|
156 |
with gr.Row():
|
157 |
-
dataset_name = gr.Dropdown(
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
|
|
|
|
163 |
with gr.Row():
|
164 |
-
df = get_class_name_df(
|
165 |
-
class_names = gr.Dataframe(df,
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
model_name.change(
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
from model import Model
|
11 |
|
12 |
+
DESCRIPTION = "# [StyleGAN-XL](https://github.com/autonomousvision/stylegan_xl)"
|
13 |
|
14 |
|
15 |
def update_class_index(name: str) -> dict:
|
16 |
+
if "imagenet" in name:
|
17 |
+
return gr.Slider(maximum=999, visible=True)
|
18 |
+
elif "cifar" in name:
|
19 |
+
return gr.Slider(maximum=9, visible=True)
|
20 |
else:
|
21 |
+
return gr.Slider(visible=False)
|
22 |
|
23 |
|
24 |
def get_sample_image_url(name: str) -> str:
|
25 |
+
sample_image_dir = "https://huggingface.co/spaces/hysts/StyleGAN-XL/resolve/main/samples"
|
26 |
+
return f"{sample_image_dir}/{name}.jpg"
|
27 |
|
28 |
|
29 |
def get_sample_image_markdown(name: str) -> str:
|
30 |
url = get_sample_image_url(name)
|
31 |
+
if name == "imagenet":
|
32 |
size = 128
|
33 |
+
class_index = "0-999"
|
34 |
+
seed = "0"
|
35 |
+
elif name == "cifar10":
|
36 |
size = 32
|
37 |
+
class_index = "0-9"
|
38 |
+
seed = "0-9"
|
39 |
+
elif name == "ffhq":
|
40 |
size = 256
|
41 |
+
class_index = "N/A"
|
42 |
+
seed = "0-99"
|
43 |
+
elif name == "pokemon":
|
44 |
size = 256
|
45 |
+
class_index = "N/A"
|
46 |
+
seed = "0-99"
|
47 |
else:
|
48 |
raise ValueError
|
49 |
|
50 |
+
return f"""
|
51 |
- size: {size}x{size}
|
52 |
- class_index: {class_index}
|
53 |
- seed: {seed}
|
54 |
- truncation: 0.7
|
55 |
+
![sample images]({url})"""
|
56 |
|
57 |
|
58 |
def load_class_names(name: str) -> list[str]:
|
59 |
+
with open(f"labels/{name}_classes.json") as f:
|
60 |
names = json.load(f)
|
61 |
return names
|
62 |
|
|
|
66 |
return list(map(list, enumerate(names))) # type: ignore
|
67 |
|
68 |
|
69 |
+
IMAGENET_NAMES = load_class_names("imagenet")
|
70 |
+
CIFAR10_NAMES = load_class_names("cifar10")
|
71 |
|
72 |
|
73 |
def update_class_name(model_name: str, index: int) -> dict:
|
74 |
+
if "imagenet" in model_name:
|
75 |
if index < len(IMAGENET_NAMES):
|
76 |
value = IMAGENET_NAMES[index]
|
77 |
else:
|
78 |
+
value = "-"
|
79 |
+
return gr.Textbox(value=value, visible=True)
|
80 |
+
elif "cifar" in model_name:
|
81 |
if index < len(CIFAR10_NAMES):
|
82 |
value = CIFAR10_NAMES[index]
|
83 |
else:
|
84 |
+
value = "-"
|
85 |
+
return gr.Textbox(value=value, visible=True)
|
86 |
else:
|
87 |
+
return gr.Textbox(visible=False)
|
88 |
|
89 |
|
90 |
model = Model()
|
91 |
|
92 |
+
with gr.Blocks(css="style.css") as demo:
|
93 |
gr.Markdown(DESCRIPTION)
|
94 |
|
95 |
with gr.Tabs():
|
96 |
+
with gr.TabItem("App"):
|
97 |
with gr.Row():
|
98 |
with gr.Column():
|
99 |
with gr.Group():
|
100 |
+
model_name = gr.Dropdown(label="Model", choices=model.MODEL_NAMES, value=model.MODEL_NAMES[3])
|
101 |
+
seed = gr.Slider(label="Seed", minimum=0, maximum=np.iinfo(np.uint32).max, step=1, value=0)
|
102 |
+
psi = gr.Slider(label="Truncation psi", minimum=0, maximum=2, step=0.05, value=0.7)
|
103 |
+
class_index = gr.Slider(label="Class Index", minimum=0, maximum=999, step=1, value=83)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
class_name = gr.Textbox(
|
105 |
+
label="Class Label", value=IMAGENET_NAMES[class_index.value], interactive=False
|
106 |
+
)
|
107 |
+
tx = gr.Slider(label="Translate X", minimum=-1, maximum=1, step=0.05, value=0)
|
108 |
+
ty = gr.Slider(label="Translate Y", minimum=-1, maximum=1, step=0.05, value=0)
|
109 |
+
angle = gr.Slider(label="Angle", minimum=-180, maximum=180, step=5, value=0)
|
110 |
+
run_button = gr.Button()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
with gr.Column():
|
112 |
+
result = gr.Image(label="Result")
|
113 |
|
114 |
+
with gr.TabItem("Sample Images"):
|
115 |
with gr.Row():
|
116 |
+
model_name2 = gr.Dropdown(
|
117 |
+
label="Model",
|
118 |
+
choices=[
|
119 |
+
"imagenet",
|
120 |
+
"cifar10",
|
121 |
+
"ffhq",
|
122 |
+
"pokemon",
|
123 |
+
],
|
124 |
+
value="imagenet",
|
125 |
+
)
|
126 |
with gr.Row():
|
127 |
text = get_sample_image_markdown(model_name2.value)
|
128 |
sample_images = gr.Markdown(text)
|
129 |
|
130 |
+
with gr.TabItem("Class Names"):
|
131 |
with gr.Row():
|
132 |
+
dataset_name = gr.Dropdown(
|
133 |
+
label="Dataset",
|
134 |
+
choices=[
|
135 |
+
"imagenet",
|
136 |
+
"cifar10",
|
137 |
+
],
|
138 |
+
value="imagenet",
|
139 |
+
)
|
140 |
with gr.Row():
|
141 |
+
df = get_class_name_df("imagenet")
|
142 |
+
class_names = gr.Dataframe(value=df, col_count=2, headers=["Class Index", "Label"], interactive=False)
|
143 |
+
|
144 |
+
model_name.change(
|
145 |
+
fn=update_class_index,
|
146 |
+
inputs=model_name,
|
147 |
+
outputs=class_index,
|
148 |
+
queue=False,
|
149 |
+
api_name=False,
|
150 |
+
)
|
151 |
+
model_name.change(
|
152 |
+
fn=update_class_name,
|
153 |
+
inputs=[
|
154 |
+
model_name,
|
155 |
+
class_index,
|
156 |
+
],
|
157 |
+
outputs=class_name,
|
158 |
+
queue=False,
|
159 |
+
api_name=False,
|
160 |
+
)
|
161 |
+
class_index.change(
|
162 |
+
fn=update_class_name,
|
163 |
+
inputs=[
|
164 |
+
model_name,
|
165 |
+
class_index,
|
166 |
+
],
|
167 |
+
outputs=class_name,
|
168 |
+
queue=False,
|
169 |
+
api_name=False,
|
170 |
+
)
|
171 |
+
run_button.click(
|
172 |
+
fn=model.set_model_and_generate_image,
|
173 |
+
inputs=[
|
174 |
+
model_name,
|
175 |
+
seed,
|
176 |
+
psi,
|
177 |
+
class_index,
|
178 |
+
tx,
|
179 |
+
ty,
|
180 |
+
angle,
|
181 |
+
],
|
182 |
+
outputs=result,
|
183 |
+
api_name="run",
|
184 |
+
)
|
185 |
+
model_name2.change(
|
186 |
+
fn=get_sample_image_markdown,
|
187 |
+
inputs=model_name2,
|
188 |
+
outputs=sample_images,
|
189 |
+
queue=False,
|
190 |
+
api_name=False,
|
191 |
+
)
|
192 |
+
dataset_name.change(
|
193 |
+
fn=get_class_name_df,
|
194 |
+
inputs=dataset_name,
|
195 |
+
outputs=class_names,
|
196 |
+
queue=False,
|
197 |
+
api_name=False,
|
198 |
+
)
|
199 |
+
|
200 |
+
if __name__ == "__main__":
|
201 |
+
demo.queue(max_size=10).launch()
|
model.py
CHANGED
@@ -10,34 +10,32 @@ import torch.nn as nn
|
|
10 |
from huggingface_hub import hf_hub_download
|
11 |
|
12 |
current_dir = pathlib.Path(__file__).parent
|
13 |
-
submodule_dir = current_dir /
|
14 |
sys.path.insert(0, submodule_dir.as_posix())
|
15 |
|
16 |
|
17 |
class Model:
|
18 |
|
19 |
MODEL_NAMES = [
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
]
|
28 |
|
29 |
def __init__(self):
|
30 |
-
self.device = torch.device(
|
31 |
-
'cuda:0' if torch.cuda.is_available() else 'cpu')
|
32 |
self._download_all_models()
|
33 |
self.model_name = self.MODEL_NAMES[3]
|
34 |
self.model = self._load_model(self.model_name)
|
35 |
|
36 |
def _load_model(self, model_name: str) -> nn.Module:
|
37 |
-
path = hf_hub_download(
|
38 |
-
|
39 |
-
|
40 |
-
model = pickle.load(f)['G_ema']
|
41 |
model.eval()
|
42 |
model.to(self.device)
|
43 |
return model
|
@@ -53,8 +51,7 @@ class Model:
|
|
53 |
self._load_model(name)
|
54 |
|
55 |
@staticmethod
|
56 |
-
def make_transform(translate: tuple[float, float],
|
57 |
-
angle: float) -> np.ndarray:
|
58 |
mat = np.eye(3)
|
59 |
sin = np.sin(angle / 360 * np.pi * 2)
|
60 |
cos = np.cos(angle / 360 * np.pi * 2)
|
@@ -72,8 +69,7 @@ class Model:
|
|
72 |
return torch.from_numpy(z).float().to(self.device)
|
73 |
|
74 |
def postprocess(self, tensor: torch.Tensor) -> np.ndarray:
|
75 |
-
tensor = (tensor.permute(0, 2, 3, 1) * 127.5 + 128).clamp(0, 255).to(
|
76 |
-
torch.uint8)
|
77 |
return tensor.cpu().numpy()
|
78 |
|
79 |
def make_label_tensor(self, class_index: int) -> torch.Tensor:
|
@@ -92,13 +88,12 @@ class Model:
|
|
92 |
self.model.synthesis.input.transform.copy_(torch.from_numpy(mat))
|
93 |
|
94 |
@torch.inference_mode()
|
95 |
-
def generate(self, z: torch.Tensor, label: torch.Tensor,
|
96 |
-
truncation_psi: float) -> torch.Tensor:
|
97 |
return self.model(z, label, truncation_psi=truncation_psi)
|
98 |
|
99 |
-
def generate_image(
|
100 |
-
|
101 |
-
|
102 |
self.set_transform(tx, ty, angle)
|
103 |
|
104 |
z = self.generate_z(seed)
|
@@ -108,10 +103,8 @@ class Model:
|
|
108 |
out = self.postprocess(out)
|
109 |
return out[0]
|
110 |
|
111 |
-
def set_model_and_generate_image(
|
112 |
-
|
113 |
-
|
114 |
-
angle: float) -> np.ndarray:
|
115 |
self.set_model(model_name)
|
116 |
-
return self.generate_image(seed, truncation_psi, class_index, tx, ty,
|
117 |
-
angle)
|
|
|
10 |
from huggingface_hub import hf_hub_download
|
11 |
|
12 |
current_dir = pathlib.Path(__file__).parent
|
13 |
+
submodule_dir = current_dir / "stylegan_xl"
|
14 |
sys.path.insert(0, submodule_dir.as_posix())
|
15 |
|
16 |
|
17 |
class Model:
|
18 |
|
19 |
MODEL_NAMES = [
|
20 |
+
"imagenet16",
|
21 |
+
"imagenet32",
|
22 |
+
"imagenet64",
|
23 |
+
"imagenet128",
|
24 |
+
"cifar10",
|
25 |
+
"ffhq256",
|
26 |
+
"pokemon256",
|
27 |
]
|
28 |
|
29 |
def __init__(self):
|
30 |
+
self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
|
|
31 |
self._download_all_models()
|
32 |
self.model_name = self.MODEL_NAMES[3]
|
33 |
self.model = self._load_model(self.model_name)
|
34 |
|
35 |
def _load_model(self, model_name: str) -> nn.Module:
|
36 |
+
path = hf_hub_download("public-data/StyleGAN-XL", f"models/{model_name}.pkl")
|
37 |
+
with open(path, "rb") as f:
|
38 |
+
model = pickle.load(f)["G_ema"]
|
|
|
39 |
model.eval()
|
40 |
model.to(self.device)
|
41 |
return model
|
|
|
51 |
self._load_model(name)
|
52 |
|
53 |
@staticmethod
|
54 |
+
def make_transform(translate: tuple[float, float], angle: float) -> np.ndarray:
|
|
|
55 |
mat = np.eye(3)
|
56 |
sin = np.sin(angle / 360 * np.pi * 2)
|
57 |
cos = np.cos(angle / 360 * np.pi * 2)
|
|
|
69 |
return torch.from_numpy(z).float().to(self.device)
|
70 |
|
71 |
def postprocess(self, tensor: torch.Tensor) -> np.ndarray:
|
72 |
+
tensor = (tensor.permute(0, 2, 3, 1) * 127.5 + 128).clamp(0, 255).to(torch.uint8)
|
|
|
73 |
return tensor.cpu().numpy()
|
74 |
|
75 |
def make_label_tensor(self, class_index: int) -> torch.Tensor:
|
|
|
88 |
self.model.synthesis.input.transform.copy_(torch.from_numpy(mat))
|
89 |
|
90 |
@torch.inference_mode()
|
91 |
+
def generate(self, z: torch.Tensor, label: torch.Tensor, truncation_psi: float) -> torch.Tensor:
|
|
|
92 |
return self.model(z, label, truncation_psi=truncation_psi)
|
93 |
|
94 |
+
def generate_image(
|
95 |
+
self, seed: int, truncation_psi: float, class_index: int, tx: float, ty: float, angle: float
|
96 |
+
) -> np.ndarray:
|
97 |
self.set_transform(tx, ty, angle)
|
98 |
|
99 |
z = self.generate_z(seed)
|
|
|
103 |
out = self.postprocess(out)
|
104 |
return out[0]
|
105 |
|
106 |
+
def set_model_and_generate_image(
|
107 |
+
self, model_name: str, seed: int, truncation_psi: float, class_index: int, tx: float, ty: float, angle: float
|
108 |
+
) -> np.ndarray:
|
|
|
109 |
self.set_model(model_name)
|
110 |
+
return self.generate_image(seed, truncation_psi, class_index, tx, ty, angle)
|
|
requirements.txt
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
-
ftfy==6.1.
|
2 |
-
|
3 |
-
|
4 |
-
|
|
|
5 |
torch==2.0.1
|
6 |
torchvision==0.15.2
|
|
|
1 |
+
ftfy==6.1.3
|
2 |
+
gradio==4.20.0
|
3 |
+
numpy==1.26.4
|
4 |
+
Pillow==10.2.0
|
5 |
+
scipy==1.12.0
|
6 |
torch==2.0.1
|
7 |
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 |
}
|