Example #1
0
def test_semantic_segmentation_labelling_sparse():
    """Test `semantic_segmentation_labelling` function in `generator` module by considering a
    sparse labelling, *i.e.* the array contains unknown values (to mimic the non-evaluated label
    situations)
    * as a preliminary verification, check if passing string labels raises an AttributeError
    exception
    * test if output shape is input shape + an additional dimension given by the
      `label_ids` length
    * test if both representation provides the same information (native array on the
      first hand and its one-hot version on the second hand)

    """
    a = np.array([[[1, 1, 3, 1], [3, 3, 1, 1], [3, 4, 3, 1]],
                  [[1, 1, 0, 0], [3, 4, 0, 1], [1, 1, 0, 0]]])
    labels = [0, 2, 3]
    asum, _ = np.histogram(a.reshape(-1), range=(np.amin(a), np.amax(a)))
    with pytest.raises(ValueError):
        b = generator.semantic_segmentation_labelling(a, ['0', '2', '3'])
    b = generator.semantic_segmentation_labelling(a, labels)
    assert len(labels) != np.amax(a) - np.amin(a) + 1
    assert b.shape == (a.shape[0], a.shape[1], a.shape[2], len(labels))
    assert b.tolist() == [[[[False, False, False], [False, False, False],
                            [False, False, True], [False, False, False]],
                           [[False, False, True], [False, False, True],
                            [False, False, False], [False, False, False]],
                           [[False, False, True], [False, False, False],
                            [False, False, True], [False, False, False]]],
                          [[[False, False, False], [False, False, False],
                            [True, False, False], [True, False, False]],
                           [[False, False, True], [False, False, False],
                            [True, False, False], [False, False, False]],
                           [[False, False, False], [False, False, False],
                            [True, False, False], [True, False, False]]]]
def test_semantic_segmentation_labelling_sparse():
    """Test `semantic_segmentation_labelling` function in `generator` module by
    considering a sparse labelling, *i.e.* the array contains unknown values
    (to mimic the non-evaluated label situations)
    * as a preliminary verification, check if passing string labels raises an
    AttributeError exception
    * test if output shape is input shape + an additional dimension given by
    the `label_ids` length
    * test if both representation provides the same information (native array
    on the first hand and its one-hot version on the second hand)

    """
    a = np.array([
        [
            [[200, 10, 10], [200, 10, 10], [200, 200, 200]],
            [[200, 200, 200], [200, 200, 200], [200, 10, 10]],
            [[200, 200, 200], [100, 100, 100], [200, 200, 200]],
        ],
        [
            [[200, 10, 10], [200, 10, 10], [10, 10, 200]],
            [[200, 200, 200], [100, 100, 100], [10, 10, 200]],
            [[200, 10, 10], [200, 10, 10], [10, 10, 200]],
        ],
    ])
    asum, _ = np.histogram(a.reshape(-1), range=(np.amin(a), np.amax(a)))
    wrong_config = [
        {
            "id": "0",
            "color": [10, 10, 200],
            "is_evaluate": True
        },
        {
            "id": "2",
            "color": [10, 200, 10],
            "is_evaluate": True
        },
        {
            "id": "3",
            "color": [200, 200, 200],
            "is_evaluate": True
        },
    ]
    with pytest.raises(ValueError):
        b = generator.semantic_segmentation_labelling(a, wrong_config)
    config = [
        {
            "id": 0,
            "color": [10, 10, 200],
            "is_evaluate": True
        },
        {
            "id": 2,
            "color": [10, 200, 10],
            "is_evaluate": True
        },
        {
            "id": 3,
            "color": [200, 200, 200],
            "is_evaluate": True
        },
    ]
    labels = [item["id"] for item in config]
    b = generator.semantic_segmentation_labelling(a, config)
    assert len(labels) != np.amax(a) - np.amin(a) + 1
    assert b.shape == (a.shape[0], a.shape[1], a.shape[2], len(labels))
    assert b.tolist() == [
        [
            [
                [False, False, False],
                [False, False, False],
                [False, False, True],
            ],
            [
                [False, False, True],
                [False, False, True],
                [False, False, False],
            ],
            [
                [False, False, True],
                [False, False, False],
                [False, False, True],
            ],
        ],
        [
            [
                [False, False, False],
                [False, False, False],
                [True, False, False],
            ],
            [
                [False, False, True],
                [False, False, False],
                [True, False, False],
            ],
            [
                [False, False, False],
                [False, False, False],
                [True, False, False],
            ],
        ],
    ]
def test_semantic_segmentation_labelling_concise():
    """Test `semantic_segmentation_labelling` function in `generator` module by considering a
    concise labelling, *i.e.* the labels correspond to array values
    * as a preliminary verification, check if passing string labels raises an AttributeError
    exception
    * test if output shape is input shape + an additional dimension given by the
      `label_ids` length
    * test if both representation provides the same information (native array on the
      first hand and its one-hot version on the second hand)

    """
    a = np.array([[[[200, 10, 10], [200, 10, 10], [200, 200, 200]],
                   [[200, 200, 200], [200, 200, 200], [200, 10, 10]],
                   [[200, 200, 200], [200, 200, 200], [200, 200, 200]]],
                  [[[200, 10, 10], [200, 10, 10], [10, 10, 200]],
                   [[10, 200, 10], [10, 200, 10], [10, 10, 200]],
                   [[200, 10, 10], [200, 10, 10], [10, 10, 200]]]])
    labels = np.unique(a.reshape(-1, 3), axis=0).tolist()
    wrong_config = [{
        'id': '0',
        'color': [10, 10, 200],
        'is_evaluate': True
    }, {
        'id': '1',
        'color': [200, 10, 10],
        'is_evaluate': True
    }, {
        'id': '2',
        'color': [10, 200, 10],
        'is_evaluate': True
    }, {
        'id': '3',
        'color': [200, 200, 200],
        'is_evaluate': True
    }]
    asum, _ = np.histogram(a.reshape(-1), range=(np.amin(a), np.amax(a)))
    with pytest.raises(ValueError):
        b = generator.semantic_segmentation_labelling(a, wrong_config)
    config = [{
        'id': 0,
        'color': [10, 10, 200],
        'is_evaluate': True
    }, {
        'id': 1,
        'color': [200, 10, 10],
        'is_evaluate': True
    }, {
        'id': 2,
        'color': [10, 200, 10],
        'is_evaluate': True
    }, {
        'id': 3,
        'color': [200, 200, 200],
        'is_evaluate': True
    }]
    b = generator.semantic_segmentation_labelling(a, config)
    assert b.shape == (a.shape[0], a.shape[1], a.shape[2], len(labels))
    assert b.tolist() == [[[[False, True, False, False],
                            [False, True, False, False],
                            [False, False, False, True]],
                           [[False, False, False, True],
                            [False, False, False, True],
                            [False, True, False, False]],
                           [[False, False, False, True],
                            [False, False, False, True],
                            [False, False, False, True]]],
                          [[[False, True, False, False],
                            [False, True, False, False],
                            [True, False, False, False]],
                           [[False, False, True, False],
                            [False, False, True, False],
                            [True, False, False, False]],
                           [[False, True, False, False],
                            [False, True, False, False],
                            [True, False, False, False]]]]