예제 #1
0
def test_pso_attack():
    """
    PSO_Attack test
    """
    context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
    batch_size = 6

    net = SimpleNet()
    inputs = np.random.rand(batch_size, 10)

    model = ModelToBeAttacked(net)
    labels = np.random.randint(low=0, high=10, size=batch_size)
    labels = np.eye(10)[labels]
    labels = labels.astype(np.float32)

    attack = PSOAttack(model, bounds=(0.0, 1.0), pm=0.5, sparse=False)
    _, adv_data, _ = attack.generate(inputs, labels)
    assert np.any(inputs != adv_data)
예제 #2
0
def test_pso_attack_detection_cpu():
    """
    PSO_Attack test
    """
    context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
    batch_size = 2
    inputs = np.random.random((batch_size, 100, 100, 3))
    model = DetectionModel()
    attack = PSOAttack(model,
                       t_max=30,
                       pm=0.5,
                       model_type='detection',
                       reserve_ratio=0.5)

    # generate adversarial samples
    adv_imgs = []
    for i in range(batch_size):
        img_data = np.expand_dims(inputs[i], axis=0)
        pre_gt_boxes, pre_gt_labels = model.predict(inputs)
        _, adv_img, _ = attack.generate(img_data,
                                        (pre_gt_boxes, pre_gt_labels))
        adv_imgs.append(adv_img)
    assert np.any(inputs != np.array(adv_imgs))