Example #1
0
def test_find_local_peaks_rough():
    xv, yv = make_grid_vectors(image_height=16,
                               image_width=16,
                               output_stride=1)
    instances = tf.cast(
        [
            [[1, 2], [3, 4]],
            [[5, 6], [7, 8]],
            [[np.nan, np.nan], [11, 12]],
        ],
        tf.float32,
    )
    cms = make_multi_confmaps(instances, xv=xv, yv=yv, sigma=1.0)
    instances2 = tf.cast([[[2, 3], [4, 5]], [[6, 7], [8, 9]]], tf.float32)
    cms = tf.stack(
        [cms, make_multi_confmaps(instances2, xv=xv, yv=yv, sigma=1.0)],
        axis=0)

    peak_points, peak_vals, peak_sample_inds, peak_channel_inds = find_local_peaks(
        cms, threshold=0.1, refinement=None)

    assert peak_points.shape == (9, 2)
    assert peak_vals.shape == (9, )
    assert peak_sample_inds.shape == (9, )
    assert peak_channel_inds.shape == (9, )

    assert_array_equal(
        peak_points.numpy(),
        [
            [1, 2],
            [3, 4],
            [5, 6],
            [7, 8],
            [11, 12],
            [2, 3],
            [4, 5],
            [6, 7],
            [8, 9],
        ],
    )
    assert_array_equal(peak_vals, [1, 1, 1, 1, 1, 1, 1, 1, 1])
    assert_array_equal(peak_sample_inds, [0, 0, 0, 0, 0, 1, 1, 1, 1])
    assert_array_equal(peak_channel_inds, [0, 1, 0, 1, 1, 0, 1, 0, 1])

    peak_points, peak_vals, peak_sample_inds, peak_channel_inds = find_local_peaks(
        tf.zeros([1, 4, 4, 3], tf.float32), threshold=0.1, refinement=None)
    assert peak_points.shape == (0, 2)
    assert peak_vals.shape == (0, )
    assert peak_sample_inds.shape == (0, )
    assert peak_channel_inds.shape == (0, )
Example #2
0
def test_centroid_crop_layer():
    xv, yv = make_grid_vectors(image_height=12, image_width=12, output_stride=1)
    points = tf.cast([[[1.75, 2.75]], [[3.75, 4.75]], [[5.75, 6.75]]], tf.float32)
    cms = tf.expand_dims(make_multi_confmaps(points, xv, yv, sigma=1.5), axis=0)

    x_in = tf.keras.layers.Input([12, 12, 1])
    x_out = tf.keras.layers.Lambda(lambda x: x, name="CentroidConfmapsHead")(x_in)
    model = tf.keras.Model(inputs=x_in, outputs=x_out)

    layer = CentroidCrop(
        keras_model=model,
        input_scale=1.0,
        crop_size=3,
        pad_to_stride=1,
        output_stride=None,
        refinement="local",
        integral_patch_size=5,
        peak_threshold=0.2,
        return_confmaps=False,
    )

    out = layer(cms)
    assert tuple(out["centroids"].shape) == (1, None, 2)
    assert tuple(out["centroid_vals"].shape) == (1, None)
    assert tuple(out["crops"].shape) == (1, None, 3, 3, 1)
    assert tuple(out["crop_offsets"].shape) == (1, None, 2)

    assert tuple(out["centroids"].bounding_shape()) == (1, 3, 2)
    assert tuple(out["centroid_vals"].bounding_shape()) == (1, 3)
    assert tuple(out["crops"].bounding_shape()) == (1, 3, 3, 3, 1)
    assert tuple(out["crop_offsets"].bounding_shape()) == (1, 3, 2)

    assert_allclose(out["centroids"][0].numpy(), points.numpy().squeeze(axis=1))
    assert_allclose(out["centroid_vals"][0].numpy(), [1, 1, 1], atol=0.1)
Example #3
0
def test_find_local_peaks_local():
    xv, yv = make_grid_vectors(image_height=32,
                               image_width=32,
                               output_stride=1)
    instances = (tf.cast(
        [
            [[1, 2], [3, 4]],
            [[5, 6], [7, 8]],
            [[np.nan, np.nan], [11, 12]],
        ],
        tf.float32,
    ) * 2 + 0.25)
    cms = make_multi_confmaps(instances, xv=xv, yv=yv, sigma=1.0)
    instances2 = tf.cast([[[2, 3], [4, 5]], [[6, 7], [8, 9]]],
                         tf.float32) * 2 + 0.25
    cms = tf.stack(
        [cms, make_multi_confmaps(instances2, xv=xv, yv=yv, sigma=1.0)],
        axis=0)

    peak_points, peak_vals, peak_sample_inds, peak_channel_inds = find_local_peaks(
        cms, threshold=0.1, refinement="local")

    assert peak_points.shape == (9, 2)
    assert peak_vals.shape == (9, )
    assert peak_sample_inds.shape == (9, )
    assert peak_channel_inds.shape == (9, )

    assert_allclose(
        peak_points.numpy(),
        np.array([
            [1, 2],
            [3, 4],
            [5, 6],
            [7, 8],
            [11, 12],
            [2, 3],
            [4, 5],
            [6, 7],
            [8, 9],
        ]) * 2 + 0.25,
    )
    assert_allclose(peak_vals, [1, 1, 1, 1, 1, 1, 1, 1, 1], atol=0.1)
    assert_array_equal(peak_sample_inds, [0, 0, 0, 0, 0, 1, 1, 1, 1])
    assert_array_equal(peak_channel_inds, [0, 1, 0, 1, 1, 0, 1, 0, 1])
Example #4
0
def test_make_multi_confmaps():
    xv, yv = make_grid_vectors(image_height=4, image_width=5, output_stride=1)
    instances = tf.cast([
        [[0.5, 1.], [2., 2.]],
        [[1.5, 1.], [2., 3.]],
        [[np.nan, np.nan], [-1., 5.]],
    ], tf.float32)
    cms = make_multi_confmaps(instances, xv=xv, yv=yv, sigma=1.)
    assert cms.shape == (4, 5, 2)
    assert cms.dtype == tf.float32

    cm0 = make_confmaps(instances[0], xv=xv, yv=yv, sigma=1.)
    cm1 = make_confmaps(instances[1], xv=xv, yv=yv, sigma=1.)
    cm2 = make_confmaps(instances[2], xv=xv, yv=yv, sigma=1.)

    np.testing.assert_array_equal(
        cms, tf.reduce_max(tf.stack([cm0, cm1, cm2], axis=-1), axis=-1))