update to latest version of gradio
#1
by
akhaliq
HF staff
- opened
- .pre-commit-config.yaml +0 -60
- .vscode/settings.json +0 -30
- README.md +1 -4
- app.py +146 -96
- cluster_center_images/horses_256.jpg +0 -3
- cluster_center_images/lions_512.jpg +0 -3
- cluster_center_images/parrots_512.jpg +0 -3
- model.py +0 -192
- requirements.txt +5 -6
- cluster_center_images/elephants_512.jpg β samples/bicycles.jpg +2 -2
- samples/bicycles_256_global.jpg +0 -3
- samples/bicycles_256_multimodal_l2.jpg +0 -3
- samples/bicycles_256_multimodal_lpips.jpg +0 -3
- cluster_center_images/dogs_1024.jpg β samples/dogs.jpg +2 -2
- samples/dogs_1024_global.jpg +0 -3
- samples/dogs_1024_multimodal_l2.jpg +0 -3
- samples/dogs_1024_multimodal_lpips.jpg +0 -3
- samples/elephants.jpg +3 -0
- samples/elephants_512_global.jpg +0 -3
- samples/elephants_512_multimodal_l2.jpg +0 -3
- samples/elephants_512_multimodal_lpips.jpg +0 -3
- samples/giraffes.jpg +3 -0
- samples/giraffes_512_global.jpg +0 -3
- samples/giraffes_512_multimodal_l2.jpg +0 -3
- samples/giraffes_512_multimodal_lpips.jpg +0 -3
- cluster_center_images/giraffes_512.jpg β samples/horses.jpg +2 -2
- samples/horses_256_global.jpg +0 -3
- samples/horses_256_multimodal_l2.jpg +0 -3
- samples/horses_256_multimodal_lpips.jpg +0 -3
- samples/lions.jpg +3 -0
- samples/lions_512_global.jpg +0 -3
- samples/lions_512_multimodal_l2.jpg +0 -3
- samples/lions_512_multimodal_lpips.jpg +0 -3
- cluster_center_images/bicycles_256.jpg β samples/parrots.jpg +2 -2
- samples/parrots_512_global.jpg +0 -3
- samples/parrots_512_multimodal_l2.jpg +0 -3
- samples/parrots_512_multimodal_lpips.jpg +0 -3
- style.css +0 -8
.pre-commit-config.yaml
DELETED
@@ -1,60 +0,0 @@
|
|
1 |
-
repos:
|
2 |
-
- repo: https://github.com/pre-commit/pre-commit-hooks
|
3 |
-
rev: v4.6.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.10.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.4.2
|
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.8.5
|
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
DELETED
@@ -1,30 +0,0 @@
|
|
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,12 +4,9 @@ emoji: π¨
|
|
4 |
colorFrom: indigo
|
5 |
colorTo: red
|
6 |
sdk: gradio
|
7 |
-
sdk_version:
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
-
suggested_hardware: t4-small
|
11 |
---
|
12 |
|
13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces#reference
|
14 |
-
|
15 |
-
https://arxiv.org/abs/2202.12211
|
|
|
4 |
colorFrom: indigo
|
5 |
colorTo: red
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 3.0.5
|
8 |
app_file: app.py
|
9 |
pinned: false
|
|
|
10 |
---
|
11 |
|
12 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces#reference
|
|
|
|
app.py
CHANGED
@@ -2,105 +2,155 @@
|
|
2 |
|
3 |
from __future__ import annotations
|
4 |
|
5 |
-
import
|
|
|
|
|
|
|
|
|
6 |
|
7 |
import gradio as gr
|
8 |
import numpy as np
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
],
|
92 |
-
outputs=
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
)
|
104 |
|
105 |
-
|
106 |
-
|
|
|
|
2 |
|
3 |
from __future__ import annotations
|
4 |
|
5 |
+
import argparse
|
6 |
+
import functools
|
7 |
+
import os
|
8 |
+
import pickle
|
9 |
+
import sys
|
10 |
|
11 |
import gradio as gr
|
12 |
import numpy as np
|
13 |
+
import torch
|
14 |
+
import torch.nn as nn
|
15 |
+
from huggingface_hub import hf_hub_download
|
16 |
+
|
17 |
+
sys.path.insert(0, 'stylegan3')
|
18 |
+
|
19 |
+
TITLE = 'Self-Distilled StyleGAN'
|
20 |
+
DESCRIPTION = '''This is an unofficial demo for models provided in https://github.com/self-distilled-stylegan/self-distilled-internet-photos.
|
21 |
+
|
22 |
+
Expected execution time on Hugging Face Spaces: 2s
|
23 |
+
'''
|
24 |
+
SAMPLE_IMAGE_DIR = 'https://huggingface.co/spaces/hysts/Self-Distilled-StyleGAN/resolve/main/samples'
|
25 |
+
ARTICLE = f'''## Generated images
|
26 |
+
- truncation: 0.7
|
27 |
+
### Dogs
|
28 |
+
- size: 1024x1024
|
29 |
+
- seed: 0-99
|
30 |
+
![Dogs]({SAMPLE_IMAGE_DIR}/dogs.jpg)
|
31 |
+
### Elephants
|
32 |
+
- size: 512x512
|
33 |
+
- seed: 0-99
|
34 |
+
![Elephants]({SAMPLE_IMAGE_DIR}/elephants.jpg)
|
35 |
+
### Horses
|
36 |
+
- size: 256x256
|
37 |
+
- seed: 0-99
|
38 |
+
![Horses]({SAMPLE_IMAGE_DIR}/horses.jpg)
|
39 |
+
### Bicycles
|
40 |
+
- size: 256x256
|
41 |
+
- seed: 0-99
|
42 |
+
![Bicycles]({SAMPLE_IMAGE_DIR}/bicycles.jpg)
|
43 |
+
### Lions
|
44 |
+
- size: 512x512
|
45 |
+
- seed: 0-99
|
46 |
+
![Lions]({SAMPLE_IMAGE_DIR}/lions.jpg)
|
47 |
+
### Giraffes
|
48 |
+
- size: 512x512
|
49 |
+
- seed: 0-99
|
50 |
+
![Giraffes]({SAMPLE_IMAGE_DIR}/giraffes.jpg)
|
51 |
+
### Parrots
|
52 |
+
- size: 512x512
|
53 |
+
- seed: 0-99
|
54 |
+
![Parrots]({SAMPLE_IMAGE_DIR}/parrots.jpg)
|
55 |
+
|
56 |
+
<center><img src="https://visitor-badge.glitch.me/badge?page_id=hysts.self-distilled-stylegan" alt="visitor badge"/></center>
|
57 |
+
'''
|
58 |
+
|
59 |
+
TOKEN = os.environ['TOKEN']
|
60 |
+
|
61 |
+
|
62 |
+
def parse_args() -> argparse.Namespace:
|
63 |
+
parser = argparse.ArgumentParser()
|
64 |
+
parser.add_argument('--device', type=str, default='cpu')
|
65 |
+
parser.add_argument('--theme', type=str)
|
66 |
+
parser.add_argument('--live', action='store_true')
|
67 |
+
parser.add_argument('--share', action='store_true')
|
68 |
+
parser.add_argument('--port', type=int)
|
69 |
+
parser.add_argument('--disable-queue',
|
70 |
+
dest='enable_queue',
|
71 |
+
action='store_false')
|
72 |
+
parser.add_argument('--allow-flagging', type=str, default='never')
|
73 |
+
return parser.parse_args()
|
74 |
+
|
75 |
+
|
76 |
+
def generate_z(z_dim: int, seed: int, device: torch.device) -> torch.Tensor:
|
77 |
+
return torch.from_numpy(np.random.RandomState(seed).randn(
|
78 |
+
1, z_dim)).to(device).float()
|
79 |
+
|
80 |
+
|
81 |
+
@torch.inference_mode()
|
82 |
+
def generate_image(model_name: str, seed: int, truncation_psi: float,
|
83 |
+
model_dict: dict[str, nn.Module],
|
84 |
+
device: torch.device) -> np.ndarray:
|
85 |
+
model = model_dict[model_name]
|
86 |
+
seed = int(np.clip(seed, 0, np.iinfo(np.uint32).max))
|
87 |
+
|
88 |
+
z = generate_z(model.z_dim, seed, device)
|
89 |
+
label = torch.zeros([1, model.c_dim], device=device)
|
90 |
+
|
91 |
+
out = model(z, label, truncation_psi=truncation_psi)
|
92 |
+
out = (out.permute(0, 2, 3, 1) * 127.5 + 128).clamp(0, 255).to(torch.uint8)
|
93 |
+
return out[0].cpu().numpy()
|
94 |
+
|
95 |
+
|
96 |
+
def load_model(model_name: str, device: torch.device) -> nn.Module:
|
97 |
+
path = hf_hub_download('hysts/Self-Distilled-StyleGAN',
|
98 |
+
f'models/{model_name}_pytorch.pkl',
|
99 |
+
use_auth_token=TOKEN)
|
100 |
+
with open(path, 'rb') as f:
|
101 |
+
model = pickle.load(f)['G_ema']
|
102 |
+
model.eval()
|
103 |
+
model.to(device)
|
104 |
+
with torch.inference_mode():
|
105 |
+
z = torch.zeros((1, model.z_dim)).to(device)
|
106 |
+
label = torch.zeros([1, model.c_dim], device=device)
|
107 |
+
model(z, label)
|
108 |
+
return model
|
109 |
+
|
110 |
+
|
111 |
+
def main():
|
112 |
+
args = parse_args()
|
113 |
+
device = torch.device(args.device)
|
114 |
+
|
115 |
+
model_names = [
|
116 |
+
'dogs_1024',
|
117 |
+
'elephants_512',
|
118 |
+
'horses_256',
|
119 |
+
'bicycles_256',
|
120 |
+
'lions_512',
|
121 |
+
'giraffes_512',
|
122 |
+
'parrots_512',
|
123 |
+
]
|
124 |
+
|
125 |
+
model_dict = {name: load_model(name, device) for name in model_names}
|
126 |
+
|
127 |
+
func = functools.partial(generate_image,
|
128 |
+
model_dict=model_dict,
|
129 |
+
device=device)
|
130 |
+
func = functools.update_wrapper(func, generate_image)
|
131 |
+
|
132 |
+
gr.Interface(
|
133 |
+
func,
|
134 |
+
[
|
135 |
+
gr.inputs.Radio(
|
136 |
+
model_names, type='value', default='dogs_1024', label='Model'),
|
137 |
+
gr.inputs.Number(default=0, label='Seed'),
|
138 |
+
gr.inputs.Slider(
|
139 |
+
0, 2, step=0.05, default=0.7, label='Truncation psi'),
|
140 |
],
|
141 |
+
gr.outputs.Image(type='numpy', label='Output'),
|
142 |
+
title=TITLE,
|
143 |
+
description=DESCRIPTION,
|
144 |
+
article=ARTICLE,
|
145 |
+
theme=args.theme,
|
146 |
+
allow_flagging=args.allow_flagging,
|
147 |
+
live=args.live,
|
148 |
+
).launch(
|
149 |
+
enable_queue=args.enable_queue,
|
150 |
+
server_port=args.port,
|
151 |
+
share=args.share,
|
152 |
)
|
153 |
|
154 |
+
|
155 |
+
if __name__ == '__main__':
|
156 |
+
main()
|
cluster_center_images/horses_256.jpg
DELETED
Git LFS Details
|
cluster_center_images/lions_512.jpg
DELETED
Git LFS Details
|
cluster_center_images/parrots_512.jpg
DELETED
Git LFS Details
|
model.py
DELETED
@@ -1,192 +0,0 @@
|
|
1 |
-
from __future__ import annotations
|
2 |
-
|
3 |
-
import pathlib
|
4 |
-
import pickle
|
5 |
-
import sys
|
6 |
-
|
7 |
-
import lpips
|
8 |
-
import numpy as np
|
9 |
-
import torch
|
10 |
-
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 / "stylegan3"
|
15 |
-
sys.path.insert(0, submodule_dir.as_posix())
|
16 |
-
|
17 |
-
|
18 |
-
class LPIPS(lpips.LPIPS):
|
19 |
-
@staticmethod
|
20 |
-
def preprocess(image: np.ndarray) -> torch.Tensor:
|
21 |
-
data = torch.from_numpy(image).float() / 255
|
22 |
-
data = data * 2 - 1
|
23 |
-
return data.permute(2, 0, 1).unsqueeze(0)
|
24 |
-
|
25 |
-
@torch.inference_mode()
|
26 |
-
def compute_features(self, data: torch.Tensor) -> list[torch.Tensor]:
|
27 |
-
data = self.scaling_layer(data)
|
28 |
-
data = self.net(data)
|
29 |
-
return [lpips.normalize_tensor(x) for x in data]
|
30 |
-
|
31 |
-
@torch.inference_mode()
|
32 |
-
def compute_distance(self, features0: list[torch.Tensor], features1: list[torch.Tensor]) -> float:
|
33 |
-
res = 0
|
34 |
-
for lin, x0, x1 in zip(self.lins, features0, features1):
|
35 |
-
d = (x0 - x1) ** 2
|
36 |
-
y = lin(d)
|
37 |
-
y = lpips.lpips.spatial_average(y)
|
38 |
-
res += y.item()
|
39 |
-
return res
|
40 |
-
|
41 |
-
|
42 |
-
class Model:
|
43 |
-
|
44 |
-
MODEL_NAMES = [
|
45 |
-
"dogs_1024",
|
46 |
-
"elephants_512",
|
47 |
-
"horses_256",
|
48 |
-
"bicycles_256",
|
49 |
-
"lions_512",
|
50 |
-
"giraffes_512",
|
51 |
-
"parrots_512",
|
52 |
-
]
|
53 |
-
TRUNCATION_TYPES = [
|
54 |
-
"Multimodal (LPIPS)",
|
55 |
-
"Multimodal (L2)",
|
56 |
-
"Global",
|
57 |
-
]
|
58 |
-
|
59 |
-
def __init__(self):
|
60 |
-
self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
61 |
-
self._download_all_models()
|
62 |
-
self._download_all_cluster_centers()
|
63 |
-
self._download_all_cluster_center_images()
|
64 |
-
|
65 |
-
self.model_name = self.MODEL_NAMES[0]
|
66 |
-
self.model = self._load_model(self.model_name)
|
67 |
-
self.cluster_centers = self._load_cluster_centers(self.model_name)
|
68 |
-
self.cluster_center_images = self._load_cluster_center_images(self.model_name)
|
69 |
-
|
70 |
-
self.lpips = LPIPS()
|
71 |
-
self.cluster_center_lpips_feature_dict = self._compute_cluster_center_lpips_features()
|
72 |
-
|
73 |
-
def _load_model(self, model_name: str) -> nn.Module:
|
74 |
-
path = hf_hub_download("public-data/Self-Distilled-StyleGAN", f"models/{model_name}_pytorch.pkl")
|
75 |
-
with open(path, "rb") as f:
|
76 |
-
model = pickle.load(f)["G_ema"]
|
77 |
-
model.eval()
|
78 |
-
model.to(self.device)
|
79 |
-
return model
|
80 |
-
|
81 |
-
def _load_cluster_centers(self, model_name: str) -> torch.Tensor:
|
82 |
-
path = hf_hub_download("public-data/Self-Distilled-StyleGAN", f"cluster_centers/{model_name}.npy")
|
83 |
-
centers = np.load(path)
|
84 |
-
centers = torch.from_numpy(centers).float().to(self.device)
|
85 |
-
return centers
|
86 |
-
|
87 |
-
def _load_cluster_center_images(self, model_name: str) -> np.ndarray:
|
88 |
-
path = hf_hub_download("public-data/Self-Distilled-StyleGAN", f"cluster_center_images/{model_name}.npy")
|
89 |
-
return np.load(path)
|
90 |
-
|
91 |
-
def set_model(self, model_name: str) -> None:
|
92 |
-
if model_name == self.model_name:
|
93 |
-
return
|
94 |
-
self.model_name = model_name
|
95 |
-
self.model = self._load_model(model_name)
|
96 |
-
self.cluster_centers = self._load_cluster_centers(model_name)
|
97 |
-
self.cluster_center_images = self._load_cluster_center_images(model_name)
|
98 |
-
|
99 |
-
def _download_all_models(self):
|
100 |
-
for name in self.MODEL_NAMES:
|
101 |
-
self._load_model(name)
|
102 |
-
|
103 |
-
def _download_all_cluster_centers(self):
|
104 |
-
for name in self.MODEL_NAMES:
|
105 |
-
self._load_cluster_centers(name)
|
106 |
-
|
107 |
-
def _download_all_cluster_center_images(self):
|
108 |
-
for name in self.MODEL_NAMES:
|
109 |
-
self._load_cluster_center_images(name)
|
110 |
-
|
111 |
-
def generate_z(self, seed: int) -> torch.Tensor:
|
112 |
-
seed = int(np.clip(seed, 0, np.iinfo(np.uint32).max))
|
113 |
-
return torch.from_numpy(np.random.RandomState(seed).randn(1, self.model.z_dim)).float().to(self.device)
|
114 |
-
|
115 |
-
def compute_w(self, z: torch.Tensor) -> torch.Tensor:
|
116 |
-
label = torch.zeros((1, self.model.c_dim), device=self.device)
|
117 |
-
w = self.model.mapping(z, label)
|
118 |
-
return w
|
119 |
-
|
120 |
-
@staticmethod
|
121 |
-
def truncate_w(w_center: torch.Tensor, w: torch.Tensor, psi: float) -> torch.Tensor:
|
122 |
-
if psi == 1:
|
123 |
-
return w
|
124 |
-
return w_center.lerp(w, psi)
|
125 |
-
|
126 |
-
@torch.inference_mode()
|
127 |
-
def synthesize(self, w: torch.Tensor) -> torch.Tensor:
|
128 |
-
return self.model.synthesis(w)
|
129 |
-
|
130 |
-
def postprocess(self, tensor: torch.Tensor) -> np.ndarray:
|
131 |
-
tensor = (tensor.permute(0, 2, 3, 1) * 127.5 + 128).clamp(0, 255).to(torch.uint8)
|
132 |
-
return tensor.cpu().numpy()
|
133 |
-
|
134 |
-
def compute_lpips_features(self, image: np.ndarray) -> list[torch.Tensor]:
|
135 |
-
data = self.lpips.preprocess(image)
|
136 |
-
return self.lpips.compute_features(data)
|
137 |
-
|
138 |
-
def _compute_cluster_center_lpips_features(self) -> dict[str, list[list[torch.Tensor]]]:
|
139 |
-
res = dict()
|
140 |
-
for name in self.MODEL_NAMES:
|
141 |
-
images = self._load_cluster_center_images(name)
|
142 |
-
res[name] = [self.compute_lpips_features(image) for image in images]
|
143 |
-
return res
|
144 |
-
|
145 |
-
def compute_distance_to_cluster_centers(self, ws: torch.Tensor, distance_type: str) -> list[torch.Tensor]:
|
146 |
-
if distance_type == "l2":
|
147 |
-
return self._compute_l2_distance_to_cluster_centers(ws)
|
148 |
-
elif distance_type == "lpips":
|
149 |
-
return self._compute_lpips_distance_to_cluster_centers(ws)
|
150 |
-
else:
|
151 |
-
raise ValueError
|
152 |
-
|
153 |
-
def _compute_l2_distance_to_cluster_centers(self, ws: torch.Tensor) -> np.ndarray:
|
154 |
-
dist2 = ((self.cluster_centers - ws[0, 0]) ** 2).sum(dim=1)
|
155 |
-
return dist2.cpu().numpy()
|
156 |
-
|
157 |
-
def _compute_lpips_distance_to_cluster_centers(self, ws: torch.Tensor) -> np.ndarray:
|
158 |
-
x = self.synthesize(ws)
|
159 |
-
x = self.postprocess(x)[0]
|
160 |
-
feat0 = self.compute_lpips_features(x)
|
161 |
-
cluster_center_features = self.cluster_center_lpips_feature_dict[self.model_name]
|
162 |
-
distances = [self.lpips.compute_distance(feat0, feat1) for feat1 in cluster_center_features]
|
163 |
-
return np.asarray(distances)
|
164 |
-
|
165 |
-
def find_nearest_cluster_center(self, ws: torch.Tensor, distance_type: str) -> int:
|
166 |
-
distances = self.compute_distance_to_cluster_centers(ws, distance_type)
|
167 |
-
return int(np.argmin(distances))
|
168 |
-
|
169 |
-
def generate_image(self, seed: int, truncation_psi: float, truncation_type: str) -> np.ndarray:
|
170 |
-
z = self.generate_z(seed)
|
171 |
-
ws = self.compute_w(z)
|
172 |
-
if truncation_type == self.TRUNCATION_TYPES[2]:
|
173 |
-
w0 = self.model.mapping.w_avg
|
174 |
-
else:
|
175 |
-
if truncation_type == self.TRUNCATION_TYPES[0]:
|
176 |
-
distance_type = "lpips"
|
177 |
-
elif truncation_type == self.TRUNCATION_TYPES[1]:
|
178 |
-
distance_type = "l2"
|
179 |
-
else:
|
180 |
-
raise ValueError
|
181 |
-
cluster_index = self.find_nearest_cluster_center(ws, distance_type)
|
182 |
-
w0 = self.cluster_centers[cluster_index]
|
183 |
-
new_ws = self.truncate_w(w0, ws, truncation_psi)
|
184 |
-
out = self.synthesize(new_ws)
|
185 |
-
out = self.postprocess(out)
|
186 |
-
return out[0]
|
187 |
-
|
188 |
-
def set_model_and_generate_image(
|
189 |
-
self, model_name: str, seed: int, truncation_psi: float, truncation_type: str
|
190 |
-
) -> np.ndarray:
|
191 |
-
self.set_model(model_name)
|
192 |
-
return self.generate_image(seed, truncation_psi, truncation_type)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
@@ -1,6 +1,5 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
torchvision==0.15.2
|
|
|
1 |
+
numpy==1.22.3
|
2 |
+
Pillow==9.0.1
|
3 |
+
scipy==1.8.0
|
4 |
+
torch==1.11.0
|
5 |
+
torchvision==0.12.0
|
|
cluster_center_images/elephants_512.jpg β samples/bicycles.jpg
RENAMED
File without changes
|
samples/bicycles_256_global.jpg
DELETED
Git LFS Details
|
samples/bicycles_256_multimodal_l2.jpg
DELETED
Git LFS Details
|
samples/bicycles_256_multimodal_lpips.jpg
DELETED
Git LFS Details
|
cluster_center_images/dogs_1024.jpg β samples/dogs.jpg
RENAMED
File without changes
|
samples/dogs_1024_global.jpg
DELETED
Git LFS Details
|
samples/dogs_1024_multimodal_l2.jpg
DELETED
Git LFS Details
|
samples/dogs_1024_multimodal_lpips.jpg
DELETED
Git LFS Details
|
samples/elephants.jpg
ADDED
Git LFS Details
|
samples/elephants_512_global.jpg
DELETED
Git LFS Details
|
samples/elephants_512_multimodal_l2.jpg
DELETED
Git LFS Details
|
samples/elephants_512_multimodal_lpips.jpg
DELETED
Git LFS Details
|
samples/giraffes.jpg
ADDED
Git LFS Details
|
samples/giraffes_512_global.jpg
DELETED
Git LFS Details
|
samples/giraffes_512_multimodal_l2.jpg
DELETED
Git LFS Details
|
samples/giraffes_512_multimodal_lpips.jpg
DELETED
Git LFS Details
|
cluster_center_images/giraffes_512.jpg β samples/horses.jpg
RENAMED
File without changes
|
samples/horses_256_global.jpg
DELETED
Git LFS Details
|
samples/horses_256_multimodal_l2.jpg
DELETED
Git LFS Details
|
samples/horses_256_multimodal_lpips.jpg
DELETED
Git LFS Details
|
samples/lions.jpg
ADDED
Git LFS Details
|
samples/lions_512_global.jpg
DELETED
Git LFS Details
|
samples/lions_512_multimodal_l2.jpg
DELETED
Git LFS Details
|
samples/lions_512_multimodal_lpips.jpg
DELETED
Git LFS Details
|
cluster_center_images/bicycles_256.jpg β samples/parrots.jpg
RENAMED
File without changes
|
samples/parrots_512_global.jpg
DELETED
Git LFS Details
|
samples/parrots_512_multimodal_l2.jpg
DELETED
Git LFS Details
|
samples/parrots_512_multimodal_lpips.jpg
DELETED
Git LFS Details
|
style.css
DELETED
@@ -1,8 +0,0 @@
|
|
1 |
-
h1 {
|
2 |
-
text-align: center;
|
3 |
-
display: block;
|
4 |
-
}
|
5 |
-
div#result {
|
6 |
-
max-width: 600px;
|
7 |
-
max-height: 600px;
|
8 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|