LDA人脸识别的matlab程序
更新时间:2023-10-14 01:31:01 阅读量: 综合文库 文档下载
- LDA人脸识别推荐度:
- 相关推荐
LDA人脸识别的matlab程序
以下是LDA的m文件函数: 你稍稍改改就能用了!
function [eigvector, eigvalue, elapse] = LDA(gnd,options,data) % LDA: Linear Discriminant Analysis %
% [eigvector, eigvalue] = LDA(gnd, options, data) %
% Input:
% data - Data matrix. Each row vector of fea is a data point. % gnd - Colunm vector of the label information for each % data point.
% options - Struct value in Matlab. The fields in options % that can be set: %
% Regu - 1: regularized solution, % a* = argmax (a'X'WXa)/(a'X'Xa+ReguAlpha*I)
% 0: solve the sinularity problem by SVD
% Default: 0 %
% ReguAlpha - The regularization parameter. Valid
% when Regu==1. Default value is 0.1. %
% ReguType - 'Ridge': Tikhonov regularization
% 'Custom': User provided % regularization matrix
% Default: 'Ridge'
% regularizerR - (nFea x nFea) regularization % matrix which should be provided
% if ReguType is 'Custom'. nFea is
% the feature number of data % matrix
% Fisherface - 1: Fisherface approach
% PCARatio = nSmp - nClass
% Default: 0 %
% PCARatio - The percentage of principal
% component kept in the PCA
% step. The percentage is % calculated based on the % eigenvalue. Default is 1 % (100%, all the non-zero % eigenvalues will be kept. % If PCARatio > 1, the PCA step
% will keep exactly PCARatio principle
% components (does not exceed the
% exact number of non-zero components).
% %
% Output:
% eigvector - Each column is an embedding function, for a new
% data point (row vector) x, y = x*eigvector % will be the embedding result of x.
% eigvalue - The sorted eigvalue of LDA eigen-problem. % elapse - Time spent on different steps %
% Examples: %
% fea = rand(50,70);
% gnd = [ones(10,1);ones(15,1)*2;ones(10,1)*3;ones(15,1)*4]; % options = [];
% options.Fisherface = 1;
% [eigvector, eigvalue] = LDA(gnd, options, fea); % Y = fea*eigvector; % %
% See also LPP, constructW, LGE % %
%
%Reference: %
% P. N. Belhumeur, J. P. Hespanha, and D. J. Kriegman, 揈igenfaces % vs. fisherfaces: recognition using class specific linear % projection,? IEEE Transactions on Pattern Analysis and Machine % Intelligence, vol. 19, no. 7, pp. 711-720, July 1997. %
% Deng Cai, Xiaofei He, Yuxiao Hu, Jiawei Han, and Thomas Huang, % \CVPR'2007 %
% Deng Cai, Xiaofei He, Jiawei Han, \% Large Scale Discriminant Analysis\and
% Data Engineering, 2007. %
% version 2.1 --June/2007 % version 2.0 --May/2007 % version 1.1 --Feb/2006 % version 1.0 --April/2004 %
% Written by Deng Cai (dengcai2 AT cs.uiuc.edu)
%
if ~exist('data','var') global data; end
if (~exist('options','var')) options = []; end
if ~isfield(options,'Regu') | ~options.Regu bPCA = 1;
if ~isfield(options,'PCARatio') options.PCARatio = 1; end else
bPCA = 0;
if ~isfield(options,'ReguType') options.ReguType = 'Ridge'; end
if ~isfield(options,'ReguAlpha')
options.ReguAlpha = 0.1; end end
tmp_T = cputime;
% ====== Initialization [nSmp,nFea] = size(data); if length(gnd) ~= nSmp
error('gnd and data mismatch!'); end
classLabel = unique(gnd); nClass = length(classLabel); Dim = nClass - 1;
if bPCA & isfield(options,'Fisherface') & options.Fisherface options.PCARatio = nSmp - nClass; end
if issparse(data)
data = full(data); end
sampleMean = mean(data,1);
data = (data - repmat(sampleMean,nSmp,1));
bChol = 0;
if bPCA & (nSmp > nFea+1) & (options.PCARatio >= 1) DPrime = data'*data;
DPrime = max(DPrime,DPrime'); [R,p] = chol(DPrime);
if p == 0
bPCA = 0; bChol = 1; end end
%====================================== % SVD
%====================================== if bPCA
if nSmp > nFea
ddata = data'*data;
ddata = max(ddata,ddata');
[eigvector_PCA, eigvalue_PCA] = eig(ddata); eigvalue_PCA = diag(eigvalue_PCA); clear ddata;
maxEigValue = max(abs(eigvalue_PCA));
eigIdx = find(eigvalue_PCA/maxEigValue < 1e-12); eigvalue_PCA(eigIdx) = []; eigvector_PCA(:,eigIdx) = [];
[junk, index] = sort(-eigvalue_PCA); eigvalue_PCA = eigvalue_PCA(index); eigvector_PCA = eigvector_PCA(:, index);
%======================================= if options.PCARatio > 1
idx = options.PCARatio;
if idx < length(eigvalue_PCA)
eigvalue_PCA = eigvalue_PCA(1:idx); eigvector_PCA = eigvector_PCA(:,1:idx); end
elseif options.PCARatio < 1
sumEig = sum(eigvalue_PCA);
sumEig = sumEig*options.PCARatio; sumNow = 0;
for idx = 1:length(eigvalue_PCA)
sumNow = sumNow + eigvalue_PCA(idx); if sumNow >= sumEig break; end end
eigvalue_PCA = eigvalue_PCA(1:idx); eigvector_PCA = eigvector_PCA(:,1:idx); end
%=======================================
eigvalue_PCA = eigvalue_PCA.^-.5;
data = (data*eigvector_PCA).*repmat(eigvalue_PCA',nSmp,1); else
ddata = data*data';
ddata = max(ddata,ddata');
[eigvector, eigvalue_PCA] = eig(ddata); eigvalue_PCA = diag(eigvalue_PCA); clear ddata;
maxEigValue = max(eigvalue_PCA);
eigIdx = find(eigvalue_PCA/maxEigValue < 1e-12); eigvalue_PCA(eigIdx) = []; eigvector(:,eigIdx) = [];
[junk, index] = sort(-eigvalue_PCA); eigvalue_PCA = eigvalue_PCA(index); eigvector = eigvector(:, index);
%======================================= if options.PCARatio > 1
idx = options.PCARatio;
if idx < length(eigvalue_PCA)
eigvalue_PCA = eigvalue_PCA(1:idx); eigvector = eigvector(:,1:idx); end
elseif options.PCARatio < 1
sumEig = sum(eigvalue_PCA);
sumEig = sumEig*options.PCARatio; sumNow = 0;
for idx = 1:length(eigvalue_PCA)
sumNow = sumNow + eigvalue_PCA(idx); if sumNow >= sumEig break; end end
eigvalue_PCA = eigvalue_PCA(1:idx); eigvector = eigvector(:,1:idx); end
%=======================================
eigvalue_PCA = eigvalue_PCA.^-.5;
eigvector_PCA = (data'*eigvector).*repmat(eigvalue_PCA',nFea,1);
data = eigvector; clear eigvector;
end else
if ~bChol
DPrime = data'*data;
% options.ReguAlpha = nSmp*options.ReguAlpha;
switch lower(options.ReguType) case {lower('Ridge')}
for i=1:size(DPrime,1)
DPrime(i,i) = DPrime(i,i) + options.ReguAlpha; end
case {lower('Tensor')} DPrime = DPrime + options.ReguAlpha*options.regularizerR; case {lower('Custom')} DPrime = DPrime + options.ReguAlpha*options.regularizerR; otherwise
error('ReguType does not exist!'); end
DPrime = max(DPrime,DPrime'); end end
[nSmp,nFea] = size(data);
Hb = zeros(nClass,nFea); for i = 1:nClass,
index = find(gnd==classLabel(i)); classMean = mean(data(index,:),1); Hb (i,:) = sqrt(length(index))*classMean; end
elapse.timeW = 0;
elapse.timePCA = cputime - tmp_T;
tmp_T = cputime;
if bPCA
[dumpVec,eigvalue,eigvector] = svd(Hb,'econ');
eigvalue = diag(eigvalue); eigIdx = find(eigvalue < 1e-3); eigvalue(eigIdx) = []; eigvector(:,eigIdx) = [];
eigvalue = eigvalue.^2; eigvector =
eigvector_PCA*(repmat(eigvalue_PCA,1,length(eigvalue)).*eigvector); else
WPrime = Hb'*Hb;
WPrime = max(WPrime,WPrime');
dimMatrix = size(WPrime,2); if Dim > dimMatrix Dim = dimMatrix; end
if isfield(options,'bEigs') if options.bEigs bEigs = 1; else
bEigs = 0; end else
if (dimMatrix > 1000 & Dim < dimMatrix/10) | (dimMatrix > 500 & Dim < dimMatrix/20) | (dimMatrix > 250 & Dim < dimMatrix/30) bEigs = 1; else
bEigs = 0; end end
if bEigs
%disp('use eigs to speed up!'); option = struct('disp',0); if bChol
option.cholB = 1;
[eigvector, eigvalue] = eigs(WPrime,R,Dim,'la',option); else
[eigvector, eigvalue] = eigs(WPrime,DPrime,Dim,'la',option); end
eigvalue = diag(eigvalue); else
[eigvector, eigvalue] = eig(WPrime,DPrime); eigvalue = diag(eigvalue);
[junk, index] = sort(-eigvalue); eigvalue = eigvalue(index); eigvector = eigvector(:,index);
if Dim < size(eigvector,2)
eigvector = eigvector(:, 1:Dim); eigvalue = eigvalue(1:Dim); end end end
for i = 1:size(eigvector,2)
eigvector(:,i) = eigvector(:,i)./norm(eigvector(:,i)); end
elapse.timeMethod = cputime - tmp_T;
elapse.timeAll = elapse.timePCA + elapse.timeMethod;
正在阅读:
LDA人脸识别的matlab程序10-14
亿以内数的读法和写法练习题01-21
电力系统继电保护1-3章习题解答03-11
研讨会 新闻通稿03-16
C++程序设计大作业(含源代码)11-12
试析如何运用多媒体技术进行小学数学教学11-26
北京大学14秋微观经济学答案08-29
静坐与禅修09-18
机械检修中级工题库04-07
- 多层物业服务方案
- (审判实务)习惯法与少数民族地区民间纠纷解决问题(孙 潋)
- 人教版新课标六年级下册语文全册教案
- 词语打卡
- photoshop实习报告
- 钢结构设计原理综合测试2
- 2014年期末练习题
- 高中数学中的逆向思维解题方法探讨
- 名师原创 全国通用2014-2015学年高二寒假作业 政治(一)Word版
- 北航《建筑结构检测鉴定与加固》在线作业三
- XX县卫生监督所工程建设项目可行性研究报告
- 小学四年级观察作文经典评语
- 浅谈110KV变电站电气一次设计-程泉焱(1)
- 安全员考试题库
- 国家电网公司变电运维管理规定(试行)
- 义务教育课程标准稿征求意见提纲
- 教学秘书面试技巧
- 钢结构工程施工组织设计
- 水利工程概论论文
- 09届九年级数学第四次模拟试卷
- 人脸
- 别的
- 程序
- matlab
- LDA
- 2017年上半年海南省一级建造师:项目管理十大关键流程试题
- 2017新人教部编本二年级上册语文《我要的是葫芦》导学案
- 九年级英语月考试卷11
- 第五届全国中学英语教师教学技能大赛命题大纲
- 军事理论论述题
- 高一语文开学第一课
- 2013新课标《人类对细菌和真菌的利用》说课稿(说稿)
- 2017届高三数学一轮复习 专题突破训练 数列 文 - 图文
- 人教版三年级下册数学广角重叠问题
- 为爸爸妈妈写歌词作文300字
- 直角坐标机器人结构设计
- 转发关于加强建筑材料价格风险控制的指导意见
- 某医院室外10KV进线电缆工程施工组织设计方案
- 汝城县卢阳镇一完小朗读比赛方案
- 环保水保监理实施细则(已修改)
- 《诗的材料》教案教学设计
- 四川省宜宾市一中2017-2018学年高中英语中心发言提纲
- 浅谈小学英语课堂导入和呈现的几种方法
- 广东省高级人民法院、广东省劳动人事争议仲裁委员会关于审理劳动争议案件若干问题的指导意见(建议稿)
- 工业成本报表与分析