felixrosberg
commited on
Commit
Β·
b6d1b78
1
Parent(s):
694ad99
Update app.py
Browse files
app.py
CHANGED
@@ -17,11 +17,11 @@ token = os.environ['model_fetch']
|
|
17 |
|
18 |
opt = SwapOptions().parse()
|
19 |
|
20 |
-
|
21 |
retina_repo = Repository(local_dir="retina_model", clone_from="felixrosberg/retinaface_resnet50",
|
22 |
private=True, use_auth_token=token, git_user="felixrosberg")
|
23 |
-
|
24 |
from retina_model.models import *
|
|
|
25 |
RetinaFace = load_model("retina_model/retinaface_res50.h5",
|
26 |
custom_objects={"FPN": FPN,
|
27 |
"SSH": SSH,
|
@@ -32,31 +32,38 @@ RetinaFace = load_model("retina_model/retinaface_res50.h5",
|
|
32 |
arc_repo = Repository(local_dir="arcface_model", clone_from="felixrosberg/arcface_tf",
|
33 |
private=True, use_auth_token=token)
|
34 |
ArcFace = load_model("arcface_model/arc_res50.h5")
|
|
|
35 |
|
36 |
g_repo = Repository(local_dir="g_model_c_hq", clone_from="felixrosberg/affa_config_c_hq",
|
37 |
private=True, use_auth_token=token)
|
38 |
G = load_model("g_model_c_hq/generator_t_28.h5", custom_objects={"AdaIN": AdaIN,
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
permuter_repo = Repository(local_dir="identity_permuter", clone_from="felixrosberg/identitypermuter",
|
43 |
-
|
44 |
-
|
45 |
from identity_permuter.id_permuter import identity_permuter
|
|
|
46 |
IDP = identity_permuter(emb_size=32, min_arg=False)
|
47 |
IDP.load_weights("identity_permuter/id_permuter.h5")
|
48 |
|
49 |
-
|
50 |
blend_mask_base = np.zeros(shape=(256, 256, 1))
|
51 |
blend_mask_base[80:244, 32:224] = 1
|
52 |
blend_mask_base = gaussian_filter(blend_mask_base, sigma=7)
|
53 |
|
54 |
|
55 |
-
def run_inference(target, source, slider, settings):
|
56 |
try:
|
57 |
source = np.array(source)
|
58 |
target = np.array(target)
|
59 |
-
|
60 |
# Prepare to load video
|
61 |
if "anonymize" not in settings:
|
62 |
source_a = RetinaFace(np.expand_dims(source, axis=0)).numpy()[0]
|
@@ -66,18 +73,18 @@ def run_inference(target, source, slider, settings):
|
|
66 |
source_z = ArcFace.predict(np.expand_dims(tf.image.resize(source_aligned, [112, 112]) / 255.0, axis=0))
|
67 |
else:
|
68 |
source_z = None
|
69 |
-
|
70 |
# read frame
|
71 |
im = target
|
72 |
im_h, im_w, _ = im.shape
|
73 |
im_shape = (im_w, im_h)
|
74 |
-
|
75 |
detection_scale = im_w // 640 if im_w > 640 else 1
|
76 |
-
|
77 |
faces = RetinaFace(np.expand_dims(cv2.resize(im,
|
78 |
(im_w // detection_scale,
|
79 |
im_h // detection_scale)), axis=0)).numpy()
|
80 |
-
|
81 |
total_img = im / 255.0
|
82 |
for annotation in faces:
|
83 |
lm_align = np.array([[annotation[4] * im_w, annotation[5] * im_h],
|
@@ -86,50 +93,85 @@ def run_inference(target, source, slider, settings):
|
|
86 |
[annotation[10] * im_w, annotation[11] * im_h],
|
87 |
[annotation[12] * im_w, annotation[13] * im_h]],
|
88 |
dtype=np.float32)
|
89 |
-
|
90 |
# align the detected face
|
91 |
M, pose_index = estimate_norm(lm_align, 256, "arcface", shrink_factor=1.0)
|
92 |
im_aligned = cv2.warpAffine(im, M, (256, 256), borderValue=0.0)
|
93 |
|
94 |
-
if "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
"""source_z = ArcFace.predict(np.expand_dims(tf.image.resize(im_aligned, [112, 112]) / 255.0, axis=0))
|
96 |
anon_ratio = int(512 * (slider / 100))
|
97 |
anon_vector = np.ones(shape=(1, 512))
|
98 |
anon_vector[:, :anon_ratio] = -1
|
99 |
np.random.shuffle(anon_vector)
|
100 |
source_z *= anon_vector"""
|
101 |
-
|
102 |
slider_weight = slider / 100
|
103 |
-
|
104 |
target_z = ArcFace.predict(np.expand_dims(tf.image.resize(im_aligned, [112, 112]) / 255.0, axis=0))
|
105 |
source_z = IDP.predict(target_z)
|
106 |
-
|
107 |
-
source_z = slider_weight * source_z + (1 - slider_weight
|
108 |
-
|
|
|
|
|
|
|
109 |
# face swap
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
if "compare" in settings:
|
127 |
total_img = np.concatenate((im / 255.0, total_img), axis=1)
|
128 |
-
|
129 |
total_img = np.clip(total_img, 0, 1)
|
130 |
total_img *= 255.0
|
131 |
total_img = total_img.astype('uint8')
|
132 |
-
|
133 |
return total_img
|
134 |
except Exception as e:
|
135 |
print(e)
|
@@ -143,17 +185,22 @@ description = "Performs subject agnostic identity transfer from a source face to
|
|
143 |
"NOTE: There is no guarantees with the anonymization process currently.\n" \
|
144 |
"\n" \
|
145 |
"Note, source image with too high resolution may not work properly!"
|
146 |
-
examples = [["assets/rick.jpg", "assets/musk.jpg",
|
147 |
-
["assets/musk.jpg", "assets/musk.jpg",
|
148 |
-
article="""
|
149 |
Demo is based of recent research from my Ph.D work. Results expects to be published in the coming months.
|
150 |
"""
|
151 |
|
152 |
iface = gradio.Interface(run_inference,
|
153 |
[gradio.inputs.Image(shape=None, label='Target'),
|
154 |
gradio.inputs.Image(shape=None, label='Source'),
|
155 |
-
gradio.inputs.Slider(0, 100, default=
|
156 |
-
gradio.inputs.
|
|
|
|
|
|
|
|
|
|
|
157 |
gradio.outputs.Image(),
|
158 |
title="Face Swap",
|
159 |
description=description,
|
|
|
17 |
|
18 |
opt = SwapOptions().parse()
|
19 |
|
|
|
20 |
retina_repo = Repository(local_dir="retina_model", clone_from="felixrosberg/retinaface_resnet50",
|
21 |
private=True, use_auth_token=token, git_user="felixrosberg")
|
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,
|
|
|
32 |
arc_repo = Repository(local_dir="arcface_model", clone_from="felixrosberg/arcface_tf",
|
33 |
private=True, use_auth_token=token)
|
34 |
ArcFace = load_model("arcface_model/arc_res50.h5")
|
35 |
+
ArcFaceE = load_model("arcface_model/arc_res50e.h5")
|
36 |
|
37 |
g_repo = Repository(local_dir="g_model_c_hq", clone_from="felixrosberg/affa_config_c_hq",
|
38 |
private=True, use_auth_token=token)
|
39 |
G = load_model("g_model_c_hq/generator_t_28.h5", custom_objects={"AdaIN": AdaIN,
|
40 |
+
"AdaptiveAttention": AdaptiveAttention,
|
41 |
+
"InstanceNormalization": InstanceNormalization})
|
42 |
+
|
43 |
+
r_repo = Repository(local_dir="reconstruction_attack", clone_from="felixrosberg/reconstruction_attack",
|
44 |
+
private=True, use_auth_token=token)
|
45 |
+
R = load_model("reconstruction_attack/reconstructor_42.h5", custom_objects={"AdaIN": AdaIN,
|
46 |
+
"AdaptiveAttention": AdaptiveAttention,
|
47 |
+
"InstanceNormalization": InstanceNormalization})
|
48 |
|
49 |
permuter_repo = Repository(local_dir="identity_permuter", clone_from="felixrosberg/identitypermuter",
|
50 |
+
private=True, use_auth_token=token, git_user="felixrosberg")
|
51 |
+
|
52 |
from identity_permuter.id_permuter import identity_permuter
|
53 |
+
|
54 |
IDP = identity_permuter(emb_size=32, min_arg=False)
|
55 |
IDP.load_weights("identity_permuter/id_permuter.h5")
|
56 |
|
|
|
57 |
blend_mask_base = np.zeros(shape=(256, 256, 1))
|
58 |
blend_mask_base[80:244, 32:224] = 1
|
59 |
blend_mask_base = gaussian_filter(blend_mask_base, sigma=7)
|
60 |
|
61 |
|
62 |
+
def run_inference(target, source, slider, adv_slider, settings):
|
63 |
try:
|
64 |
source = np.array(source)
|
65 |
target = np.array(target)
|
66 |
+
|
67 |
# Prepare to load video
|
68 |
if "anonymize" not in settings:
|
69 |
source_a = RetinaFace(np.expand_dims(source, axis=0)).numpy()[0]
|
|
|
73 |
source_z = ArcFace.predict(np.expand_dims(tf.image.resize(source_aligned, [112, 112]) / 255.0, axis=0))
|
74 |
else:
|
75 |
source_z = None
|
76 |
+
|
77 |
# read frame
|
78 |
im = target
|
79 |
im_h, im_w, _ = im.shape
|
80 |
im_shape = (im_w, im_h)
|
81 |
+
|
82 |
detection_scale = im_w // 640 if im_w > 640 else 1
|
83 |
+
|
84 |
faces = RetinaFace(np.expand_dims(cv2.resize(im,
|
85 |
(im_w // detection_scale,
|
86 |
im_h // detection_scale)), axis=0)).numpy()
|
87 |
+
|
88 |
total_img = im / 255.0
|
89 |
for annotation in faces:
|
90 |
lm_align = np.array([[annotation[4] * im_w, annotation[5] * im_h],
|
|
|
93 |
[annotation[10] * im_w, annotation[11] * im_h],
|
94 |
[annotation[12] * im_w, annotation[13] * im_h]],
|
95 |
dtype=np.float32)
|
96 |
+
|
97 |
# align the detected face
|
98 |
M, pose_index = estimate_norm(lm_align, 256, "arcface", shrink_factor=1.0)
|
99 |
im_aligned = cv2.warpAffine(im, M, (256, 256), borderValue=0.0)
|
100 |
|
101 |
+
if "adversarial defense" in settings:
|
102 |
+
eps = adv_slider / 200
|
103 |
+
with tf.GradientTape() as tape:
|
104 |
+
tape.watch(im_aligned)
|
105 |
+
|
106 |
+
X_z = ArcFaceE(tf.image.resize((im_aligned + 1) / 2, [112, 112]))
|
107 |
+
output = R([im_aligned, X_z])
|
108 |
+
|
109 |
+
loss = tf.reduce_mean(tf.abs(target - output))
|
110 |
+
|
111 |
+
gradient = tf.sign(tape.gradient(loss, im_aligned))
|
112 |
+
|
113 |
+
adv_x = im_aligned + eps * gradient
|
114 |
+
im_aligned = tf.clip_by_value(adv_x, -1, 1)
|
115 |
+
|
116 |
+
if "anonymize" in settings and "reconstruction attack" not in settings:
|
117 |
"""source_z = ArcFace.predict(np.expand_dims(tf.image.resize(im_aligned, [112, 112]) / 255.0, axis=0))
|
118 |
anon_ratio = int(512 * (slider / 100))
|
119 |
anon_vector = np.ones(shape=(1, 512))
|
120 |
anon_vector[:, :anon_ratio] = -1
|
121 |
np.random.shuffle(anon_vector)
|
122 |
source_z *= anon_vector"""
|
123 |
+
|
124 |
slider_weight = slider / 100
|
125 |
+
|
126 |
target_z = ArcFace.predict(np.expand_dims(tf.image.resize(im_aligned, [112, 112]) / 255.0, axis=0))
|
127 |
source_z = IDP.predict(target_z)
|
128 |
+
|
129 |
+
source_z = slider_weight * source_z + (1 - slider_weight) * target_z
|
130 |
+
|
131 |
+
if "reconstruction attack" in settings:
|
132 |
+
source_z = ArcFaceE.predict(np.expand_dims(tf.image.resize(im_aligned, [112, 112]) / 255.0, axis=0))
|
133 |
+
|
134 |
# face swap
|
135 |
+
if "reconstruction attack" not in settings:
|
136 |
+
changed_face_cage = G.predict([np.expand_dims((im_aligned - 127.5) / 127.5, axis=0),
|
137 |
+
source_z])
|
138 |
+
changed_face = (changed_face_cage[0] + 1) / 2
|
139 |
+
|
140 |
+
# get inverse transformation landmarks
|
141 |
+
transformed_lmk = transform_landmark_points(M, lm_align)
|
142 |
+
|
143 |
+
# warp image back
|
144 |
+
iM, _ = inverse_estimate_norm(lm_align, transformed_lmk, 256, "arcface", shrink_factor=1.0)
|
145 |
+
iim_aligned = cv2.warpAffine(changed_face, iM, im_shape, borderValue=0.0)
|
146 |
+
|
147 |
+
# blend swapped face with target image
|
148 |
+
blend_mask = cv2.warpAffine(blend_mask_base, iM, im_shape, borderValue=0.0)
|
149 |
+
blend_mask = np.expand_dims(blend_mask, axis=-1)
|
150 |
+
total_img = (iim_aligned * blend_mask + total_img * (1 - blend_mask))
|
151 |
+
else:
|
152 |
+
changed_face_cage = R.predict([np.expand_dims((im_aligned - 127.5) / 127.5, axis=0),
|
153 |
+
source_z])
|
154 |
+
changed_face = (changed_face_cage[0] + 1) / 2
|
155 |
+
|
156 |
+
# get inverse transformation landmarks
|
157 |
+
transformed_lmk = transform_landmark_points(M, lm_align)
|
158 |
+
|
159 |
+
# warp image back
|
160 |
+
iM, _ = inverse_estimate_norm(lm_align, transformed_lmk, 256, "arcface", shrink_factor=1.0)
|
161 |
+
iim_aligned = cv2.warpAffine(changed_face, iM, im_shape, borderValue=0.0)
|
162 |
+
|
163 |
+
# blend swapped face with target image
|
164 |
+
blend_mask = cv2.warpAffine(blend_mask_base, iM, im_shape, borderValue=0.0)
|
165 |
+
blend_mask = np.expand_dims(blend_mask, axis=-1)
|
166 |
+
total_img = (iim_aligned * blend_mask + total_img * (1 - blend_mask))
|
167 |
+
|
168 |
if "compare" in settings:
|
169 |
total_img = np.concatenate((im / 255.0, total_img), axis=1)
|
170 |
+
|
171 |
total_img = np.clip(total_img, 0, 1)
|
172 |
total_img *= 255.0
|
173 |
total_img = total_img.astype('uint8')
|
174 |
+
|
175 |
return total_img
|
176 |
except Exception as e:
|
177 |
print(e)
|
|
|
185 |
"NOTE: There is no guarantees with the anonymization process currently.\n" \
|
186 |
"\n" \
|
187 |
"Note, source image with too high resolution may not work properly!"
|
188 |
+
examples = [["assets/rick.jpg", "assets/musk.jpg", 100, ["compare"]],
|
189 |
+
["assets/musk.jpg", "assets/musk.jpg", 100, ["anonymize"]]]
|
190 |
+
article = """
|
191 |
Demo is based of recent research from my Ph.D work. Results expects to be published in the coming months.
|
192 |
"""
|
193 |
|
194 |
iface = gradio.Interface(run_inference,
|
195 |
[gradio.inputs.Image(shape=None, label='Target'),
|
196 |
gradio.inputs.Image(shape=None, label='Source'),
|
197 |
+
gradio.inputs.Slider(0, 100, default=100, label="Anonymization ratio (%)"),
|
198 |
+
gradio.inputs.Slider(0, 100, default=100, label="Adversarial defense ratio (%)"),
|
199 |
+
gradio.inputs.CheckboxGroup(["compare",
|
200 |
+
"anonymize",
|
201 |
+
"reconstruction attack",
|
202 |
+
"adversarial defense"],
|
203 |
+
label='Options')],
|
204 |
gradio.outputs.Image(),
|
205 |
title="Face Swap",
|
206 |
description=description,
|