hysts HF staff commited on
Commit
88298b8
1 Parent(s): 4dce061
Files changed (4) hide show
  1. .gitmodules +3 -0
  2. StyleGAN-Human +1 -0
  3. app.py +108 -0
  4. requirements.txt +5 -0
.gitmodules ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ [submodule "StyleGAN-Human"]
2
+ path = StyleGAN-Human
3
+ url = https://github.com/stylegan-human/StyleGAN-Human
StyleGAN-Human ADDED
@@ -0,0 +1 @@
 
 
1
+ Subproject commit d2514c145a451453804f60a12de8f13d40c9fe4f
app.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ 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, 'StyleGAN-Human')
18
+
19
+ TITLE = 'StyleGAN-Human'
20
+ DESCRIPTION = 'This is a demo for https://github.com/stylegan-human/StyleGAN-Human.'
21
+ ARTICLE = None
22
+
23
+ TOKEN = os.environ['TOKEN']
24
+
25
+
26
+ def parse_args() -> argparse.Namespace:
27
+ parser = argparse.ArgumentParser()
28
+ parser.add_argument('--device', type=str, default='cpu')
29
+ parser.add_argument('--theme', type=str)
30
+ parser.add_argument('--live', action='store_true')
31
+ parser.add_argument('--share', action='store_true')
32
+ parser.add_argument('--port', type=int)
33
+ parser.add_argument('--disable-queue',
34
+ dest='enable_queue',
35
+ action='store_false')
36
+ parser.add_argument('--allow-flagging', type=str, default='never')
37
+ parser.add_argument('--allow-screenshot', action='store_true')
38
+ return parser.parse_args()
39
+
40
+
41
+ def generate_z(z_dim: int, seed: int, device: torch.device) -> torch.Tensor:
42
+ return torch.from_numpy(np.random.RandomState(seed).randn(
43
+ 1, z_dim)).to(device).float()
44
+
45
+
46
+ @torch.inference_mode()
47
+ def generate_image(seed: int, truncation_psi: float, model: nn.Module,
48
+ device: torch.device) -> np.ndarray:
49
+ seed = int(np.clip(seed, 0, np.iinfo(np.uint32).max))
50
+
51
+ z = generate_z(model.z_dim, seed, device)
52
+ label = torch.zeros([1, model.c_dim], device=device)
53
+
54
+ out = model(z, label, truncation_psi=truncation_psi, force_fp32=True)
55
+ out = (out.permute(0, 2, 3, 1) * 127.5 + 128).clamp(0, 255).to(torch.uint8)
56
+ return out[0].cpu().numpy()
57
+
58
+
59
+ def load_model(file_name: str, device: torch.device) -> nn.Module:
60
+ path = hf_hub_download('hysts/StyleGAN-Human',
61
+ f'models/{file_name}',
62
+ use_auth_token=TOKEN)
63
+ with open(path, 'rb') as f:
64
+ model = pickle.load(f)['G_ema']
65
+ model.eval()
66
+ model.to(device)
67
+ with torch.inference_mode():
68
+ z = torch.zeros((1, model.z_dim)).to(device)
69
+ label = torch.zeros([1, model.c_dim], device=device)
70
+ model(z, label, force_fp32=True)
71
+ return model
72
+
73
+
74
+ def main():
75
+ gr.close_all()
76
+
77
+ args = parse_args()
78
+ device = torch.device(args.device)
79
+
80
+ model = load_model('stylegan_human_v2_1024.pkl', device)
81
+
82
+ func = functools.partial(generate_image, model=model, device=device)
83
+ func = functools.update_wrapper(func, generate_image)
84
+
85
+ gr.Interface(
86
+ func,
87
+ [
88
+ gr.inputs.Number(default=0, label='Seed'),
89
+ gr.inputs.Slider(
90
+ 0, 2, step=0.05, default=0.7, label='Truncation psi'),
91
+ ],
92
+ gr.outputs.Image(type='numpy', label='Output'),
93
+ title=TITLE,
94
+ description=DESCRIPTION,
95
+ article=ARTICLE,
96
+ theme=args.theme,
97
+ allow_screenshot=args.allow_screenshot,
98
+ allow_flagging=args.allow_flagging,
99
+ live=args.live,
100
+ ).launch(
101
+ enable_queue=args.enable_queue,
102
+ server_port=args.port,
103
+ share=args.share,
104
+ )
105
+
106
+
107
+ if __name__ == '__main__':
108
+ main()
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ numpy==1.22.3
2
+ Pillow==9.1.0
3
+ scipy==1.8.0
4
+ torch==1.11.0
5
+ torchvision==0.12.0