hysts HF staff commited on
Commit
f8fdc71
1 Parent(s): d8b8cd5
Files changed (7) hide show
  1. .pre-commit-config.yaml +36 -0
  2. README.md +2 -1
  3. app.py +89 -76
  4. patch +0 -54
  5. patch-cpu +54 -0
  6. requirements.txt +4 -4
  7. style.css +7 -0
.pre-commit-config.yaml ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ exclude: ^patch
2
+ repos:
3
+ - repo: https://github.com/pre-commit/pre-commit-hooks
4
+ rev: v4.2.0
5
+ hooks:
6
+ - id: check-executables-have-shebangs
7
+ - id: check-json
8
+ - id: check-merge-conflict
9
+ - id: check-shebang-scripts-are-executable
10
+ - id: check-toml
11
+ - id: check-yaml
12
+ - id: double-quote-string-fixer
13
+ - id: end-of-file-fixer
14
+ - id: mixed-line-ending
15
+ args: ['--fix=lf']
16
+ - id: requirements-txt-fixer
17
+ - id: trailing-whitespace
18
+ - repo: https://github.com/myint/docformatter
19
+ rev: v1.4
20
+ hooks:
21
+ - id: docformatter
22
+ args: ['--in-place']
23
+ - repo: https://github.com/pycqa/isort
24
+ rev: 5.12.0
25
+ hooks:
26
+ - id: isort
27
+ - repo: https://github.com/pre-commit/mirrors-mypy
28
+ rev: v0.991
29
+ hooks:
30
+ - id: mypy
31
+ args: ['--ignore-missing-imports']
32
+ - repo: https://github.com/google/yapf
33
+ rev: v0.32.0
34
+ hooks:
35
+ - id: yapf
36
+ args: ['--parallel', '--in-place']
README.md CHANGED
@@ -4,9 +4,10 @@ emoji: 📊
4
  colorFrom: red
5
  colorTo: yellow
6
  sdk: gradio
7
- sdk_version: 3.0.17
8
  app_file: app.py
9
  pinned: false
 
10
  ---
11
 
12
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces#reference
4
  colorFrom: red
5
  colorTo: yellow
6
  sdk: gradio
7
+ sdk_version: 3.36.1
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
app.py CHANGED
@@ -2,9 +2,10 @@
2
 
3
  from __future__ import annotations
4
 
5
- import argparse
6
  import functools
7
  import os
 
 
8
  import subprocess
9
  import sys
10
 
@@ -16,16 +17,20 @@ from huggingface_hub import hf_hub_download
16
 
17
  if os.environ.get('SYSTEM') == 'spaces':
18
  with open('patch') as f:
19
- subprocess.run('patch -p1'.split(), cwd='stylegan2-pytorch', stdin=f)
 
 
 
 
 
 
 
20
 
21
  sys.path.insert(0, 'stylegan2-pytorch')
22
 
23
  from model import Generator
24
 
25
- TITLE = 'TADNE (This Anime Does Not Exist) Interpolation'
26
- DESCRIPTION = '''The original TADNE site is https://thisanimedoesnotexist.ai/.
27
-
28
- Expected execution time on Hugging Face Spaces: 4s for one image
29
 
30
  Related Apps:
31
  - [TADNE](https://huggingface.co/spaces/hysts/TADNE)
@@ -33,26 +38,19 @@ Related Apps:
33
  - [TADNE Image Selector](https://huggingface.co/spaces/hysts/TADNE-image-selector)
34
  - [TADNE Image Search with DeepDanbooru](https://huggingface.co/spaces/hysts/TADNE-image-search-with-DeepDanbooru)
35
  '''
36
- ARTICLE = '<center><img src="https://visitor-badge.glitch.me/badge?page_id=hysts.tadne-interpolation" alt="visitor badge"/></center>'
 
37
 
38
 
39
- def parse_args() -> argparse.Namespace:
40
- parser = argparse.ArgumentParser()
41
- parser.add_argument('--device', type=str, default='cpu')
42
- parser.add_argument('--theme', type=str)
43
- parser.add_argument('--live', action='store_true')
44
- parser.add_argument('--share', action='store_true')
45
- parser.add_argument('--port', type=int)
46
- parser.add_argument('--disable-queue',
47
- dest='enable_queue',
48
- action='store_false')
49
- parser.add_argument('--allow-flagging', type=str, default='never')
50
- return parser.parse_args()
51
 
52
 
53
  def load_model(device: torch.device) -> nn.Module:
54
  model = Generator(512, 1024, 4, channel_multiplier=2)
55
- path = hf_hub_download('hysts/TADNE',
56
  'models/aydao-anime-danbooru2019s-512-5268480.pt')
57
  checkpoint = torch.load(path)
58
  model.load_state_dict(checkpoint['g_ema'])
@@ -86,14 +84,10 @@ def generate_interpolated_images(seed0: int, seed1: int, num_intermediate: int,
86
  psi0: float, psi1: float,
87
  randomize_noise: bool, model: nn.Module,
88
  device: torch.device) -> list[np.ndarray]:
89
- seed0 = int(np.clip(seed0, 0, np.iinfo(np.uint32).max))
90
- seed1 = int(np.clip(seed1, 0, np.iinfo(np.uint32).max))
91
 
92
  z0 = generate_z(model.style_dim, seed0, device)
93
- if num_intermediate == -1:
94
- out = generate_image(model, z0, psi0, randomize_noise)
95
- return [out], None
96
-
97
  z1 = generate_z(model.style_dim, seed1, device)
98
  vec = z1 - z0
99
  dvec = vec / (num_intermediate + 1)
@@ -107,56 +101,75 @@ def generate_interpolated_images(seed0: int, seed1: int, num_intermediate: int,
107
  return res
108
 
109
 
110
- def main():
111
- args = parse_args()
112
- device = torch.device(args.device)
113
-
114
- model = load_model(device)
115
-
116
- func = functools.partial(generate_interpolated_images,
117
- model=model,
118
- device=device)
119
- func = functools.update_wrapper(func, generate_interpolated_images)
120
-
121
- examples = [
122
- [29703, 55376, 3, 0.7, 0.7, False],
123
- [34141, 36864, 5, 0.7, 0.7, False],
124
- [74650, 88322, 7, 0.7, 0.7, False],
125
- [84314, 70317410, 9, 0.7, 0.7, False],
126
- [55376, 55376, 5, 0.3, 1.3, False],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
  ]
128
-
129
- gr.Interface(
130
- func,
131
- [
132
- gr.inputs.Number(default=29703, label='Seed 1'),
133
- gr.inputs.Number(default=55376, label='Seed 2'),
134
- gr.inputs.Slider(-1,
135
- 21,
136
- step=1,
137
- default=3,
138
- label='Number of Intermediate Frames'),
139
- gr.inputs.Slider(
140
- 0, 2, step=0.05, default=0.7, label='Truncation psi 1'),
141
- gr.inputs.Slider(
142
- 0, 2, step=0.05, default=0.7, label='Truncation psi 2'),
143
- gr.inputs.Checkbox(default=False, label='Randomize Noise'),
144
- ],
145
- gr.Gallery(type='numpy', label='Output Images'),
146
  examples=examples,
147
- title=TITLE,
148
- description=DESCRIPTION,
149
- article=ARTICLE,
150
- theme=args.theme,
151
- allow_flagging=args.allow_flagging,
152
- live=args.live,
153
- cache_examples=False,
154
- ).launch(
155
- enable_queue=args.enable_queue,
156
- server_port=args.port,
157
- share=args.share,
158
  )
159
-
160
-
161
- if __name__ == '__main__':
162
- main()
 
 
 
2
 
3
  from __future__ import annotations
4
 
 
5
  import functools
6
  import os
7
+ import random
8
+ import shlex
9
  import subprocess
10
  import sys
11
 
17
 
18
  if os.environ.get('SYSTEM') == 'spaces':
19
  with open('patch') as f:
20
+ subprocess.run(shlex.split('patch -p1'),
21
+ cwd='stylegan2-pytorch',
22
+ stdin=f)
23
+ if not torch.cuda.is_available():
24
+ with open('patch-cpu') as f:
25
+ subprocess.run(shlex.split('patch -p1'),
26
+ cwd='stylegan2-pytorch',
27
+ stdin=f)
28
 
29
  sys.path.insert(0, 'stylegan2-pytorch')
30
 
31
  from model import Generator
32
 
33
+ DESCRIPTION = '''# [TADNE](https://thisanimedoesnotexist.ai/) (This Anime Does Not Exist) interpolation
 
 
 
34
 
35
  Related Apps:
36
  - [TADNE](https://huggingface.co/spaces/hysts/TADNE)
38
  - [TADNE Image Selector](https://huggingface.co/spaces/hysts/TADNE-image-selector)
39
  - [TADNE Image Search with DeepDanbooru](https://huggingface.co/spaces/hysts/TADNE-image-search-with-DeepDanbooru)
40
  '''
41
+
42
+ MAX_SEED = np.iinfo(np.int32).max
43
 
44
 
45
+ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
46
+ if randomize_seed:
47
+ seed = random.randint(0, MAX_SEED)
48
+ return seed
 
 
 
 
 
 
 
 
49
 
50
 
51
  def load_model(device: torch.device) -> nn.Module:
52
  model = Generator(512, 1024, 4, channel_multiplier=2)
53
+ path = hf_hub_download('public-data/TADNE',
54
  'models/aydao-anime-danbooru2019s-512-5268480.pt')
55
  checkpoint = torch.load(path)
56
  model.load_state_dict(checkpoint['g_ema'])
84
  psi0: float, psi1: float,
85
  randomize_noise: bool, model: nn.Module,
86
  device: torch.device) -> list[np.ndarray]:
87
+ seed0 = int(np.clip(seed0, 0, MAX_SEED))
88
+ seed1 = int(np.clip(seed1, 0, MAX_SEED))
89
 
90
  z0 = generate_z(model.style_dim, seed0, device)
 
 
 
 
91
  z1 = generate_z(model.style_dim, seed1, device)
92
  vec = z1 - z0
93
  dvec = vec / (num_intermediate + 1)
101
  return res
102
 
103
 
104
+ device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
105
+ model = load_model(device)
106
+ fn = functools.partial(generate_interpolated_images,
107
+ model=model,
108
+ device=device)
109
+
110
+ examples = [
111
+ [29703, 55376, 3, 0.7, 0.7, False],
112
+ [34141, 36864, 5, 0.7, 0.7, False],
113
+ [74650, 88322, 7, 0.7, 0.7, False],
114
+ [84314, 70317410, 9, 0.7, 0.7, False],
115
+ [55376, 55376, 5, 0.3, 1.3, False],
116
+ ]
117
+
118
+ with gr.Blocks(css='style.css') as demo:
119
+ gr.Markdown(DESCRIPTION)
120
+ with gr.Row():
121
+ with gr.Column():
122
+ seed_1 = gr.Slider(label='Seed 1',
123
+ minimum=0,
124
+ maximum=MAX_SEED,
125
+ step=1,
126
+ value=29703)
127
+ seed_2 = gr.Slider(label='Seed 2',
128
+ minimum=0,
129
+ maximum=MAX_SEED,
130
+ step=1,
131
+ value=55376)
132
+ num_intermediate_frames = gr.Slider(
133
+ label='Number of Intermediate Frames',
134
+ minimum=1,
135
+ maximum=21,
136
+ step=1,
137
+ value=3,
138
+ )
139
+ psi_1 = gr.Slider(label='Truncation psi 1',
140
+ minimum=0,
141
+ maximum=2,
142
+ step=0.05,
143
+ value=0.7)
144
+ psi_2 = gr.Slider(label='Truncation psi 2',
145
+ minimum=0,
146
+ maximum=2,
147
+ step=0.05,
148
+ value=0.7)
149
+ randomize_noise = gr.Checkbox(label='Randomize Noise', value=False)
150
+ run_button = gr.Button('Run')
151
+ with gr.Column():
152
+ result = gr.Gallery(label='Output')
153
+
154
+ inputs = [
155
+ seed_1,
156
+ seed_2,
157
+ num_intermediate_frames,
158
+ psi_1,
159
+ psi_2,
160
+ randomize_noise,
161
  ]
162
+ gr.Examples(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163
  examples=examples,
164
+ inputs=inputs,
165
+ outputs=result,
166
+ fn=fn,
167
+ cache_examples=os.getenv('CACHE_EXAMPLES') == '1',
 
 
 
 
 
 
 
168
  )
169
+ run_button.click(
170
+ fn=fn,
171
+ inputs=inputs,
172
+ outputs=result,
173
+ api_name='run',
174
+ )
175
+ demo.queue(max_size=10).launch()
patch CHANGED
@@ -29,57 +29,3 @@ index 0134c39..3a7826c 100755
29
  style_t = []
30
 
31
  for style in styles:
32
- diff --git a/op/fused_act.py b/op/fused_act.py
33
- index 5d46e10..bc522ed 100755
34
- --- a/op/fused_act.py
35
- +++ b/op/fused_act.py
36
- @@ -1,5 +1,3 @@
37
- -import os
38
- -
39
- import torch
40
- from torch import nn
41
- from torch.nn import functional as F
42
- @@ -7,16 +5,6 @@ from torch.autograd import Function
43
- from torch.utils.cpp_extension import load
44
-
45
-
46
- -module_path = os.path.dirname(__file__)
47
- -fused = load(
48
- - "fused",
49
- - sources=[
50
- - os.path.join(module_path, "fused_bias_act.cpp"),
51
- - os.path.join(module_path, "fused_bias_act_kernel.cu"),
52
- - ],
53
- -)
54
- -
55
- -
56
- class FusedLeakyReLUFunctionBackward(Function):
57
- @staticmethod
58
- def forward(ctx, grad_output, out, bias, negative_slope, scale):
59
- diff --git a/op/upfirdn2d.py b/op/upfirdn2d.py
60
- index 67e0375..6c5840e 100755
61
- --- a/op/upfirdn2d.py
62
- +++ b/op/upfirdn2d.py
63
- @@ -1,5 +1,4 @@
64
- from collections import abc
65
- -import os
66
-
67
- import torch
68
- from torch.nn import functional as F
69
- @@ -7,16 +6,6 @@ from torch.autograd import Function
70
- from torch.utils.cpp_extension import load
71
-
72
-
73
- -module_path = os.path.dirname(__file__)
74
- -upfirdn2d_op = load(
75
- - "upfirdn2d",
76
- - sources=[
77
- - os.path.join(module_path, "upfirdn2d.cpp"),
78
- - os.path.join(module_path, "upfirdn2d_kernel.cu"),
79
- - ],
80
- -)
81
- -
82
- -
83
- class UpFirDn2dBackward(Function):
84
- @staticmethod
85
- def forward(
29
  style_t = []
30
 
31
  for style in styles:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
patch-cpu ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ diff --git a/op/fused_act.py b/op/fused_act.py
2
+ index 5d46e10..bc522ed 100755
3
+ --- a/op/fused_act.py
4
+ +++ b/op/fused_act.py
5
+ @@ -1,5 +1,3 @@
6
+ -import os
7
+ -
8
+ import torch
9
+ from torch import nn
10
+ from torch.nn import functional as F
11
+ @@ -7,16 +5,6 @@ from torch.autograd import Function
12
+ from torch.utils.cpp_extension import load
13
+
14
+
15
+ -module_path = os.path.dirname(__file__)
16
+ -fused = load(
17
+ - "fused",
18
+ - sources=[
19
+ - os.path.join(module_path, "fused_bias_act.cpp"),
20
+ - os.path.join(module_path, "fused_bias_act_kernel.cu"),
21
+ - ],
22
+ -)
23
+ -
24
+ -
25
+ class FusedLeakyReLUFunctionBackward(Function):
26
+ @staticmethod
27
+ def forward(ctx, grad_output, out, bias, negative_slope, scale):
28
+ diff --git a/op/upfirdn2d.py b/op/upfirdn2d.py
29
+ index 67e0375..6c5840e 100755
30
+ --- a/op/upfirdn2d.py
31
+ +++ b/op/upfirdn2d.py
32
+ @@ -1,5 +1,4 @@
33
+ from collections import abc
34
+ -import os
35
+
36
+ import torch
37
+ from torch.nn import functional as F
38
+ @@ -7,16 +6,6 @@ from torch.autograd import Function
39
+ from torch.utils.cpp_extension import load
40
+
41
+
42
+ -module_path = os.path.dirname(__file__)
43
+ -upfirdn2d_op = load(
44
+ - "upfirdn2d",
45
+ - sources=[
46
+ - os.path.join(module_path, "upfirdn2d.cpp"),
47
+ - os.path.join(module_path, "upfirdn2d_kernel.cu"),
48
+ - ],
49
+ -)
50
+ -
51
+ -
52
+ class UpFirDn2dBackward(Function):
53
+ @staticmethod
54
+ def forward(
requirements.txt CHANGED
@@ -1,4 +1,4 @@
1
- numpy==1.22.3
2
- Pillow==9.0.1
3
- torch==1.11.0
4
- torchvision==0.12.0
1
+ numpy==1.23.5
2
+ Pillow==10.0.0
3
+ torch==2.0.1
4
+ torchvision==0.15.2
style.css ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
1
+ h1 {
2
+ text-align: center;
3
+ }
4
+
5
+ #duplicate-button {
6
+ margin: auto;
7
+ }