def test_untargeted_Xception(image, label=None): import keras from perceptron.models.classification.keras import KerasModel mean = np.array([0.485, 0.456, 0.406]).reshape((1, 1, 3)) std = np.array([0.229, 0.224, 0.225]).reshape((1, 1, 3)) model_keras = keras.applications.xception.Xception(weights='imagenet') model = KerasModel(model_keras, bounds=(0, 1), preprocessing=(mean, std)) print(np.argmax(model.predictions(image))) attack = Attack(model, criterion=Misclassification()) adversarial_obj = attack(image, label, unpack=False, epsilons=10000) distance = adversarial_obj.distance adversarial = adversarial_obj.image return distance, adversarial
import keras.applications as models from perceptron.models.classification.keras import KerasModel from perceptron.utils.image import imagenet_example from perceptron.benchmarks.additive_noise import AdditiveGaussianNoiseMetric from perceptron.utils.criteria.classification import TopKMisclassification from perceptron.utils.tools import plot_image from perceptron.utils.tools import bcolors # instantiate the model from keras applications xception = models.Xception(weights='imagenet') # initialize the KerasModel # keras xception has input bound (0, 1) mean = np.array([0.485, 0.456, 0.406]).reshape((1, 1, 3)) std = np.array([0.229, 0.224, 0.225]).reshape((1, 1, 3)) kmodel = KerasModel(xception, bounds=(0, 1), preprocessing=(mean, std)) # get source image and label # the model Xception expects values in [0, 1] with shape (299, 299), and channles_last image, _ = imagenet_example(shape=(299, 299), data_format='channels_last') image /= 255.0 label = np.argmax(kmodel.predictions(image)) metric = AdditiveGaussianNoiseMetric(kmodel, criterion=TopKMisclassification(10)) print(bcolors.BOLD + 'Process start' + bcolors.ENDC) adversary = metric( image, label, unpack=False, epsilons=1000) # choose 1000 different epsilon values in [0, 1] print(bcolors.BOLD + 'Process finished' + bcolors.ENDC)
import keras.applications as models from perceptron.models.classification.keras import KerasModel from perceptron.utils.image import imagenet_example from perceptron.benchmarks.blended_noise import BlendedUniformNoiseMetric from perceptron.utils.criteria.classification import TopKMisclassification from perceptron.utils.tools import plot_image from perceptron.utils.tools import bcolors # instantiate the model from keras applications vgg16 = models.VGG16(weights='imagenet') # initialize the KerasModel # keras vgg16 has input bound (0, 255) preprocessing = (np.array([104, 116, 123]), 1) # the mean and stv of the whole dataset kmodel = KerasModel(vgg16, bounds=(0, 255), preprocessing=preprocessing) # get source image and label # the model expects values in [0, 255], and channles_last image, _ = imagenet_example(data_format='channels_last') label = np.argmax(kmodel.predictions(image)) metric = BlendedUniformNoiseMetric(kmodel, criterion=TopKMisclassification(10)) print(bcolors.BOLD + 'Process start' + bcolors.ENDC) adversary = metric( image, label, unpack=False, epsilons=100) # choose 100 different epsilon values in [0, 1] print(bcolors.BOLD + 'Process finished' + bcolors.ENDC) if adversary.image is None: