def increment_deep_cf(As, Bs, X, y, increment_cf=increment_mccf,
                      n_levels=3, l=0.01, boundary='constant'):
    r"""
    Increment Deep Correlation Filter (DCF) filter.

    Parameters
    ----------
    X : ``(n_images, n_channels, height, width)`` `ndarray`
        Training images.
    y : ``(1, height, width)`` `ndarray`
        Desired response.
    learn_cf : `callable`, optional
        Callable used to learn a single shallow Correlation Filter (CF) (
        `learn_mosse` for single channel images or `learn_mccf` for
        multi-channel images).
    n_levels : int, optional
        The number of level.
    l: `float`, optional
        Regularization parameter.
    boundary: str {`constant`, `symmetric`}, optional
        Determines how the image is padded.

    Returns
    -------
    deep_mosse: ``(1, height, width)`` `ndarray`
        Deep Correlation Filter (DCF) filter associated to the training images.
    """
    # learn mosse filter
    f, A, B = increment_cf(As[0], Bs[0], X, y, l=l, boundary=boundary)

    # initialize filters, numerators and denominators arrays
    fs = np.empty((n_levels,) + f.shape)
    As[0] = A
    Bs[0] = B
    # store filter
    fs[0] = f
    # for each level
    for k in range(1, n_levels):
        nX = []
        # for each training image
        for j, x in enumerate(X):
            # convolve image and filter
            x = np.sum(fast2dconv(x, f, mode='full', boundary=boundary),
                       axis=0)[None]
            # replace image with response
            nX.append(x)
        X = np.asarray(nX)
        # learn mosse filter from responses
        f, A, B = increment_cf(As[k], Bs[k], X, y, l=l, boundary=boundary)
        # store filter, numerator and denominator
        fs[k] = f
        As[k] = A
        Bs[k] = B

    # compute equivalent deep mosse filter
    df = fs[0]
    for f in fs[1:]:
        df = fast2dconv(df, f, boundary=boundary)

    return df, As, Bs
Beispiel #2
0
def learn_deep_kcf(x, y, n_levels=3, kernel_correlation=gaussian_correlation,
                   l=0.01, boundary='constant', **kwargs):
    r"""
    Deep Kernelized Correlation Filter (DKCF).

    Parameters
    ----------
    x : ``(channels, height, width)`` `ndarray`
        Template image.
    y : ``(1, height, width)`` `ndarray`
        Desired response.
    n_levels: `int`, optional
        Number of levels.
    kernel_correlation: `callable`, optional
        Callable implementing a particular type of kernel correlation i.e.
        gaussian, polynomial or linear.
    l: `float`, optional
        Regularization parameter.
    boundary: str {`constant`, `symmetric`}, optional
        Determines how the image is padded.

    Returns
    -------
    deep_alpha: ``(channels, height, width)`` `ndarray`
        Deep Kernelized Correlation Filter (DKFC), in the frequency domain,
        associated to the template image.
    """
    # learn alpha
    alpha = learn_kcf(x, y, kernel_correlation=kernel_correlation, l=l,
                      boundary=boundary, **kwargs)

    # initialize alphas
    alphas = np.empty((n_levels,) + alpha.shape)
    # for each level
    for l in range(n_levels):
        # store filter
        alphas[l] = alpha
        # compute kernel correlation between template and image
        kxz = kernel_correlation(x, x, **kwargs)
        # compute kernel correlation response
        x = fast2dconv(kxz, alpha)
        # learn mosse filter from responses
        alpha = learn_kcf(x, y, kernel_correlation=kernel_correlation, l=l,
                          boundary=boundary, **kwargs)

    # compute equivalent deep mosse filter
    deep_alpha = alphas[0]
    for a in alphas[1:]:
        deep_alpha = fast2dconv(a, a, boundary=boundary)

    return deep_alpha, alphas
Beispiel #3
0
def gaussian_correlation(x, z, sigma=0.2, boundary='constant'):
    r"""
    Gaussian kernel correlation.

    Parameters
    ----------
    x : ``(channels, height, width)`` `ndarray`
        Template image.
    z : ``(channels, height, width)`` `ndarray`
        Input image.
    sigma: `float`, optional
        Kernel std.

    Returns
    -------
    xz: ``(1, height, width)`` `ndarray`
        Gaussian kernel correlation between the image and the template.
    """
    # norms
    x_norm = x.ravel().T.dot(x.ravel())
    z_norm = z.ravel().T.dot(z.ravel())
    # cross correlation
    xz = np.sum(fast2dconv(z, x[:, ::-1, ::-1], boundary=boundary), axis=0)
    # gaussian kernel
    kxz = np.exp(-(1/sigma**2) * np.maximum(0, x_norm + z_norm - 2 * xz))
    return kxz[None]
Beispiel #4
0
def polynomial_correlation(x, z, a=5, b=1, boundary='constant'):
    r"""
    Polynomial kernel correlation.

    Parameters
    ----------
    x : ``(channels, height, width)`` `ndarray`
        Template image.
    z : ``(channels, height, width)`` `ndarray`
        Input image.
    a: `float`, optional
        Kernel exponent.
    b: `float`, optional
        Kernel constant.

    Returns
    -------
    kxz: ``(1, height, width)`` `ndarray`
        Polynomial kernel correlation between the image and the template.
    """
    # cross correlation
    xz = np.sum(fast2dconv(z, x[:, ::-1, ::-1], boundary=boundary), axis=0)
    # polynomial kernel
    kxz = (xz + b) ** a
    return kxz[None]
Beispiel #5
0
def linear_correlation(x, z, boundary='constant'):
    r"""
    Linear kernel correlation (dot product).

    Parameters
    ----------
    x : ``(channels, height, width)`` `ndarray`
        Template image.
    z : ``(channels, height, width)`` `ndarray`
        Input image.

    Returns
    -------
    xz: ``(1, height, width)`` `ndarray`
        Linear kernel correlation between the image and the template.
    """
    # cross correlation
    xz = np.sum(fast2dconv(z, x[:, ::-1, ::-1], boundary=boundary), axis=0)
    return xz[None]