def __init__(self, keras_model, rc_name): # Example of rc_name: FeatureSqueezing?squeezer=bit_depth_1 self.model_predict = lambda x: keras_model.predict(x) subject, params = parse_params(rc_name) assert subject == 'MagNet' if FLAGS.dataset_name == "MNIST": self.reformer = SimpleReformer(mnist_autoencoder_fpath) elif FLAGS.dataset_name == "CIFAR-10": self.reformer = SimpleReformer(cifar10_autoencoder_fpath) self.filter = lambda x: self.reformer.heal(x)
def __init__(self, model, detector_name): classifier = ClassifierWrapper(model) autoencoder_model_fpath = os.path.join( project_dir, "downloads/MagNet/defensive_models/CIFAR") reformer = SimpleReformer(autoencoder_model_fpath) id_reformer = IdReformer() # Note: we may swap the two. reconstructor = id_reformer prober = reformer # reconstructor = reformer # prober = id_reformer eb_detector = AEDetector(autoencoder_model_fpath, p=1) db_detector_I = DBDetector(reconstructor, prober, classifier, T=10) db_detector_II = DBDetector(reconstructor, prober, classifier, T=40) detector_dict = dict() detector_dict["db-I"] = db_detector_I detector_dict["db-II"] = db_detector_II detector_dict['eb'] = eb_detector self.operator = Operator(CIFAR(), classifier, detector_dict, reformer)
def __init__(self, model, detector_name): cur_folder = os.path.dirname(os.path.abspath(__file__)) autoencoder_model_I_fpath = os.path.join(project_dir, "downloads/MagNet/defensive_models/MNIST_I") autoencoder_model_II_fpath = os.path.join(project_dir, "downloads/MagNet/defensive_models/MNIST_II") detector_I = AEDetector(autoencoder_model_I_fpath, p=2) detector_II = AEDetector(autoencoder_model_II_fpath, p=1) reformer = SimpleReformer(autoencoder_model_I_fpath) id_reformer = IdReformer() classifier = ClassifierWrapper(model) detector_dict = dict() detector_dict["I"] = detector_I detector_dict["II"] = detector_II self.operator = Operator(MNIST(), classifier, detector_dict, reformer)
class MagNetRC: def __init__(self, keras_model, rc_name): # Example of rc_name: FeatureSqueezing?squeezer=bit_depth_1 self.model_predict = lambda x: keras_model.predict(x) subject, params = parse_params(rc_name) assert subject == 'MagNet' if tf.flags.FLAGS.dataset_name == "MNIST": self.reformer = SimpleReformer(mnist_autoencoder_fpath) elif tf.flags.FLAGS.dataset_name == "CIFAR-10": self.reformer = SimpleReformer(cifar10_autoencoder_fpath) self.filter = lambda x: self.reformer.heal(x) def predict(self, X): X_filtered = self.filter(X) Y_pred = self.model_predict(X_filtered) return Y_pred def visualize_and_predict(self, X): X_filtered = self.filter(X) Y_pred = self.model_predict(X_filtered) return X_filtered, Y_pred
y = tf.py_func(my_func, [imgs], tf.float32, stateful=False) return y none_tf = none_py = lambda x:x import sys, os project_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.append(project_path) from externals.MagNet.worker import SimpleReformer mnist_autoencoder_fpath = os.path.join(project_path, "downloads/MagNet/defensive_models/MNIST_I") cifar10_autoencoder_fpath = os.path.join(project_path, "downloads/MagNet/defensive_models/CIFAR") reformer_mnist = SimpleReformer(mnist_autoencoder_fpath) reformer_cifar10 = SimpleReformer(cifar10_autoencoder_fpath) def magnet_mnist_py(imgs): return reformer_mnist.heal(imgs) def magnet_cifar10_py(imgs): return reformer_cifar10.heal(imgs) # Construct a name search function. def isfloat(value): try: float(value) return True except: return False