2020-07-02 23:02:05 +05:30
|
|
|
# from __future__ import absolute_import
|
|
|
|
# from __future__ import division
|
|
|
|
# from __future__ import print_function
|
|
|
|
|
2020-05-18 05:48:41 +05:30
|
|
|
import argparse
|
2020-06-29 05:43:14 +05:30
|
|
|
import glob
|
2020-05-18 05:48:41 +05:30
|
|
|
import os
|
2020-05-19 02:05:14 +05:30
|
|
|
import random
|
|
|
|
import sys
|
2020-07-07 03:22:46 +05:30
|
|
|
import time
|
2020-05-19 02:05:14 +05:30
|
|
|
|
|
|
|
import numpy as np
|
2020-07-02 23:02:05 +05:30
|
|
|
|
|
|
|
from .differentiator import FawkesMaskGeneration
|
|
|
|
from .utils import load_extractor, init_gpu, select_target_label, dump_image, reverse_process_cloaked, \
|
2020-07-02 07:46:03 +05:30
|
|
|
Faces
|
2020-05-18 05:48:41 +05:30
|
|
|
|
|
|
|
random.seed(12243)
|
|
|
|
np.random.seed(122412)
|
|
|
|
|
|
|
|
|
2020-07-02 23:02:05 +05:30
|
|
|
def generate_cloak_images(sess, feature_extractors, image_X, target_emb=None, th=0.01, faces=None, sd=1e9, lr=2,
|
2020-07-07 03:22:46 +05:30
|
|
|
max_step=500, batch_size=1):
|
|
|
|
batch_size = batch_size if len(image_X) > batch_size else len(image_X)
|
2020-06-29 10:04:48 +05:30
|
|
|
|
2020-06-29 05:43:14 +05:30
|
|
|
differentiator = FawkesMaskGeneration(sess, feature_extractors,
|
|
|
|
batch_size=batch_size,
|
2020-05-18 05:48:41 +05:30
|
|
|
mimic_img=True,
|
|
|
|
intensity_range='imagenet',
|
2020-07-02 23:02:05 +05:30
|
|
|
initial_const=sd,
|
|
|
|
learning_rate=lr,
|
|
|
|
max_iterations=max_step,
|
2020-05-18 05:48:41 +05:30
|
|
|
l_threshold=th,
|
2020-07-02 07:46:03 +05:30
|
|
|
verbose=1, maximize=False, keep_final=False, image_shape=image_X.shape[1:],
|
|
|
|
faces=faces)
|
2020-05-18 05:48:41 +05:30
|
|
|
|
2020-07-02 07:46:03 +05:30
|
|
|
cloaked_image_X = differentiator.attack(image_X, target_emb)
|
2020-05-18 05:48:41 +05:30
|
|
|
return cloaked_image_X
|
|
|
|
|
|
|
|
|
2020-07-02 07:46:03 +05:30
|
|
|
def check_imgs(imgs):
|
|
|
|
if np.max(imgs) <= 1 and np.min(imgs) >= 0:
|
|
|
|
imgs = imgs * 255.0
|
|
|
|
elif np.max(imgs) <= 255 and np.min(imgs) >= 0:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
raise Exception("Image values ")
|
|
|
|
return imgs
|
2020-05-18 05:48:41 +05:30
|
|
|
|
2020-05-19 02:05:14 +05:30
|
|
|
|
2020-07-02 23:02:05 +05:30
|
|
|
def main(*argv):
|
2020-07-07 03:22:46 +05:30
|
|
|
start_time = time.time()
|
2020-07-02 23:02:05 +05:30
|
|
|
if not argv:
|
|
|
|
argv = list(sys.argv)
|
|
|
|
|
2020-07-07 03:22:46 +05:30
|
|
|
try:
|
2020-07-02 23:02:05 +05:30
|
|
|
import signal
|
|
|
|
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
|
|
|
|
except Exception as e:
|
|
|
|
pass
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument('--directory', '-d', type=str,
|
|
|
|
help='directory that contain images for cloaking', default='imgs/')
|
|
|
|
|
|
|
|
parser.add_argument('--gpu', type=str,
|
|
|
|
help='GPU id', default='0')
|
|
|
|
|
|
|
|
parser.add_argument('--mode', type=str,
|
|
|
|
help='cloak generation mode', default='high')
|
|
|
|
parser.add_argument('--feature-extractor', type=str,
|
|
|
|
help="name of the feature extractor used for optimization",
|
|
|
|
default="high_extract")
|
|
|
|
|
|
|
|
parser.add_argument('--th', type=float, default=0.01)
|
|
|
|
parser.add_argument('--max-step', type=int, default=500)
|
|
|
|
parser.add_argument('--sd', type=int, default=1e9)
|
|
|
|
parser.add_argument('--lr', type=float, default=2)
|
|
|
|
|
2020-07-07 03:22:46 +05:30
|
|
|
parser.add_argument('--batch-size', type=int, default=1)
|
2020-07-02 23:02:05 +05:30
|
|
|
parser.add_argument('--separate_target', action='store_true')
|
|
|
|
|
|
|
|
parser.add_argument('--format', type=str,
|
|
|
|
help="final image format",
|
2020-07-07 03:22:46 +05:30
|
|
|
default="png")
|
2020-07-02 23:02:05 +05:30
|
|
|
args = parser.parse_args(argv[1:])
|
|
|
|
|
|
|
|
if args.mode == 'low':
|
|
|
|
args.feature_extractor = "high_extract"
|
|
|
|
args.th = 0.003
|
2020-07-07 03:22:46 +05:30
|
|
|
args.max_step = 100
|
|
|
|
args.lr = 15
|
2020-07-02 23:02:05 +05:30
|
|
|
elif args.mode == 'mid':
|
|
|
|
args.feature_extractor = "high_extract"
|
|
|
|
args.th = 0.005
|
2020-07-07 03:22:46 +05:30
|
|
|
args.max_step = 100
|
|
|
|
args.lr = 15
|
2020-07-02 23:02:05 +05:30
|
|
|
elif args.mode == 'high':
|
|
|
|
args.feature_extractor = "high_extract"
|
|
|
|
args.th = 0.007
|
2020-07-07 03:22:46 +05:30
|
|
|
args.max_step = 100
|
|
|
|
args.lr = 10
|
2020-07-02 23:02:05 +05:30
|
|
|
elif args.mode == 'ultra':
|
|
|
|
args.feature_extractor = "high_extract"
|
|
|
|
args.th = 0.01
|
2020-07-07 03:22:46 +05:30
|
|
|
args.max_step = 1000
|
|
|
|
args.lr = 5
|
2020-07-02 23:02:05 +05:30
|
|
|
elif args.mode == 'custom':
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
raise Exception("mode must be one of 'low', 'mid', 'high', 'ultra', 'custom'")
|
|
|
|
|
2020-07-02 07:46:03 +05:30
|
|
|
assert args.format in ['png', 'jpg', 'jpeg']
|
|
|
|
if args.format == 'jpg':
|
|
|
|
args.format = 'jpeg'
|
2020-06-29 05:43:14 +05:30
|
|
|
|
|
|
|
sess = init_gpu(args.gpu)
|
2020-07-02 07:46:03 +05:30
|
|
|
fs_names = [args.feature_extractor]
|
|
|
|
feature_extractors_ls = [load_extractor(name) for name in fs_names]
|
2020-06-29 05:43:14 +05:30
|
|
|
|
|
|
|
image_paths = glob.glob(os.path.join(args.directory, "*"))
|
|
|
|
image_paths = [path for path in image_paths if "_cloaked" not in path.split("/")[-1]]
|
2020-07-02 23:02:05 +05:30
|
|
|
if not image_paths:
|
|
|
|
print("No images in the directory")
|
|
|
|
exit(1)
|
2020-05-18 05:48:41 +05:30
|
|
|
|
2020-07-07 03:22:46 +05:30
|
|
|
faces = Faces(image_paths, sess, verbose=1)
|
2020-05-18 05:48:41 +05:30
|
|
|
|
2020-07-02 07:46:03 +05:30
|
|
|
orginal_images = faces.cropped_faces
|
2020-06-29 05:43:14 +05:30
|
|
|
orginal_images = np.array(orginal_images)
|
2020-05-18 05:48:41 +05:30
|
|
|
|
2020-07-02 07:46:03 +05:30
|
|
|
if args.separate_target:
|
|
|
|
target_embedding = []
|
2020-06-29 05:43:14 +05:30
|
|
|
for org_img in orginal_images:
|
|
|
|
org_img = org_img.reshape([1] + list(org_img.shape))
|
2020-07-02 07:46:03 +05:30
|
|
|
tar_emb = select_target_label(org_img, feature_extractors_ls, fs_names)
|
|
|
|
target_embedding.append(tar_emb)
|
|
|
|
target_embedding = np.concatenate(target_embedding)
|
2020-06-29 05:43:14 +05:30
|
|
|
else:
|
2020-07-02 07:46:03 +05:30
|
|
|
target_embedding = select_target_label(orginal_images, feature_extractors_ls, fs_names)
|
2020-05-18 05:48:41 +05:30
|
|
|
|
2020-06-29 05:43:14 +05:30
|
|
|
protected_images = generate_cloak_images(sess, feature_extractors_ls, orginal_images,
|
2020-07-02 23:02:05 +05:30
|
|
|
target_emb=target_embedding, th=args.th, faces=faces, sd=args.sd,
|
2020-07-07 03:22:46 +05:30
|
|
|
lr=args.lr, max_step=args.max_step, batch_size=args.batch_size)
|
2020-07-02 07:46:03 +05:30
|
|
|
|
|
|
|
faces.cloaked_cropped_faces = protected_images
|
|
|
|
|
|
|
|
cloak_perturbation = reverse_process_cloaked(protected_images) - reverse_process_cloaked(orginal_images)
|
|
|
|
final_images = faces.merge_faces(cloak_perturbation)
|
2020-06-29 05:43:14 +05:30
|
|
|
|
2020-07-02 07:46:03 +05:30
|
|
|
for p_img, cloaked_img, path in zip(final_images, protected_images, image_paths):
|
2020-07-07 03:22:46 +05:30
|
|
|
file_name = "{}_{}_cloaked.{}".format(".".join(path.split(".")[:-1]), args.mode, args.format)
|
2020-07-02 07:46:03 +05:30
|
|
|
dump_image(p_img, file_name, format=args.format)
|
2020-05-18 05:48:41 +05:30
|
|
|
|
2020-07-07 03:22:46 +05:30
|
|
|
elapsed_time = time.time() - start_time
|
|
|
|
print('attack cost %f s' % (elapsed_time))
|
|
|
|
|
2020-05-18 05:48:41 +05:30
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2020-07-02 23:02:05 +05:30
|
|
|
main(*sys.argv)
|