Пример #1
0
def test_reshaping():

    a = np.random.randint(0, 10000, size=(30, 30, 30))
    assert_almost_equal(a, col2im_nd(im2col_nd(a, (2,2,2), (1,1,1)), (2,2,2), (30,30,30), (1,1,1)))
    assert_almost_equal(a, col2im_nd(im2col_nd(a, (3,3,3), (2,2,2)), (3,3,3), (30,30,30), (2,2,2)))

    b = np.random.randint(0, 10000, size=(30, 30, 30, 10))
    assert_almost_equal(b, col2im_nd(im2col_nd(b, (2,2,2,10), (1,1,1,1)), (2,2,2,10), (30,30,30,10), (1,1,1,1)))
    assert_almost_equal(b, col2im_nd(im2col_nd(b, (3,3,3,10), (2,2,2,1)), (3,3,3,10), (30,30,30,10), (2,2,2,1)))

    a = np.random.rand(1000).reshape(10,10,10)
    out = im2col_nd(a, (3,3,3),(2,2,2))
    redo = col2im_nd(out, (3,3,3), (10,10,10), (2,2,2))
    np.testing.assert_allclose(a,redo)

    a = np.random.rand(100000).reshape(10,100,10,10)
    out = im2col_nd(a, (3,3,3,10),(2,2,2,0))
    redo = col2im_nd(out, (3,3,3,10), (10,100,10,10), (2,2,2,0))
    np.testing.assert_allclose(a,redo)

    a = np.random.rand(1000).reshape(10,10,10)
    out = im2col_nd(a, (2,2,2),(0,0,0))
    redo = col2im_nd(out, (2,2,2), (10,10,10), (0,0,0))
    np.testing.assert_allclose(a,redo)

    a = np.random.rand(10000).reshape(10,10,10,10)
    out = im2col_nd(a, (2,2,2,10),(0,0,0,0))
    redo = col2im_nd(out, (2,2,2,10), (10,10,10,10), (0,0,0,0))
    np.testing.assert_allclose(a,redo)
Пример #2
0
def test_reshaping():

    a = np.random.randint(0, 10000, size=(30, 30, 30))
    assert_almost_equal(
        a,
        col2im_nd(im2col_nd(a, (2, 2, 2), (1, 1, 1)), (2, 2, 2), (30, 30, 30),
                  (1, 1, 1)))
    assert_almost_equal(
        a,
        col2im_nd(im2col_nd(a, (3, 3, 3), (2, 2, 2)), (3, 3, 3), (30, 30, 30),
                  (2, 2, 2)))

    b = np.random.randint(0, 10000, size=(30, 30, 30, 10))
    assert_almost_equal(
        b,
        col2im_nd(im2col_nd(b, (2, 2, 2, 10), (1, 1, 1, 1)), (2, 2, 2, 10),
                  (30, 30, 30, 10), (1, 1, 1, 1)))
    assert_almost_equal(
        b,
        col2im_nd(im2col_nd(b, (3, 3, 3, 10), (2, 2, 2, 1)), (3, 3, 3, 10),
                  (30, 30, 30, 10), (2, 2, 2, 1)))

    a = np.random.rand(1000).reshape(10, 10, 10)
    out = im2col_nd(a, (3, 3, 3), (2, 2, 2))
    redo = col2im_nd(out, (3, 3, 3), (10, 10, 10), (2, 2, 2))
    np.testing.assert_allclose(a, redo)

    a = np.random.rand(100000).reshape(10, 100, 10, 10)
    out = im2col_nd(a, (3, 3, 3, 10), (2, 2, 2, 0))
    redo = col2im_nd(out, (3, 3, 3, 10), (10, 100, 10, 10), (2, 2, 2, 0))
    np.testing.assert_allclose(a, redo)

    a = np.random.rand(1000).reshape(10, 10, 10)
    out = im2col_nd(a, (2, 2, 2), (0, 0, 0))
    redo = col2im_nd(out, (2, 2, 2), (10, 10, 10), (0, 0, 0))
    np.testing.assert_allclose(a, redo)

    a = np.random.rand(10000).reshape(10, 10, 10, 10)
    out = im2col_nd(a, (2, 2, 2, 10), (0, 0, 0, 0))
    redo = col2im_nd(out, (2, 2, 2, 10), (10, 10, 10, 10), (0, 0, 0, 0))
    np.testing.assert_allclose(a, redo)
Пример #3
0
def processer(data,
              mask,
              variance,
              block_size,
              overlap,
              param_alpha,
              param_D,
              dtype=np.float64,
              n_iter=10,
              gamma=3.,
              tau=1.,
              tolerance=1e-5):

    orig_shape = data.shape
    mask_array = im2col_nd(mask, block_size[:-1], overlap[:-1])
    train_idx = np.sum(mask_array, axis=0) > (mask_array.shape[0] / 2.)

    # If mask is empty, return a bunch of zeros as blocks
    if not np.any(train_idx):
        return np.zeros_like(data)

    X = im2col_nd(data, block_size, overlap)
    var_mat = np.median(im2col_nd(variance, block_size[:-1],
                                  overlap[:-1])[:, train_idx],
                        axis=0)
    X_full_shape = X.shape
    X = X[:, train_idx].astype(dtype)

    param_alpha['L'] = int(0.5 * X.shape[0])

    D = param_alpha['D']

    alpha = lil_matrix((D.shape[1], X.shape[1]))
    W = np.ones(alpha.shape, dtype=dtype, order='F')

    DtD = np.dot(D.T, D)
    DtX = np.dot(D.T, X)
    DtXW = np.empty_like(DtX, order='F')

    alpha_old = np.ones(alpha.shape, dtype=dtype)
    has_converged = np.zeros(alpha.shape[1], dtype=np.bool)
    arr = np.empty(alpha.shape)

    xi = np.random.randn(X.shape[0], X.shape[1]) * var_mat
    eps = np.max(np.abs(np.dot(D.T, xi)), axis=0)

    for _ in range(n_iter):
        not_converged = np.equal(has_converged, False)
        DtXW[:, not_converged] = DtX[:, not_converged] / W[:, not_converged]

        for i in range(alpha.shape[1]):
            if not has_converged[i]:
                param_alpha['lambda1'] = var_mat[i] * (
                    X.shape[0] + gamma * np.sqrt(2 * X.shape[0]))
                DtDW = (1. / W[..., None, i]) * DtD * (1. / W[:, i])
                alpha[:, i:i + 1] = spams.lasso(X[:, i:i + 1],
                                                Q=np.asfortranarray(DtDW),
                                                q=DtXW[:, i:i + 1],
                                                **param_alpha)

        alpha.toarray(out=arr)
        nonzero_ind = arr != 0
        arr[nonzero_ind] /= W[nonzero_ind]
        has_converged = np.max(np.abs(alpha_old - arr), axis=0) < tolerance

        if np.all(has_converged):
            break

        alpha_old[:] = arr
        W[:] = 1. / (np.abs(alpha_old**tau) + eps)

    weigths = np.ones(X_full_shape[1], dtype=dtype, order='F')
    weigths[train_idx] = 1. / (alpha.getnnz(axis=0) + 1.)

    X = np.zeros(X_full_shape, dtype=dtype, order='F')
    X[:, train_idx] = np.dot(D, arr)

    return col2im_nd(X, block_size, orig_shape, overlap, weigths)
Пример #4
0
def _processer(data, mask, variance, block_size, overlap, param_alpha, param_D, dtype=np.float64, n_iter=10, gamma=3., tau=1.):
    # data, mask, variance, block_size, overlap, param_alpha, param_D, dtype, n_iter = arglist
    # gamma = 3.
    # tau = 1.

    orig_shape = data.shape
    mask_array = im2col_nd(mask, block_size[:3], overlap[:3])
    train_idx = np.sum(mask_array, axis=0) > mask_array.shape[0]/2

    # If mask is empty, return a bunch of zeros as blocks
    if not np.any(train_idx):
        return np.zeros_like(data)

    X = im2col_nd(data, block_size, overlap)
    var_mat = np.median(im2col_nd(variance[..., 0:orig_shape[-1]], block_size, overlap)[:, train_idx], axis=0).astype(dtype)
    X_full_shape = X.shape
    X = X[:, train_idx]

    param_alpha['L'] = int(0.5 * X.shape[0])

    D = param_alpha['D']

    alpha = lil_matrix((D.shape[1], X.shape[1]))
    W = np.ones(alpha.shape, dtype=dtype, order='F')

    DtD = np.dot(D.T, D)
    DtX = np.dot(D.T, X)
    DtXW = np.empty_like(DtX, order='F')

    alpha_old = np.ones(alpha.shape, dtype=dtype)
    has_converged = np.zeros(alpha.shape[1], dtype=np.bool)

    xi = np.random.randn(X.shape[0], X.shape[1]) * var_mat
    eps = np.max(np.abs(np.dot(D.T, xi)), axis=0)
    param_alpha['mode'] = 1
    param_alpha['pos'] = True

    for _ in range(n_iter):
        not_converged = np.equal(has_converged, False)
        DtXW[:, not_converged] = DtX[:, not_converged] / W[:, not_converged]

        for i in range(alpha.shape[1]):
            if not has_converged[i]:

                param_alpha['lambda1'] = var_mat[i] * (X.shape[0] + gamma * np.sqrt(2 * X.shape[0]))
                DtDW = (1. / W[..., None, i]) * DtD * (1. / W[:, i])
                alpha[:, i:i+1] = spams.lasso(X[:, i:i+1], Q=np.asfortranarray(DtDW), q=DtXW[:, i:i+1], **param_alpha)

        arr = alpha.toarray()
        nonzero_ind = arr != 0
        arr[nonzero_ind] /= W[nonzero_ind]
        has_converged = np.max(np.abs(alpha_old - arr), axis=0) < 1e-5

        if np.all(has_converged):
            break

        alpha_old = arr
        W[:] = 1. / (np.abs(alpha_old**tau) + eps)

        # compute_weights(alpha_old, alpha, W, tau, eps)

    # alpha = arr
    # X = D.dot(alpha)
    # X = sparse_dot(D,alpha)
    X = np.dot(D, arr)
    weigths = np.ones(X_full_shape[1], dtype=dtype, order='F')
    weigths[train_idx] = 1. / (alpha.getnnz(axis=0) + 1.)

    X2 = np.zeros(X_full_shape, dtype=dtype, order='F')
    X2[:, train_idx] = X

    return col2im_nd(X2, block_size, orig_shape, overlap, weigths)
Пример #5
0
def test_reshaping():

    a = np.random.randint(0, 10000, size=(30, 30, 30))
    assert_allclose(
        a,
        col2im_nd(im2col_nd(a, (2, 2, 2), (1, 1, 1)), (2, 2, 2), (30, 30, 30),
                  (1, 1, 1)))
    assert_allclose(
        a,
        col2im_nd(im2col_nd(a, (3, 3, 3), (2, 2, 2)), (3, 3, 3), (30, 30, 30),
                  (2, 2, 2)))

    b = np.random.randint(0, 10000, size=(30, 30, 30, 10))
    assert_allclose(
        b,
        col2im_nd(im2col_nd(b, (2, 2, 2, 10), (1, 1, 1, 1)), (2, 2, 2, 10),
                  (30, 30, 30, 10), (1, 1, 1, 1)))
    assert_allclose(
        b,
        col2im_nd(im2col_nd(b, (3, 3, 3, 10), (2, 2, 2, 1)), (3, 3, 3, 10),
                  (30, 30, 30, 10), (2, 2, 2, 1)))

    a = np.random.rand(1000).reshape(10, 10, 10)
    out = im2col_nd(a, (3, 3, 3), (2, 2, 2))
    redo = col2im_nd(out, (3, 3, 3), (10, 10, 10), (2, 2, 2))
    assert_allclose(a, redo)

    out = extract_patches(a, (3, 3, 3), (1, 1, 1)).reshape(-1, 3**3).T
    redo = col2im_nd(out, (3, 3, 3), (10, 10, 10), (2, 2, 2))
    assert_allclose(a, redo)

    a = np.random.rand(100000).reshape(10, 100, 10, 10)
    out = extract_patches(a, (3, 3, 3, 10),
                          (1, 1, 1, 10)).reshape(-1, 3**3 * 10).T
    redo = col2im_nd(out, (3, 3, 3, 10), (10, 100, 10, 10), (2, 2, 2, 0))
    assert_allclose(a, redo)

    out = im2col_nd(a, (3, 3, 3, 10), (2, 2, 2, 0))
    redo = col2im_nd(out, (3, 3, 3, 10), (10, 100, 10, 10), (2, 2, 2, 0))
    assert_allclose(a, redo)

    a = np.random.rand(1000).reshape(10, 10, 10)
    out = im2col_nd(a, (2, 2, 2), (0, 0, 0))
    redo = col2im_nd(out, (2, 2, 2), (10, 10, 10), (0, 0, 0))
    assert_allclose(a, redo)

    out = extract_patches(a, (2, 2, 2), (2, 2, 2)).reshape(-1, 2**3).T
    redo = col2im_nd(out, (2, 2, 2), (10, 10, 10), (0, 0, 0))
    assert_allclose(a, redo)

    a = np.random.rand(10000).reshape(10, 10, 10, 10)
    out = im2col_nd(a, (2, 2, 2, 10), (0, 0, 0, 0))
    redo = col2im_nd(out, (2, 2, 2, 10), (10, 10, 10, 10), (0, 0, 0, 0))
    assert_allclose(a, redo)

    out = extract_patches(a, (2, 2, 2, 10),
                          (1, 1, 1, 10)).reshape(-1, 2**3 * 10).T
    redo = col2im_nd(out, (2, 2, 2, 10), (10, 10, 10, 10), (0, 0, 0, 0))
    assert_allclose(a, redo)