Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Hiroyuki KASAI
committed
Jul 12, 2017
1 parent
b704d92
commit 36ba9c0
Showing
14 changed files
with
1,311 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
% ======================================================================== | ||
% Fisher Discriminative Dictionary Learning (FDDL), Version 1.0 | ||
% Copyright(c) 2011 Meng YANG, Lei Zhang, Xiangchu Feng and David Zhang | ||
% All Rights Reserved. | ||
% | ||
% The code is for the paper: | ||
|
||
% M. Yang, L. Zhang, X. Feng and D. Zhang, | ||
% ��Fisher Discrimination Dictionary Learning for Sparse Representation,�� in ICCV 2011. | ||
|
||
% ---------------------------------------------------------------------- | ||
% Permission to use, copy, or modify this software and its documentation | ||
% for educational and research purposes only and without fee is here | ||
% granted, provided that this copyright notice and the original authors' | ||
% names appear on all copies and supporting documentation. This program | ||
% shall not be used, rewritten, or adapted as the basis of a commercial | ||
% software or hardware product without first obtaining permission of the | ||
% authors. The authors make no representations about the suitability of | ||
% this software for any purpose. It is provided "as is" without express | ||
% or implied warranty. | ||
%---------------------------------------------------------------------- | ||
|
||
demo.m Face recognition demo on AR database with 300-d Eigenface feature | ||
|
||
utilier : folder of FDDL functions, including | ||
Eigenface_f: function of computing PCA Projection Matrix | ||
FDDL: main function of FDDL | ||
FDDL_Class_Energy: function of computing energy of certain class | ||
FDDL_FDL_Energy: function of computing energy of all classes | ||
FDDL_Gradient_Comp: function of computing coding model's gradient | ||
FDDL_INIC: function of initializing representation coef | ||
FDDL_INID: function of initializing dictionary | ||
FDDL_SpaCoef: function of computing coding coefficient | ||
FDDL_UpdateDi: function of updating dictioary | ||
IPM_SC: sparse coding function | ||
soft: soft threholding function | ||
|
||
%------------------------------------------------------------------------- | ||
Contact: {csmyang,cslzhang}@comp.polyu.edu.hk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
close all; | ||
clear all; | ||
clc; | ||
|
||
addpath([cd '/utilies']); | ||
load(['AR_EigenFace']); | ||
|
||
%%%%%%%%%%%%%%%%%%%%%%%% | ||
%FDDL parameter | ||
%%%%%%%%%%%%%%%%%%%%%%%% | ||
opts.nClass = 100; | ||
opts.wayInit = 'PCA'; | ||
opts.lambda1 = 0.005; | ||
opts.lambda2 = 0.05; | ||
opts.nIter = 15; | ||
opts.show = true; | ||
[Dict,Drls,CoefM,CMlabel] = FDDL(tr_dat,trls,opts); | ||
|
||
%%%%%%%%%%%%%%%%%%%%%%%% | ||
% Sparse Classification | ||
%%%%%%%%%%%%%%%%%%%%%%%% | ||
lambda = 0.005; | ||
nClass = opts.nClass; | ||
weight = 0.5; | ||
|
||
td1_ipts.D = Dict; | ||
td1_ipts.tau1 = lambda; | ||
if size(td1_ipts.D,1)>=size(td1_ipts.D,2) | ||
td1_par.eigenv = eigs(td1_ipts.D'*td1_ipts.D,1); | ||
else | ||
td1_par.eigenv = eigs(td1_ipts.D*td1_ipts.D',1); | ||
end | ||
|
||
ID = []; | ||
for indTest = 1:size(tt_dat,2) | ||
fprintf(['Totalnum:' num2str(size(tt_dat,2)) 'Nowprocess:' num2str(indTest) '\n']); | ||
td1_ipts.y = tt_dat(:,indTest); | ||
[opts] = IPM_SC(td1_ipts,td1_par); | ||
s = opts.x; | ||
|
||
for indClass = 1:nClass | ||
temp_s = zeros(size(s)); | ||
temp_s(indClass==Drls) = s(indClass==Drls); | ||
zz = tt_dat(:,indTest)-td1_ipts.D*temp_s; | ||
gap(indClass) = zz(:)'*zz(:); | ||
|
||
mean_coef_c = CoefM(:,indClass); | ||
gCoef3(indClass) = norm(s-mean_coef_c,2)^2; | ||
end | ||
|
||
wgap3 = gap + weight*gCoef3; | ||
index3 = find(wgap3==min(wgap3)); | ||
id3 = index3(1); | ||
ID = [ID id3]; | ||
end | ||
|
||
fprintf('%s%8f\n','reco_rate = ',sum(ID==ttls)/(length(ttls))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
function [disc_set,disc_value,Mean_Image]=Eigenface_f(Train_SET,Eigen_NUM) | ||
|
||
% the magnitude of eigenvalues of this function is corrected right !!!!!!!!! | ||
% Centralized PCA | ||
[NN,Train_NUM]=size(Train_SET); | ||
|
||
if NN<=Train_NUM % for small sample size case | ||
|
||
Mean_Image=mean(Train_SET,2); | ||
Train_SET=Train_SET-Mean_Image*ones(1,Train_NUM); | ||
R=Train_SET*Train_SET'/(Train_NUM-1); | ||
|
||
[V,S]=Find_K_Max_Eigen(R,Eigen_NUM); | ||
disc_value=S; | ||
disc_set=V; | ||
|
||
else % for small sample size case | ||
|
||
Mean_Image=mean(Train_SET,2); | ||
Train_SET=Train_SET-Mean_Image*ones(1,Train_NUM); | ||
|
||
R=Train_SET'*Train_SET/(Train_NUM-1); | ||
|
||
[V,S]=Find_K_Max_Eigen(R,Eigen_NUM); | ||
disc_value=S; | ||
disc_set=zeros(NN,Eigen_NUM); | ||
|
||
Train_SET=Train_SET/sqrt(Train_NUM-1); | ||
for k=1:Eigen_NUM | ||
disc_set(:,k)=(1/sqrt(disc_value(k)))*Train_SET*V(:,k); | ||
end | ||
|
||
end | ||
|
||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
|
||
function [Eigen_Vector,Eigen_Value]=Find_K_Max_Eigen(Matrix,Eigen_NUM) | ||
|
||
[NN,NN]=size(Matrix); | ||
[V,S]=eig(Matrix); %Note this is equivalent to; [V,S]=eig(St,SL); also equivalent to [V,S]=eig(Sn,St); % | ||
|
||
S=diag(S); | ||
[S,index]=sort(S); | ||
|
||
Eigen_Vector=zeros(NN,Eigen_NUM); | ||
Eigen_Value=zeros(1,Eigen_NUM); | ||
|
||
p=NN; | ||
for t=1:Eigen_NUM | ||
Eigen_Vector(:,t)=V(:,index(p)); | ||
Eigen_Value(t)=S(p); | ||
p=p-1; | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
function [Dict,Drls,CoefM,CMlabel] = FDDL(TrainDat,TrainLabel,opts) | ||
% ======================================================================== | ||
% Fisher Discriminative Dictionary Learning (FDDL), Version 1.0 | ||
% Copyright(c) 2011 Meng YANG, Lei Zhang, Xiangchu Feng and David Zhang | ||
% All Rights Reserved. | ||
% | ||
% ---------------------------------------------------------------------- | ||
% Permission to use, copy, or modify this software and its documentation | ||
% for educational and research purposes only and without fee is here | ||
% granted, provided that this copyright notice and the original authors' | ||
% names appear on all copies and supporting documentation. This program | ||
% shall not be used, rewritten, or adapted as the basis of a commercial | ||
% software or hardware product without first obtaining permission of the | ||
% authors. The authors make no representations about the suitability of | ||
% this software for any purpose. It is provided "as is" without express | ||
% or implied warranty. | ||
%---------------------------------------------------------------------- | ||
% | ||
% This is an implementation of the algorithm for learning the | ||
% Fisher Discriminative Dictionary from a labeled training data | ||
% | ||
% Please refer to the following paper | ||
% | ||
% Meng Yang, Lei Zhang, Xiangchu Feng, and David Zhang,"Fisher Discrimination | ||
% Dictionary Learning for Sparse Representation", In IEEE Int. Conf. on | ||
% Computer Vision, 2011. | ||
% | ||
%---------------------------------------------------------------------- | ||
% | ||
%Input : (1) TrainDat: the training data matrix. | ||
% Each column is a training sample | ||
% (2) TrainDabel: the training data labels | ||
% (3) opts : the struture of parameters | ||
% .nClass the number of classes | ||
% .wayInit the way to initialize the dictionary | ||
% .lambda1 the parameter of l1-norm energy of coefficient | ||
% .lambda2 the parameter of l2-norm of Fisher Discriminative | ||
% coefficient term | ||
% .nIter the number of FDDL's iteration | ||
% .show sign value of showing the gap sequence | ||
% | ||
%Output: (1) Dict: the learnt dictionary via FDDL | ||
% (2) Drls: the labels of learnt dictionary's columns | ||
% (2) CoefM: Mean Coefficient Matrix. Each column is a mean coef | ||
% vector | ||
% (3) CMlabel: the labels of CoefM's columns. | ||
% | ||
%----------------------------------------------------------------------- | ||
% | ||
%Usage: | ||
%Given a training data, including TrainDat and TrainLabel, and the | ||
%parameters, opts. | ||
% | ||
%[Dict,CoefM,CMlabel] = FDDL(TrainDat,TrainLabel,opts) | ||
%----------------------------------------------------------------------- | ||
%%%%%%%%%%%%%%%%%% | ||
% normalize energy | ||
%%%%%%%%%%%%%%%%%% | ||
TrainDat = TrainDat*diag(1./sqrt(sum(TrainDat.*TrainDat))); | ||
|
||
%%%%%%%%%%%%%%%%%% | ||
%initialize dict | ||
%%%%%%%%%%%%%%%%%% | ||
Dict_ini = []; | ||
Dlabel_ini = []; | ||
for ci = 1:opts.nClass | ||
cdat = TrainDat(:,TrainLabel==ci); | ||
dict = FDDL_INID(cdat,size(cdat,2),opts.wayInit); | ||
Dict_ini = [Dict_ini dict]; | ||
Dlabel_ini = [Dlabel_ini repmat(ci,[1 size(dict,2)])]; | ||
end | ||
|
||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
%initialize coef without between-class scatter | ||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
ini_par.tau = opts.lambda1; | ||
ini_par.lambda = opts.lambda2; | ||
ini_ipts.D = Dict_ini; | ||
coef = zeros(size(Dict_ini,2),size(TrainDat,2)); | ||
if size(Dict_ini,1)>size(Dict_ini,2) | ||
ini_par.c = 1.05*eigs(Dict_ini'*Dict_ini,1); | ||
else | ||
ini_par.c = 1.05*eigs(Dict_ini*Dict_ini',1); | ||
end | ||
for ci = 1:opts.nClass | ||
fprintf(['Initializing Coef: Class ' num2str(ci) '\n']); | ||
ini_ipts.X = TrainDat(:,TrainLabel==ci); | ||
[ini_opts] = FDDL_INIC (ini_ipts,ini_par); | ||
coef(:,TrainLabel ==ci) = ini_opts.A; | ||
end | ||
|
||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
%Main loop of Fisher Discriminative Dictionary Learning | ||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
Fish_par.dls = Dlabel_ini; | ||
Fish_ipts.D = Dict_ini; | ||
Fish_ipts.trls = TrainLabel; | ||
Fish_par.tau = opts.lambda1; | ||
Fish_par.lambda2 = opts.lambda2; | ||
|
||
Fish_nit = 1; | ||
drls = Dlabel_ini; | ||
while Fish_nit<=opts.nIter | ||
if size(Fish_ipts.D,1)>size(Fish_ipts.D,2) | ||
Fish_par.c = 1.05*eigs(Fish_ipts.D'*Fish_ipts.D,1); | ||
else | ||
Fish_par.c = 1.05*eigs(Fish_ipts.D*Fish_ipts.D',1); | ||
end | ||
%------------------------- | ||
%updating the coefficient | ||
%------------------------- | ||
for ci = 1:opts.nClass | ||
fprintf(['Updating coefficients, class: ' num2str(ci) '\n']) | ||
Fish_ipts.X = TrainDat(:,TrainLabel==ci); | ||
Fish_ipts.A = coef; | ||
Fish_par.index = ci; | ||
[Copts] = FDDL_SpaCoef (Fish_ipts,Fish_par); | ||
coef(:,TrainLabel==ci) = Copts.A; | ||
CMlabel(ci) = ci; | ||
CoefM(:,ci) = mean(Copts.A,2); | ||
end | ||
[GAP_coding(Fish_nit)] = FDDL_FDL_Energy(TrainDat,coef,opts.nClass,Fish_par,Fish_ipts); | ||
|
||
%------------------------ | ||
%updating the dictionary | ||
%------------------------ | ||
for ci = 1:opts.nClass | ||
fprintf(['Updating dictionary, class: ' num2str(ci) '\n']); | ||
[Fish_ipts.D(:,drls==ci),Delt(ci).delet]= FDDL_UpdateDi (TrainDat,coef,... | ||
ci,opts.nClass,Fish_ipts,Fish_par); | ||
end | ||
[GAP_dict(Fish_nit)] = FDDL_FDL_Energy(TrainDat,coef,opts.nClass,Fish_par,Fish_ipts); | ||
|
||
newD = []; newdrls = []; newcoef = []; | ||
for ci = 1:opts.nClass | ||
delet = Delt(ci).delet; | ||
if isempty(delet) | ||
classD = Fish_ipts.D(:,drls==ci); | ||
newD = [newD classD]; | ||
newdrls = [newdrls repmat(ci,[1 size(classD,2)])]; | ||
newcoef = [newcoef; coef(drls==ci,:)]; | ||
else | ||
temp = Fish_ipts.D(:,drls==ci); | ||
temp_coef = coef(drls==ci,:); | ||
for temp_i = 1:size(temp,2) | ||
if sum(delet==temp_i)==0 | ||
newD = [newD temp(:,temp_i)]; | ||
newdrls = [newdrls ci]; | ||
newcoef = [newcoef;temp_coef(temp_i,:)]; | ||
end | ||
end | ||
end | ||
end | ||
|
||
Fish_ipts.D = newD; | ||
coef = newcoef; | ||
drls = newdrls; | ||
Fish_par.dls = drls; | ||
|
||
Fish_nit = Fish_nit +1; | ||
end | ||
|
||
Dict = Fish_ipts.D; | ||
Drls = drls; | ||
|
||
if opts.show | ||
subplot(1,2,1);plot(GAP_coding,'-*');title('GAP_coding'); | ||
subplot(1,2,2);plot(GAP_dict,'-o');title('GAP_dict'); | ||
end | ||
return; |
Oops, something went wrong.