Exemple #1
0
def testDictionary(NFactors, K, lam, showDict=True):
    import spams
    from VideoTools import loadImageIOVideo
    import time
    (I, IDims) = loadImageIOVideo("jumpingjacks2men.ogg")

    #Step 1: Build dictionary based on first frame
    F1 = np.reshape(I[0, :], IDims)
    P = getColorPatchesImageSet([F1], K, getPatches)
    ShapeBefore = P.shape[1::]
    P = np.reshape(P, [P.shape[0] * P.shape[1] * P.shape[2], P.shape[3]])
    print("Doing NNSC for %i factors on %i patches..." %
          (NFactors, P.shape[0]))
    tic = time.time()
    U = spams.nnsc(P.T,
                   lambda1=lam,
                   return_lasso=False,
                   K=NFactors,
                   numThreads=8)
    print("Elapsed Time: %g" % (time.time() - tic))
    if showDict:
        k = int(np.sqrt(NFactors))
        for i in range(k * k):
            plt.subplot(k, k, i + 1)
            p = unwrapColorPatch(U[:, i], K)
            p = p / np.max(p)
            plt.imshow(p, interpolation='none')
            plt.axis('off')
        plt.savefig("Dict_%i_%i_%.3g.png" % (NFactors, K, lam),
                    bbox_inches='tight')

    #Step 2: Represent another frame in the constructed dictionary
    F2 = np.reshape(I[10, :], IDims)
    P2 = getColorPatchesImageSet([F2], K, getPatches)
    ShapeBefore = P2.shape[1::]
    P2 = np.reshape(P2, [P2.shape[0] * P2.shape[1] * P2.shape[2], P2.shape[3]])
    print("Solving for factors")
    tic = time.time()
    Alpha = spams.lasso(P2.T, U, lambda1=lam, pos=True, numThreads=8)
    print("Elapsed Time: %g" % (time.time() - tic))
    P2New = np.reshape((U * Alpha).T, ShapeBefore)
    F2Reconstructed = recombineColorPatches(P2New, K, F2.shape[0], F2.shape[1])
    writeImage(F2, "Orig.png")
    writeImage(F2Reconstructed,
               "Reconstructed_%i_%i_%.3g.png" % (NFactors, K, lam))
    print("%i Factors, Avg nonzero elems: %.3g" %
          (NFactors,
           NFactors * Alpha.size / float(Alpha.shape[0] * Alpha.shape[1])))
Exemple #2
0
    def online_NMF(self,n_components=30,method='nnsc',lambda1=100,iterations=-5,batchsize=512,model=None,**kwargs):
        """ Method performing online matrix factorization and using the spams (http://spams-devel.gforge.inria.fr/doc-python/html/index.html) package from Inria.
        Implements bith the nmf and nnsc methods

        Parameters
        ----------
        n_components: int

        method: 'nnsc' or 'nmf' (see http://spams-devel.gforge.inria.fr/doc-python/html/index.html)

        lambda1: see http://spams-devel.gforge.inria.fr/doc-python/html/index.html
        iterations: see http://spams-devel.gforge.inria.fr/doc-python/html/index.html
        batchsize: see http://spams-devel.gforge.inria.fr/doc-python/html/index.html
        model: see http://spams-devel.gforge.inria.fr/doc-python/html/index.html
        **kwargs: more arguments to be passed to nmf or nnsc

        Return:
        -------
        time_comps
        space_comps
        """
        try:
            import spams
        except:
            print("You need to install the SPAMS package")
            raise

        T,d1,d2=np.shape(self)
        d=d1*d2
        X=np.asfortranarray(np.reshape(self,[T,d],order='F'))

        if method == 'nmf':
            (time_comps,V) = spams.nmf(X,return_lasso= True ,K = n_components,numThreads=4,iter = iterations,**kwargs)

        elif method == 'nnsc':
            (time_comps,V) = spams.nnsc(X,return_lasso=True,K=n_components, lambda1 = lambda1,iter = iterations, model = model, **kwargs)
        else:
            raise Exception('Method unknown')

        space_comps=[]

        for idx,mm in enumerate(V):
            space_comps.append(np.reshape(mm.todense(),(d1,d2),order='F'))

        return time_comps,np.array(space_comps)
Exemple #3
0
else:
    assign = [
        np.loadtxt(x, ndmin=2) for x in files.glob('*mutation_assignment*')
    ]
    ccms = [np.dot(x, x.T) for x in assign]
    CCM = sum(ccms)
    counts = [sum(x.T)[:, None] for x in assign]
    counting = sum([np.dot(x, x.T) for x in counts])
    X = np.round(CCM / (1e-12 + counting), 3)
    X = np.asfortranarray(X / np.tile(np.sqrt((X * X).sum(axis=0)),
                                      (X.shape[0], 1)),
                          dtype=float)
    # SPAMS
    (U, V) = spams.nnsc(X,
                        return_lasso=True,
                        K=int(NCL),
                        numThreads=-1,
                        iter=iters,
                        lambda1=0)
    lab = list(map(np.argmax, U))
    label = np.array(lab)
    labels = np.unique(label)
    NCL = len(labels)
    ssm = [np.sum(label == x) for x in labels]
    tmp = [np.mean(phi[label == x]) for x in labels]
    suma = np.zeros((NCL, 3))
    suma[:, 0] = labels
    suma[:, 1] = ssm
    suma[:, 2] = tmp
    suma = suma[np.argsort(suma[:, 2]), ]
    refine = False
    if NCL > 1 and any((suma[1:, 2] - suma[:-1, 2]) < 0.05):
Exemple #4
0
#%%
mcr = cm.load(fname)

ycr = mcr.to_2D() 

ycr = ycr - ycr.min()

mcr_lc = local_correlations_movie_offline(fname)
ycr_lc = mcr_lc.to_2D()
#%%
D_lc,tr_lc = spams.nmf(np.asfortranarray(ycr_lc.T), K=2, return_lasso=True)   
plt.figure();plt.plot(tr_lc.T.toarray()) 
plt.figure();plt.imshow(D_lc[:,0].reshape(mcr.shape[1:], order='F'), cmap='gray')
#%%
D,tr = spams.trainDL(np.asfortranarray(ycr.T), K=2, D=D_lc, lambda1=0)


#%%
D,tr = spams.nnsc(np.asfortranarray(ycr.T), K=2, return_lasso=True, lambda1=0)
#%%
D,tr = spams.nmf(np.asfortranarray(np.abs(ycr.T)), K=2, return_lasso=True) 
#%%
D,tr = spams.nnsc(np.asfortranarray(ycr.T), K=2, return_lasso=True, lambda1=1)
#%%
plt.figure();plt.plot(tr.T.toarray()) 
plt.figure();plt.imshow(D[:,1].reshape(mcr.shape[1:], order='F'), cmap='gray');plt.title('comp1') 
plt.figure();plt.imshow(D[:,0].reshape(mcr.shape[1:], order='F'), cmap='gray');plt.title('comp0')

#%%
plt.figure();plt.plot(tr.T.toarray()) 
plt.figure();plt.imshow(D[:,1].reshape(mcr.shape[1:], order='F'), cmap='gray') 
    yr = np.reshape(m1, [T, d], order='F').T


X = np.asfortranarray(yr, dtype=myfloat)
########## FIRST EXPERIMENT ###########
tic = time.time()
# if you want to use NMF uncomment the following line
#(U,V) = spams.nmf(X,return_lasso= True,K = n_components,iter = -5)

# regularizer on space components
#spams.trainDL(X,return_model = True,D=np.asfortranarray(V.todense().T),posAlpha=True,posD=True,modeD=3,gamma1=.001,lambda1=3000,lambda2=0,mode=spams.spams_wrap.PENALTY,iter=-5)
if not use_pixels_as_basis:
    #    (D,model)=spams.trainDL(X,return_model = True,K=n_components,posAlpha=True,posD=True,modeD=3,gamma1=.001,lambda1=2000,lambda2=0,mode=spams.spams_wrap.PENALTY,iter=-5)
    #    U=model['B']
    #    V=model['A']
    (U, V) = spams.nnsc(X, return_lasso=True,
                        K=n_components, lambda1=1000, iter=-5)
else:
    (U, V) = spams.nnsc(X, return_lasso=True,
                        K=n_components, lambda1=1000, iter=-5)


#(U,V) = spams.nnsc(np.asfortranarray(yr.T,dtype = myfloat),return_lasso=True,K=15,lambda1=100,iter=-5)
# model=dict()
# model['A']=np.asfortranarray(V.todense().T)
# model['B']=np.asfortranarray(U)
# model['iter']=100
#(D,model) = spams.trainDL(X,return_model = True,D=np.asfortranarray(V.todense().T),posAlpha=True,posD=True,modeD=3,gamma1=.001,lambda1=3000,lambda2=0,mode=spams.spams_wrap.PENALTY,iter=-5)


if use_pixels_as_basis:
    comp = V.todense()
Exemple #6
0
    def online_NMF(self,
                   n_components=30,
                   method='nnsc',
                   lambda1=100,
                   iterations=-5,
                   batchsize=512,
                   model=None,
                   **kwargs):
        """ Method performing online matrix factorization and using the spams (http://spams-devel.gforge.inria.fr/doc-python/html/index.html) package from Inria.
        Implements bith the nmf and nnsc methods

        Parameters
        ----------
        n_components: int

        method: 'nnsc' or 'nmf' (see http://spams-devel.gforge.inria.fr/doc-python/html/index.html)

        lambda1: see http://spams-devel.gforge.inria.fr/doc-python/html/index.html
        iterations: see http://spams-devel.gforge.inria.fr/doc-python/html/index.html
        batchsize: see http://spams-devel.gforge.inria.fr/doc-python/html/index.html
        model: see http://spams-devel.gforge.inria.fr/doc-python/html/index.html
        **kwargs: more arguments to be passed to nmf or nnsc

        Return:
        -------
        time_comps
        space_comps
        """
        try:
            import spams
        except:
            print "You need to install the SPAMS package"
            raise

        T, d1, d2 = np.shape(self)
        d = d1 * d2
        X = np.asfortranarray(np.reshape(self, [T, d], order='F'))

        if method == 'nmf':
            (time_comps, V) = spams.nmf(X,
                                        return_lasso=True,
                                        K=n_components,
                                        numThreads=4,
                                        iter=iterations,
                                        **kwargs)

        elif method == 'nnsc':
            (time_comps, V) = spams.nnsc(X,
                                         return_lasso=True,
                                         K=n_components,
                                         lambda1=lambda1,
                                         iter=iterations,
                                         model=model,
                                         **kwargs)
        else:
            raise Exception('Method unknown')

        space_comps = []

        for idx, mm in enumerate(V):
            space_comps.append(np.reshape(mm.todense(), (d1, d2), order='F'))

        return time_comps, np.array(space_comps)
        fout.write(line)
        foutD.write(line)
        line = fin.readline()
        X = [float(a) for a in line.split(",")[0:-1]]
        X = np.reshape(X, [len(X)/12, 12])
        ND = X.shape[0] - BeatsPerBlock + 1
        if ND <= 0:
            fout.write("\n")
            foutD.write("\n")
            print "ND = 0"
            continue
        print "(NPixels, ND) = (%i, %i)"%(NPixels, ND)
        OutDs = np.zeros((NPixels, ND))
        for i in range(ND):
            x = X[i:i+BeatsPerBlock, :]
            D = getSSM(x, DPixels)
            OutDs[:, i] = D[I < J]
        Dict = spams.nnsc(np.asfortranarray(OutDs), lambda1 = 1, K = NDictElems)
        #Output colomn-major
        Dict = (Dict.T).flatten()
        OutDs = (OutDs.T).flatten()
        np.savetxt(fout, OutDs, fmt="%g", newline=",")
        fout.write("\n")
        np.savetxt(foutD, Dict, fmt="%g", newline=",")
        foutD.write("\n")
        line = fin.readline()
        idx += 1
    fin.close()
    fout.close()
    foutD.close()