def main(image_path):

    alexnet = mx.gluon.model_zoo.vision.alexnet(pretrained=True)

    # print(alexnet)

    orig = cv2.imread(image_path)[..., ::-1]
    orig = cv2.resize(orig, (224, 224))
    img = orig.copy().astype(np.float32)

    mean = [0.485, 0.456, 0.406]
    std = [0.229, 0.224, 0.225]
    img /= 255.0
    img = old_div((img - mean), std)
    img = img.transpose(2, 0, 1)

    img = np.expand_dims(img, axis=0)

    #array = mx.nd.array(img)

    # advbox demo
    m = MxNetModel(alexnet, None, (-1, 1), channel_axis=1)
    attack = FGSMT(m)
    #attack = FGSM(m)

    # 静态epsilons
    attack_config = {"epsilons": 0.2, "epsilon_steps": 1, "steps": 100}

    inputs = img
    #labels=388
    labels = None

    print(inputs.shape)

    adversary = Adversary(inputs, labels)
    #adversary = Adversary(inputs, 388)

    tlabel = 538
    adversary.set_target(is_targeted_attack=True, target_label=tlabel)

    adversary = attack(adversary, **attack_config)

    if adversary.is_successful():
        print('attack success, adversarial_label=%d' %
              (adversary.adversarial_label))

        adv = adversary.adversarial_example[0]
        adv = adv.transpose(1, 2, 0)
        adv = (adv * std) + mean
        adv = adv * 255.0
        adv = adv[..., ::-1]  # RGB to BGR
        adv = np.clip(adv, 0, 255).astype(np.uint8)
        cv2.imwrite('img_adv.png', adv)

    else:
        print('attack failed')

    print("fgsm attack done")
def main():
    """
    Advbox demo which demonstrate how to use advbox.
    """
    TOTAL_NUM = 50
    IMG_NAME = 'img'
    LABEL_NAME = 'label'

    img = fluid.layers.data(name=IMG_NAME, shape=[1, 28, 28], dtype='float32')
    # gradient should flow
    img.stop_gradient = False
    label = fluid.layers.data(name=LABEL_NAME, shape=[1], dtype='int64')

    logits = mnist_cnn_model(img)
    #logits = vgg_bn_drop(img)
    #logits = resnet_cifar10(img,32)

    cost = fluid.layers.cross_entropy(input=logits, label=label)
    avg_cost = fluid.layers.mean(x=cost)

    # use CPU
    place = fluid.CPUPlace()
    # use GPU
    #place = fluid.CUDAPlace(0)
    exe = fluid.Executor(place)

    BATCH_SIZE = 1
    test_reader = paddle.batch(paddle.reader.shuffle(
        paddle.dataset.mnist.test(), buf_size=128 * 10),
                               batch_size=BATCH_SIZE)

    fluid.io.load_params(exe,
                         "mnist/",
                         main_program=fluid.default_main_program())

    # advbox demo
    m = PaddleModel(fluid.default_main_program(),
                    IMG_NAME,
                    LABEL_NAME,
                    logits.name,
                    avg_cost.name, (-1, 1),
                    channel_axis=1)
    #attack = FGSM(m)
    attack = FGSMT(m)
    attack_config = {"epsilons": 0.3}
    # use test data to generate adversarial examples
    total_count = 0
    fooling_count = 0
    for data in test_reader():
        total_count += 1
        adversary = Adversary(data[0][0], data[0][1])

        # FGSM non-targeted attack
        #adversary = attack(adversary, **attack_config)

        # FGSMT targeted attack
        tlabel = 8
        adversary.set_target(is_targeted_attack=True, target_label=tlabel)
        adversary = attack(adversary, **attack_config)

        if adversary.is_successful():
            fooling_count += 1
            print(
                'attack success, original_label=%d, adversarial_label=%d, count=%d'
                % (data[0][1], adversary.adversarial_label, total_count))

            adversarial_example = adversary.adversarial_example
            #print adversarial_example

            #原始数据归一化到(-1,1)之间了 需要还原到(0,255)

            adversarial_example /= 2.
            adversarial_example += 0.5
            adversarial_example *= 255.

            adversarial_example = adversarial_example.astype(np.uint8)

            #print adversarial_example

            adversarial_example = np.reshape(adversarial_example, (28, 28))

            im = Image.fromarray(adversarial_example)

            filename = "original-%d-adversarial-%d-targeted-by-fgsm.jpg" % (
                data[0][1], adversary.adversarial_label)

            im.save("output/" + filename)

        else:
            print('attack failed, original_label=%d, count=%d' %
                  (data[0][1], total_count))

        if total_count >= TOTAL_NUM:
            print(
                "[TEST_DATASET]: fooling_count=%d, total_count=%d, fooling_rate=%f"
                % (fooling_count, total_count,
                   float(fooling_count) / total_count))
            break
    print("fgsmt attack done")
예제 #3
0
def main(modulename, imagename):
    '''
    Kera的应用模块Application提供了带有预训练权重的Keras模型,这些模型可以用来进行预测、特征提取和finetune
    模型的预训练权重将下载到~/.keras/models/并在载入模型时自动载入
    '''

    # 设置为测试模式
    keras.backend.set_learning_phase(0)

    model = ResNet50(weights=modulename)
    #model = InceptionV3(weights=modulename)

    logging.info(model.summary())

    img = image.load_img(imagename, target_size=(224, 224))
    raw_imagedata = image.img_to_array(img)
    raw_imagedata = np.expand_dims(raw_imagedata, axis=0)

    # 'RGB'->'BGR'
    imagedata = raw_imagedata[:, :, :, ::-1]

    #logging.info(raw_imagedata)
    #logging.info(imagedata)

    #logit fc1000
    logits = model.get_layer('fc1000').output

    #keras中获取指定层的方法为:
    #base_model.get_layer('block4_pool').output)
    # advbox demo
    # 因为原始数据没有归一化  所以bounds=(0, 255)  KerasMode内部在进行预测和计算梯度时会进行预处理
    # imagenet数据集归一化时 标准差为1  mean为[104, 116, 123]
    # featurefqueezing_bit_depth featurefqueezing防御算法 提高生成攻击样本的质量 为特征数据的bit位 一般8就ok了
    m = KerasModel(model,
                   model.input,
                   None,
                   logits,
                   None,
                   bounds=(0, 255.0),
                   channel_axis=3,
                   preprocess=([104, 116, 123], 1),
                   featurefqueezing_bit_depth=8)

    attack = FGSM(m)
    #设置epsilons时不用考虑特征范围 算法实现时已经考虑了取值范围的问题 epsilons取值范围为(0,1)
    #epsilon支持动态调整 epsilon_steps为epsilon变化的个数
    #epsilons为下限 epsilons_max为上限
    #attack_config = {"epsilons": 0.3, "epsilons_max": 0.5, "epsilon_steps": 100}
    #静态epsilons
    attack_config = {
        "epsilons": 1,
        "epsilons_max": 10,
        "epsilon_steps": 1,
        "steps": 100
    }

    #y设置为空 会自动计算
    adversary = Adversary(imagedata.copy(), None)

    # FGSM non-targeted attack
    adversary = attack(adversary, **attack_config)

    if adversary.is_successful():
        print('attack success, adversarial_label=%d' %
              (adversary.adversarial_label))

        #对抗样本保存在adversary.adversarial_example
        adversary_image = np.copy(adversary.adversarial_example)

        logging.info("adversary_image label={0} ".format(
            np.argmax(m.predict(adversary_image))))
        #logging.info(adversary_image)

        #强制类型转换 之前是float 现在要转换成uint8
        adversary_image = np.array(adversary_image).astype("uint8").reshape(
            [224, 224, 3])

        #logging.info(adversary_image)
        adversary_image = adversary_image[:, :, ::-1]
        logging.info(adversary_image - raw_imagedata)

        img = array_to_img(adversary_image)
        img.save('adversary_image_nontarget.jpg')

    print("fgsm non-target attack done")

    attack = FGSMT(m)
    #静态epsilons
    attack_config = {
        "epsilons": 10,
        "epsilons_max": 10,
        "epsilon_steps": 1,
        "steps": 100
    }

    adversary = Adversary(imagedata, None)

    tlabel = 489
    adversary.set_target(is_targeted_attack=True, target_label=tlabel)

    # FGSM targeted attack
    adversary = attack(adversary, **attack_config)

    if adversary.is_successful():
        print('attack success, adversarial_label=%d' %
              (adversary.adversarial_label))

        #对抗样本保存在adversary.adversarial_example
        adversary_image = np.copy(adversary.adversarial_example)
        #强制类型转换 之前是float 现在要转换成int8

        adversary_image = np.array(adversary_image).astype("uint8").reshape(
            [224, 224, 3])

        adversary_image = adversary_image[:, :, ::-1]
        logging.info(adversary_image - raw_imagedata)

        img = array_to_img(adversary_image)
        img.save('adversary_image_target.jpg')

    print("fgsm target attack done")
def get_adversarial_examples_from_model2():
    """
    Advbox demo which demonstrate how to use advbox.
    """
    TOTAL_NUM = 500
    IMG_NAME = 'img'
    LABEL_NAME = 'label'

    #保存对抗样本
    x = []
    y = []

    img = fluid.layers.data(name=IMG_NAME, shape=[1, 28, 28], dtype='float32')
    # gradient should flow
    img.stop_gradient = False
    label = fluid.layers.data(name=LABEL_NAME, shape=[1], dtype='int64')

    #logits = mnist_cnn_model(img)
    logits = mnist_mlp_model(img)
    #logits = vgg_bn_drop(img)
    #logits = resnet_cifar10(img,32)

    cost = fluid.layers.cross_entropy(input=logits, label=label)
    avg_cost = fluid.layers.mean(x=cost)

    # use CPU
    place = fluid.CPUPlace()
    # use GPU
    #place = fluid.CUDAPlace(0)
    exe = fluid.Executor(place)

    BATCH_SIZE = 1
    test_reader = paddle.batch(paddle.reader.shuffle(
        paddle.dataset.mnist.test(), buf_size=128 * 10),
                               batch_size=BATCH_SIZE)

    #fluid.io.load_params(
    #    exe, model1_path, main_program=fluid.default_main_program())

    fluid.io.load_params(exe,
                         model2_path,
                         main_program=fluid.default_main_program())

    # advbox demo
    m = PaddleModel(fluid.default_main_program(),
                    IMG_NAME,
                    LABEL_NAME,
                    logits.name,
                    avg_cost.name, (-1, 1),
                    channel_axis=1)
    #attack = FGSM(m)
    attack = FGSMT(m)
    attack_config = {"epsilons": 0.3}
    # use test data to generate adversarial examples
    total_count = 0
    fooling_count = 0
    for data in test_reader():
        total_count += 1
        adversary = Adversary(data[0][0], data[0][1])

        # FGSM non-targeted attack
        adversary = attack(adversary, **attack_config)

        # FGSMT targeted attack
        #tlabel = 8
        #adversary.set_target(is_targeted_attack=True, target_label=tlabel)
        #adversary = attack(adversary, **attack_config)

        if adversary.is_successful():
            fooling_count += 1
            print(
                'attack success, original_label=%d, adversarial_label=%d, count=%d'
                % (data[0][1], adversary.adversarial_label, total_count))

            adversarial_example = adversary.adversarial_example

            x.append(adversarial_example)
            y.append(data[0][1])

        else:
            print('attack failed, original_label=%d, count=%d' %
                  (data[0][1], total_count))

        if total_count >= TOTAL_NUM:
            print(
                "[TEST_DATASET]: fooling_count=%d, total_count=%d, fooling_rate=%f"
                % (fooling_count, total_count,
                   float(fooling_count) / total_count))
            break
    print("fgsm attack done")

    return x, y
예제 #5
0
def main(image_path):

    # Define what device we are using
    logging.info("CUDA Available: {}".format(torch.cuda.is_available()))
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

    orig = cv2.imread(image_path)[..., ::-1]
    orig = cv2.resize(orig, (224, 224))
    img = orig.copy().astype(np.float32)

    mean = [0.485, 0.456, 0.406]
    std = [0.229, 0.224, 0.225]
    img /= 255.0
    img = (img - mean) / std
    img = img.transpose(2, 0, 1)

    img = Variable(
        torch.from_numpy(img).to(device).float().unsqueeze(0)).cpu().numpy()

    # Initialize the network
    #Alexnet
    model = models.alexnet(pretrained=True).to(device).eval()
    #model = models.resnet18(pretrained=True).to(device).eval()

    #print(model)

    #设置为不保存梯度值 自然也无法修改
    for param in model.parameters():
        #print(param)
        #print(param.requires_grad)
        param.requires_grad = False

    #loss_func=nn.CrossEntropyLoss()

    # advbox demo
    m = PytorchModel(model, None, (-1, 1), channel_axis=1)
    attack = FGSMT(m)
    #attack = FGSM(m)

    # 静态epsilons
    attack_config = {"epsilons": 0.2, "epsilon_steps": 1, "steps": 100}

    inputs = img
    #labels=388
    labels = None

    print(inputs.shape)

    adversary = Adversary(inputs, labels)
    #adversary = Adversary(inputs, 388)

    tlabel = 538
    adversary.set_target(is_targeted_attack=True, target_label=tlabel)

    adversary = attack(adversary, **attack_config)

    if adversary.is_successful():
        print('attack success, adversarial_label=%d' %
              (adversary.adversarial_label))

        adv = adversary.adversarial_example[0]
        adv = adv.transpose(1, 2, 0)
        adv = (adv * std) + mean
        adv = adv * 255.0
        adv = adv[..., ::-1]  # RGB to BGR
        adv = np.clip(adv, 0, 255).astype(np.uint8)
        cv2.imwrite('img_adv.png', adv)

    else:
        print('attack failed')

    print("fgsm attack done")
예제 #6
0
def main(dirname, imagename):

    #加载解码的图像 这里是个大坑 tf提供的imagenet预训练好的模型pb文件中 包含针对图像的预处理环节 即解码jpg文件 这部分没有梯度
    #需要直接处理解码后的数据
    image = None
    with tf.gfile.Open(imagename, 'rb') as f:
        image = np.array(Image.open(f).convert('RGB')).astype(np.float)

    image = [image]

    session = tf.Session()

    def create_graph(dirname):
        with tf.gfile.FastGFile(dirname, 'rb') as f:
            graph_def = session.graph_def
            graph_def.ParseFromString(f.read())

            _ = tf.import_graph_def(graph_def, name='')

    create_graph(dirname)

    # 初始化参数  非常重要
    session.run(tf.global_variables_initializer())

    tensorlist = [n.name for n in session.graph_def.node]

    logger.info(tensorlist)

    #获取logits
    logits = session.graph.get_tensor_by_name('softmax/logits:0')

    x = session.graph.get_tensor_by_name('ExpandDims:0')

    #y = tf.placeholder(tf.int64, None, name='label')

    # advbox demo
    # 因为原始数据没有归一化  所以bounds=(0, 255)
    m = TensorflowModel(session,
                        x,
                        None,
                        logits,
                        None,
                        bounds=(0, 255),
                        channel_axis=3,
                        preprocess=None)

    attack = FGSM(m)
    #设置epsilons时不用考虑特征范围 算法实现时已经考虑了取值范围的问题 epsilons取值范围为(0,1)
    #epsilon支持动态调整 epsilon_steps为epsilon变化的个数
    #epsilons为下限 epsilons_max为上限
    attack_config = {
        "epsilons": 20,
        "epsilons_max": 10,
        "epsilon_steps": 1,
        "steps": 100
    }

    #y设置为空 会自动计算
    adversary = Adversary(image, None)

    # FGSM non-targeted attack
    adversary = attack(adversary, **attack_config)

    if adversary.is_successful():
        print('attack success, adversarial_label=%d' %
              (adversary.adversarial_label))

        #对抗样本保存在adversary.adversarial_example
        adversary_image = np.copy(adversary.adversarial_example)
        #强制类型转换 之前是float 现在要转换成int8
        #print(adversary_image)
        adversary_image = np.array(adversary_image).astype("uint8").reshape(
            [100, 100, 3])

        logging.info(adversary_image - image)

        im = Image.fromarray(adversary_image)
        im.save("adversary_image_nontarget.jpg")

    print("fgsm non-target attack done")

    attack = FGSMT(m)
    attack_config = {
        "epsilons": 30,
        "epsilons_max": 10,
        "epsilon_steps": 1,
        "steps": 100
    }

    adversary = Adversary(image, None)
    #麦克风
    tlabel = 651
    adversary.set_target(is_targeted_attack=True, target_label=tlabel)

    # FGSM targeted attack
    adversary = attack(adversary, **attack_config)

    if adversary.is_successful():
        print('attack success, adversarial_label=%d' %
              (adversary.adversarial_label))

        #对抗样本保存在adversary.adversarial_example
        adversary_image = np.copy(adversary.adversarial_example)
        #强制类型转换 之前是float 现在要转换成int8

        adversary_image = np.array(adversary_image).astype("uint8").reshape(
            [100, 100, 3])

        logging.info(adversary_image - image)

        im = Image.fromarray(adversary_image)
        im.save("adversary_image_target.jpg")

    print("fgsm target attack done")
def main():
    """
    Advbox demo which demonstrate how to use advbox.
    """
    TOTAL_NUM = 50
    IMG_NAME = 'img'
    LABEL_NAME = 'label'

    img = fluid.layers.data(name=IMG_NAME, shape=[3, 32, 32], dtype='float32')
    # gradient should flow
    img.stop_gradient = False
    label = fluid.layers.data(name=LABEL_NAME, shape=[1], dtype='int64')
    
    #logits = mnist_cnn_model(img)
    #logits = vgg_bn_drop(img)
    logits = resnet_cifar10(img,32)
    
    cost = fluid.layers.cross_entropy(input=logits, label=label)
    avg_cost = fluid.layers.mean(x=cost)

    # use CPU
    place = fluid.CPUPlace()
    # use GPU
    #place = fluid.CUDAPlace(0)
    exe = fluid.Executor(place)

    BATCH_SIZE = 1
    test_reader = paddle.batch(
        paddle.dataset.cifar.test10(), batch_size=BATCH_SIZE)

    fluid.io.load_params(
        exe, "cifar10/", main_program=fluid.default_main_program())

    # advbox demo
    m = PaddleModel(
        fluid.default_main_program(),
        IMG_NAME,
        LABEL_NAME,
        logits.name,
        avg_cost.name, (-1, 1),
        channel_axis=1)
    #attack = FGSM(m)
    attack = FGSMT(m)
    attack_config = {"epsilons": 0.3}
    # use test data to generate adversarial examples
    total_count = 0
    fooling_count = 0
    for data in test_reader():
        total_count += 1
        adversary = Adversary(data[0][0], data[0][1])

        # FGSM non-targeted attack
        adversary = attack(adversary, **attack_config)

        # FGSMT targeted attack
        #tlabel = 0
        #adversary.set_target(is_targeted_attack=True, target_label=tlabel)
        #adversary = attack(adversary, **attack_config)

        if adversary.is_successful():
            fooling_count += 1
            print(
                'attack success, original_label=%d, adversarial_label=%d, count=%d'
                % (data[0][1], adversary.adversarial_label, total_count))
            plt.imshow(adversary.target, cmap='Greys_r')
            plt.show()
            #np.save('adv_img', adversary.target)
        else:
            print('attack failed, original_label=%d, count=%d' %
                  (data[0][1], total_count))

        if total_count >= TOTAL_NUM:
            print(
                "[TEST_DATASET]: fooling_count=%d, total_count=%d, fooling_rate=%f"
                % (fooling_count, total_count,
                   float(fooling_count) / total_count))
            break
    print("fgsmt attack done")