Esempio n. 1
0
def test_check_drop_3d():
    image = np.zeros((10, 10, 10, 2))
    image[5:, :, :, 0] = 1
    image[5:, :5, :, 1] = 1

    images = np.array([image for _ in range(2)])

    indice = get_patch_indice((10, 10, 10), (5, 5, 5), 0)
    indice = list(product([0, 1], indice))

    res = check_drop(images, indice, (5, 5, 5), 0.4, None)[0]
    expected = [
        False, False, False, False, True, True, True, True
    ] * 2

    assert np.all(res == expected)

    res = check_drop(images, indice, (5, 5, 5), 0.9, None)[0]
    expected = [
        False, False, False, False, True, True, False, False
    ] * 2

    assert np.all(res == expected)

    res = check_drop(images, indice, (5, 5, 5), 0.9, 0)[0]
    expected = [
        False, False, False, False, True, True, True, True
    ] * 2

    assert np.all(res == expected)
Esempio n. 2
0
def test_get_patch_indice():
    indice = get_patch_indice((20, 20, 20), (10, 10, 10), 0)

    expected = [(0, 0, 0), (0, 0, 10),
                (0, 10, 0), (0, 10, 10),
                (10, 0, 0), (10, 0, 10),
                (10, 10, 0), (10, 10, 10)]

    assert np.all(indice == expected)

    indice = get_patch_indice((20, 20, 20), (10, 10, 10), 0.5)

    expected = [(0, 0, 0), (0, 0, 5), (0, 0, 10),
                (0, 5, 0), (0, 5, 5), (0, 5, 10),
                (0, 10, 0), (0, 10, 5), (0, 10, 10),
                (5, 0, 0), (5, 0, 5), (5, 0, 10),
                (5, 5, 0), (5, 5, 5), (5, 5, 10),
                (5, 10, 0), (5, 10, 5), (5, 10, 10),
                (10, 0, 0), (10, 0, 5), (10, 0, 10),
                (10, 5, 0), (10, 5, 5), (10, 5, 10),
                (10, 10, 0), (10, 10, 5), (10, 10, 10)]

    assert np.all(indice == expected)

    indice = get_patch_indice((20, 20, 19), (10, 10, 10), 0.5)

    expected = [(0, 0, 0), (0, 0, 5), (0, 0, 9),
                (0, 5, 0), (0, 5, 5), (0, 5, 9),
                (0, 10, 0), (0, 10, 5), (0, 10, 9),
                (5, 0, 0), (5, 0, 5), (5, 0, 9),
                (5, 5, 0), (5, 5, 5), (5, 5, 9),
                (5, 10, 0), (5, 10, 5), (5, 10, 9),
                (10, 0, 0), (10, 0, 5), (10, 0, 9),
                (10, 5, 0), (10, 5, 5), (10, 5, 9),
                (10, 10, 0), (10, 10, 5), (10, 10, 9)]

    assert np.all(indice == expected)
Esempio n. 3
0
def test_get_patches_2d():
    image = np.zeros((10, 10, 2))
    image[5:, :, 0] = 1
    image[5:, :5, 1] = 1
    images = np.array([image for _ in range(2)])

    target = np.zeros((10, 10, 1))
    target[5:, :5, 0] = 1
    targets = np.array([target for _ in range(2)])

    indice = get_patch_indice((10, 10), (5, 5), 0)

    patches, labels = get_patches(images, target=targets,
                                  patch_indice=indice, patch_size=(5, 5),
                                  stratified=True, drop_fraction=0.4,
                                  check_drop_channel=0, batch_size=2)

    half_patches = np.zeros((5, 5, 2))
    half_patches[..., 0] = 1
    full_patches = np.ones((5, 5, 2))

    assert len(patches) == 4

    full_patches_num = 0
    half_patches_num = 0
    for patch, label in zip(patches[:2], labels[:2]):
        if np.all(full_patches == patch):
            assert np.all(label == np.ones((5, 5, 1)))
            full_patches_num += 1
        elif np.all(half_patches == patch):
            assert np.all(label == np.zeros((5, 5, 1)))
            half_patches_num += 1

    assert full_patches_num == 1 and half_patches_num == 1

    full_patches_num = 0
    half_patches_num = 0
    for patch, label in zip(patches[2:], labels[2:]):
        if np.all(full_patches == patch):
            assert np.all(label == np.ones((5, 5, 1)))
            full_patches_num += 1
        elif np.all(half_patches == patch):
            assert np.all(label == np.zeros((5, 5, 1)))
            half_patches_num += 1

    assert full_patches_num == 1 and half_patches_num == 1
Esempio n. 4
0
def test_get_patch_indice_error():
    with pytest.raises(ValueError):
        get_patch_indice((2, 3, 4), (13, 13), 0)
Esempio n. 5
0
def test_get_patches_3d():
    image = np.zeros((10, 10, 10, 2))
    image[5:, :, :, 0] = 1
    image[5:, :5, :, 1] = 1

    images = np.array([image for _ in range(2)])

    indice = get_patch_indice((10, 10, 10), (5, 5, 5), 0)

    # Only patches, in order
    patches = get_patches(images,
                          patch_indice=indice, patch_size=(5, 5, 5),
                          stratified=False, drop_fraction=0,
                          check_drop_channel=None)

    empty_patches = np.zeros((5, 5, 5, 2))
    half_patches = np.zeros((5, 5, 5, 2))
    half_patches[..., 0] = 1
    full_patches = np.ones((5, 5, 5, 2))

    expected = np.array([
        empty_patches, empty_patches, empty_patches, empty_patches,
        full_patches, full_patches, half_patches, half_patches
    ] * 2)

    assert len(patches) == 16
    assert np.all(patches == expected)

    # Only patches, in order, drop half
    patches = get_patches(images,
                          patch_indice=indice, patch_size=(5, 5, 5),
                          stratified=False, drop_fraction=0.4,
                          check_drop_channel=None)

    expected = np.array([
        full_patches, full_patches, half_patches, half_patches
    ] * 2)

    assert len(patches) == 8
    assert np.all(patches == expected)

    # Only patches, in order, only keep full
    patches = get_patches(images,
                          patch_indice=indice, patch_size=(5, 5, 5),
                          stratified=False, drop_fraction=0.9,
                          check_drop_channel=None)

    expected = np.array([
        full_patches, full_patches
    ] * 2)

    assert len(patches) == 4
    assert np.all(patches == expected)

    # Only patches, in order, drop half based on one channel
    patches = get_patches(images,
                          patch_indice=indice, patch_size=(5, 5, 5),
                          stratified=False, drop_fraction=0.4,
                          check_drop_channel=0)

    expected = np.array([
        full_patches, full_patches, half_patches, half_patches
    ] * 2)

    assert len(patches) == 8
    assert np.all(patches == expected)

    # patches, targets, stratified, without batch_size
    target = np.zeros((10, 10, 10, 1))
    target[5:, :5, :, 0] = 1

    targets = np.array([target for _ in range(2)])

    patches, labels = get_patches(images, target=targets,
                                  patch_indice=indice, patch_size=(5, 5, 5),
                                  stratified=True, drop_fraction=0.4,
                                  check_drop_channel=0)

    assert len(patches) == 8
    full_patches_num = 0
    half_patches_num = 0

    for patch, label in zip(patches, labels):
        if np.all(full_patches == patch):
            assert np.all(label == np.ones((5, 5, 5, 1)))
            full_patches_num += 1
        elif np.all(half_patches == patch):
            assert np.all(label == np.zeros((5, 5, 5, 1)))
            half_patches_num += 1

    assert full_patches_num == 4 and half_patches_num == 4

    # patches, targets, stratified based on target
    patches, labels = get_patches(images, target=targets,
                                  patch_indice=indice, patch_size=(5, 5, 5),
                                  stratified=True, drop_fraction=0.4,
                                  check_drop_channel=0, batch_size=4)

    assert len(patches) == 8

    full_patches_num = 0
    half_patches_num = 0
    for patch, label in zip(patches[:4], labels[:4]):
        if np.all(full_patches == patch):
            assert np.all(label == np.ones((5, 5, 5, 1)))
            full_patches_num += 1
        elif np.all(half_patches == patch):
            assert np.all(label == np.zeros((5, 5, 5, 1)))
            half_patches_num += 1

    assert full_patches_num == 2 and half_patches_num == 2

    full_patches_num = 0
    half_patches_num = 0
    for patch, label in zip(patches[4:], labels[4:]):
        if np.all(full_patches == patch):
            assert np.all(label == np.ones((5, 5, 5, 1)))
            full_patches_num += 1
        elif np.all(half_patches == patch):
            assert np.all(label == np.zeros((5, 5, 5, 1)))
            half_patches_num += 1

    assert full_patches_num == 2 and half_patches_num == 2
Esempio n. 6
0
        targets = f['y'][:index]

    return normalize(images), targets


if __name__ == "__main__":
    images, targets = load_images()
    patch_size = (128, 128, 128)
    overlap = 0.5
    stratified = True

    batch_size = 8
    drop_fraction = 0.1
    check_drop_channel = 0

    indice = get_patch_indice(images.shape[1:-1], patch_size, overlap)
    # print(indice, len(indice))

    patches, labels = get_patches(images, targets, indice, patch_size,
                                  stratified=stratified,
                                  batch_size=batch_size,
                                  drop_fraction=drop_fraction,
                                  check_drop_channel=check_drop_channel)

    print(patches.shape)

    fig, ax = plt.subplots(1, 1)
    ax.axis('off')

    initialize = False
    mask_levels = [0.5]