Example #1
0
def test_multi_scale_loss_kernel():
    """
    Test multi-scale loss kernel returns the appropriate
    loss tensor for same inputs and jaccard cal.
    """
    loss_values = np.asarray([1, 2, 3])
    array_eye = np.identity((3))
    tensor_pred = np.zeros((3, 3, 3, 3))
    tensor_eye = np.zeros((3, 3, 3, 3))

    tensor_eye[:, :, 0:3, 0:3] = array_eye
    tensor_pred[:, :, 0, 0] = array_eye
    tensor_eye = tf.convert_to_tensor(tensor_eye, dtype=tf.double)
    tensor_pred = tf.convert_to_tensor(tensor_pred, dtype=tf.double)
    list_losses = np.array([
        label.single_scale_loss(
            y_true=label.separable_filter3d(tensor_eye,
                                            label.gauss_kernel1d(s)),
            y_pred=label.separable_filter3d(tensor_pred,
                                            label.gauss_kernel1d(s)),
            loss_type="jaccard",
        ) for s in loss_values
    ])
    expect = np.mean(list_losses, axis=0)
    get = label.multi_scale_loss(tensor_eye, tensor_pred, "jaccard",
                                 loss_values)
    assert assertTensorsEqual(get, expect)
Example #2
0
def test_gauss_kernel1d_0():
    """
    Testing case where sigma = 0, expect 0 return
    """
    sigma = tf.constant(0, dtype=tf.float32)
    expect = tf.constant(0, dtype=tf.float32)
    get = label.gauss_kernel1d(sigma)
    assert get == expect
Example #3
0
def test_gauss_kernel1d_else():
    """
    Testing case where sigma is not 0,
    expect a tensor returned.
    """
    sigma = 3
    get = tf.cast(label.gauss_kernel1d(sigma), dtype=tf.float64)
    list_vals = range(-sigma * 3, sigma * 3 + 1)
    exp = [np.exp(-0.5 * x**2 / sigma**2) for x in list_vals]
    expect = tf.convert_to_tensor(exp, dtype=tf.float64)
    expect = expect / tf.reduce_sum(expect)
    assert assertTensorsEqual(get, expect)
Example #4
0
def test_gauss_kernel1d_else():
    """
    Testing case where sigma is not 0,
    expect a tensor returned.
    """
    sigma = 3
    get = tf.cast(label.gauss_kernel1d(sigma), dtype=tf.float32)
    expect = [
        np.exp(-0.5 * x**2 / sigma**2)
        for x in range(-sigma * 3, sigma * 3 + 1)
    ]
    expect = tf.convert_to_tensor(expect, dtype=tf.float32)
    expect = expect / tf.reduce_sum(expect)
    assert is_equal_tf(get, expect)