Пример #1
0
def test_ensemble_detector():
    """
    Compute mindspore result.
    """
    np.random.seed(6)
    adv = np.random.rand(4, 4).astype(np.float32)
    model = Model(Net())
    auto_encoder = Model(AutoNet())
    random_label = np.random.randint(10, size=4)
    labels = np.eye(10)[random_label]
    magnet_detector = ErrorBasedDetector(auto_encoder)
    region_detector = RegionBasedDetector(model)
    # use this to enable radius in region_detector
    region_detector.fit(adv, labels)
    detectors = [magnet_detector, region_detector]
    detector = EnsembleDetector(detectors)
    detected_res = detector.detect(adv)
    expected_value = np.array([0, 1, 0, 0])
    assert np.all(detected_res == expected_value)
Пример #2
0
def test_region_based_classification():
    """
    Compute mindspore result.
    """
    np.random.seed(5)
    ori = np.random.rand(4, 4).astype(np.float32)
    labels = np.array([[1, 0, 0, 0], [0, 0, 1, 0], [0, 0, 1, 0],
                       [0, 1, 0, 0]]).astype(np.int32)
    np.random.seed(6)
    adv = np.random.rand(4, 4).astype(np.float32)
    model = Model(Net())
    detector = RegionBasedDetector(model)
    radius = detector.fit(ori, labels)
    detector.set_radius(radius)
    detected_res = detector.detect(adv)
    expected_value = np.array([0, 0, 1, 0])
    assert np.all(detected_res == expected_value)
Пример #3
0
def test_value_error():
    np.random.seed(5)
    ori = np.random.rand(4, 4).astype(np.float32)
    labels = np.array([[1, 0, 0, 0], [0, 0, 1, 0], [0, 0, 1, 0],
                       [0, 1, 0, 0]]).astype(np.int32)
    np.random.seed(6)
    adv = np.random.rand(4, 4).astype(np.float32)
    model = Model(Net())
    # model should be mindspore model
    with pytest.raises(TypeError):
        assert RegionBasedDetector(Net())

    with pytest.raises(ValueError):
        assert RegionBasedDetector(model, number_points=-1)

    with pytest.raises(ValueError):
        assert RegionBasedDetector(model, initial_radius=-1)

    with pytest.raises(ValueError):
        assert RegionBasedDetector(model, max_radius=-2.2)

    with pytest.raises(ValueError):
        assert RegionBasedDetector(model, search_step=0)

    with pytest.raises(TypeError):
        assert RegionBasedDetector(model, sparse='False')

    detector = RegionBasedDetector(model)
    with pytest.raises(TypeError):
        # radius must not empty
        assert detector.detect(adv)

    radius = detector.fit(ori, labels)
    detector.set_radius(radius)
    with pytest.raises(TypeError):
        # adv type should be in (list, tuple, numpy.ndarray)
        assert detector.detect(adv.tostring())