diff --git a/fawkes/protection.py b/fawkes/protection.py index 79a41f3..68f5b70 100644 --- a/fawkes/protection.py +++ b/fawkes/protection.py @@ -102,26 +102,26 @@ class Fawkes(object): faces = Faces(image_paths, self.sess, verbose=1) - orginal_images = faces.cropped_faces - orginal_images = np.array(orginal_images) + original_images = faces.cropped_faces + original_images = np.array(original_images) if separate_target: target_embedding = [] - for org_img in orginal_images: + 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(orginal_images, self.feature_extractors_ls, self.fs_names) + 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, orginal_images, + 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 - cloak_perturbation = reverse_process_cloaked(protected_images) - reverse_process_cloaked(orginal_images) + 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): @@ -129,7 +129,7 @@ class Fawkes(object): dump_image(p_img, file_name, format=format) elapsed_time = time.time() - start_time - print('attack cost %f s' % (elapsed_time)) + print('attack cost %f s' % elapsed_time) print("Done!") diff --git a/fawkes/utils.py b/fawkes/utils.py index 3e1b7d0..6fae615 100644 --- a/fawkes/utils.py +++ b/fawkes/utils.py @@ -25,7 +25,6 @@ from keras.layers import Dense, Activation from keras.models import Model from keras.preprocessing import image from skimage.transform import resize -from sklearn.metrics import pairwise_distances from fawkes.align_face import align, aligner from six.moves.urllib.request import urlopen @@ -422,11 +421,27 @@ def extractor_ls_predict(feature_extractors_ls, X): return concated_feature_ls +def pairwise_l2_distance(A, B): + BT = B.transpose() + vecProd = np.dot(A, BT) + SqA = A ** 2 + sumSqA = np.matrix(np.sum(SqA, axis=1)) + sumSqAEx = np.tile(sumSqA.transpose(), (1, vecProd.shape[1])) + + SqB = B ** 2 + sumSqB = np.sum(SqB, axis=1) + sumSqBEx = np.tile(sumSqB, (vecProd.shape[0], 1)) + SqED = sumSqBEx + sumSqAEx - 2 * vecProd + SqED[SqED < 0] = 0.0 + ED = np.sqrt(SqED) + return ED + + def calculate_dist_score(a, b, feature_extractors_ls, metric='l2'): features1 = extractor_ls_predict(feature_extractors_ls, a) features2 = extractor_ls_predict(feature_extractors_ls, b) - pair_cos = pairwise_distances(features1, features2, metric) + pair_cos = pairwise_l2_distance(features1, features2) max_sum = np.min(pair_cos, axis=0) max_sum_arg = np.argsort(max_sum)[::-1] max_sum_arg = max_sum_arg[:len(a)] @@ -447,7 +462,7 @@ def select_target_label(imgs, feature_extractors_ls, feature_extractors_names, m embs = [p[1] for p in items] embs = np.array(embs) - pair_dist = pairwise_distances(original_feature_x, embs, metric) + pair_dist = pairwise_l2_distance(original_feature_x, embs) max_sum = np.min(pair_dist, axis=0) max_id = np.argmax(max_sum) diff --git a/setup.py b/setup.py index fbec16e..3fb4385 100644 --- a/setup.py +++ b/setup.py @@ -82,8 +82,7 @@ install_requires = [ 'keras==2.2.5', 'scikit-image', 'pillow>=7.0.0', - 'opencv-python>=4.2.0.34', - 'sklearn', + 'opencv-python>=4.2.0.34' ] setup(