コード例 #1
0
def test_from_spec():
    """Test case to load key points from a spec function.
    """
    if not Network.has_connection():
        pytest.skip("No server connected.")

    network = Network([
        ReluLayer(),
        FullyConnectedLayer(np.eye(2), np.zeros(shape=(2,)))
    ])
    layer_index = 1
    region_of_interest = np.array([
        [0.5, -3.0],
        [1.0, -3.0],
        [1.0, 9.0],
        [0.5, 9.0],
    ])
    spec_fn = lambda i: np.isclose(i[:, 0], 1.0).astype(np.float32)
    patcher = NetPatcher.from_spec_function(network, layer_index,
                                            region_of_interest, spec_fn)
    assert patcher.network is network
    assert patcher.layer_index is layer_index
    assert len(patcher.inputs) == (4 + 2)
    true_key_points = list(region_of_interest)
    true_key_points += [np.array([0.5, 0.0]), np.array([1.0, 0.0])]
    true_labels = [0, 1, 1, 0, 0, 1]
    for true_point, true_label in zip(true_key_points, true_labels):
        try:
            i = next(i for i, point in enumerate(patcher.inputs)
                     if np.allclose(point, true_point))
        except StopIteration:
            assert False
        assert true_label == patcher.labels[i]
コード例 #2
0
def test_from_planes():
    """Test case to load key points from a set of labeled 2D polytopes.
    """
    if not Network.has_connection():
        pytest.skip("No server connected.")

    network = Network([
        ReluLayer(),
        FullyConnectedLayer(np.eye(2), np.zeros(shape=(2,)))
    ])
    layer_index = 1
    planes = [
        np.array([[-1.0, -3.0], [-0.5, -3.0], [-0.5, 9.0], [-1.0, 9.0]]),
        np.array([[8.0, -2.0], [16.0, -2.0], [16.0, 6.0], [8.0, 6.0]]),
    ]
    labels = [1, 0]
    patcher = NetPatcher.from_planes(network, layer_index, planes, labels)
    assert patcher.network is network
    assert patcher.layer_index is layer_index
    assert len(patcher.inputs) == (4 + 2) + (4 + 2)
    true_key_points = list(planes[0])
    true_key_points += [np.array([-1.0, 0.0]), np.array([-0.5, 0.0])]
    true_key_points += list(planes[1])
    true_key_points += [np.array([8.0, 0.0]), np.array([16.0, 0.0])]
    true_labels = ([1] * 6) + ([0] * 6)
    for true_point, true_label in zip(true_key_points, true_labels):
        try:
            i = next(i for i, point in enumerate(patcher.inputs)
                     if np.allclose(point, true_point))
        except StopIteration:
            assert False
        assert true_label == patcher.labels[i]
コード例 #3
0
ファイル: classify_planes.py プロジェクト: 95616ARG/SyReNN
def test_compute_from_network():
    """Tests the it works given an arbitrary network and planes.
    """
    if not Network.has_connection():
        pytest.skip("No server connected.")
    network = Network([ReluLayer()])
    planes = [np.array([[1.0, 2.0], [3.0, 4.0], [1.0, 2.0]]),
              np.array([[3.0, 2.0], [7.0, 4.0], [5.0, 2.0]])]
    classifier = PlanesClassifier(network, planes, preimages=True)

    classifier.partial_compute()
    assert classifier.partially_computed
    transformed = network.transform_planes(planes, True, True)
    assert all(all(np.allclose(actual_polytope[0], truth_polytope[0]) and
                   np.allclose(actual_polytope[1], truth_polytope[1])
                   for actual_polytope, truth_polytope in zip(actual, truth))
               for actual, truth in zip(classifier.transformed_planes,
                                        transformed))

    classified = classifier.compute()
    assert len(classified) == len(planes)
    regions, labels = classified[0]
    assert np.allclose(regions, planes[0])
    assert np.allclose(labels, [1])
    regions, labels = classified[1]
    assert np.allclose(regions, planes[1])
    assert np.allclose(labels, [0])

    # Ensure it doesn't re-compute things it already knows.
    assert classifier.compute() is classified
コード例 #4
0
ファイル: classify_planes.py プロジェクト: 95616ARG/SyReNN
def test_compute_from_syrenn():
    """Tests the it works given an arbitrary network and planes.
    """
    if not Network.has_connection():
        pytest.skip("No server connected.")

    network = Network([ReluLayer()])
    planes = [np.array([[1.0, 2.0], [3.0, 4.0], [1.0, 2.0]]),
              np.array([[3.0, 2.0], [7.0, 4.0], [5.0, 2.0]])]
    transformed = network.transform_planes(planes, True, True)

    classifier = PlanesClassifier.from_syrenn(transformed)
    assert classifier.partially_computed

    classified = classifier.compute()
    assert len(classified) == len(planes)
    regions, labels = classified[0]
    assert np.allclose(regions, planes[0])
    assert np.allclose(labels, [1])
    regions, labels = classified[1]
    assert np.allclose(regions, planes[1])
    assert np.allclose(labels, [0])

    # Ensure it doesn't re-compute things it already knows.
    assert classifier.compute() is classified
コード例 #5
0
ファイル: classify_lines.py プロジェクト: 95616ARG/SyReNN
def test_compute_from_network():
    """Tests the it works given an arbitrary network and lines.
    """
    if not Network.has_connection():
        pytest.skip("No server connected.")

    network = Network([ReluLayer()])
    lines = [(np.array([0.0, 1.0]), np.array([0.0, -1.0])),
             (np.array([2.0, 3.0]), np.array([4.0, 3.0]))]
    classifier = LinesClassifier(network, lines, preimages=True)

    classifier.partial_compute()
    exactlines = network.exactlines(lines, True, True)
    assert all(
        np.allclose(actual[0], truth[0]) and np.allclose(actual[1], truth[1])
        for actual, truth in zip(classifier.transformed_lines, exactlines))

    classified = classifier.compute()
    assert len(classified) == len(lines)
    regions, labels = classified[0]
    assert np.allclose(regions,
                       [[[0.0, 1.0], [0.0, 0.0]], [[0.0, 0.0], [0.0, -1.0]]])
    assert np.allclose(labels, [1, 0])
    regions, labels = classified[1]
    assert np.allclose(regions,
                       [[[2.0, 3.0], [3.0, 3.0]], [[3.0, 3.0], [4.0, 3.0]]])
    assert np.allclose(labels, [1, 0])

    # Ensure it doesn't re-compute things it already knows.
    assert classifier.compute() is classified
コード例 #6
0
ファイル: classify_lines.py プロジェクト: 95616ARG/SyReNN
def test_compute_from_exactline_error():
    """Tests that it requires the plural exactline*s*(), not the singular.
    """
    if not Network.has_connection():
        pytest.skip("No server connected.")

    network = Network([ReluLayer()])
    exactline = network.exactline([-1.0, 1.0], [1.0, 0.0], True, True)

    try:
        classifier = LinesClassifier.from_exactlines(exactline)
        assert False
    except TypeError as e:
        pass
コード例 #7
0
def test_compute_from_network():
    """Tests the it works given an arbitrary network and lines.
    """
    if not Network.has_connection():
        pytest.skip("No server connected.")
    network = Network([ReluLayer()])
    lines = [(np.array([0.0, 1.0]), np.array([0.0, -1.0])),
             (np.array([2.0, 3.0]), np.array([4.0, 3.0]))]

    helper = IntegratedGradients(network, lines)
    helper.partial_compute()
    assert len(helper.exactlines) == len(lines)
    assert np.allclose(helper.exactlines[0], [0.0, 0.5, 1.0])
    assert np.allclose(helper.exactlines[1], [0.0, 1.0])
    assert helper.n_samples == [2, 1]

    attributions_0 = helper.compute_attributions(0)
    assert len(attributions_0) == len(lines)
    # The second component doesn't affect the 0-label at all, and the first
    # component is 0 everywhere, so we have int_0^0 0.0dx = 0.0
    assert np.allclose(attributions_0[0], [0.0, 0.0])
    # Gradient of the 0-label is (1.0, 0.0) everywhere since its in the first
    # orthant, and the partition has a size of (2.0, 0.0), so the IG is (2.0,
    # 0.0).
    assert np.allclose(attributions_0[1], [2.0, 0.0])

    attributions_1 = helper.compute_attributions(1)
    assert len(attributions_1) == len(lines)
    # The gradient in the first partition is (0.0, 1.0) with a size of (0.0,
    # -1.0) -> contribution of (0.0, -1.0). In the second partition, (0.0,
    # 0.0)*(0.0, -1.0) = (0.0, 0.0).
    assert np.allclose(attributions_1[0], [0.0, -1.0])
    # Gradient is (0, 1) and the size is (2, 0) so IG is (0, 0).
    assert np.allclose(attributions_1[1], [0.0, 0.0])

    attributions_1_re = helper.compute_attributions(1)
    # Ensure it doesn't re-compute the attributions.
    assert attributions_1 is attributions_1_re
コード例 #8
0
ファイル: classify_lines.py プロジェクト: 95616ARG/SyReNN
def test_compute_from_exactlines():
    """Tests the it works given pre-transformed lines.
    """
    if not Network.has_connection():
        pytest.skip("No server connected.")

    network = Network([ReluLayer()])
    lines = [(np.array([0.0, 1.0]), np.array([0.0, -1.0])),
             (np.array([2.0, 3.0]), np.array([4.0, 3.0]))]
    exactlines = network.exactlines(lines, True, True)

    classifier = LinesClassifier.from_exactlines(exactlines)
    classified = classifier.compute()

    assert len(classified) == len(lines)
    regions, labels = classified[0]
    assert np.allclose(regions,
                       [[[0.0, 1.0], [0.0, 0.0]], [[0.0, 0.0], [0.0, -1.0]]])
    assert np.allclose(labels, [1, 0])
    regions, labels = classified[1]
    assert np.allclose(regions,
                       [[[2.0, 3.0], [3.0, 3.0]], [[3.0, 3.0], [4.0, 3.0]]])
    assert np.allclose(labels, [1, 0])