hysts
commited on
Commit
•
7a39e7e
1
Parent(s):
2243d0c
Add files
Browse files- .gitattributes +1 -0
- .gitmodules +3 -0
- app.py +156 -0
- requirements.txt +5 -0
- samples/bicycles.jpg +3 -0
- samples/dogs.jpg +3 -0
- samples/elephants.jpg +3 -0
- samples/giraffes.jpg +3 -0
- samples/horses.jpg +3 -0
- samples/lions.jpg +3 -0
- samples/parrots.jpg +3 -0
- stylegan3 +1 -0
.gitattributes
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
*.bin filter=lfs diff=lfs merge=lfs -text
|
|
|
1 |
+
*.jpg filter=lfs diff=lfs merge=lfs -text
|
2 |
*.7z filter=lfs diff=lfs merge=lfs -text
|
3 |
*.arrow filter=lfs diff=lfs merge=lfs -text
|
4 |
*.bin filter=lfs diff=lfs merge=lfs -text
|
.gitmodules
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
[submodule "stylegan3"]
|
2 |
+
path = stylegan3
|
3 |
+
url = https://github.com/NVlabs/stylegan3
|
app.py
ADDED
@@ -0,0 +1,156 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
|
3 |
+
from __future__ import annotations
|
4 |
+
|
5 |
+
import argparse
|
6 |
+
import functools
|
7 |
+
import os
|
8 |
+
import pickle
|
9 |
+
import sys
|
10 |
+
|
11 |
+
sys.path.insert(0, 'stylegan3')
|
12 |
+
|
13 |
+
import gradio as gr
|
14 |
+
import numpy as np
|
15 |
+
import torch
|
16 |
+
import torch.nn as nn
|
17 |
+
from huggingface_hub import hf_hub_download
|
18 |
+
|
19 |
+
ORIGINAL_REPO_URL = 'https://github.com/self-distilled-stylegan/self-distilled-internet-photos'
|
20 |
+
TITLE = 'Self-Distilled StyleGAN'
|
21 |
+
DESCRIPTION = f'This is a demo for models provided in {ORIGINAL_REPO_URL}.'
|
22 |
+
SAMPLE_IMAGE_DIR = 'https://huggingface.co/spaces/hysts/Self-Distilled-StyleGAN/resolve/main/samples'
|
23 |
+
ARTICLE = f'''## Generated images
|
24 |
+
- truncation: 0.7
|
25 |
+
### Dogs
|
26 |
+
- size: 1024x1024
|
27 |
+
- seed: 0-99
|
28 |
+
![Dogs]({SAMPLE_IMAGE_DIR}/dogs.jpg)
|
29 |
+
### Elephants
|
30 |
+
- size: 512x512
|
31 |
+
- seed: 0-99
|
32 |
+
![Elephants]({SAMPLE_IMAGE_DIR}/elephants.jpg)
|
33 |
+
### Horses
|
34 |
+
- size: 256x256
|
35 |
+
- seed: 0-99
|
36 |
+
![Horses]({SAMPLE_IMAGE_DIR}/horses.jpg)
|
37 |
+
### Bicycles
|
38 |
+
- size: 256x256
|
39 |
+
- seed: 0-99
|
40 |
+
![Bicycles]({SAMPLE_IMAGE_DIR}/bicycles.jpg)
|
41 |
+
### Lions
|
42 |
+
- size: 512x512
|
43 |
+
- seed: 0-99
|
44 |
+
![Lions]({SAMPLE_IMAGE_DIR}/lions.jpg)
|
45 |
+
### Giraffes
|
46 |
+
- size: 512x512
|
47 |
+
- seed: 0-99
|
48 |
+
![Giraffes]({SAMPLE_IMAGE_DIR}/giraffes.jpg)
|
49 |
+
### Parrots
|
50 |
+
- size: 512x512
|
51 |
+
- seed: 0-99
|
52 |
+
![Parrots]({SAMPLE_IMAGE_DIR}/parrots.jpg)
|
53 |
+
'''
|
54 |
+
|
55 |
+
TOKEN = os.environ['TOKEN']
|
56 |
+
|
57 |
+
|
58 |
+
def parse_args() -> argparse.Namespace:
|
59 |
+
parser = argparse.ArgumentParser()
|
60 |
+
parser.add_argument('--device', type=str, default='cpu')
|
61 |
+
parser.add_argument('--theme', type=str)
|
62 |
+
parser.add_argument('--live', action='store_true')
|
63 |
+
parser.add_argument('--share', action='store_true')
|
64 |
+
parser.add_argument('--port', type=int)
|
65 |
+
parser.add_argument('--disable-queue',
|
66 |
+
dest='enable_queue',
|
67 |
+
action='store_false')
|
68 |
+
parser.add_argument('--allow-flagging', type=str, default='never')
|
69 |
+
parser.add_argument('--allow-screenshot', action='store_true')
|
70 |
+
return parser.parse_args()
|
71 |
+
|
72 |
+
|
73 |
+
def generate_z(z_dim: int, seed: int, device: torch.device) -> torch.Tensor:
|
74 |
+
return torch.from_numpy(np.random.RandomState(seed).randn(
|
75 |
+
1, z_dim)).to(device).float()
|
76 |
+
|
77 |
+
|
78 |
+
@torch.inference_mode()
|
79 |
+
def generate_image(model_name: str, seed: int, truncation_psi: float,
|
80 |
+
model_dict: dict[str, nn.Module],
|
81 |
+
device: torch.device) -> np.ndarray:
|
82 |
+
model = model_dict[model_name]
|
83 |
+
seed = int(np.clip(seed, 0, np.iinfo(np.uint32).max))
|
84 |
+
|
85 |
+
z = generate_z(model.z_dim, seed, device)
|
86 |
+
label = torch.zeros([1, model.c_dim], device=device)
|
87 |
+
|
88 |
+
out = model(z, label, truncation_psi=truncation_psi)
|
89 |
+
out = (out.permute(0, 2, 3, 1) * 127.5 + 128).clamp(0, 255).to(torch.uint8)
|
90 |
+
return out[0].cpu().numpy()
|
91 |
+
|
92 |
+
|
93 |
+
def load_model(model_name: str, device: torch.device) -> nn.Module:
|
94 |
+
path = hf_hub_download('hysts/Self-Distilled-StyleGAN',
|
95 |
+
f'models/{model_name}_pytorch.pkl',
|
96 |
+
use_auth_token=TOKEN)
|
97 |
+
with open(path, 'rb') as f:
|
98 |
+
model = pickle.load(f)['G_ema']
|
99 |
+
model.eval()
|
100 |
+
model.to(device)
|
101 |
+
with torch.inference_mode():
|
102 |
+
z = torch.zeros((1, model.z_dim)).to(device)
|
103 |
+
label = torch.zeros([1, model.c_dim], device=device)
|
104 |
+
model(z, label)
|
105 |
+
return model
|
106 |
+
|
107 |
+
|
108 |
+
def main():
|
109 |
+
gr.close_all()
|
110 |
+
|
111 |
+
args = parse_args()
|
112 |
+
device = torch.device(args.device)
|
113 |
+
|
114 |
+
model_names = [
|
115 |
+
'dogs_1024',
|
116 |
+
'elephants_512',
|
117 |
+
'horses_256',
|
118 |
+
'bicycles_256',
|
119 |
+
'lions_512',
|
120 |
+
'giraffes_512',
|
121 |
+
'parrots_512',
|
122 |
+
]
|
123 |
+
|
124 |
+
model_dict = {name: load_model(name, device) for name in model_names}
|
125 |
+
|
126 |
+
func = functools.partial(generate_image,
|
127 |
+
model_dict=model_dict,
|
128 |
+
device=device)
|
129 |
+
func = functools.update_wrapper(func, generate_image)
|
130 |
+
|
131 |
+
gr.Interface(
|
132 |
+
func,
|
133 |
+
[
|
134 |
+
gr.inputs.Radio(
|
135 |
+
model_names, type='value', default='dogs_1024', label='Model'),
|
136 |
+
gr.inputs.Number(default=0, label='Seed'),
|
137 |
+
gr.inputs.Slider(
|
138 |
+
0, 2, step=0.05, default=0.7, label='Truncation psi'),
|
139 |
+
],
|
140 |
+
gr.outputs.Image(type='numpy', label='Output'),
|
141 |
+
title=TITLE,
|
142 |
+
description=DESCRIPTION,
|
143 |
+
article=ARTICLE,
|
144 |
+
theme=args.theme,
|
145 |
+
allow_screenshot=args.allow_screenshot,
|
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()
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
samples/bicycles.jpg
ADDED
Git LFS Details
|
samples/dogs.jpg
ADDED
Git LFS Details
|
samples/elephants.jpg
ADDED
Git LFS Details
|
samples/giraffes.jpg
ADDED
Git LFS Details
|
samples/horses.jpg
ADDED
Git LFS Details
|
samples/lions.jpg
ADDED
Git LFS Details
|
samples/parrots.jpg
ADDED
Git LFS Details
|
stylegan3
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
Subproject commit a5a69f58294509598714d1e88c9646c3d7c6ec94
|