2
0
mirror of https://github.com/Shawn-Shan/fawkes.git synced 2026-06-12 21:50:46 +05:30
Former-commit-id: e7e46967035dfb727d180de0a0780ca9e026dd02 [formerly 0e703ac63e52aafbaa3033759553e2f3b31d2886]
Former-commit-id: 9dfff8ea4c2646d90203b378e0732330e655086a
This commit is contained in:
Shawn-Shan
2020-07-11 18:12:32 -05:00
parent 8a81c16c6d
commit 81a6fed188
7 changed files with 200 additions and 134 deletions
+2 -2
View File
@@ -8,7 +8,7 @@ __version__ = '0.0.6'
from .detect_faces import create_mtcnn, run_detect_face
from .differentiator import FawkesMaskGeneration
from .protection import main
from .protection import main, Fawkes
from .utils import load_extractor, init_gpu, select_target_label, dump_image, reverse_process_cloaked, Faces, get_file
__all__ = (
@@ -16,5 +16,5 @@ __all__ = (
'FawkesMaskGeneration', 'load_extractor',
'init_gpu',
'select_target_label', 'dump_image', 'reverse_process_cloaked',
'Faces', 'get_file', 'main',
'Faces', 'get_file', 'main', 'Fawkes'
)
+3 -3
View File
@@ -405,9 +405,9 @@ class FawkesMaskGeneration:
if all_clear:
break
# if iteration != 0 and iteration % (self.MAX_ITERATIONS // 2) == 0:
# LR = LR / 2
# print("Learning Rate: ", LR)
if iteration != 0 and iteration % (self.MAX_ITERATIONS // 2) == 0:
LR = LR * 0.8
print("Learning Rate: ", LR)
if iteration % (self.MAX_ITERATIONS // 5) == 0:
if self.verbose == 1:
+37 -33
View File
@@ -18,7 +18,7 @@ import numpy as np
from fawkes.differentiator import FawkesMaskGeneration
from fawkes.utils import load_extractor, init_gpu, select_target_label, dump_image, reverse_process_cloaked, \
Faces
from fawkes.align_face import aligner
random.seed(12243)
np.random.seed(122412)
@@ -54,10 +54,14 @@ def check_imgs(imgs):
class Fawkes(object):
def __init__(self, feature_extractor, gpu, batch_size):
global graph
graph = tf.get_default_graph()
self.feature_extractor = feature_extractor
self.gpu = gpu
self.batch_size = batch_size
self.sess = init_gpu(gpu)
self.aligner = aligner(self.sess)
self.fs_names = [feature_extractor]
if isinstance(feature_extractor, list):
self.fs_names = feature_extractor
@@ -67,23 +71,23 @@ class Fawkes(object):
def mode2param(self, mode):
if mode == 'low':
th = 0.003
max_step = 20
max_step = 50
lr = 20
elif mode == 'mid':
th = 0.005
max_step = 50
lr = 15
max_step = 100
lr = 30
elif mode == 'high':
th = 0.008
max_step = 500
lr = 15
max_step = 200
lr = 20
elif mode == 'ultra':
if not tf.test.is_gpu_available():
print("Please enable GPU for ultra setting...")
sys.exit(1)
th = 0.01
max_step = 2000
lr = 8
max_step = 200
lr = 20
else:
raise Exception("mode must be one of 'low', 'mid', 'high', 'ultra', 'custom'")
return th, max_step, lr
@@ -99,38 +103,38 @@ class Fawkes(object):
if not image_paths:
raise Exception("No images in the directory")
with graph.as_default():
faces = Faces(image_paths, self.aligner, verbose=1)
faces = Faces(image_paths, self.sess, verbose=1)
original_images = faces.cropped_faces
original_images = np.array(original_images)
original_images = faces.cropped_faces
original_images = np.array(original_images)
if separate_target:
target_embedding = []
for org_img in original_images:
org_img = org_img.reshape([1] + list(org_img.shape))
tar_emb = select_target_label(org_img, self.feature_extractors_ls, self.fs_names)
target_embedding.append(tar_emb)
target_embedding = np.concatenate(target_embedding)
else:
target_embedding = select_target_label(original_images, self.feature_extractors_ls, self.fs_names)
if separate_target:
target_embedding = []
for org_img in original_images:
org_img = org_img.reshape([1] + list(org_img.shape))
tar_emb = select_target_label(org_img, self.feature_extractors_ls, self.fs_names)
target_embedding.append(tar_emb)
target_embedding = np.concatenate(target_embedding)
else:
target_embedding = select_target_label(original_images, self.feature_extractors_ls, self.fs_names)
protected_images = generate_cloak_images(self.sess, self.feature_extractors_ls, original_images,
target_emb=target_embedding, th=th, faces=faces, sd=sd,
lr=lr, max_step=max_step, batch_size=batch_size)
protected_images = generate_cloak_images(self.sess, self.feature_extractors_ls, original_images,
target_emb=target_embedding, th=th, faces=faces, sd=sd,
lr=lr, max_step=max_step, batch_size=batch_size)
faces.cloaked_cropped_faces = protected_images
faces.cloaked_cropped_faces = protected_images
cloak_perturbation = reverse_process_cloaked(protected_images) - reverse_process_cloaked(original_images)
final_images = faces.merge_faces(cloak_perturbation)
cloak_perturbation = reverse_process_cloaked(protected_images) - reverse_process_cloaked(original_images)
final_images = faces.merge_faces(cloak_perturbation)
for p_img, cloaked_img, path in zip(final_images, protected_images, image_paths):
file_name = "{}_{}_cloaked.{}".format(".".join(path.split(".")[:-1]), mode, format)
dump_image(p_img, file_name, format=format)
for p_img, cloaked_img, path in zip(final_images, protected_images, image_paths):
file_name = "{}_{}_cloaked.{}".format(".".join(path.split(".")[:-1]), mode, format)
dump_image(p_img, file_name, format=format)
elapsed_time = time.time() - start_time
print('attack cost %f s' % elapsed_time)
print("Done!")
elapsed_time = time.time() - start_time
print('attack cost %f s' % elapsed_time)
print("Done!")
def main(*argv):
+2 -2
View File
@@ -86,7 +86,7 @@ def load_image(path):
class Faces(object):
def __init__(self, image_paths, sess, verbose=1):
def __init__(self, image_paths, aligner, verbose=1):
model_dir = os.path.join(os.path.expanduser('~'), '.fawkes')
if not os.path.exists(os.path.join(model_dir, "mtcnn.p.gz")):
os.makedirs(model_dir, exist_ok=True)
@@ -94,7 +94,7 @@ class Faces(object):
cache_subdir='')
self.verbose = verbose
self.aligner = aligner(sess)
self.aligner = aligner
self.org_faces = []
self.cropped_faces = []
self.cropped_faces_shape = []