├── Frontal2Lateral.m
├── LICENSE
├── Lateral2Frontal.m
├── PSNR.m
├── README.md
├── lrtc_tnn.m
├── lrtr_Gaussian_tnn.m
├── prox_tnn.m
├── run_image_inpainting.m
├── run_lrtr_Gaussian_tnn.m
├── run_ltrc_tnn.m
├── testimg.jpg
├── tprod.m
└── tubalrank.m
/Frontal2Lateral.m:
--------------------------------------------------------------------------------
1 | function Y = Frontal2Lateral(X)
2 |
3 | % Convert the frontal slices of a tensor to the lateral slices of a new tensor
4 | %
5 | % version 1.0 - 11/02/2018
6 | %
7 | % Written by Canyi Lu (canyilu@gmail.com)
8 | %
9 |
10 | [n1,n2,n3] = size(X);
11 | Y = zeros(n1,n3,n2);
12 |
13 | for i = 1 : n2
14 | Z = squeeze(X(:,i,:));
15 | Y(:,:,i) = Z;
16 | end
17 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2018 Canyi Lu
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/Lateral2Frontal.m:
--------------------------------------------------------------------------------
1 | function B = Lateral2Frontal(A)
2 | %
3 | % Written by Canyi Lu (canyilu@gmail.com)
4 | %
5 | [n1,n3,n2] = size(A);
6 | B = zeros(n1,n2,n3);
7 | for i = 1 : n3
8 | slice = A(:,i,:);
9 | B(:,:,i) = reshape(slice,n1,n2);
10 | end
11 |
--------------------------------------------------------------------------------
/PSNR.m:
--------------------------------------------------------------------------------
1 | function psnr = PSNR(Xfull,Xrecover,maxP)
2 | %
3 | % Written by Canyi Lu (canyilu@gmail.com)
4 | %
5 | Xrecover = max(0,Xrecover);
6 | Xrecover = min(maxP,Xrecover);
7 | [n1,n2,n3] = size(Xrecover);
8 | MSE = norm(Xfull(:)-Xrecover(:))^2/(n1*n2*n3);
9 | psnr = 10*log10(maxP^2/MSE);
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## Tensor completion and Tensor recovery from Gaussian measurements
2 |
3 | ### Introduction
4 |
5 | In our work [1], we give the exact recovery guarantees of tensor completion and tensor recovery from Gaussian measurements by tensor nuclear norm minimization. The tensor nuclear norm was proposed in our works [3][4]. A more general tensor nuclear norm undear general invertible linear transform was proposed in [5] and applied to tensor completion [5] and tensor robust PCA [6].
6 |
7 | We provide the codes of the following two models in [1].
8 |
9 | - Tensor completion by tensor nuclear norm minimization
10 |
11 |
12 |
13 |
14 | - Tensor recovery from Gaussian measurements by tensor nuclear norm minimization
15 |
16 |
17 |
18 |
19 |
20 |
21 | ### Related Toolboxes
22 |
28 |
29 |
30 |
31 |
32 | ### References
33 |
34 | - Canyi Lu, Jiashi Feng, Zhouchen Lin, Shuicheng Yan. Exact Low Tubal Rank Tensor Recovery from Gaussian Measurements. International Joint Conference on Artificial Intelligence (IJCAI). 2018
35 |
36 |
- Canyi Lu. Tensor-Tensor Product Toolbox. Carnegie Mellon University, June 2018. https://github.com/canyilu/tproduct.
37 |
38 |
- Canyi Lu, Jiashi Feng, Yudong Chen, Wei Liu, Zhouchen Lin, Shuicheng Yan. Tensor Robust Principal Component Analysis with A New Tensor Nuclear Norm. TPAMI. 2019
39 |
40 |
- Canyi Lu, Jiashi Feng, Yudong Chen, Wei Liu, Zhouchen Lin, Shuicheng Yan. Tensor Robust Principal Component Analysis: Exact Recovery of Corrupted Low-Rank Tensors via Convex Optimization. CVPR, 2016
41 |
42 |
- Canyi Lu, Xi Peng, Yunchao Wei. Low-Rank Tensor Completion With a New Tensor Nuclear Norm Induced by Invertible Linear Transforms. IEEE International Conference on Computer Vision and Pattern Recognition (CVPR), 2019
43 |
44 |
- Canyi Lu, Pan Zhou. Exact Recovery of Tensor Robust Principal Component Analysis under Linear Transforms. arXiv preprint arXiv:1907.08288. 2019
45 |
46 |
47 |
--------------------------------------------------------------------------------
/lrtc_tnn.m:
--------------------------------------------------------------------------------
1 | function [X,obj,err,iter] = lrtc_tnn(M,omega,opts)
2 |
3 | % Solve the Low-Rank Tensor Completion (LRTC) based on Tensor Nuclear Norm (TNN) problem by M-ADMM
4 | %
5 | % min_X ||X||_*, s.t. P_Omega(X) = P_Omega(M)
6 | %
7 | % ---------------------------------------------
8 | % Input:
9 | % M - d1*d2*d3 tensor
10 | % omega - index of the observed entries
11 | % opts - Structure value in Matlab. The fields are
12 | % opts.tol - termination tolerance
13 | % opts.max_iter - maximum number of iterations
14 | % opts.mu - stepsize for dual variable updating in ADMM
15 | % opts.max_mu - maximum stepsize
16 | % opts.rho - rho>=1, ratio used to increase mu
17 | % opts.DEBUG - 0 or 1
18 | %
19 | % Output:
20 | % X - d1*d2*d3 tensor
21 | % err - residual
22 | % obj - objective function value
23 | % iter - number of iterations
24 | %
25 | % version 1.0 - 25/06/2016
26 | %
27 | % Written by Canyi Lu (canyilu@gmail.com)
28 | %
29 | % References:
30 | % Canyi Lu, Jiashi Feng, Zhouchen Lin, Shuicheng Yan
31 | % Exact Low Tubal Rank Tensor Recovery from Gaussian Measurements
32 | % International Joint Conference on Artificial Intelligence (IJCAI). 2018
33 |
34 |
35 | tol = 1e-8;
36 | max_iter = 500;
37 | rho = 1.1;
38 | mu = 1e-4;
39 | max_mu = 1e10;
40 | DEBUG = 0;
41 |
42 | if ~exist('opts', 'var')
43 | opts = [];
44 | end
45 | if isfield(opts, 'tol'); tol = opts.tol; end
46 | if isfield(opts, 'max_iter'); max_iter = opts.max_iter; end
47 | if isfield(opts, 'rho'); rho = opts.rho; end
48 | if isfield(opts, 'mu'); mu = opts.mu; end
49 | if isfield(opts, 'max_mu'); max_mu = opts.max_mu; end
50 | if isfield(opts, 'DEBUG'); DEBUG = opts.DEBUG; end
51 |
52 | dim = size(M);
53 | X = zeros(dim);
54 | X(omega) = M(omega);
55 | E = zeros(dim);
56 | Y = E;
57 |
58 | iter = 0;
59 | for iter = 1 : max_iter
60 | Xk = X;
61 | Ek = E;
62 | % update X
63 | [X,tnnX] = prox_tnn(-E+M+Y/mu,1/mu);
64 | % update E
65 | E = M-X+Y/mu;
66 | E(omega) = 0;
67 |
68 | dY = M-X-E;
69 | chgX = max(abs(Xk(:)-X(:)));
70 | chgE = max(abs(Ek(:)-E(:)));
71 | chg = max([chgX chgE max(abs(dY(:)))]);
72 | if DEBUG
73 | if iter == 1 || mod(iter, 10) == 0
74 | obj = tnnX;
75 | err = norm(dY(:));
76 | disp(['iter ' num2str(iter) ', mu=' num2str(mu) ...
77 | ', obj=' num2str(obj) ', err=' num2str(err)]);
78 | end
79 | end
80 |
81 | if chg < tol
82 | break;
83 | end
84 | Y = Y + mu*dY;
85 | mu = min(rho*mu,max_mu);
86 | end
87 | obj = tnnX;
88 | err = norm(dY(:));
89 |
90 |
--------------------------------------------------------------------------------
/lrtr_Gaussian_tnn.m:
--------------------------------------------------------------------------------
1 | function [X,obj,err,iter] = lrtr_Gaussian_tnn(A,b,Xsize,opts)
2 |
3 | % Low tubal rank tensor recovery from Gaussian measurements by tensor
4 | % nuclear norm minimization
5 | %
6 | % min_X ||X||_*, s.t. A*vec(X) = b
7 | %
8 | % ---------------------------------------------
9 | % Input:
10 | % A - m*n matrix
11 | % b - m*1 vector
12 | % Xsize - Structure value in Matlab. The fields
13 | % (Xsize.n1,Xsize.n2,Xsize.n3) give the size of X.
14 | %
15 | % opts - Structure value in Matlab. The fields are
16 | % opts.tol - termination tolerance
17 | % opts.max_iter - maximum number of iterations
18 | % opts.mu - stepsize for dual variable updating in ADMM
19 | % opts.max_mu - maximum stepsize
20 | % opts.rho - rho>=1, ratio used to increase mu
21 | % opts.DEBUG - 0 or 1
22 | %
23 | % Output:
24 | % X - n1*n2*n3 tensor (n=n1*n2*n3)
25 | % obj - objective function value
26 | % err - residual
27 | % iter - number of iterations
28 | %
29 | % version 1.0 - 09/10/2017
30 | %
31 | % Written by Canyi Lu (canyilu@gmail.com)
32 | %
33 | % References:
34 | % Canyi Lu, Jiashi Feng, Zhouchen Lin, Shuicheng Yan
35 | % Exact Low Tubal Rank Tensor Recovery from Gaussian Measurements
36 | % International Joint Conference on Artificial Intelligence (IJCAI). 2018
37 |
38 |
39 | tol = 1e-8;
40 | max_iter = 1000;
41 | rho = 1.1;
42 | mu = 1e-6;
43 | max_mu = 1e10;
44 | DEBUG = 0;
45 |
46 | if ~exist('opts', 'var')
47 | opts = [];
48 | end
49 | if isfield(opts, 'tol'); tol = opts.tol; end
50 | if isfield(opts, 'max_iter'); max_iter = opts.max_iter; end
51 | if isfield(opts, 'rho'); rho = opts.rho; end
52 | if isfield(opts, 'mu'); mu = opts.mu; end
53 | if isfield(opts, 'max_mu'); max_mu = opts.max_mu; end
54 | if isfield(opts, 'DEBUG'); DEBUG = opts.DEBUG; end
55 |
56 | n1 = Xsize.n1;
57 | n2 = Xsize.n2;
58 | n3 = Xsize.n3;
59 | X = zeros(n1,n2,n3);
60 | Z = X;
61 | m = length(b);
62 | Y1 = zeros(m,1);
63 | Y2 = X;
64 | I = eye(n1*n2*n3);
65 | invA = (A'*A+I)\I;
66 | iter = 0;
67 | for iter = 1 : max_iter
68 | Xk = X;
69 | Zk = Z;
70 | % update X
71 | [X,Xtnn] = prox_tnn(Z-Y2/mu,1/mu);
72 | % update Z
73 | vecZ = invA*(A'*(-Y1/mu+b)+Y2(:)/mu+X(:));
74 | Z = reshape(vecZ,n1,n2,n3);
75 |
76 | dY1 = A*vecZ-b;
77 | dY2 = X-Z;
78 | chgX = max(abs(Xk(:)-X(:)));
79 | chgZ = max(abs(Zk(:)-Z(:)));
80 | chg = max([chgX chgZ max(abs(dY1)) max(abs(dY2(:)))]);
81 | if DEBUG
82 | if iter == 1 || mod(iter, 10) == 0
83 | obj = Xtnn;
84 | err = norm(dY1)^2+norm(dY2(:))^2;
85 | disp(['iter ' num2str(iter) ', mu=' num2str(mu) ...
86 | ', obj=' num2str(obj) ', err=' num2str(err)]);
87 | end
88 | end
89 |
90 | if chg < tol
91 | break;
92 | end
93 | Y1 = Y1 + mu*dY1;
94 | Y2 = Y2 + mu*dY2;
95 | mu = min(rho*mu,max_mu);
96 | end
97 | obj = Xtnn;
98 | err = norm(dY1)^2+norm(dY2(:))^2;
99 |
--------------------------------------------------------------------------------
/prox_tnn.m:
--------------------------------------------------------------------------------
1 | function [X,tnn,trank] = prox_tnn(Y,rho)
2 |
3 | % The proximal operator of the tensor nuclear norm of a 3 way tensor
4 | %
5 | % min_X rho*||X||_*+0.5*||X-Y||_F^2
6 | %
7 | % Y - n1*n2*n3 tensor
8 | %
9 | % X - n1*n2*n3 tensor
10 | % tnn - tensor nuclear norm of X
11 | % trank - tensor tubal rank of X
12 | %
13 | % version 2.1 - 14/06/2018
14 | %
15 | % Written by Canyi Lu (canyilu@gmail.com)
16 | %
17 | %
18 | % References:
19 | % Canyi Lu, Tensor-Tensor Product Toolbox. Carnegie Mellon University.
20 | % June, 2018. https://github.com/canyilu/tproduct.
21 | %
22 | % Canyi Lu, Jiashi Feng, Yudong Chen, Wei Liu, Zhouchen Lin and Shuicheng
23 | % Yan, Tensor Robust Principal Component Analysis with A New Tensor Nuclear
24 | % Norm, arXiv preprint arXiv:1804.03728, 2018
25 | %
26 |
27 | [n1,n2,n3] = size(Y);
28 | X = zeros(n1,n2,n3);
29 | Y = fft(Y,[],3);
30 | tnn = 0;
31 | trank = 0;
32 |
33 | % first frontal slice
34 | [U,S,V] = svd(Y(:,:,1),'econ');
35 | S = diag(S);
36 | r = length(find(S>rho));
37 | if r>=1
38 | S = S(1:r)-rho;
39 | X(:,:,1) = U(:,1:r)*diag(S)*V(:,1:r)';
40 | tnn = tnn+sum(S);
41 | trank = max(trank,r);
42 | end
43 | % i=2,...,halfn3
44 | halfn3 = round(n3/2);
45 | for i = 2 : halfn3
46 | [U,S,V] = svd(Y(:,:,i),'econ');
47 | S = diag(S);
48 | r = length(find(S>rho));
49 | if r>=1
50 | S = S(1:r)-rho;
51 | X(:,:,i) = U(:,1:r)*diag(S)*V(:,1:r)';
52 | tnn = tnn+sum(S)*2;
53 | trank = max(trank,r);
54 | end
55 | X(:,:,n3+2-i) = conj(X(:,:,i));
56 | end
57 |
58 | % if n3 is even
59 | if mod(n3,2) == 0
60 | i = halfn3+1;
61 | [U,S,V] = svd(Y(:,:,i),'econ');
62 | S = diag(S);
63 | r = length(find(S>rho));
64 | if r>=1
65 | S = S(1:r)-rho;
66 | X(:,:,i) = U(:,1:r)*diag(S)*V(:,1:r)';
67 | tnn = tnn+sum(S);
68 | trank = max(trank,r);
69 | end
70 | end
71 | tnn = tnn/n3;
72 | X = ifft(X,[],3);
73 |
--------------------------------------------------------------------------------
/run_image_inpainting.m:
--------------------------------------------------------------------------------
1 | % applying tnsor completion for image inpainting
2 |
3 | clear
4 | X = double(imread('testimg.jpg'));
5 | X = X/255;
6 | maxP = max(abs(X(:)));
7 | [n1,n2,n3] = size(X);
8 |
9 | p = 0.5; % sampling rate
10 |
11 | omega = find(rand(n1*n2*n3,1) tol);
44 |
--------------------------------------------------------------------------------