Example #1
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 #2
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 #3
0
def test_single_instance_inference():
    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)
    points = np.stack([points, points + 1], axis=0)
    cms = tf.stack(
        [
            make_confmaps(points[0], xv, yv, sigma=1.0),
            make_confmaps(points[1], xv, yv, sigma=1.0),
        ],
        axis=0,
    )

    x_in = tf.keras.layers.Input([12, 12, 3])
    x = tf.keras.layers.Lambda(lambda x: x, name="SingleInstanceConfmapsHead")(x_in)
    keras_model = tf.keras.Model(x_in, x)

    layer = SingleInstanceInferenceLayer(keras_model=keras_model, refinement="local")
    assert layer.output_stride == 1

    out = layer(cms)

    assert tuple(out["instance_peaks"].shape) == (2, 1, 3, 2)
    out["instance_peaks"] = tf.squeeze(out["instance_peaks"], axis=1)
    assert tuple(out["instance_peak_vals"].shape) == (2, 1, 3)
    out["instance_peak_vals"] = tf.squeeze(out["instance_peak_vals"], axis=1)
    assert_array_equal(out["instance_peaks"], points)
    assert_allclose(out["instance_peak_vals"], 1.0, atol=0.1)
    assert "confmaps" not in out

    out = layer({"image": cms})
    assert tuple(out["instance_peaks"].shape) == (2, 1, 3, 2)
    out["instance_peaks"] = tf.squeeze(out["instance_peaks"], axis=1)
    assert_array_equal(out["instance_peaks"], points)

    layer = SingleInstanceInferenceLayer(
        keras_model=keras_model, refinement="local", return_confmaps=True
    )
    out = layer(cms)
    assert "confmaps" in out
    assert_array_equal(out["confmaps"], cms)

    model = SingleInstanceInferenceModel(layer)
    preds = model.predict(cms)
    assert preds["instance_peaks"].shape == (2, 1, 3, 2)
    preds["instance_peaks"] = preds["instance_peaks"].squeeze(axis=1)
    assert_array_equal(preds["instance_peaks"], points)
    assert "instance_peak_vals" in preds
    assert "confmaps" in preds
Example #4
0
def test_find_global_peaks_integral():
    xv, yv = make_grid_vectors(image_height=12,
                               image_width=12,
                               output_stride=1)
    points = tf.cast([[1.5, 2.5], [3.5, 4.5], [5.5, 6.5]], tf.float32)
    cm = make_confmaps(points, xv, yv, sigma=1.0)

    peaks, peak_vals = find_global_peaks(
        tf.expand_dims(cm, axis=0),
        threshold=0.1,
        refinement="integral",
        integral_patch_size=5,
    )

    assert peaks.shape == (1, 3, 2)
    assert peak_vals.shape == (1, 3)
    assert_allclose(peaks[0].numpy(), points.numpy(), atol=0.1)
    assert_allclose(peak_vals[0].numpy(), [1, 1, 1], atol=0.3)

    peaks, peak_vals = find_global_peaks(
        tf.zeros((1, 8, 8, 3), dtype=tf.float32),
        threshold=0.1,
        refinement="integral",
        integral_patch_size=5,
    )
    assert peaks.shape == (1, 3, 2)
    assert peak_vals.shape == (1, 3)
    assert tf.reduce_all(tf.math.is_nan(peaks))
    assert_array_equal(peak_vals, [[0, 0, 0]])

    peaks, peak_vals = find_global_peaks(
        tf.stack([tf.zeros([12, 12, 3], dtype=tf.float32), cm], axis=0),
        threshold=0.1,
        refinement="integral",
        integral_patch_size=5,
    )
    assert peaks.shape == (2, 3, 2)
    assert tf.reduce_all(tf.math.is_nan(peaks[0]))
    assert_allclose(peaks[1].numpy(), points.numpy(), atol=0.1)

    peaks, peak_vals = find_global_peaks_integral(
        tf.stack([tf.zeros([12, 12, 3], dtype=tf.float32), cm], axis=0),
        threshold=0.1,
        crop_size=5,
    )
    assert peaks.shape == (2, 3, 2)
    assert tf.reduce_all(tf.math.is_nan(peaks[0]))
    assert_allclose(peaks[1].numpy(), points.numpy(), atol=0.1)
Example #5
0
def test_find_global_peaks_local():
    xv, yv = make_grid_vectors(image_height=12,
                               image_width=12,
                               output_stride=1)
    points = tf.cast([[1.6, 2.6], [3.6, 4.6], [5.6, 6.6]], tf.float32)
    cm = make_confmaps(points, xv, yv, sigma=1.0)

    peaks, peak_vals = find_global_peaks(tf.expand_dims(cm, axis=0),
                                         threshold=0.1,
                                         refinement="local")

    assert peaks.shape == (1, 3, 2)
    assert peak_vals.shape == (1, 3)
    assert_allclose(peaks[0].numpy(),
                    np.array([[1.75, 2.75], [3.75, 4.75], [5.75, 6.75]]))
    assert_allclose(peak_vals[0].numpy(), [1, 1, 1], atol=0.3)
Example #6
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 #7
0
def test_find_global_peaks_rough():
    xv, yv = make_grid_vectors(image_height=8, image_width=8, output_stride=1)
    points = tf.cast([[1, 2], [3, 4], [5, 6]], tf.float32)
    cm = make_confmaps(points, xv, yv, sigma=1.0)
    points2 = points + 1
    cms = tf.stack([cm, make_confmaps(points2, xv, yv, sigma=1.0)])

    peaks, peak_vals = find_global_peaks(cms, threshold=0.1, refinement=None)

    assert peaks.shape == (2, 3, 2)
    assert peak_vals.shape == (2, 3)
    assert_array_equal(peaks[0], points)
    assert_array_equal(peak_vals[0], [1, 1, 1])
    assert_array_equal(peaks[1], points2)
    assert_array_equal(peak_vals[1], [1, 1, 1])

    peaks, peak_vals = find_global_peaks_rough(tf.zeros((1, 8, 8, 3),
                                                        dtype=tf.float32),
                                               threshold=0.1)
    assert peaks.shape == (1, 3, 2)
    assert peak_vals.shape == (1, 3)
    assert tf.reduce_all(tf.math.is_nan(peaks))
    assert_array_equal(peak_vals, [[0, 0, 0]])
Example #8
0
def test_instance_peaks_layer():
    xv, yv = make_grid_vectors(image_height=12, image_width=12, output_stride=1)
    points = tf.cast([[1.5, 2.5], [3.5, 4.5], [5.5, 6.5]], tf.float32)
    cms = tf.stack(
        [
            make_confmaps(points, xv, yv, sigma=1.0),
            make_confmaps(points + 1, xv, yv, sigma=1.0),
        ],
        axis=0,
    )

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

    instance_peaks_layer = FindInstancePeaks(
        keras_model=model,
        input_scale=1.0,
        peak_threshold=0.2,
        return_confmaps=False,
        refinement="integral",
    )

    # Raw tensor
    out = instance_peaks_layer(cms)
    assert tuple(out["instance_peaks"].shape) == (2, None, 3, 2)
    assert tuple(out["instance_peak_vals"].shape) == (2, None, 3)
    assert tuple(out["instance_peaks"].bounding_shape()) == (2, 1, 3, 2)
    assert tuple(out["instance_peak_vals"].bounding_shape()) == (2, 1, 3)
    assert_allclose(out["instance_peaks"][0][0].numpy(), points.numpy(), atol=0.1)
    assert_allclose(out["instance_peak_vals"][0][0].numpy(), [1, 1, 1], atol=0.3)
    assert_allclose(out["instance_peaks"][1][0].numpy(), points.numpy() + 1, atol=0.1)
    assert_allclose(out["instance_peak_vals"][1][0].numpy(), [1, 1, 1], atol=0.3)

    # Batched example
    crops = tf.RaggedTensor.from_tensor(tf.expand_dims(cms, axis=1), lengths=[1, 1])
    out = instance_peaks_layer({"crops": crops})
    assert tuple(out["instance_peaks"].shape) == (2, None, 3, 2)
    assert tuple(out["instance_peak_vals"].shape) == (2, None, 3)
    assert tuple(out["instance_peaks"].bounding_shape()) == (2, 1, 3, 2)
    assert tuple(out["instance_peak_vals"].bounding_shape()) == (2, 1, 3)
    assert_allclose(out["instance_peaks"][0][0].numpy(), points.numpy(), atol=0.1)
    assert_allclose(out["instance_peak_vals"][0][0].numpy(), [1, 1, 1], atol=0.3)
    assert_allclose(out["instance_peaks"][1][0].numpy(), points.numpy() + 1, atol=0.1)
    assert_allclose(out["instance_peak_vals"][1][0].numpy(), [1, 1, 1], atol=0.3)

    # Batch size = 1, multi-instance example
    crops = tf.RaggedTensor.from_tensor(tf.expand_dims(cms, axis=0), lengths=[2])
    out = instance_peaks_layer({"crops": crops})
    assert tuple(out["instance_peaks"].shape) == (1, None, 3, 2)
    assert tuple(out["instance_peak_vals"].shape) == (1, None, 3)
    assert tuple(out["instance_peaks"].bounding_shape()) == (1, 2, 3, 2)
    assert tuple(out["instance_peak_vals"].bounding_shape()) == (1, 2, 3)
    assert_allclose(out["instance_peaks"][0][0].numpy(), points.numpy(), atol=0.1)
    assert_allclose(out["instance_peak_vals"][0][0].numpy(), [1, 1, 1], atol=0.3)
    assert_allclose(out["instance_peaks"][0][1].numpy(), points.numpy() + 1, atol=0.1)
    assert_allclose(out["instance_peak_vals"][0][1].numpy(), [1, 1, 1], atol=0.3)

    # Offset adjustment and pass through centroids
    instance_peaks_layer = FindInstancePeaks(
        keras_model=model,
        input_scale=1.0,
        peak_threshold=0.2,
        return_confmaps=True,
        refinement="integral",
    )
    # (samples, h, w, c) -> (samples, ?, h, w, c)
    crops = tf.RaggedTensor.from_tensor(tf.expand_dims(cms, axis=1), lengths=[1, 1])
    # (samples, centroids, 2) -> (samples, ?, 2)
    crop_offsets = tf.RaggedTensor.from_tensor(
        tf.reshape(tf.cast([1, 2, 3, 4], tf.float32), [2, 1, 2]), lengths=[1, 1]
    )
    out = instance_peaks_layer(
        {
            "crops": crops,
            "centroids": tf.zeros([]),
            "centroid_vals": tf.zeros([]),
            "crop_offsets": crop_offsets,
        }
    )
    assert "centroids" in out
    assert "centroid_vals" in out
    assert_allclose(
        out["instance_peaks"][0][0].numpy(),
        points.numpy() + np.array([[1, 2]]),
        atol=0.1,
    )
    assert_allclose(
        out["instance_peaks"][1][0].numpy(),
        points.numpy() + 1 + np.array([[3, 4]]),
        atol=0.1,
    )

    # Input scaling
    scale = 0.5
    instance_peaks_layer = FindInstancePeaks(
        keras_model=model,
        input_scale=scale,
        peak_threshold=0.2,
        return_confmaps=False,
        refinement="integral",
    )
    xv, yv = make_grid_vectors(
        image_height=12 / scale, image_width=12 / scale, output_stride=1
    )
    points = tf.cast([[1.5, 2.5], [3.5, 4.5], [5.5, 6.5]], tf.float32)
    cms = tf.stack(
        [
            make_confmaps(points / scale, xv, yv, sigma=1.0 / scale),
            make_confmaps((points + 1) / scale, xv, yv, sigma=1.0 / scale),
        ],
        axis=0,
    )
    out = instance_peaks_layer(cms)

    assert_allclose(
        out["instance_peaks"][0][0].numpy(), points.numpy() / scale, atol=0.15
    )
    assert_allclose(
        out["instance_peaks"][1][0].numpy(), (points.numpy() + 1) / scale, atol=0.15
    )