andrewssobral / lrslibrary

Low-Rank and Sparse Tools for Background Modeling and Subtraction in Videos
826 stars 376 forks source link

matrix completion usage? #4

Closed tachim closed 7 years ago

tachim commented 7 years ago

Hey, this is an extremely impressive library in terms of coverage. Can you explain a bit, though, how to use the matrix completion routines? Suppose I have a matrix M that has NaNs where the entries are missing and I want to run PG-RMC: out = run_algorithm('MC', 'PG-RMC', M, good_idxs); yields

06:35:26 Running Matrix Completion with Nearly Optimal Robust matrix Completion (Cherapanamjeri et al. 2016)
Index exceeds matrix dimensions.

Error in spdiags (line 102)
         a((len(k)+1):len(k+1),:) = [i i+d(k) B(i+(m>=n)*d(k),k)];

Error in lanbpro (line 664)
B_k = spdiags([alpha(1:k) [beta(2:k);0]],[0 -1],k,k);

Error in lansvd (line 250)
        [U,B,V,p,ierr,w] = lanbpro(A,Atrans,m,n,j,p,options,U,B,V,anorm);

Error in fastsvd (line 11)
  [U,S,V] = lansvd(myAfunc,myAtfunc, m,n,k,'L');

Error in ncrmc (line 53)
    [U_t, Sig_t, V_t] = fastsvd(U_t, Sig_t, V_t, (1 / p) * (D_t - S_t), r, c, r_hat + 1);

Error in run_mc (line 6)
  [U_t, SV_t] = ncrmc(M, params.Idx);

Error in run_alg (line 7)
L = run_mc(params);

Error in run_algorithm (line 47)
  run_alg;

where the NaNs were replaced by 0's and goodIdxs is a boolean matrix corresponding to whether or not an entry was non-NaN in the original matrix.

How is the function supposed to be used?

andrewssobral commented 7 years ago

Hi @tachim , thank you for reporting this issue. I fixed it in the current version of the lrslibrary (committed today). Now you can run the matrix completion algorithms by:

obs = 0.5; % Percentage of observed entries [0...1]
[params.Idx, params.Omega] = subsampling(M, obs);
out = run_algorithm('MC', 'GROUSE', M, params);

where M is your input matrix, params.Idx is a vector of observed indexes and params.Omega is a binary matrix/tensor ('1's represent the observed entries and '0' otherwise). subsampling(.) is a function that performs a random subsampling over M and obs is the percentage of observed entries [0...1]. You can also set manually your own observation matrix by:

params.Omega = randi([0 1],size(M)); % '0's and '1's are generated from a uniform discrete distribution.
out = run_algorithm('MC', 'GROUSE', M, params);

or by:

params.Idx = find(randi([0 1],size(M))); % observed indexes.
out = run_algorithm('MC', 'GROUSE', M, params);

Note that you don't need to set params.Idx and params.Omega at same time. If params.Idx and params.Omega are not defined, the run_algorithm function sets them automatically using subsampling function with obs = 0.5.

Please let me know if it works well for you, Best regards, Andrews