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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from huggingface_hub import Repository
|
3 |
+
import os
|
4 |
|
5 |
+
from utils.utils import norm_crop, estimate_norm, inverse_estimate_norm, transform_landmark_points, get_lm
|
6 |
+
from networks.layers import AdaIN, AdaptiveAttention
|
7 |
+
from tensorflow_addons.layers import InstanceNormalization
|
8 |
+
import numpy as np
|
9 |
+
import cv2
|
10 |
+
from scipy.ndimage import gaussian_filter
|
11 |
|
12 |
+
from tensorflow.keras.models import load_model
|
13 |
+
from options.swap_options import SwapOptions
|
14 |
+
|
15 |
+
|
16 |
+
|
17 |
+
token = os.environ['model_fetch']
|
18 |
+
|
19 |
+
opt = SwapOptions().parse()
|
20 |
+
|
21 |
+
retina_repo = Repository(local_dir="retina_model", clone_from="felixrosberg/retinaface_resnet50", use_auth_token=token)
|
22 |
+
|
23 |
+
from retina_model.models import *
|
24 |
+
|
25 |
+
RetinaFace = load_model("retina_model/retinaface_res50.h5",
|
26 |
+
custom_objects={"FPN": FPN,
|
27 |
+
"SSH": SSH,
|
28 |
+
"BboxHead": BboxHead,
|
29 |
+
"LandmarkHead": LandmarkHead,
|
30 |
+
"ClassHead": ClassHead})
|
31 |
+
|
32 |
+
arc_repo = Repository(local_dir="arcface_model", clone_from="felixrosberg/arcface_tf", use_auth_token=token)
|
33 |
+
ArcFace = load_model("arcface_model/arc_res50.h5")
|
34 |
+
ArcFaceE = load_model("arcface_model/arc_res50e.h5")
|
35 |
+
|
36 |
+
g_repo = Repository(local_dir="g_model_c_hq", clone_from="felixrosberg/affa_config_c_hq", use_auth_token=token)
|
37 |
+
G = load_model("g_model_c_hq/generator_t_28.h5", custom_objects={"AdaIN": AdaIN,
|
38 |
+
"AdaptiveAttention": AdaptiveAttention,
|
39 |
+
"InstanceNormalization": InstanceNormalization})
|
40 |
+
|
41 |
+
r_repo = Repository(local_dir="reconstruction_attack", clone_from="felixrosberg/reconstruction_attack", use_auth_token=token)
|
42 |
+
R = load_model("reconstruction_attack/reconstructor_42.h5", custom_objects={"AdaIN": AdaIN,
|
43 |
+
"AdaptiveAttention": AdaptiveAttention,
|
44 |
+
"InstanceNormalization": InstanceNormalization})
|
45 |
+
|
46 |
+
permuter_repo = Repository(local_dir="identity_permuter", clone_from="felixrosberg/identitypermuter", use_auth_token=token, git_user="felixrosberg")
|
47 |
+
|
48 |
+
from identity_permuter.id_permuter import identity_permuter
|
49 |
+
|
50 |
+
IDP = identity_permuter(emb_size=32, min_arg=False)
|
51 |
+
IDP.load_weights("identity_permuter/id_permuter.h5")
|
52 |
+
|
53 |
+
blend_mask_base = np.zeros(shape=(256, 256, 1))
|
54 |
+
blend_mask_base[80:244, 32:224] = 1
|
55 |
+
blend_mask_base = gaussian_filter(blend_mask_base, sigma=7)
|
56 |
+
|
57 |
+
|
58 |
+
theme = gr.themes.Base(
|
59 |
+
primary_hue="neutral",
|
60 |
+
radius_size="none",
|
61 |
+
).set(
|
62 |
+
embed_radius='*radius_xxs',
|
63 |
+
|
64 |
+
loader_color="#303A3F",
|
65 |
+
loader_color_dark="#303A3F",
|
66 |
+
|
67 |
+
body_text_color='*neutral_800',
|
68 |
+
body_text_color_dark='*neutral_800',
|
69 |
+
|
70 |
+
body_text_color_subdued="*neutral_400",
|
71 |
+
body_text_color_subdued_dark="*neutral_400",
|
72 |
+
|
73 |
+
button_primary_background_fill='*primary_700',
|
74 |
+
button_primary_background_fill_dark='*primary_700',
|
75 |
+
|
76 |
+
button_primary_background_fill_hover='*primary_400',
|
77 |
+
button_primary_background_fill_hover_dark='*primary_400',
|
78 |
+
|
79 |
+
button_primary_border_color='*primary_200',
|
80 |
+
button_primary_border_color_dark='*primary_200',
|
81 |
+
|
82 |
+
button_primary_text_color='#303A3F',
|
83 |
+
button_primary_text_color_dark='#303A3F',
|
84 |
+
|
85 |
+
button_secondary_text_color="#303A3F",
|
86 |
+
button_secondary_text_color_dark="#303A3F",
|
87 |
+
|
88 |
+
button_primary_text_color_hover='*primary_50',
|
89 |
+
button_primary_text_color_hover_dark='*primary_50',
|
90 |
+
|
91 |
+
button_secondary_background_fill="*primary_200",
|
92 |
+
button_secondary_background_fill_dark="*primary_200",
|
93 |
+
|
94 |
+
button_secondary_background_fill_hover='#667D88',
|
95 |
+
button_secondary_background_fill_hover_dark='#667D88',
|
96 |
+
|
97 |
+
input_background_fill="*neutral_100",
|
98 |
+
input_background_fill_dark="*neutral_100",
|
99 |
+
|
100 |
+
input_background_fill_focus="*secondary_500",
|
101 |
+
input_background_fill_focus_dark="*secondary_500",
|
102 |
+
|
103 |
+
body_background_fill="#FFFFFF",
|
104 |
+
body_background_fill_dark="#FFFFFF",
|
105 |
+
|
106 |
+
background_fill_secondary="*neutral_50",
|
107 |
+
background_fill_secondary_dark="*neutral_50",
|
108 |
+
|
109 |
+
border_color_accent="#303A3F",
|
110 |
+
border_color_accent_dark="#303A3F",
|
111 |
+
|
112 |
+
border_color_primary="#303A3F",
|
113 |
+
border_color_primary_dark="#303A3F",
|
114 |
+
|
115 |
+
color_accent="*primary_500",
|
116 |
+
color_accent_soft="*primary_50",
|
117 |
+
color_accent_soft_dark="*primary_50",
|
118 |
+
|
119 |
+
background_fill_primary="#FFFFFF",
|
120 |
+
background_fill_primary_dark="#FFFFFF",
|
121 |
+
|
122 |
+
block_title_background_fill="#FFFFFF",
|
123 |
+
block_title_background_fill_dark="#FFFFFF",
|
124 |
+
|
125 |
+
block_background_fill="#FFFFFF",
|
126 |
+
block_background_fill_dark="#FFFFFF",
|
127 |
+
|
128 |
+
block_label_border_color="*neutral_200",
|
129 |
+
block_label_border_color_dark="*neutral_200",
|
130 |
+
|
131 |
+
block_label_text_color="*neutral_500",
|
132 |
+
block_label_text_color_dark="*neutral_500",
|
133 |
+
|
134 |
+
block_info_text_color="*neutral_400",
|
135 |
+
block_info_text_color_dark="*neutral_400",
|
136 |
+
|
137 |
+
block_border_color="*neutral_700",
|
138 |
+
block_border_color_dark="*neutral_700",
|
139 |
+
|
140 |
+
block_border_width="1px",
|
141 |
+
block_border_width_dark="1px",
|
142 |
+
|
143 |
+
block_label_background_fill="#FFFFFF",
|
144 |
+
block_label_background_fill_dark="#FFFFFF",
|
145 |
+
|
146 |
+
block_label_border_width="1px",
|
147 |
+
block_label_border_width_dark="1px",
|
148 |
+
|
149 |
+
block_title_text_color="*neutral_500",
|
150 |
+
block_title_text_color_dark="*neutral_500",
|
151 |
+
|
152 |
+
checkbox_background_color="#FFFFFF",
|
153 |
+
checkbox_background_color_dark="#FFFFFF",
|
154 |
+
|
155 |
+
checkbox_background_color_focus="#FFFFFF",
|
156 |
+
checkbox_background_color_focus_dark="#FFFFFF",
|
157 |
+
|
158 |
+
checkbox_background_color_hover="#FFFFFF",
|
159 |
+
checkbox_background_color_hover_dark="#FFFFFF",
|
160 |
+
|
161 |
+
panel_background_fill="*neutral_50",
|
162 |
+
panel_background_fill_dark="*neutral_50",
|
163 |
+
|
164 |
+
slider_color="#303A3F",
|
165 |
+
slider_color_dark="#303A3F",
|
166 |
+
)
|
167 |
+
|
168 |
+
|
169 |
+
def run_inference(target, source, slider, adv_slider, settings):
|
170 |
+
try:
|
171 |
+
source = np.array(source)
|
172 |
+
target = np.array(target)
|
173 |
+
|
174 |
+
# Prepare to load video
|
175 |
+
if "anonymize" not in settings:
|
176 |
+
source_a = RetinaFace(np.expand_dims(source, axis=0)).numpy()[0]
|
177 |
+
source_h, source_w, _ = source.shape
|
178 |
+
source_lm = get_lm(source_a, source_w, source_h)
|
179 |
+
source_aligned = norm_crop(source, source_lm, image_size=256)
|
180 |
+
source_z = ArcFace.predict(np.expand_dims(tf.image.resize(source_aligned, [112, 112]) / 255.0, axis=0))
|
181 |
+
else:
|
182 |
+
source_z = None
|
183 |
+
|
184 |
+
# read frame
|
185 |
+
im = target
|
186 |
+
im_h, im_w, _ = im.shape
|
187 |
+
im_shape = (im_w, im_h)
|
188 |
+
|
189 |
+
detection_scale = im_w // 640 if im_w > 640 else 1
|
190 |
+
|
191 |
+
faces = RetinaFace(np.expand_dims(cv2.resize(im,
|
192 |
+
(im_w // detection_scale,
|
193 |
+
im_h // detection_scale)), axis=0)).numpy()
|
194 |
+
|
195 |
+
total_img = im / 255.0
|
196 |
+
for annotation in faces:
|
197 |
+
lm_align = np.array([[annotation[4] * im_w, annotation[5] * im_h],
|
198 |
+
[annotation[6] * im_w, annotation[7] * im_h],
|
199 |
+
[annotation[8] * im_w, annotation[9] * im_h],
|
200 |
+
[annotation[10] * im_w, annotation[11] * im_h],
|
201 |
+
[annotation[12] * im_w, annotation[13] * im_h]],
|
202 |
+
dtype=np.float32)
|
203 |
+
|
204 |
+
# align the detected face
|
205 |
+
M, pose_index = estimate_norm(lm_align, 256, "arcface", shrink_factor=1.0)
|
206 |
+
im_aligned = (cv2.warpAffine(im, M, (256, 256), borderValue=0.0) - 127.5) / 127.5
|
207 |
+
|
208 |
+
if "adversarial defense" in settings:
|
209 |
+
eps = adv_slider / 200
|
210 |
+
X = tf.convert_to_tensor(np.expand_dims(im_aligned, axis=0))
|
211 |
+
with tf.GradientTape() as tape:
|
212 |
+
tape.watch(X)
|
213 |
+
|
214 |
+
X_z = ArcFaceE(tf.image.resize(X * 0.5 + 0.5, [112, 112]))
|
215 |
+
output = R([X, X_z])
|
216 |
+
|
217 |
+
loss = tf.reduce_mean(tf.abs(0 - output))
|
218 |
+
|
219 |
+
gradient = tf.sign(tape.gradient(loss, X))
|
220 |
+
|
221 |
+
adv_x = X + eps * gradient
|
222 |
+
im_aligned = tf.clip_by_value(adv_x, -1, 1)[0]
|
223 |
+
|
224 |
+
if "anonymize" in settings and "reconstruction attack" not in settings:
|
225 |
+
"""source_z = ArcFace.predict(np.expand_dims(tf.image.resize(im_aligned, [112, 112]) / 255.0, axis=0))
|
226 |
+
anon_ratio = int(512 * (slider / 100))
|
227 |
+
anon_vector = np.ones(shape=(1, 512))
|
228 |
+
anon_vector[:, :anon_ratio] = -1
|
229 |
+
np.random.shuffle(anon_vector)
|
230 |
+
source_z *= anon_vector"""
|
231 |
+
|
232 |
+
slider_weight = slider / 100
|
233 |
+
|
234 |
+
target_z = ArcFace.predict(np.expand_dims(tf.image.resize(im_aligned, [112, 112]) * 0.5 + 0.5, axis=0))
|
235 |
+
source_z = IDP.predict(target_z)
|
236 |
+
|
237 |
+
source_z = slider_weight * source_z + (1 - slider_weight) * target_z
|
238 |
+
|
239 |
+
if "reconstruction attack" in settings:
|
240 |
+
source_z = ArcFaceE.predict(np.expand_dims(tf.image.resize(im_aligned, [112, 112]) * 0.5 + 0.5, axis=0))
|
241 |
+
|
242 |
+
# face swap
|
243 |
+
if "reconstruction attack" not in settings:
|
244 |
+
changed_face_cage = G.predict([np.expand_dims(im_aligned, axis=0),
|
245 |
+
source_z])
|
246 |
+
changed_face = changed_face_cage[0] * 0.5 + 0.5
|
247 |
+
|
248 |
+
# get inverse transformation landmarks
|
249 |
+
transformed_lmk = transform_landmark_points(M, lm_align)
|
250 |
+
|
251 |
+
# warp image back
|
252 |
+
iM, _ = inverse_estimate_norm(lm_align, transformed_lmk, 256, "arcface", shrink_factor=1.0)
|
253 |
+
iim_aligned = cv2.warpAffine(changed_face, iM, im_shape, borderValue=0.0)
|
254 |
+
|
255 |
+
# blend swapped face with target image
|
256 |
+
blend_mask = cv2.warpAffine(blend_mask_base, iM, im_shape, borderValue=0.0)
|
257 |
+
blend_mask = np.expand_dims(blend_mask, axis=-1)
|
258 |
+
total_img = (iim_aligned * blend_mask + total_img * (1 - blend_mask))
|
259 |
+
else:
|
260 |
+
changed_face_cage = R.predict([np.expand_dims(im_aligned, axis=0),
|
261 |
+
source_z])
|
262 |
+
changed_face = changed_face_cage[0] * 0.5 + 0.5
|
263 |
+
|
264 |
+
# get inverse transformation landmarks
|
265 |
+
transformed_lmk = transform_landmark_points(M, lm_align)
|
266 |
+
|
267 |
+
# warp image back
|
268 |
+
iM, _ = inverse_estimate_norm(lm_align, transformed_lmk, 256, "arcface", shrink_factor=1.0)
|
269 |
+
iim_aligned = cv2.warpAffine(changed_face, iM, im_shape, borderValue=0.0)
|
270 |
+
|
271 |
+
# blend swapped face with target image
|
272 |
+
blend_mask = cv2.warpAffine(blend_mask_base, iM, im_shape, borderValue=0.0)
|
273 |
+
blend_mask = np.expand_dims(blend_mask, axis=-1)
|
274 |
+
total_img = (iim_aligned * blend_mask + total_img * (1 - blend_mask))
|
275 |
+
|
276 |
+
if "compare" in settings:
|
277 |
+
total_img = np.concatenate((im / 255.0, total_img), axis=1)
|
278 |
+
|
279 |
+
total_img = np.clip(total_img, 0, 1)
|
280 |
+
total_img *= 255.0
|
281 |
+
total_img = total_img.astype('uint8')
|
282 |
+
|
283 |
+
return total_img
|
284 |
+
except Exception as e:
|
285 |
+
print(e)
|
286 |
+
return None
|
287 |
+
|
288 |
+
|
289 |
+
description = "Performs subject agnostic identity transfer from a source face to all target faces. \n\n" \
|
290 |
+
"Implementation and demo of FaceDancer, accepted to WACV 2023. \n\n" \
|
291 |
+
"Pre-print: https://arxiv.org/abs/2210.10473 \n\n" \
|
292 |
+
"Code: https://github.com/felixrosberg/FaceDancer \n\n" \
|
293 |
+
"\n\n" \
|
294 |
+
"Options:\n\n" \
|
295 |
+
"-Compare returns the target image concatenated with the results.\n\n" \
|
296 |
+
"-Anonymize will ignore the source image and perform an identity permutation of target faces.\n\n" \
|
297 |
+
"-Reconstruction attack will attempt to invert the face swap or the anonymization.\n\n" \
|
298 |
+
"-Adversarial defense will add a permutation noise that disrupts the reconstruction attack.\n\n" \
|
299 |
+
"NOTE: There is no guarantees with the anonymization process currently.\n\n" \
|
300 |
+
"NOTE: source image with too high resolution may not work properly!"
|
301 |
+
examples = [["assets/rick.jpg", "assets/musk.jpg", 100, 10, []],
|
302 |
+
["assets/rick.jpg", "assets/rick.jpg", 100, 10, ["anonymize"]]]
|
303 |
+
article = """
|
304 |
+
Demo is based of recent research from my Ph.D work. Results expects to be published in the coming months.
|
305 |
+
"""
|
306 |
+
|
307 |
+
with gr.Blocks(theme=theme) as blk_demo:
|
308 |
+
gr.Markdown(value="# Face Dancer")
|
309 |
+
gr.Markdown(value="# Paper: [FaceDancer: Pose- and Occlusion-Aware High Fidelity Face Swapping](https://arxiv.org/abs/2210.10473)")
|
310 |
+
gr.Markdown(value="# Check out the code [here](https://github.com/felixrosberg/FaceDancer)")
|
311 |
+
with gr.Row():
|
312 |
+
with gr.Column():
|
313 |
+
with gr.Group():
|
314 |
+
trg_in = gr.Image(type="pil", label='Target')
|
315 |
+
src_in = gr.Image(type="pil", label='Source')
|
316 |
+
with gr.Row():
|
317 |
+
b1 = gr.Button("Face Swap")
|
318 |
+
with gr.Row():
|
319 |
+
with gr.Accordion("Options", open=False):
|
320 |
+
chk_in = gr.CheckboxGroup(["Compare",
|
321 |
+
"Anonymize",
|
322 |
+
"Reconstruction Attack",
|
323 |
+
"Adversarial Defense"],
|
324 |
+
label="Mode",
|
325 |
+
info="Anonymize mode? "
|
326 |
+
"Apply reconstruction attack? "
|
327 |
+
"Apply defense against reconstruction attack?")
|
328 |
+
def_in = gr.Slider(0, 100, value=100,
|
329 |
+
label='Anonymization ratio (%)')
|
330 |
+
mrg_in = gr.Slider(0, 100, value=100,
|
331 |
+
label='Adversarial defense ratio (%)')
|
332 |
+
gr.Examples(examples=[["assets/musk.jpg"], ["assets/rick.jpg"]],
|
333 |
+
inputs=trg_in)
|
334 |
+
with gr.Column():
|
335 |
+
with gr.Group():
|
336 |
+
ano_out = gr.Image(type="pil", label='Output')
|
337 |
+
|
338 |
+
b1.click(run_inference, inputs=[trg_in, src_in, def_in, mrg_in, chk_in], outputs=ano_out)
|
339 |
+
"""iface = gradio.Interface(run_inference,
|
340 |
+
[gradio.Image(shape=None, type="pil", label='Target'),
|
341 |
+
gradio.Image(shape=None, type="pil", label='Source'),
|
342 |
+
gradio.Slider(0, 100, value=100, label="Anonymization ratio (%)"),
|
343 |
+
gradio.Slider(0, 100, value=100, label="Adversarial defense ratio (%)"),
|
344 |
+
gradio.CheckboxGroup(["compare",
|
345 |
+
"anonymize",
|
346 |
+
"reconstruction attack",
|
347 |
+
"adversarial defense"],
|
348 |
+
label='Options')],
|
349 |
+
"image",
|
350 |
+
title="Face Swap",
|
351 |
+
description=description,
|
352 |
+
examples=examples,
|
353 |
+
article=article,
|
354 |
+
layout="vertical")"""
|
355 |
+
blk_demo.launch()
|