2
0
mirror of https://github.com/Shawn-Shan/fawkes.git synced 2024-12-22 07:09:33 +05:30
Former-commit-id: c3687684a19c18309f97b69f8161af7a31fe0fb8 [formerly b68719d5e14a54377fafbea9f2c7c9b996bea583]
Former-commit-id: f17d9cbb79833f56450e0518d978603997037e94
This commit is contained in:
Shawn-Shan 2020-07-02 12:32:05 -05:00
parent 71094033f2
commit d7a25eb292
13 changed files with 282 additions and 233 deletions

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
# @Date : 2020-07-01
# @Author : Shawn Shan (shansixiong@cs.uchicago.edu)
# @Link : https://www.shawnshan.com/
__version__ = '0.0.2'
from .differentiator import FawkesMaskGeneration
from .utils import load_extractor, init_gpu, select_target_label, dump_image, reverse_process_cloaked, \
Faces
from .protection import main
import logging
import sys
import os
logging.getLogger('tensorflow').disabled = True
__all__ = (
'__version__',
'FawkesMaskGeneration', 'load_extractor',
'init_gpu',
'select_target_label', 'dump_image', 'reverse_process_cloaked', 'Faces', 'main'
)

4
fawkes/__main__.py Normal file
View File

@ -0,0 +1,4 @@
from .protection import main
if __name__ == '__main__':
main()

View File

@ -1,6 +1,5 @@
import detect_face from .detect_face import detect_face, create_mtcnn
import numpy as np import numpy as np
import tensorflow as tf
# modify the default parameters of np.load # modify the default parameters of np.load
np_load_old = np.load np_load_old = np.load
@ -15,7 +14,7 @@ def to_rgb(img):
def aligner(sess): def aligner(sess):
pnet, rnet, onet = detect_face.create_mtcnn(sess, None) pnet, rnet, onet = create_mtcnn(sess, None)
return [pnet, rnet, onet] return [pnet, rnet, onet]
@ -31,7 +30,7 @@ def align(orig_img, aligner, margin=0.8, detect_multiple_faces=True):
orig_img = to_rgb(orig_img) orig_img = to_rgb(orig_img)
orig_img = orig_img[:, :, 0:3] orig_img = orig_img[:, :, 0:3]
bounding_boxes, _ = detect_face.detect_face(orig_img, minsize, pnet, rnet, onet, threshold, factor) bounding_boxes, _ = detect_face(orig_img, minsize, pnet, rnet, onet, threshold, factor)
nrof_faces = bounding_boxes.shape[0] nrof_faces = bounding_boxes.shape[0]
if nrof_faces > 0: if nrof_faces > 0:
det = bounding_boxes[:, 0:4] det = bounding_boxes[:, 0:4]

View File

@ -29,7 +29,6 @@ from __future__ import print_function
import os import os
# from math import floor
import cv2 import cv2
import numpy as np import numpy as np
import tensorflow as tf import tensorflow as tf

View File

@ -10,7 +10,7 @@ from decimal import Decimal
import numpy as np import numpy as np
import tensorflow as tf import tensorflow as tf
from utils import preprocess, reverse_preprocess from .utils import preprocess, reverse_preprocess
class FawkesMaskGeneration: class FawkesMaskGeneration:

View File

@ -1,3 +1,7 @@
# from __future__ import absolute_import
# from __future__ import division
# from __future__ import print_function
import argparse import argparse
import glob import glob
import os import os
@ -5,26 +9,28 @@ import random
import sys import sys
import numpy as np import numpy as np
from differentiator import FawkesMaskGeneration
from utils import load_extractor, init_gpu, select_target_label, dump_image, reverse_process_cloaked, \ from .differentiator import FawkesMaskGeneration
from .utils import load_extractor, init_gpu, select_target_label, dump_image, reverse_process_cloaked, \
Faces Faces
random.seed(12243) random.seed(12243)
np.random.seed(122412) np.random.seed(122412)
BATCH_SIZE = 10 BATCH_SIZE = 32
def generate_cloak_images(sess, feature_extractors, image_X, target_emb=None, th=0.01, faces=None): def generate_cloak_images(sess, feature_extractors, image_X, target_emb=None, th=0.01, faces=None, sd=1e9, lr=2,
max_step=500):
batch_size = BATCH_SIZE if len(image_X) > BATCH_SIZE else len(image_X) batch_size = BATCH_SIZE if len(image_X) > BATCH_SIZE else len(image_X)
differentiator = FawkesMaskGeneration(sess, feature_extractors, differentiator = FawkesMaskGeneration(sess, feature_extractors,
batch_size=batch_size, batch_size=batch_size,
mimic_img=True, mimic_img=True,
intensity_range='imagenet', intensity_range='imagenet',
initial_const=args.sd, initial_const=sd,
learning_rate=args.lr, learning_rate=lr,
max_iterations=args.max_step, max_iterations=max_step,
l_threshold=th, l_threshold=th,
verbose=1, maximize=False, keep_final=False, image_shape=image_X.shape[1:], verbose=1, maximize=False, keep_final=False, image_shape=image_X.shape[1:],
faces=faces) faces=faces)
@ -33,26 +39,6 @@ def generate_cloak_images(sess, feature_extractors, image_X, target_emb=None, th
return cloaked_image_X return cloaked_image_X
def get_mode_config(mode):
if mode == 'low':
args.feature_extractor = "low_extract"
# args.th = 0.003
args.th = 0.001
elif mode == 'mid':
args.feature_extractor = "mid_extract"
args.th = 0.004
elif mode == 'high':
args.feature_extractor = "high_extract"
args.th = 0.004
elif mode == 'ultra':
args.feature_extractor = "high_extract"
args.th = 0.03
elif mode == 'custom':
pass
else:
raise Exception("mode must be one of 'low', 'mid', 'high', 'ultra', 'custom'")
def check_imgs(imgs): def check_imgs(imgs):
if np.max(imgs) <= 1 and np.min(imgs) >= 0: if np.max(imgs) <= 1 and np.min(imgs) >= 0:
imgs = imgs * 255.0 imgs = imgs * 255.0
@ -63,20 +49,72 @@ def check_imgs(imgs):
return imgs return imgs
def fawkes(): def main(*argv):
if not argv:
argv = list(sys.argv)
# attach SIGPIPE handler to properly handle broken pipe
try: # sigpipe not available under windows. just ignore in this case
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)
parser.add_argument('--separate_target', action='store_true')
parser.add_argument('--format', type=str,
help="final image format",
default="jpg")
args = parser.parse_args(argv[1:])
if args.mode == 'low':
args.feature_extractor = "high_extract"
args.th = 0.003
elif args.mode == 'mid':
args.feature_extractor = "high_extract"
args.th = 0.005
elif args.mode == 'high':
args.feature_extractor = "high_extract"
args.th = 0.007
elif args.mode == 'ultra':
args.feature_extractor = "high_extract"
args.th = 0.01
elif args.mode == 'custom':
pass
else:
raise Exception("mode must be one of 'low', 'mid', 'high', 'ultra', 'custom'")
assert args.format in ['png', 'jpg', 'jpeg'] assert args.format in ['png', 'jpg', 'jpeg']
if args.format == 'jpg': if args.format == 'jpg':
args.format = 'jpeg' args.format = 'jpeg'
get_mode_config(args.mode)
sess = init_gpu(args.gpu) sess = init_gpu(args.gpu)
# feature_extractors_ls = [load_extractor(args.feature_extractor)]
# fs_names = ['mid_extract', 'high_extract']
fs_names = [args.feature_extractor] fs_names = [args.feature_extractor]
feature_extractors_ls = [load_extractor(name) for name in fs_names] feature_extractors_ls = [load_extractor(name) for name in fs_names]
image_paths = glob.glob(os.path.join(args.directory, "*")) image_paths = glob.glob(os.path.join(args.directory, "*"))
image_paths = [path for path in image_paths if "_cloaked" not in path.split("/")[-1]] image_paths = [path for path in image_paths if "_cloaked" not in path.split("/")[-1]]
if not image_paths:
print("No images in the directory")
exit(1)
faces = Faces(image_paths, sess) faces = Faces(image_paths, sess)
@ -94,7 +132,8 @@ def fawkes():
target_embedding = select_target_label(orginal_images, feature_extractors_ls, fs_names) target_embedding = select_target_label(orginal_images, feature_extractors_ls, fs_names)
protected_images = generate_cloak_images(sess, feature_extractors_ls, orginal_images, protected_images = generate_cloak_images(sess, feature_extractors_ls, orginal_images,
target_emb=target_embedding, th=args.th, faces=faces) target_emb=target_embedding, th=args.th, faces=faces, sd=args.sd,
lr=args.lr, max_step=args.max_step)
faces.cloaked_cropped_faces = protected_images faces.cloaked_cropped_faces = protected_images
@ -102,42 +141,9 @@ def fawkes():
final_images = faces.merge_faces(cloak_perturbation) final_images = faces.merge_faces(cloak_perturbation)
for p_img, cloaked_img, path in zip(final_images, protected_images, image_paths): for p_img, cloaked_img, path in zip(final_images, protected_images, image_paths):
file_name = "{}_{}_{}_{}_cloaked.{}".format(".".join(path.split(".")[:-1]), args.mode, args.th, file_name = "{}_{}_{}_cloaked.{}".format(".".join(path.split(".")[:-1]), args.mode, args.th, args.format)
args.feature_extractor, args.format)
dump_image(p_img, file_name, format=args.format) dump_image(p_img, file_name, format=args.format)
#
# file_name = "{}_{}_{}_{}_cloaked_cropped.png".format(".".join(path.split(".")[:-1]), args.mode, args.th,
# args.feature_extractor)
# dump_image(reverse_process_cloaked(cloaked_img), file_name, format="png")
def parse_arguments(argv):
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=200)
parser.add_argument('--sd', type=int, default=1e9)
parser.add_argument('--lr', type=float, default=10)
parser.add_argument('--separate_target', action='store_true')
parser.add_argument('--format', type=str,
help="final image format",
default="jpg")
return parser.parse_args(argv)
if __name__ == '__main__': if __name__ == '__main__':
args = parse_arguments(sys.argv[1:]) main(*sys.argv)
fawkes()

View File

@ -4,21 +4,26 @@ import json
import os import os
import pickle import pickle
import random import random
import sys
stderr = sys.stderr
sys.stderr = open(os.devnull, 'w')
import keras import keras
sys.stderr = stderr
import keras.backend as K import keras.backend as K
import numpy as np import numpy as np
import tensorflow as tf import tensorflow as tf
from align_face import align, aligner from PIL import Image, ExifTags
from keras.applications.vgg16 import preprocess_input # from keras.applications.vgg16 import preprocess_input
from keras.layers import Dense, Activation from keras.layers import Dense, Activation
from keras.models import Model from keras.models import Model
from keras.preprocessing import image from keras.preprocessing import image
from keras.utils import get_file from keras.utils import get_file
from keras.utils import to_categorical
from skimage.transform import resize from skimage.transform import resize
from sklearn.metrics import pairwise_distances from sklearn.metrics import pairwise_distances
from PIL import Image, ExifTags
from .align_face import align, aligner
def clip_img(X, preprocessing='raw'): def clip_img(X, preprocessing='raw'):
@ -81,7 +86,14 @@ class Faces(object):
self.cropped_index.extend(cur_index) self.cropped_index.extend(cur_index)
self.callback_idx.extend([i] * len(cur_faces_square)) self.callback_idx.extend([i] * len(cur_faces_square))
self.cropped_faces = preprocess_input(np.array(self.cropped_faces)) if not self.cropped_faces:
print("No faces detected")
exit(1)
self.cropped_faces = np.array(self.cropped_faces)
self.cropped_faces = preprocess(self.cropped_faces, 'imagenet')
self.cloaked_cropped_faces = None self.cloaked_cropped_faces = None
self.cloaked_faces = np.copy(self.org_faces) self.cloaked_faces = np.copy(self.org_faces)
@ -89,8 +101,6 @@ class Faces(object):
return self.cropped_faces return self.cropped_faces
def merge_faces(self, cloaks): def merge_faces(self, cloaks):
# import pdb
# pdb.set_trace()
self.cloaked_faces = np.copy(self.org_faces) self.cloaked_faces = np.copy(self.org_faces)
@ -300,7 +310,6 @@ def load_extractor(name):
return model return model
def get_dataset_path(dataset): def get_dataset_path(dataset):
model_dir = os.path.join(os.path.expanduser('~'), '.fawkes') model_dir = os.path.join(os.path.expanduser('~'), '.fawkes')
if not os.path.exists(os.path.join(model_dir, "config.json")): if not os.path.exists(os.path.join(model_dir, "config.json")):
@ -335,7 +344,7 @@ def load_dir(path):
im = image.img_to_array(im) im = image.img_to_array(im)
x_ls.append(im) x_ls.append(im)
raw_x = np.array(x_ls) raw_x = np.array(x_ls)
return preprocess_input(raw_x) return preprocess(raw_x, 'imagenet')
def load_embeddings(feature_extractors_names): def load_embeddings(feature_extractors_names):
@ -394,10 +403,19 @@ def select_target_label(imgs, feature_extractors_ls, feature_extractors_names, m
max_sum = np.min(pair_dist, axis=0) max_sum = np.min(pair_dist, axis=0)
max_id = np.argmax(max_sum) max_id = np.argmax(max_sum)
image_paths = glob.glob(os.path.join(model_dir, "target_data/{}/*".format(paths[int(max_id)]))) target_data_id = paths[int(max_id)]
image_dir = os.path.join(model_dir, "target_data/{}/*".format(target_data_id))
if not os.path.exists(image_dir):
get_file("{}.h5".format(name), "http://sandlab.cs.uchicago.edu/fawkes/files/target_images".format(name),
cache_dir=model_dir, cache_subdir='')
image_paths = glob.glob(image_dir)
target_images = [image.img_to_array(image.load_img(cur_path)) for cur_path in target_images = [image.img_to_array(image.load_img(cur_path)) for cur_path in
image_paths] image_paths]
target_images = preprocess_input(np.array([resize(x, (224, 224)) for x in target_images]))
target_images = np.array([resize(x, (224, 224)) for x in target_images])
target_images = preprocess(target_images, 'imagenet')
target_images = list(target_images) target_images = list(target_images)
while len(target_images) < len(imgs): while len(target_images) < len(imgs):
@ -406,152 +424,151 @@ def select_target_label(imgs, feature_extractors_ls, feature_extractors_names, m
target_images = random.sample(target_images, len(imgs)) target_images = random.sample(target_images, len(imgs))
return np.array(target_images) return np.array(target_images)
# class CloakData(object):
class CloakData(object): # def __init__(self, protect_directory=None, img_shape=(224, 224)):
def __init__(self, protect_directory=None, img_shape=(224, 224)): #
# self.img_shape = img_shape
self.img_shape = img_shape # # self.train_data_dir, self.test_data_dir, self.number_classes, self.number_samples = get_dataset_path(dataset)
# self.train_data_dir, self.test_data_dir, self.number_classes, self.number_samples = get_dataset_path(dataset) # # self.all_labels = sorted(list(os.listdir(self.train_data_dir)))
# self.all_labels = sorted(list(os.listdir(self.train_data_dir))) # self.protect_directory = protect_directory
self.protect_directory = protect_directory #
# self.protect_X = self.load_label_data(self.protect_directory)
self.protect_X = self.load_label_data(self.protect_directory) #
# self.cloaked_protect_train_X = None
self.cloaked_protect_train_X = None #
# self.label2path_train, self.label2path_test, self.path2idx = self.build_data_mapping()
self.label2path_train, self.label2path_test, self.path2idx = self.build_data_mapping() # self.all_training_path = self.get_all_data_path(self.label2path_train)
self.all_training_path = self.get_all_data_path(self.label2path_train) # self.all_test_path = self.get_all_data_path(self.label2path_test)
self.all_test_path = self.get_all_data_path(self.label2path_test) # self.protect_class_path = self.get_class_image_files(os.path.join(self.train_data_dir, self.protect_class))
self.protect_class_path = self.get_class_image_files(os.path.join(self.train_data_dir, self.protect_class)) #
# def get_class_image_files(self, path):
def get_class_image_files(self, path): # return [os.path.join(path, f) for f in os.listdir(path)]
return [os.path.join(path, f) for f in os.listdir(path)] #
# def extractor_ls_predict(self, feature_extractors_ls, X):
def extractor_ls_predict(self, feature_extractors_ls, X): # feature_ls = []
feature_ls = [] # for extractor in feature_extractors_ls:
for extractor in feature_extractors_ls: # cur_features = extractor.predict(X)
cur_features = extractor.predict(X) # feature_ls.append(cur_features)
feature_ls.append(cur_features) # concated_feature_ls = np.concatenate(feature_ls, axis=1)
concated_feature_ls = np.concatenate(feature_ls, axis=1) # concated_feature_ls = normalize(concated_feature_ls)
concated_feature_ls = normalize(concated_feature_ls) # return concated_feature_ls
return concated_feature_ls #
# def load_embeddings(self, feature_extractors_names):
def load_embeddings(self, feature_extractors_names): # dictionaries = []
dictionaries = [] # for extractor_name in feature_extractors_names:
for extractor_name in feature_extractors_names: # path2emb = pickle.load(open("../feature_extractors/embeddings/{}_emb_norm.p".format(extractor_name), "rb"))
path2emb = pickle.load(open("../feature_extractors/embeddings/{}_emb_norm.p".format(extractor_name), "rb")) # dictionaries.append(path2emb)
dictionaries.append(path2emb) #
# merge_dict = {}
merge_dict = {} # for k in dictionaries[0].keys():
for k in dictionaries[0].keys(): # cur_emb = [dic[k] for dic in dictionaries]
cur_emb = [dic[k] for dic in dictionaries] # merge_dict[k] = np.concatenate(cur_emb)
merge_dict[k] = np.concatenate(cur_emb) # return merge_dict
return merge_dict #
# def select_target_label(self, feature_extractors_ls, feature_extractors_names, metric='l2'):
def select_target_label(self, feature_extractors_ls, feature_extractors_names, metric='l2'): # original_feature_x = self.extractor_ls_predict(feature_extractors_ls, self.protect_train_X)
original_feature_x = self.extractor_ls_predict(feature_extractors_ls, self.protect_train_X) #
# path2emb = self.load_embeddings(feature_extractors_names)
path2emb = self.load_embeddings(feature_extractors_names) # items = list(path2emb.items())
items = list(path2emb.items()) # paths = [p[0] for p in items]
paths = [p[0] for p in items] # embs = [p[1] for p in items]
embs = [p[1] for p in items] # embs = np.array(embs)
embs = np.array(embs) #
# pair_dist = pairwise_distances(original_feature_x, embs, metric)
pair_dist = pairwise_distances(original_feature_x, embs, metric) # max_sum = np.min(pair_dist, axis=0)
max_sum = np.min(pair_dist, axis=0) # sorted_idx = np.argsort(max_sum)[::-1]
sorted_idx = np.argsort(max_sum)[::-1] #
# highest_num = 0
highest_num = 0 # paired_target_X = None
paired_target_X = None # final_target_class_path = None
final_target_class_path = None # for idx in sorted_idx[:5]:
for idx in sorted_idx[:5]: # target_class_path = paths[idx]
target_class_path = paths[idx] # cur_target_X = self.load_dir(target_class_path)
cur_target_X = self.load_dir(target_class_path) # cur_target_X = np.concatenate([cur_target_X, cur_target_X, cur_target_X])
cur_target_X = np.concatenate([cur_target_X, cur_target_X, cur_target_X]) # cur_tot_sum, cur_paired_target_X = self.calculate_dist_score(self.protect_train_X, cur_target_X,
cur_tot_sum, cur_paired_target_X = self.calculate_dist_score(self.protect_train_X, cur_target_X, # feature_extractors_ls,
feature_extractors_ls, # metric=metric)
metric=metric) # if cur_tot_sum > highest_num:
if cur_tot_sum > highest_num: # highest_num = cur_tot_sum
highest_num = cur_tot_sum # paired_target_X = cur_paired_target_X
paired_target_X = cur_paired_target_X # final_target_class_path = target_class_path
final_target_class_path = target_class_path #
# np.random.shuffle(paired_target_X)
np.random.shuffle(paired_target_X) # return final_target_class_path, paired_target_X
return final_target_class_path, paired_target_X #
# def calculate_dist_score(self, a, b, feature_extractors_ls, metric='l2'):
def calculate_dist_score(self, a, b, feature_extractors_ls, metric='l2'): # features1 = self.extractor_ls_predict(feature_extractors_ls, a)
features1 = self.extractor_ls_predict(feature_extractors_ls, a) # features2 = self.extractor_ls_predict(feature_extractors_ls, b)
features2 = self.extractor_ls_predict(feature_extractors_ls, b) #
# pair_cos = pairwise_distances(features1, features2, metric)
pair_cos = pairwise_distances(features1, features2, metric) # max_sum = np.min(pair_cos, axis=0)
max_sum = np.min(pair_cos, axis=0) # max_sum_arg = np.argsort(max_sum)[::-1]
max_sum_arg = np.argsort(max_sum)[::-1] # max_sum_arg = max_sum_arg[:len(a)]
max_sum_arg = max_sum_arg[:len(a)] # max_sum = [max_sum[i] for i in max_sum_arg]
max_sum = [max_sum[i] for i in max_sum_arg] # paired_target_X = [b[j] for j in max_sum_arg]
paired_target_X = [b[j] for j in max_sum_arg] # paired_target_X = np.array(paired_target_X)
paired_target_X = np.array(paired_target_X) # return np.min(max_sum), paired_target_X
return np.min(max_sum), paired_target_X #
# def get_all_data_path(self, label2path):
def get_all_data_path(self, label2path): # all_paths = []
all_paths = [] # for k, v in label2path.items():
for k, v in label2path.items(): # cur_all_paths = [os.path.join(k, cur_p) for cur_p in v]
cur_all_paths = [os.path.join(k, cur_p) for cur_p in v] # all_paths.extend(cur_all_paths)
all_paths.extend(cur_all_paths) # return all_paths
return all_paths #
# def load_label_data(self, label):
def load_label_data(self, label): # train_label_path = os.path.join(self.train_data_dir, label)
train_label_path = os.path.join(self.train_data_dir, label) # test_label_path = os.path.join(self.test_data_dir, label)
test_label_path = os.path.join(self.test_data_dir, label) # train_X = self.load_dir(train_label_path)
train_X = self.load_dir(train_label_path) # test_X = self.load_dir(test_label_path)
test_X = self.load_dir(test_label_path) # return train_X, test_X
return train_X, test_X #
# def load_dir(self, path):
def load_dir(self, path): # assert os.path.exists(path)
assert os.path.exists(path) # x_ls = []
x_ls = [] # for file in os.listdir(path):
for file in os.listdir(path): # cur_path = os.path.join(path, file)
cur_path = os.path.join(path, file) # im = image.load_img(cur_path, target_size=self.img_shape)
im = image.load_img(cur_path, target_size=self.img_shape) # im = image.img_to_array(im)
im = image.img_to_array(im) # x_ls.append(im)
x_ls.append(im) # raw_x = np.array(x_ls)
raw_x = np.array(x_ls) # return preprocess_input(raw_x)
return preprocess_input(raw_x) #
# def build_data_mapping(self):
def build_data_mapping(self): # label2path_train = {}
label2path_train = {} # label2path_test = {}
label2path_test = {} # idx = 0
idx = 0 # path2idx = {}
path2idx = {} # for label_name in self.all_labels:
for label_name in self.all_labels: # full_path_train = os.path.join(self.train_data_dir, label_name)
full_path_train = os.path.join(self.train_data_dir, label_name) # full_path_test = os.path.join(self.test_data_dir, label_name)
full_path_test = os.path.join(self.test_data_dir, label_name) # label2path_train[full_path_train] = list(os.listdir(full_path_train))
label2path_train[full_path_train] = list(os.listdir(full_path_train)) # label2path_test[full_path_test] = list(os.listdir(full_path_test))
label2path_test[full_path_test] = list(os.listdir(full_path_test)) # for img_file in os.listdir(full_path_train):
for img_file in os.listdir(full_path_train): # path2idx[os.path.join(full_path_train, img_file)] = idx
path2idx[os.path.join(full_path_train, img_file)] = idx # for img_file in os.listdir(full_path_test):
for img_file in os.listdir(full_path_test): # path2idx[os.path.join(full_path_test, img_file)] = idx
path2idx[os.path.join(full_path_test, img_file)] = idx # idx += 1
idx += 1 # return label2path_train, label2path_test, path2idx
return label2path_train, label2path_test, path2idx #
# def generate_data_post_cloak(self, sybil=False):
def generate_data_post_cloak(self, sybil=False): # assert self.cloaked_protect_train_X is not None
assert self.cloaked_protect_train_X is not None # while True:
while True: # batch_X = []
batch_X = [] # batch_Y = []
batch_Y = [] # cur_batch_path = random.sample(self.all_training_path, 32)
cur_batch_path = random.sample(self.all_training_path, 32) # for p in cur_batch_path:
for p in cur_batch_path: # cur_y = self.path2idx[p]
cur_y = self.path2idx[p] # if p in self.protect_class_path:
if p in self.protect_class_path: # cur_x = random.choice(self.cloaked_protect_train_X)
cur_x = random.choice(self.cloaked_protect_train_X) # elif sybil and (p in self.sybil_class):
elif sybil and (p in self.sybil_class): # cur_x = random.choice(self.cloaked_sybil_train_X)
cur_x = random.choice(self.cloaked_sybil_train_X) # else:
else: # im = image.load_img(p, target_size=self.img_shape)
im = image.load_img(p, target_size=self.img_shape) # im = image.img_to_array(im)
im = image.img_to_array(im) # cur_x = preprocess_input(im)
cur_x = preprocess_input(im) # batch_X.append(cur_x)
batch_X.append(cur_x) # batch_Y.append(cur_y)
batch_Y.append(cur_y) # batch_X = np.array(batch_X)
batch_X = np.array(batch_X) # batch_Y = to_categorical(np.array(batch_Y), num_classes=self.number_classes)
batch_Y = to_categorical(np.array(batch_Y), num_classes=self.number_classes) # yield batch_X, batch_Y
yield batch_X, batch_Y