Exemple #1
0
def test_make_confmaps():
    xv, yv = make_grid_vectors(image_height=4, image_width=5, output_stride=1)
    points = tf.cast([[0.5, 1.], [3, 3.5], [2., 2.]], tf.float32)
    cm = make_confmaps(points, xv, yv, sigma=1.)

    assert cm.dtype == tf.float32
    assert cm.shape == (4, 5, 3)
    np.testing.assert_allclose(cm, [[
        [0.535, 0., 0.018],
        [0.535, 0., 0.082],
        [0.197, 0.001, 0.135],
        [0.027, 0.002, 0.082],
        [0.001, 0.001, 0.018],
    ],
                                    [
                                        [0.882, 0., 0.082],
                                        [0.882, 0.006, 0.368],
                                        [0.325, 0.027, 0.607],
                                        [0.044, 0.044, 0.368],
                                        [0.002, 0.027, 0.082],
                                    ],
                                    [
                                        [0.535, 0.004, 0.135],
                                        [0.535, 0.044, 0.607],
                                        [0.197, 0.197, 1.],
                                        [0.027, 0.325, 0.607],
                                        [0.001, 0.197, 0.135],
                                    ],
                                    [
                                        [0.119, 0.01, 0.082],
                                        [0.119, 0.119, 0.368],
                                        [0.044, 0.535, 0.607],
                                        [0.006, 0.882, 0.368],
                                        [0., 0.535, 0.082],
                                    ]],
                               atol=1e-3)

    # Grid aligned peak
    points = tf.cast([[2, 3]], tf.float32)
    cm = make_confmaps(points, xv, yv, sigma=1.)
    assert cm.shape == (4, 5, 1)
    assert cm[3, 2] == 1.0

    # Output stride
    xv, yv = make_grid_vectors(image_height=8, image_width=8, output_stride=2)
    points = tf.cast([[2, 4]], tf.float32)
    cm = make_confmaps(points, xv, yv, sigma=1.)
    assert cm.shape == (4, 4, 1)
    assert cm[2, 1] == 1.0

    # Missing points
    xv, yv = make_grid_vectors(image_height=8, image_width=8, output_stride=2)
    points = tf.cast([[2, 4]], tf.float32)
    cm = make_confmaps(points, xv, yv, sigma=1.)
    points_with_nan = tf.cast([[2, 4], [np.nan, np.nan]], tf.float32)
    cm_with_nan = make_confmaps(points_with_nan, xv, yv, sigma=1.)
    assert cm_with_nan.shape == (4, 4, 2)
    assert cm_with_nan.dtype == tf.float32
    np.testing.assert_array_equal(cm_with_nan[:, :, 0], cm[:, :, 0])
    assert (cm_with_nan[:, :, 1].numpy() == 0).all()
Exemple #2
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
Exemple #3
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))
Exemple #4
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]])
Exemple #5
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)
Exemple #6
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)
Exemple #7
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
    )