Beispiel #1
0
def ksvd_image(filename, rank):  # image compression
    path, ext = os.path.splitext(filename)
    image = Image.open(filename)
    w = image.width
    h = image.height
    gray_image = image.convert('L')
    a = np.asarray(gray_image)
    ksvd = KSVD(rank=rank)
    A, X = ksvd.fit(a)
    image = Image.fromarray(np.uint8(A.dot(X)))
    print(np.sum(A > 0))
    file = path + '_r' + str(rank) + '_ksvd' + ext
    image.save(file)
    print('Saved as ' + file)
def evaluate_case(c, t):
    print(c)
    X = scl.generate_X(c.m, c.n, c.dict_size, c.target_sparsity, 13)
    ksvd_result = KSVD(X.transpose(),
                       c.dict_size,
                       c.target_sparsity,
                       c.iterations,
                       print_interval=20,
                       enable_printing=False,
                       enable_threading=True,
                       D_init='random')
    rmse1 = scl.rmse(X, ksvd_result[0].transpose(), ksvd_result[1].transpose())
    scl_result = scl.sparse_code(X,
                                 c.dict_size,
                                 c.target_sparsity,
                                 c.iterations,
                                 sparse_updater=c.sparse_updater,
                                 dict_updater=c.dict_updater)
    print(scl_result[2])
    rmse2 = scl.rmse(X, scl_result[0], scl_result[1])
    print('ksvd rmse:' + str(rmse1))
    print('scl rmse:' + str(rmse2))
    assert (approx_equal(rmse1, rmse2, t))

    check_sparsity(scl_result[1], c.target_sparsity, 0.1)
    check_D(scl_result[0])
Beispiel #3
0
def main():
    # 画像からパッチを抽出
    img = io.imread('./figure/barbara.png', as_grey=True)
    patch_size = 8
    patches = extract_patch(img, patch_size)

    # 2 次元分離可能 DCT 辞書を作成
    A_1D = np.zeros((8, 11))
    for k in range(11):
        for i in range(8):
            A_1D[i, k] = np.cos(i * k * np.pi / 11)

        if k != 0:
            A_1D[:, k] -= A_1D[:, k].mean()

    A_2D = np.kron(A_1D, A_1D)

    # 抽出したパッチで学習する
    idx = np.random.randint(0, patches.shape[0], int(patches.shape[0] / 10))
    Y = patches[idx]
    Y = Y.reshape(len(idx), 64).swapaxes(0, 1)

    sig = 0
    k0 = 4
    iter_num = 50
    
    barbara_dic, barbara_log = KSVD(Y, A_2D.shape[1], k0, sig, iter_num, initial_dictonary=A_2D.copy())
    show_dictionary(barbara_dic, './figure/ksvd_barbara_dic.png')
    np.savetxt('./result.txt', barbara_log)
Beispiel #4
0
    def denoise(self, image_file, out_image_file):
        img = util.img_as_float(io.imread(image_file))
        patches = image.extract_patches_2d(img, self.patch_size)
        signals = patches.reshape(patches.shape[0], -1)
        mean = np.mean(signals, axis=1)[:, np.newaxis]
        signals -= mean

        ksvd = KSVD(k_atoms=32, num_iterations=10, tolerance=0.000001)
        D, X = ksvd.run(signals[:self.optimal_fit_size].T)

        X = ksvd.sparse_coding(D, signals.T)

        reduced = (D.dot(X)).T + mean
        reduced_img = image.reconstruct_from_patches_2d(
            reduced.reshape(patches.shape), img.shape)
        io.imsave(out_image_file, clip(reduced_img))
Beispiel #5
0
def ksvd_random_signal():  # random signal test
    A = np.random.randn(30, 60)
    for j in range(A.shape[1]):
        A[:, j] /= np.linalg.norm(A[:, j])

    X = np.zeros((A.shape[1], 4000))
    candidate = np.array(list(range(X.shape[1])))

    for j in range(X.shape[1]):
        marker = np.random.choice(candidate, 4, replace=False)

    X[:, marker] = np.random.normal(0.0, 1.0, 4)
    Y_a = X + np.random.normal(0.0, 0.1, X.shape)

    ksvd = KSVD(rank=np.linalg.matrix_rank(Y_a.T), num_of_NZ=4)
    A, X = ksvd.fit(Y_a)
    return A, X
Beispiel #6
0
def get_psi(args, samples=10000, iterator=100):
    X = utils.load_raw(args)

    X = X[:samples, :]

    X_temp = np.array([np.max(X[args.seq_len_x + i: \
        args.seq_len_x + i + args.seq_len_y], axis=0) for i in range(samples - args.seq_len_x - args.seq_len_y)]).T

    size_D = int(math.sqrt(X.shape[1]))

    D = RandomDictionary(size_D, size_D)

    psi, _ = KSVD(D, MatchingPursuit,
                  int(args.random_rate / 100 * X.shape[1])).fit(
                      X_temp, iterator)

    return psi
Beispiel #7
0
def main():
    # 画像からパッチを抽出
    img = io.imread('./figure/babara.png', as_grey=True)
    print(img.shape)
    patch_size = 8
    patches = extract_patch(img, patch_size)
    print(patches.shape)
    
    # 2 次元分離可能 DCT 辞書を作成
    A_1D = np.zeros((8, 11))
    for k in range(11):
        for i in range(8):
            A_1D[i, k] = np.cos(i * k * np.pi / 11)

        if k != 0:
            A_1D[:, k] -= A_1D[:, k].mean()

    A_2D = np.kron(A_1D, A_1D)

    print(A_2D.shape)

    # 抽出したパッチで学習する
    idx = np.random.randint(0, patches.shape[0], int(patches.shape[0] / 10))
    Y = patches[idx]
    Y = Y.reshape(len(idx), 64).swapaxes(0, 1)
    print(Y.shape)
    sig = 0
    k0 = 4
    iter_num = 50
    
    babara_dic, babara_log = KSVD(Y, A_2D.shape[1], k0, sig, iter_num, initial_dictonary=A_2D)
    show_dictionary(babara_dic, './figure/ksvd_babara_dic.png')

    plt.plot(barbara_log, label='K-SVD')
    plt.ylabel('mean error')
    plt.xlabel('# of iteration')
    plt.legend(loc='best')
    plt.grid()
    plt.savefig('barbara_K-SVD.png', dpi=220)
Beispiel #8
0
from ksvd import KSVD
import numpy.random as rn
from numpy import array, zeros, dot, load

if __name__ == "__main__":

    dict_size = 250
    target_sparsity = 50
    n_examples = 500
    dimension = 96

    X = load('y1dump.npy').T

    KSVD(X, dict_size, target_sparsity, 500, print_interval=25, D_init="svd")
Beispiel #9
0
from ksvd import KSVD
import numpy.random as rn
from numpy import array, zeros, dot

if __name__ == "__main__":

    D = array([[1, 0, 1, 0], [-1, 0, 0, 1], [1, 1, 1, 2]])

    rs = rn.RandomState(0)

    p = D.shape[1]
    d = D.shape[0]
    n = 10

    M = zeros((n, D.shape[0] + 1))

    M[:, :2] = rs.normal(size=(n, 2))

    M = M.ravel()[:n * D.shape[0]].reshape(n, D.shape[0])

    X = dot(M, D)

    KSVD(X, 3, 2, 500, print_interval=1)
Beispiel #10
0
from ksvd import KSVD
import numpy.random as rn
from numpy import array, zeros, dot

if __name__ == "__main__":

    factor = 2

    dict_size = 5
    target_sparsity = 3
    n_examples = 10
    dimension = 4

    rs = rn.RandomState(0)

    D = rs.normal(size=(dict_size, dimension))

    M = zeros((n_examples, dict_size + 1))

    M[:, :target_sparsity] = rs.normal(size=(n_examples, target_sparsity))

    M = M.ravel()[:n_examples * dict_size].reshape(n_examples, dict_size)

    X = dot(M, D)

    KSVD(X, dict_size, target_sparsity, 1000)
Beispiel #11
0
    # D, Gamma = KSVD(X, dict_size, target_sparsity, 1000,
    #                 print_interval = 80,
    #                 enable_printing = True, enable_threading = True)

    # np.savetxt('ksvd8_g_ds32.txt', D)
    # gc.collect()

    # ### B ###
    # patches = image.extract_patches_2d(img_train[:,:,2], patch_size)
    # X = patches.reshape(patches.shape[0], -1)[::100]
    # print(X.shape)

    # D, Gamma = KSVD(X, dict_size, target_sparsity, 1000,
    #                 print_interval = 80,
    #                 enable_printing = True, enable_threading = True)

    # Treinando tudo junto
    patches = image.extract_patches_2d(img_train, patch_size)
    X = patches.reshape(patches.shape[0], -1)[::100]
    print(X.shape)

    D, Gamma = KSVD(X,
                    dict_size,
                    target_sparsity,
                    1000,
                    print_interval=80,
                    enable_printing=True,
                    enable_threading=True)

    np.savetxt('ksvd8_rgb_ds128.txt', D)
Beispiel #12
0
    dict_size = 1024
    target_sparsity = 8
    n_examples = 153000
    dimension = 512

    rs = rn.RandomState(0)

    D = rs.normal(size=(dict_size, dimension))

    M = zeros((n_examples, dict_size + 1))

    M[:, :target_sparsity] = rs.normal(size=(n_examples, target_sparsity))

    M = M.ravel()[:n_examples * dict_size].reshape(n_examples, dict_size)

    X = dot(M, D)

    del M
    del D

    print "X generated."

    KSVD(X,
         dict_size,
         target_sparsity,
         50,
         print_interval=25,
         grad_descent_iterations=1,
         enable_printing=True,
         enable_threading=True)
Beispiel #13
0
    rs = rn.RandomState(0)

    target_sparsity = 32
    p = D.shape[1]
    d = D.shape[0]
    n = 1000

    M = zeros( (n, D.shape[0] + 1) )

    M[:, :target_sparsity] = rs.normal(size = (n, target_sparsity) )
    
    M = M.ravel()[:n*D.shape[0]].reshape(n, D.shape[0])

    X = dot(M, D)

    #KSVD(X,dict_size, target_sparsity,max_iterations,D_init = 'random', enable_printing = False, print_interval = 25, grad_descent_iterations = 2, convergence_check_interval = 50, convergence_threshhold = 0, enable_eigen_threading = False, enable_threading = True, enable_32bit_initialization = True, max_initial_32bit_iterations = 0)\n    \n    Runs an approximate KSVD algorithm using batch orthogonal matching pursuit.\n\n    :`X`:\n      n by p matrix of signals, where `n` is the number of signals and\n      p is the dimension of each signal.\n\n    :`dict_size`:\n      The size of the target dictionary.\n\n    :`target_sparsity`:\n      The target sparsity of the signal.\n\n    :`max_iterations`:\n      The maximum number of iterations to perform.  Generally takes 500-5000.\n    \n    :`D_init`:\n      Initialization mode of the dictionary.  If a `dict_size` by `p`\n      array is given, then this is used to initialize the dictionary.\n      Otherwise, if ``D_init == 'random'``, It is initialized\n      randomly.  If ``D_init == 'svd'`` (default), part of the\n      dictionary is initialized using a singular value decomposition\n      of the signal.  This can give faster convergence in some cases,\n      but hits a bad local minimum in others.\n\n    :`enable_printing`:\n      If True, prints periodic status messages about the\n      convergence. (default = False).\n\n    :`print_interval`:\n      How often to print convergence information.\n\n    :`grad_descent_iterations`:\n      The number of gradient descent steps used to approximate the\n      primary svd vectors at each iteration.  Default = 2.\n\n    :`convergence_check_interval`:\n      How often to check convergence.  This step can be expensive (default = 50).\n\n    :`convergence_threshhold`:\n      When the approximation accuracy falls below this, the algorithm\n      terminates.  Approximation accuracy is measured by\n      ``||X - D * Gamma||_2 / n``, where ``n`` is the number of signals.\n      If 0"" (default), the algorithm runs for ``max_iterations`` or to\n      machine epsilon, whichever comes first.\n\n    :`enable_eigen_threading`:\n      Whether to enable threading in linear algebra operations in the\n      Eigen libraries.  This is recommended only for very large\n      problems. (default = False).\n    \n    :`enable_threading`:\n      Whether to enable threading in calculating the sparse\n      projections in the Batch OMP step.  Generally, this can give\n      substantial speedup. (default = True).\n    \n    :`enable_32bit_initialization`:\n      Whether to process as much as possible using the faster 32bit\n      mode.  This is generally recommended, as the accuracy is\n      typically good enough for the start of most problems.  Once the\n      accuracy falls below what 32bit floats can accurately determine,\n      the computation switches to 64bit.\n    \n    :`max_initial_32bit_iterations`:\n      The maximum number of 32 bit iterations to do before switching\n      to 64bit mode.  If 0 (default), no limit.\n\n    Returns tuple (D, Gamma), where X \\simeq Gamma * D.\n    ";
    [dic, gamma] = KSVD(X, p, target_sparsity, 100, print_interval = 5)
    
    r = dot( gamma, dic)
    
    print dic.shape  
    print gamma.shape

    imgNoised = io.imread("./lena_sp.jpg", True);
    viewer = ImageViewer(imgNoised)
    viewer.show() 
   
    
    res = KSVD_Encode(imgNoised, dic, target_sparsity)
    #print res
    
Beispiel #14
0
def pareto():
    #p = Parameters(1000, 200, 300, 50, 10, 'agd')
    p = Parameters(2000, 200, 300, 50, 10, 'gd')
    #p = Parameters(1000, 80, 100, 20, 10, 'agd')
    labels = sparse_updaters[:]
    labels.append('ksvd')

    iter_times = []
    iter_accuracy = []
    iterations = 20
    for iter in range(0, iterations):
        times = []
        accuracy = []
        X = scl.generate_X(p.m, p.n, p.dict_size, p.target_sparsity, iter)
        # Warmup
        scl.sparse_code(X,
                        p.dict_size,
                        p.target_sparsity,
                        p.iterations,
                        metric='pyksvd',
                        dict_updater=p.dict_updater)
        for updater in sparse_updaters:
            tmp = time.time()
            result = scl.sparse_code(X,
                                     p.dict_size,
                                     p.target_sparsity,
                                     p.iterations,
                                     metric='pyksvd',
                                     dict_updater=p.dict_updater,
                                     sparse_updater=updater)
            times.append(time.time() - tmp)
            accuracy.append(scl.rmse(X, result[0], result[1]))

        tmp = time.time()
        ksvd_result = KSVD(X.transpose(),
                           p.dict_size,
                           p.target_sparsity,
                           p.iterations,
                           print_interval=5,
                           enable_printing=True,
                           enable_threading=True,
                           D_init='random')
        times.append(time.time() - tmp)
        accuracy.append(
            scl.rmse(X, ksvd_result[0].transpose(),
                     ksvd_result[1].transpose()))
        iter_times.append(times)
        iter_accuracy.append(accuracy)

    #Average iterations
    times = [float(sum(col)) / len(col) for col in zip(*iter_times)]
    accuracy = [float(sum(col)) / len(col) for col in zip(*iter_accuracy)]

    #Plot
    plt.gca().set_position((.1, .3, .8, .6))
    plt.scatter(times, accuracy)
    plt.xlabel('time(s)')
    plt.ylabel('rmse')
    plt.title('Accuracy vs. Execution Time for Sparse Updaters')
    for label, x, y in zip(labels, times, accuracy):
        plt.annotate(label, xy=(x, y))
    plt.figtext(.02, .02, str(p))
    plt.show()
Beispiel #15
0
    dict_size = 1024
    target_sparsity = 8
    n_examples = 153000
    dimension = 512

    rs = rn.RandomState(0)

    D = rs.normal(size=(dict_size, dimension))

    M = zeros((n_examples, dict_size + 1))

    M[:, :target_sparsity] = rs.normal(size=(n_examples, target_sparsity))

    M = M.ravel()[:n_examples * dict_size].reshape(n_examples, dict_size)

    X = dot(M, D)

    del M
    del D

    print("X generated.")

    KSVD(X,
         dict_size,
         target_sparsity,
         50,
         print_interval=25,
         enable_printing=True,
         enable_threading=True)
Beispiel #16
0
wcon = ConfigObj("config.ini")["word2vec"]
kcon = ConfigObj("config.ini")["kSVD"]

from gensim.models.word2vec import Word2Vec

f_features = wcon["f_features"].format(**wcon)
clf = Word2Vec.load(f_features)
X = clf.syn0

print clf
print X.shape

result = KSVD(X,
              dict_size=kcon.as_int("basis_size"),
              target_sparsity=kcon.as_int("sparsity"),
              max_iterations=kcon.as_int("iterations"),
              enable_printing=True,
              enable_threading=True,
              print_interval=1)

D, gamma = result

f_model = kcon["f_kSVD"].format(**kcon)

h5 = h5py.File(f_model, 'w')
h5.create_dataset("D", data=D, compression="gzip")
h5.create_dataset("gamma", data=gamma, compression="gzip")

# Save the arguments (maybe later?)
#for key in args:
#    g.attrs[key] = cargs[key]