retir commited on
Commit
714a075
1 Parent(s): c490c3c

add normalization

Browse files
Files changed (2) hide show
  1. app.py +67 -51
  2. editings_table.csv +36 -36
app.py CHANGED
@@ -9,43 +9,44 @@ import pandas as pd
9
  from inference_pb2 import SFERequest, SFEResponse, SFERequestMask, SFEResponseMask
10
  from inference_pb2_grpc import SFEServiceStub
11
 
12
- PREDEFINED_EDITINGS_LIST = [
13
- "glasses",
14
- "smile",
15
- "makeup",
16
- "eye_openness",
17
- "trimmed_beard",
18
- "lipstick",
19
- "face_roundness",
20
- "nose_length",
21
- "eyebrow_thickness",
22
- "displeased",
23
- "age",
24
- "rotation",
25
- "afro",
26
- "angry",
27
- "bobcut",
28
- "bowlcut",
29
- "mohawk",
30
- "curly_hair",
31
- "purple_hair",
32
- "surprised",
33
- "beyonce",
34
- "hilary_clinton",
35
- "depp",
36
- "taylor_swift",
37
- "trump",
38
- "zuckerberg",
39
- "black hair",
40
- "blond hair",
41
- "grey hair",
42
- "wavy hair",
43
- "receding hairline",
44
- "sideburns",
45
- "goatee",
46
- "earrings",
47
- "gender"
48
- ]
 
49
 
50
  DIRECTIONS_NAME_SWAP = {
51
  "smile" : "fs_smiling",
@@ -54,6 +55,20 @@ DIRECTIONS_NAME_SWAP = {
54
  }
55
 
56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  def get_bytes(img):
58
  if img is None:
59
  return img
@@ -79,7 +94,9 @@ def edit_image(orig_image, edit_direction, edit_power, align, mask, progress=gr.
79
  if mask_bytes is None:
80
  mask_bytes = b"mask"
81
 
82
- with grpc.insecure_channel(os.environ['SERVER']) as channel:
 
 
83
  stub = SFEServiceStub(channel)
84
 
85
  output: SFEResponse = stub.edit(
@@ -105,7 +122,7 @@ def get_mask(input_image, align, mask_trashhold, progress=gr.Progress(track_tqdm
105
 
106
  input_image_bytes = get_bytes(input_image)
107
 
108
- with grpc.insecure_channel(os.environ['SERVER']) as channel:
109
  stub = SFEServiceStub(channel)
110
 
111
  output: SFEResponseMask = stub.generate_mask(
@@ -123,8 +140,8 @@ def get_mask(input_image, align, mask_trashhold, progress=gr.Progress(track_tqdm
123
 
124
  def get_demo():
125
  editings_table = pd.read_csv("editings_table.csv")
126
- editings_table = editings_table.style.set_properties(**{'text-align': 'center'})
127
- editings_table = editings_table.set_table_styles([dict(selector='th', props=[('text-align', 'center')])])
128
 
129
  with gr.Blocks() as demo:
130
  gr.Markdown("## StyleFeatureEditor")
@@ -144,22 +161,20 @@ def get_demo():
144
  align = gr.Checkbox(label="Align (crop and resize) the input image. For SFE to work well, it is necessary to align the input if it is not.", value=True)
145
  with gr.Accordion("Predefined Editings", open=True):
146
  with gr.Accordion("Description", open=False):
147
- gr.Markdown('''A branch of predefined editings gained from InterfaceGAN, Stylespace, GANSpace and StyleClip mappers. Look at the table below to see which direction is responsible for which editings, and which edit power to use.
148
 
149
- **Editing power** -- the greater the absolute value of this parameter, the more the selected edit will appear.
150
 
151
  **Editing effect** -- the effect applied to the image when positive editing power is used. If negative power is used, the effect is reversed.
152
-
153
- **Editing range** -- the approximate range of editing powers over which editing works well. We have found this empirically, so it may vary from image to image. Using powers outside the range may cause artefacts.
154
-
155
  '''
156
  )
157
 
158
- gr.Dataframe(value=editings_table, datatype=["markdown","markdown","markdown","markdown"], interactive=False, wrap=True,
159
- column_widths=["25px", "30px", "15px", "30px"], height=300)
160
  with gr.Row():
161
- predef_editing_direction = gr.Dropdown(PREDEFINED_EDITINGS_LIST, label="Editing direction", value="smile")
162
- predef_editing_power = gr.Number(value=7, label="Editing power")
 
163
  btn_predef = gr.Button("Edit image")
164
 
165
  with gr.Accordion("Text Prompt (StyleClip) Editings", open=False):
@@ -203,6 +218,7 @@ def get_demo():
203
  output_inv = gr.Image(label="Inversion result", visible=True)
204
  output_edit = gr.Image(label="Editing result", visible=True)
205
  error_message = gr.Textbox(label="⚠️ Error ⚠️", visible=False, elem_classes="error-message")
 
206
  gr.Examples(
207
  label="Input Examples",
208
  examples=[
@@ -247,6 +263,6 @@ def get_demo():
247
  return demo
248
 
249
 
250
- if __name__ == '__main__':
251
  demo = get_demo()
252
  demo.launch(server_name="0.0.0.0", server_port=7860)
 
9
  from inference_pb2 import SFERequest, SFEResponse, SFERequestMask, SFEResponseMask
10
  from inference_pb2_grpc import SFEServiceStub
11
 
12
+
13
+ PREDEFINED_EDITINGS_DATA = {
14
+ "glasses": ([-20.0, 30.0], False),
15
+ "smile": ([-10.0, 10.0], False),
16
+ "makeup": ([-10.0, 15.0], False),
17
+ "eye_openness": ([-45.0, 30.0], True),
18
+ "trimmed_beard": ([-30.0, 30.0], True),
19
+ "lipstick": ([-60.0, 60.0], False), # ??
20
+ "face_roundness": ([-20.0, 15.0], False),
21
+ "nose_length": ([-30.0, 30.0], True),
22
+ "eyebrow_thickness": ([-20.0, 20.0], True),
23
+ "displeased": ([-10.0, 10.0], False),
24
+ "age": ([-10.0, 10.0], False),
25
+ "rotation": ([-7.0, 7.0], False),
26
+ "afro": ([0.05, 0.14], False),
27
+ "angry": ([0.05, 0.14], False),
28
+ "bobcut": ([0.05, 0.18], False),
29
+ "bowlcut": ([0.05, 0.14], False),
30
+ "mohawk": ([0.05, 0.1], False),
31
+ "curly_hair": ([0.05, 0.12], False),
32
+ "purple_hair": ([0.05, 0.12], False),
33
+ "surprised": ([0.05, 0.1], False),
34
+ "beyonce": ([0.05, 0.12], False),
35
+ "hilary_clinton": ([0.05, 0.1], False),
36
+ "depp": ([0.05, 0.12], False),
37
+ "taylor_swift": ([0.05, 0.1], False),
38
+ "trump": ([0.05, 0.1], False),
39
+ "zuckerberg": ([0.05, 0.1], False),
40
+ "black hair": ([-7.0, 10.0], False),
41
+ "blond hair": ([-7.0, 10.0], True),
42
+ "grey hair": ([-7.0, 7.0], True),
43
+ "wavy hair": ([-7.0, 7.0], False),
44
+ "receding hairline": ([-10.0, 10.0], True),
45
+ "sideburns": ([-7.0, 7.0], True),
46
+ "goatee": ([-7.0, 7.0], True),
47
+ "earrings": ([-10.0, 15.0], False),
48
+ "gender": ([-10.0, 7.0], False)
49
+ }
50
 
51
  DIRECTIONS_NAME_SWAP = {
52
  "smile" : "fs_smiling",
 
55
  }
56
 
57
 
58
+ def denormalize_power(direction_name, directon_power):
59
+ if direction_name not in PREDEFINED_EDITINGS_DATA:
60
+ return directon_power
61
+ original_range, is_reversed = PREDEFINED_EDITINGS_DATA[direction_name]
62
+ if directon_power > 0:
63
+ normalized = directon_power / 15 * abs(original_range[1])
64
+ else:
65
+ normalized = directon_power / 15 * abs(original_range[0])
66
+
67
+ if is_reversed:
68
+ normalized = -normalized
69
+ return normalized
70
+
71
+
72
  def get_bytes(img):
73
  if img is None:
74
  return img
 
94
  if mask_bytes is None:
95
  mask_bytes = b"mask"
96
 
97
+ edit_power = denormalize_power(edit_direction, edit_power)
98
+
99
+ with grpc.insecure_channel(os.environ["SERVER"]) as channel:
100
  stub = SFEServiceStub(channel)
101
 
102
  output: SFEResponse = stub.edit(
 
122
 
123
  input_image_bytes = get_bytes(input_image)
124
 
125
+ with grpc.insecure_channel(os.environ["SERVER"]) as channel:
126
  stub = SFEServiceStub(channel)
127
 
128
  output: SFEResponseMask = stub.generate_mask(
 
140
 
141
  def get_demo():
142
  editings_table = pd.read_csv("editings_table.csv")
143
+ editings_table = editings_table.style.set_properties(**{"text-align": "center"})
144
+ editings_table = editings_table.set_table_styles([dict(selector="th", props=[("text-align", "center")])])
145
 
146
  with gr.Blocks() as demo:
147
  gr.Markdown("## StyleFeatureEditor")
 
161
  align = gr.Checkbox(label="Align (crop and resize) the input image. For SFE to work well, it is necessary to align the input if it is not.", value=True)
162
  with gr.Accordion("Predefined Editings", open=True):
163
  with gr.Accordion("Description", open=False):
164
+ gr.Markdown('''A branch of predefined editings gained from InterfaceGAN, Stylespace, GANSpace and StyleClip mappers. Look at the table below to see which direction is responsible for which editings.
165
 
166
+ **Editing power** -- the greater the absolute value of this parameter, the more the selected edit will appear. Better use values in the range 7 - 13, lower values may not give the desired edit, higher values -- on the contrary -- may apply edit too much and create artefacts.
167
 
168
  **Editing effect** -- the effect applied to the image when positive editing power is used. If negative power is used, the effect is reversed.
 
 
 
169
  '''
170
  )
171
 
172
+ gr.Dataframe(value=editings_table, datatype=["markdown","markdown","markdown"], interactive=False, wrap=True,
173
+ column_widths=["30px", "35px", "35px"], height=300)
174
  with gr.Row():
175
+ predef_editing_direction = gr.Dropdown(list(PREDEFINED_EDITINGS_DATA.keys()), label="Editing direction", value="smile")
176
+ # predef_editing_power = gr.Number(value=7, label="Editing power")
177
+ predef_editing_power = gr.Slider(-20, 20, value=10, step=0.1, label="Editing power")
178
  btn_predef = gr.Button("Edit image")
179
 
180
  with gr.Accordion("Text Prompt (StyleClip) Editings", open=False):
 
218
  output_inv = gr.Image(label="Inversion result", visible=True)
219
  output_edit = gr.Image(label="Editing result", visible=True)
220
  error_message = gr.Textbox(label="⚠️ Error ⚠️", visible=False, elem_classes="error-message")
221
+ gr.Markdown("If artefacts appear during editing -- try lowering the editing power or using a mask.")
222
  gr.Examples(
223
  label="Input Examples",
224
  examples=[
 
263
  return demo
264
 
265
 
266
+ if __name__ == "__main__":
267
  demo = get_demo()
268
  demo.launch(server_name="0.0.0.0", server_port=7860)
editings_table.csv CHANGED
@@ -1,36 +1,36 @@
1
- Editing name,Editing effect,Editing range,Additional comments
2
- glasses,add glasses,[-20; 30],may open mouth
3
- smile,add smile,[-10; 10],
4
- makeup,add make-up,[-10; 15],bad works with men
5
- eye_openness,close eyes,[-30; 45],
6
- trimmed_beard,remove beard,[-30; 30],bad works with women
7
- lipstick,add lipstick,[-30; 30],bad works with men
8
- face_roundness,make face rounder,[-20; 15],
9
- nose_length,decreace nose length,[-30; 30],may open mouth
10
- eyebrow_thickness,decreace eyebrow thickness,[-20; 20],
11
- displeased,add sadness,[-10; 10],
12
- age,increase age,[-10; 10],
13
- rotation,turn face right,[-7 ; 7 ],
14
- afro,afro hairstyle,[0; 0.14],
15
- angry,make angrier,[0; 0.14],cause background artefacts
16
- bobcut,bobcut hairstyle,[0; 0.18],cause background artefacts
17
- bowlcut,bowlcut hairstyle,[0; 0.14],cause background artefacts
18
- mohawk,mohawk hairstyle,[0; 0.10],cause background artefacts
19
- curly_hair,add curls,[0; 0.12],
20
- purple_hair,dye hair purple,[0; 0.12],
21
- surprised,make more surprised,[0; 0.10],
22
- beyonce,make similar to beyonce,[0; 0.12],
23
- hilary_clinton,make similar to hilary clinton,[0; 0.10],
24
- depp,make similar to johnny depp,[0; 0.12],
25
- taylor_swift,make similar to taylor swift,[0; 0.10],
26
- trump,make similar to donald trump,[0; 0.10],
27
- zuckerberg,make similar to mark zuckerberg,[0; 0.10],
28
- black hair,darken hair,[-7; 10],
29
- blond hair,darken hair,[-10; 7],"yes, positive power darkens hair, negative lightens hair"
30
- grey hair,make hair colored,[-7 ; 7],negative means make hair grey
31
- wavy hair,add hair length,[-7 ; 7],
32
- receding hairline,remove bald,[-10; 10],
33
- sideburns,remove sideburns,[-7 ; 7],bad works with women
34
- goatee,remove goatee,[-7 ; 7],bad works with women
35
- earrings,add earrings,[0 ; 15],bad works with men
36
- gender,add femininity,[-10; 7],better use global mapper
 
1
+ Editing name,Editing effect,Additional comments
2
+ glasses,add glasses,may open mouth
3
+ smile,add smile,
4
+ makeup,add make-up,bad works with men
5
+ eye_openness,open eyes,
6
+ trimmed_beard,add beard,bad works with women
7
+ lipstick,add lipstick,bad works with men
8
+ face_roundness,make face rounder,
9
+ nose_length,increase nose length,may open mouth
10
+ eyebrow_thickness,increase eyebrow thickness,
11
+ displeased,add sadness,
12
+ age,increase age,
13
+ rotation,turn face right,
14
+ afro,afro hairstyle,
15
+ angry,make angrier,cause background artefacts
16
+ bobcut,bobcut hairstyle,cause background artefacts
17
+ bowlcut,bowlcut hairstyle,cause background artefacts
18
+ mohawk,mohawk hairstyle,cause background artefacts
19
+ curly_hair,add curls,
20
+ purple_hair,dye hair purple,
21
+ surprised,make more surprised,
22
+ beyonce,make similar to beyonce,
23
+ hilary_clinton,make similar to hilary clinton,
24
+ depp,make similar to johnny depp,
25
+ taylor_swift,make similar to taylor swift,
26
+ trump,make similar to donald trump,
27
+ zuckerberg,make similar to mark zuckerberg,
28
+ black hair,darken hair,
29
+ blond hair,lighten hair,"yes, positive power darkens hair, negative lightens hair"
30
+ grey hair,make hair grey,negative means make hair grey
31
+ wavy hair,add hair length,
32
+ receding hairline,add bald,
33
+ sideburns,add sideburns,bad works with women
34
+ goatee,add goatee,bad works with women
35
+ earrings,add earrings,bad works with men
36
+ gender,add femininity,better use global mapper