def test_trainDL_Memory(): img_file = 'lena.png' try: img = Image.open(img_file) except: print("Cannot load image %s : skipping test" %img_file) return None I = np.array(img) / 255. if I.ndim == 3: A = np.asfortranarray(I.reshape((I.shape[0],I.shape[1] * I.shape[2]))) rgb = True else: A = np.asfortranarray(I) rgb = False m = 8 n = 8 X = spams.im2col_sliding(A,m,n,rgb) X = X - np.tile(np.mean(X,0),(X.shape[0],1)) X = np.asfortranarray(X / np.tile(np.sqrt((X * X).sum(axis=0)),(X.shape[0],1))) X = np.asfortranarray(X[:,np.arange(0,X.shape[1],10)],dtype = myfloat) param = { 'K' : 200, # learns a dictionary with 100 elements 'lambda1' : 0.15, 'numThreads' : 4, 'iter' : 100} ############# FIRST EXPERIMENT ################## tic = time.time() D = spams.trainDL_Memory(X,**param) tac = time.time() t = tac - tic print('time of computation for Dictionary Learning: %f' %t) print('Evaluating cost function...') lparam = _extract_lasso_param(param) alpha = spams.lasso(X,D = D,**lparam) xd = X - D * alpha R = np.mean(0.5 * (xd * xd).sum(axis=0) + param['lambda1'] * np.abs(alpha).sum(axis=0)) print("objective function: %f" %R) #* ? DISPLAY ############# SECOND EXPERIMENT ################## tic = time.time() D = spams.trainDL(X,**param) tac = time.time() t = tac - tic print('time of computation for Dictionary Learning: %f' %t) print('Evaluating cost function...') alpha = spams.lasso(X,D = D,**lparam) xd = X - D * alpha R = np.mean(0.5 * (xd * xd).sum(axis=0) + param['lambda1'] * np.abs(alpha).sum(axis=0)) print("objective function: %f" %R) #* ? DISPLAY return None
def test_trainDL_Memory(): img_file = 'lena.png' try: img = Image.open(img_file) except: print("Cannot load image %s : skipping test" %img_file) return None I = np.array(img) / 255. if I.ndim == 3: A = np.asfortranarray(I.reshape((I.shape[0],I.shape[1] * I.shape[2]))) rgb = True else: A = np.asfortranarray(I) rgb = False m = 8;n = 8; X = spams.im2col_sliding(A,m,n,rgb) X = X - np.tile(np.mean(X,0),(X.shape[0],1)) X = np.asfortranarray(X / np.tile(np.sqrt((X * X).sum(axis=0)),(X.shape[0],1))) X = np.asfortranarray(X[:,np.arange(0,X.shape[1],10)],dtype = myfloat) param = { 'K' : 200, # learns a dictionary with 100 elements 'lambda1' : 0.15, 'numThreads' : 4, 'iter' : 100} ############# FIRST EXPERIMENT ################## tic = time.time() D = spams.trainDL_Memory(X,**param) tac = time.time() t = tac - tic print('time of computation for Dictionary Learning: %f' %t) print('Evaluating cost function...') lparam = _extract_lasso_param(param) alpha = spams.lasso(X,D = D,**lparam) xd = X - D * alpha R = np.mean(0.5 * (xd * xd).sum(axis=0) + param['lambda1'] * np.abs(alpha).sum(axis=0)) print("objective function: %f" %R) #* ? DISPLAY ############# SECOND EXPERIMENT ################## tic = time.time() D = spams.trainDL(X,**param) tac = time.time() t = tac - tic print('time of computation for Dictionary Learning: %f' %t) print('Evaluating cost function...') alpha = spams.lasso(X,D = D,**lparam) xd = X - D * alpha R = np.mean(0.5 * (xd * xd).sum(axis=0) + param['lambda1'] * np.abs(alpha).sum(axis=0)) print("objective function: %f" %R) #* ? DISPLAY return None
def sparseCoding(X): X = np.asfortranarray(X) param = { 'K' : NCLUSTER, # size of the dictionary 'lambda1' : 0.15, #'posD' : True, # dictionary positive constrain #'modeD' : 1, # L1 regulization regularization on D 'iter' : ITER} # runtime limit 15mins D = spams.trainDL_Memory(X,**param) lparam = _extract_lasso_param(param) print 'genrating codes...' alpha = spams.lasso(X,D = D,**lparam) return D, alpha
R = np.mean(0.5 * (xd * xd).sum(axis=0) + param['lambda1'] * np.abs(alpha).sum(axis=0)) print "objective function: %f" %R ############ img_file = '../extdata/portrait.png' try: img = Image.open(img_file) except: print "Cannot load image %s : skipping test" %img_file exit() I = np.array(img) / 255. if I.ndim == 3: A = np.asfortranarray(I.reshape((I.shape[0],I.shape[1] * I.shape[2])),dtype = myfloat) rgb = True else: A = np.asfortranarray(I,dtype = myfloat) rgb = False m = 8;n = 8; X = spams.im2col_sliding(A,m,n,rgb) X = X - np.tile(np.mean(X,0),(X.shape[0],1)) X = np.asfortranarray(X / np.tile(np.sqrt((X * X).sum(axis=0)),(X.shape[0],1))) X = np.asfortranarray(X[:,np.arange(0,X.shape[1],10)],dtype = myfloat) param = { 'K' : 200, # learns a dictionary with 100 elements 'lambda1' : 0.15, 'numThreads' : 4, 'iter' : 100} D = spams.trainDL_Memory(X,param) print "DTYPE %s" %str(D.dtype)