hysts HF staff commited on
Commit
9b1f027
1 Parent(s): 32b09b7
Files changed (4) hide show
  1. .pre-commit-config.yaml +59 -34
  2. .style.yapf +0 -5
  3. .vscode/settings.json +30 -0
  4. app.py +20 -47
.pre-commit-config.yaml CHANGED
@@ -1,35 +1,60 @@
1
  repos:
2
- - repo: https://github.com/pre-commit/pre-commit-hooks
3
- rev: v4.2.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: double-quote-string-fixer
12
- - id: end-of-file-fixer
13
- - id: mixed-line-ending
14
- args: ['--fix=lf']
15
- - id: requirements-txt-fixer
16
- - id: trailing-whitespace
17
- - repo: https://github.com/myint/docformatter
18
- rev: v1.4
19
- hooks:
20
- - id: docformatter
21
- args: ['--in-place']
22
- - repo: https://github.com/pycqa/isort
23
- rev: 5.12.0
24
- hooks:
25
- - id: isort
26
- - repo: https://github.com/pre-commit/mirrors-mypy
27
- rev: v0.991
28
- hooks:
29
- - id: mypy
30
- args: ['--ignore-missing-imports']
31
- - repo: https://github.com/google/yapf
32
- rev: v0.32.0
33
- hooks:
34
- - id: yapf
35
- args: ['--parallel', '--in-place']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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"]
.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
+ }
app.py CHANGED
@@ -12,16 +12,16 @@ import torch
12
  import torch.nn as nn
13
  from huggingface_hub import hf_hub_download
14
 
15
- sys.path.insert(0, 'StyleGAN-Human')
16
 
17
- TITLE = 'StyleGAN-Human (Interpolation)'
18
- DESCRIPTION = 'https://github.com/stylegan-human/StyleGAN-Human'
19
 
20
 
21
  def load_model(file_name: str, device: torch.device) -> nn.Module:
22
- path = hf_hub_download('public-data/StyleGAN-Human', f'models/{file_name}')
23
- with open(path, 'rb') as f:
24
- model = pickle.load(f)['G_ema']
25
  model.eval()
26
  model.to(device)
27
  with torch.inference_mode():
@@ -32,15 +32,13 @@ def load_model(file_name: str, device: torch.device) -> nn.Module:
32
 
33
 
34
  def generate_z(z_dim: int, seed: int, device: torch.device) -> torch.Tensor:
35
- return torch.from_numpy(np.random.RandomState(seed).randn(
36
- 1, z_dim)).to(device).float()
37
 
38
 
39
  @torch.inference_mode()
40
- def generate_interpolated_images(seed0: int, psi0: float, seed1: int,
41
- psi1: float, num_intermediate: int,
42
- model: nn.Module,
43
- device: torch.device) -> list[np.ndarray]:
44
  seed0 = int(np.clip(seed0, 0, np.iinfo(np.uint32).max))
45
  seed1 = int(np.clip(seed1, 0, np.iinfo(np.uint32).max))
46
 
@@ -57,51 +55,26 @@ def generate_interpolated_images(seed0: int, psi0: float, seed1: int,
57
  res = []
58
  for z, psi in zip(zs, psis):
59
  out = model(z, label, truncation_psi=psi, force_fp32=True)
60
- out = (out.permute(0, 2, 3, 1) * 127.5 + 128).clamp(0, 255).to(
61
- torch.uint8)
62
  out = out[0].cpu().numpy()
63
  res.append(out)
64
  return res
65
 
66
 
67
- device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
68
- model = load_model('stylegan_human_v2_1024.pkl', device)
69
- fn = functools.partial(generate_interpolated_images,
70
- model=model,
71
- device=device)
72
 
73
  gr.Interface(
74
  fn=fn,
75
  inputs=[
76
- gr.Slider(label='Seed 1',
77
- minimum=0,
78
- maximum=100000,
79
- step=1,
80
- value=0,
81
- randomize=True),
82
- gr.Slider(label='Truncation psi 1',
83
- minimum=0,
84
- maximum=2,
85
- step=0.05,
86
- value=0.7),
87
- gr.Slider(label='Seed 2',
88
- minimum=0,
89
- maximum=100000,
90
- step=1,
91
- value=1,
92
- randomize=True),
93
- gr.Slider(label='Truncation psi 2',
94
- minimum=0,
95
- maximum=2,
96
- step=0.05,
97
- value=0.7),
98
- gr.Slider(label='Number of Intermediate Frames',
99
- minimum=0,
100
- maximum=21,
101
- step=1,
102
- value=7),
103
  ],
104
- outputs=gr.Gallery(label='Output Images', type='numpy'),
105
  title=TITLE,
106
  description=DESCRIPTION,
107
  ).queue(max_size=10).launch()
 
12
  import torch.nn as nn
13
  from huggingface_hub import hf_hub_download
14
 
15
+ sys.path.insert(0, "StyleGAN-Human")
16
 
17
+ TITLE = "StyleGAN-Human (Interpolation)"
18
+ DESCRIPTION = "https://github.com/stylegan-human/StyleGAN-Human"
19
 
20
 
21
  def load_model(file_name: str, device: torch.device) -> nn.Module:
22
+ path = hf_hub_download("public-data/StyleGAN-Human", f"models/{file_name}")
23
+ with open(path, "rb") as f:
24
+ model = pickle.load(f)["G_ema"]
25
  model.eval()
26
  model.to(device)
27
  with torch.inference_mode():
 
32
 
33
 
34
  def generate_z(z_dim: int, seed: int, device: torch.device) -> torch.Tensor:
35
+ return torch.from_numpy(np.random.RandomState(seed).randn(1, z_dim)).to(device).float()
 
36
 
37
 
38
  @torch.inference_mode()
39
+ def generate_interpolated_images(
40
+ seed0: int, psi0: float, seed1: int, psi1: float, num_intermediate: int, model: nn.Module, device: torch.device
41
+ ) -> list[np.ndarray]:
 
42
  seed0 = int(np.clip(seed0, 0, np.iinfo(np.uint32).max))
43
  seed1 = int(np.clip(seed1, 0, np.iinfo(np.uint32).max))
44
 
 
55
  res = []
56
  for z, psi in zip(zs, psis):
57
  out = model(z, label, truncation_psi=psi, force_fp32=True)
58
+ out = (out.permute(0, 2, 3, 1) * 127.5 + 128).clamp(0, 255).to(torch.uint8)
 
59
  out = out[0].cpu().numpy()
60
  res.append(out)
61
  return res
62
 
63
 
64
+ device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
65
+ model = load_model("stylegan_human_v2_1024.pkl", device)
66
+ fn = functools.partial(generate_interpolated_images, model=model, device=device)
 
 
67
 
68
  gr.Interface(
69
  fn=fn,
70
  inputs=[
71
+ gr.Slider(label="Seed 1", minimum=0, maximum=100000, step=1, value=0, randomize=True),
72
+ gr.Slider(label="Truncation psi 1", minimum=0, maximum=2, step=0.05, value=0.7),
73
+ gr.Slider(label="Seed 2", minimum=0, maximum=100000, step=1, value=1, randomize=True),
74
+ gr.Slider(label="Truncation psi 2", minimum=0, maximum=2, step=0.05, value=0.7),
75
+ gr.Slider(label="Number of Intermediate Frames", minimum=0, maximum=21, step=1, value=7),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
  ],
77
+ outputs=gr.Gallery(label="Output Images", type="numpy"),
78
  title=TITLE,
79
  description=DESCRIPTION,
80
  ).queue(max_size=10).launch()