hysts HF staff commited on
Commit
ae149aa
1 Parent(s): 9735cfa
Files changed (7) hide show
  1. .pre-commit-config.yaml +59 -34
  2. .style.yapf +0 -5
  3. .vscode/settings.json +30 -0
  4. README.md +1 -1
  5. app.py +62 -75
  6. requirements.txt +3 -3
  7. style.css +8 -0
.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.5.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.8.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.2.0
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.7.1
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
+ }
README.md CHANGED
@@ -4,7 +4,7 @@ emoji: 😻
4
  colorFrom: purple
5
  colorTo: red
6
  sdk: gradio
7
- sdk_version: 3.36.1
8
  app_file: app.py
9
  pinned: false
10
  suggested_hardware: t4-small
 
4
  colorFrom: purple
5
  colorTo: red
6
  sdk: gradio
7
+ sdk_version: 4.19.2
8
  app_file: app.py
9
  pinned: false
10
  suggested_hardware: t4-small
app.py CHANGED
@@ -8,24 +8,24 @@ import gradio as gr
8
 
9
  from dualstylegan import Model
10
 
11
- DESCRIPTION = '''# Portrait Style Transfer with [DualStyleGAN](https://github.com/williamyang1991/DualStyleGAN)
12
 
13
  <img id="overview" alt="overview" src="https://raw.githubusercontent.com/williamyang1991/DualStyleGAN/main/doc_images/overview.jpg" />
14
- '''
15
 
16
 
17
  def get_style_image_url(style_name: str) -> str:
18
- base_url = 'https://raw.githubusercontent.com/williamyang1991/DualStyleGAN/main/doc_images'
19
  filenames = {
20
- 'cartoon': 'cartoon_overview.jpg',
21
- 'caricature': 'caricature_overview.jpg',
22
- 'anime': 'anime_overview.jpg',
23
- 'arcane': 'Reconstruction_arcane_overview.jpg',
24
- 'comic': 'Reconstruction_comic_overview.jpg',
25
- 'pixar': 'Reconstruction_pixar_overview.jpg',
26
- 'slamdunk': 'Reconstruction_slamdunk_overview.jpg',
27
  }
28
- return f'{base_url}/{filenames[style_name]}'
29
 
30
 
31
  def get_style_image_markdown_text(style_name: str) -> str:
@@ -35,125 +35,110 @@ def get_style_image_markdown_text(style_name: str) -> str:
35
 
36
  def update_slider(choice: str) -> dict:
37
  max_vals = {
38
- 'cartoon': 316,
39
- 'caricature': 198,
40
- 'anime': 173,
41
- 'arcane': 99,
42
- 'comic': 100,
43
- 'pixar': 121,
44
- 'slamdunk': 119,
45
  }
46
- return gr.update(maximum=max_vals[choice])
47
 
48
 
49
  def update_style_image(style_name: str) -> dict:
50
  text = get_style_image_markdown_text(style_name)
51
- return gr.update(value=text)
52
 
53
 
54
  model = Model()
55
 
56
- with gr.Blocks(css='style.css') as demo:
57
  gr.Markdown(DESCRIPTION)
58
 
59
- with gr.Box():
60
- gr.Markdown('''## Step 1 (Preprocess Input Image)
 
61
 
62
  - Drop an image containing a near-frontal face to the **Input Image**.
63
  - If there are multiple faces in the image, hit the Edit button in the upper right corner and crop the input image beforehand.
64
  - Hit the **Preprocess** button.
65
  - Choose the encoder version. Default is Z+ encoder which has better stylization performance. W+ encoder better reconstructs the input image to preserve more details.
66
  - The final result will be based on this **Reconstructed Face**. So, if the reconstructed image is not satisfactory, you may want to change the input image.
67
- ''')
 
68
  with gr.Row():
69
- encoder_type = gr.Radio(label='Encoder Type',
70
- choices=[
71
- 'Z+ encoder (better stylization)',
72
- 'W+ encoder (better reconstruction)'
73
- ],
74
- value='Z+ encoder (better stylization)')
75
  with gr.Row():
76
  with gr.Column():
77
  with gr.Row():
78
- input_image = gr.Image(label='Input Image',
79
- type='filepath')
80
  with gr.Row():
81
- preprocess_button = gr.Button('Preprocess')
82
  with gr.Column():
83
  with gr.Row():
84
- aligned_face = gr.Image(label='Aligned Face',
85
- type='numpy',
86
- interactive=False)
87
  with gr.Column():
88
- reconstructed_face = gr.Image(label='Reconstructed Face',
89
- type='numpy')
90
  instyle = gr.State()
91
 
92
  with gr.Row():
93
- paths = sorted(pathlib.Path('images').glob('*.jpg'))
94
- gr.Examples(examples=[[path.as_posix()] for path in paths],
95
- inputs=input_image)
96
 
97
- with gr.Box():
98
- gr.Markdown('''## Step 2 (Select Style Image)
 
99
 
100
  - Select **Style Type**.
101
  - Select **Style Image Index** from the image table below.
102
- ''')
 
103
  with gr.Row():
104
  with gr.Column():
105
- style_type = gr.Radio(label='Style Type',
106
- choices=model.style_types,
107
- value=model.style_types[0])
108
- text = get_style_image_markdown_text('cartoon')
109
  style_image = gr.Markdown(value=text)
110
- style_index = gr.Slider(label='Style Image Index',
111
- minimum=0,
112
- maximum=316,
113
- step=1,
114
- value=26)
115
 
116
  with gr.Row():
117
  gr.Examples(
118
  examples=[
119
- ['cartoon', 26],
120
- ['caricature', 65],
121
- ['arcane', 63],
122
- ['pixar', 80],
123
  ],
124
  inputs=[style_type, style_index],
125
  )
126
 
127
- with gr.Box():
128
- gr.Markdown('''## Step 3 (Generate Style Transferred Image)
 
129
 
130
  - Adjust **Structure Weight** and **Color Weight**.
131
  - These are weights for the style image, so the larger the value, the closer the resulting image will be to the style image.
132
  - Tips: For W+ encoder, better way of (Structure Only) is to uncheck (Structure Only) and set Color weight to 0.
133
  - Hit the **Generate** button.
134
- ''')
 
135
  with gr.Row():
136
  with gr.Column():
137
  with gr.Row():
138
- structure_weight = gr.Slider(label='Structure Weight',
139
- minimum=0,
140
- maximum=1,
141
- step=0.1,
142
- value=0.6)
143
  with gr.Row():
144
- color_weight = gr.Slider(label='Color Weight',
145
- minimum=0,
146
- maximum=1,
147
- step=0.1,
148
- value=1)
149
  with gr.Row():
150
- structure_only = gr.Checkbox(label='Structure Only',
151
- value=False)
152
  with gr.Row():
153
- generate_button = gr.Button('Generate')
154
 
155
  with gr.Column():
156
- result = gr.Image(label='Result')
157
 
158
  with gr.Row():
159
  gr.Examples(
@@ -201,4 +186,6 @@ with gr.Blocks(css='style.css') as demo:
201
  ],
202
  outputs=result,
203
  )
204
- demo.queue(max_size=20).launch()
 
 
 
8
 
9
  from dualstylegan import Model
10
 
11
+ DESCRIPTION = """# Portrait Style Transfer with [DualStyleGAN](https://github.com/williamyang1991/DualStyleGAN)
12
 
13
  <img id="overview" alt="overview" src="https://raw.githubusercontent.com/williamyang1991/DualStyleGAN/main/doc_images/overview.jpg" />
14
+ """
15
 
16
 
17
  def get_style_image_url(style_name: str) -> str:
18
+ base_url = "https://raw.githubusercontent.com/williamyang1991/DualStyleGAN/main/doc_images"
19
  filenames = {
20
+ "cartoon": "cartoon_overview.jpg",
21
+ "caricature": "caricature_overview.jpg",
22
+ "anime": "anime_overview.jpg",
23
+ "arcane": "Reconstruction_arcane_overview.jpg",
24
+ "comic": "Reconstruction_comic_overview.jpg",
25
+ "pixar": "Reconstruction_pixar_overview.jpg",
26
+ "slamdunk": "Reconstruction_slamdunk_overview.jpg",
27
  }
28
+ return f"{base_url}/{filenames[style_name]}"
29
 
30
 
31
  def get_style_image_markdown_text(style_name: str) -> str:
 
35
 
36
  def update_slider(choice: str) -> dict:
37
  max_vals = {
38
+ "cartoon": 316,
39
+ "caricature": 198,
40
+ "anime": 173,
41
+ "arcane": 99,
42
+ "comic": 100,
43
+ "pixar": 121,
44
+ "slamdunk": 119,
45
  }
46
+ return gr.Slider(maximum=max_vals[choice])
47
 
48
 
49
  def update_style_image(style_name: str) -> dict:
50
  text = get_style_image_markdown_text(style_name)
51
+ return gr.Markdown(value=text)
52
 
53
 
54
  model = Model()
55
 
56
+ with gr.Blocks(css="style.css") as demo:
57
  gr.Markdown(DESCRIPTION)
58
 
59
+ with gr.Group():
60
+ gr.Markdown(
61
+ """## Step 1 (Preprocess Input Image)
62
 
63
  - Drop an image containing a near-frontal face to the **Input Image**.
64
  - If there are multiple faces in the image, hit the Edit button in the upper right corner and crop the input image beforehand.
65
  - Hit the **Preprocess** button.
66
  - Choose the encoder version. Default is Z+ encoder which has better stylization performance. W+ encoder better reconstructs the input image to preserve more details.
67
  - The final result will be based on this **Reconstructed Face**. So, if the reconstructed image is not satisfactory, you may want to change the input image.
68
+ """
69
+ )
70
  with gr.Row():
71
+ encoder_type = gr.Radio(
72
+ label="Encoder Type",
73
+ choices=["Z+ encoder (better stylization)", "W+ encoder (better reconstruction)"],
74
+ value="Z+ encoder (better stylization)",
75
+ )
 
76
  with gr.Row():
77
  with gr.Column():
78
  with gr.Row():
79
+ input_image = gr.Image(label="Input Image", type="filepath")
 
80
  with gr.Row():
81
+ preprocess_button = gr.Button("Preprocess")
82
  with gr.Column():
83
  with gr.Row():
84
+ aligned_face = gr.Image(label="Aligned Face", type="numpy", interactive=False)
 
 
85
  with gr.Column():
86
+ reconstructed_face = gr.Image(label="Reconstructed Face", type="numpy")
 
87
  instyle = gr.State()
88
 
89
  with gr.Row():
90
+ paths = sorted(pathlib.Path("images").glob("*.jpg"))
91
+ gr.Examples(examples=[[path.as_posix()] for path in paths], inputs=input_image)
 
92
 
93
+ with gr.Group():
94
+ gr.Markdown(
95
+ """## Step 2 (Select Style Image)
96
 
97
  - Select **Style Type**.
98
  - Select **Style Image Index** from the image table below.
99
+ """
100
+ )
101
  with gr.Row():
102
  with gr.Column():
103
+ style_type = gr.Radio(label="Style Type", choices=model.style_types, value=model.style_types[0])
104
+ text = get_style_image_markdown_text("cartoon")
 
 
105
  style_image = gr.Markdown(value=text)
106
+ style_index = gr.Slider(label="Style Image Index", minimum=0, maximum=316, step=1, value=26)
 
 
 
 
107
 
108
  with gr.Row():
109
  gr.Examples(
110
  examples=[
111
+ ["cartoon", 26],
112
+ ["caricature", 65],
113
+ ["arcane", 63],
114
+ ["pixar", 80],
115
  ],
116
  inputs=[style_type, style_index],
117
  )
118
 
119
+ with gr.Group():
120
+ gr.Markdown(
121
+ """## Step 3 (Generate Style Transferred Image)
122
 
123
  - Adjust **Structure Weight** and **Color Weight**.
124
  - These are weights for the style image, so the larger the value, the closer the resulting image will be to the style image.
125
  - Tips: For W+ encoder, better way of (Structure Only) is to uncheck (Structure Only) and set Color weight to 0.
126
  - Hit the **Generate** button.
127
+ """
128
+ )
129
  with gr.Row():
130
  with gr.Column():
131
  with gr.Row():
132
+ structure_weight = gr.Slider(label="Structure Weight", minimum=0, maximum=1, step=0.1, value=0.6)
 
 
 
 
133
  with gr.Row():
134
+ color_weight = gr.Slider(label="Color Weight", minimum=0, maximum=1, step=0.1, value=1)
 
 
 
 
135
  with gr.Row():
136
+ structure_only = gr.Checkbox(label="Structure Only", value=False)
 
137
  with gr.Row():
138
+ generate_button = gr.Button("Generate")
139
 
140
  with gr.Column():
141
+ result = gr.Image(label="Result")
142
 
143
  with gr.Row():
144
  gr.Examples(
 
186
  ],
187
  outputs=result,
188
  )
189
+
190
+ if __name__ == "__main__":
191
+ demo.queue(max_size=20).launch()
requirements.txt CHANGED
@@ -1,7 +1,7 @@
1
  dlib==19.24.2
2
  numpy==1.23.5
3
- opencv-python-headless==4.8.0.74
4
- Pillow==9.5.0
5
- scipy==1.11.1
6
  torch==2.0.1
7
  torchvision==0.15.2
 
1
  dlib==19.24.2
2
  numpy==1.23.5
3
+ opencv-python-headless==4.9.0.80
4
+ Pillow==10.2.0
5
+ scipy==1.12.0
6
  torch==2.0.1
7
  torchvision==0.15.2
style.css CHANGED
@@ -1,5 +1,13 @@
1
  h1 {
2
  text-align: center;
 
 
 
 
 
 
 
 
3
  }
4
 
5
  img#overview {
 
1
  h1 {
2
  text-align: center;
3
+ display: block;
4
+ }
5
+
6
+ #duplicate-button {
7
+ margin: auto;
8
+ color: #fff;
9
+ background: #1565c0;
10
+ border-radius: 100vh;
11
  }
12
 
13
  img#overview {