-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGsemiNMF_FilterBank.m
91 lines (74 loc) · 2.59 KB
/
GsemiNMF_FilterBank.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
function [V,Rx] = GsemiNMF_FilterBank(InImg, PatchSize, NumFilters)
% =======INPUT=============
% InImg Input images (cell structure)
% PatchSize the patch size, asumed to an odd number.
% NumFilters the number of PCA filters in the bank.锟斤拷NMF锟斤拷维锟斤拷
% =======OUTPUT============
% V PCA filter banks, arranged in column-by-column manner
% =========================
addpath('./Utils')
% to efficiently cope with the large training samples, if the number of training we randomly subsample 10000 the
% training set to learn PCA filter banks
ImgZ = length(InImg);
MaxSamples = 100000;
NumRSamples = min(ImgZ, MaxSamples);
RandIdx = randperm(ImgZ);
RandIdx = RandIdx(1:NumRSamples);
% NMF parameters
err_rat=0.025;
eps=1e-9;
maxiter=200;
r=NumFilters;
%% Learning PCA filters (V)
NumChls = size(InImg{1},3);%锟斤拷色通锟斤拷锟侥革拷锟斤拷
%Rx = zeros(NumChls*PatchSize^2,NumChls*PatchSize^2); % sum covariance
Rx = [];
%每锟斤拷图锟斤拷锟叫分匡拷锟斤拷锟?
for i = RandIdx %1:ImgZ
im = im2col_mean_removal(InImg{i},[PatchSize PatchSize]);% collect all the patches of the ith image in a matrix, and perform patch mean removal
B = im(:);
Rx = [Rx,B];
%Rx = Rx + im*im'; % sum of all the input images' covariance matrix
end
[a,b]=size(im);
[m, N]=size(Rx); % V contains your data in its column vectors
W0=rand(m,r); % randomly initialize basis
W0=W0*diag(1./sum(W0));
H0=rand(r,N);
W=W0;
W1=zeros(a,b,r);
W2=zeros(b,a,r);
W3=zeros(a,r);
H=H0;
F_obj(1,1)=sum(sum((Rx-W*H).*(Rx-W*H)))/sum(sum(Rx.*Rx)); %值为0-1之锟斤拷欧锟斤拷锟斤拷锟?锟斤拷锟轿拷锟接υ拷锟斤拷锟剿o拷sum为每锟斤拷元锟斤拷锟斤拷锟?
X = Rx;
%for iter=1:maxiter
%
% H=H.*(W'*Rx+eps)./(W'*W*H+eps);
% W=W.*(Rx*H'+eps)./(W*H*H'+eps);
% W=W*diag(1./sum(W,1));
%W = X * pinv(H);%鏇存柊Z鐭╅樀
%A = W' * X;%姹俍鐨勮浆缃箻浠
%Ap = (abs(A)+A)./2;
%An = (abs(A)-A)./2;
%B = W' * W;%Z^T*Z
%Bp = (abs(B)+B)./2;
%Bn = (abs(B)-B)./2;
%H = H .* sqrt((Ap + Bn * H) ./ max(An + Bp * H, eps));%鏇存柊H鐭╅樀鎬诲叕寮?
% load FERET_crop_database;
load PIE_pose27;
fa_label = gnd;
W = ssnmf(Rx,fa_label,r);
% F_obj(1,iter+1)=sum(sum((Rx-W*H).*(Rx-W*H)))/sum(sum(Rx.*Rx));
% if F_obj(iter+1)<err_rat
% break;
% end
%end
W1 = reshape(W,a,b,r);
W2 = permute(W1,[2 1 3]);
W3 = reshape(sum(W2),a,r);
V = W3;
% Rx = Rx/(NumRSamples*size(im,2));
% [E D] = eig(Rx); %锟斤拷锟斤拷锟斤拷锟斤拷值锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷eig,Rx为协锟斤拷锟斤拷锟斤拷锟?
% [~, ind] = sort(diag(D),'descend');
% V = E(:,ind(1:NumFilters)); % principal eigenvectors