BP 人脸图像识别 matlab代码
时间: 2024-02-06 11:03:19 浏览: 38
以下是一个基于BP神经网络的人脸识别Matlab代码示例:
```matlab
clc;
clear all;
close all;
train_num=5; %训练样本个数
test_num=3; %测试样本个数
n=5; %每个样本的图像个数
m=112*92; %每幅图像的大小
p=train_num*n; %训练样本总数
q=test_num*n; %测试样本总数
x=[]; %训练样本空间
t=[]; %测试样本空间
for i=1:train_num
for j=1:n
str=strcat('F:\matlab\data\orl_faces\s',num2str(i),'\',num2str(j),'.bmp');
img=imread(str);
img=double(img);
img=reshape(img,m,1);
x=[x,img];
end
end
for i=1:test_num
for j=1:n
str=strcat('F:\matlab\data\orl_faces\s',num2str(i+35),'\',num2str(j),'.bmp');
img=imread(str);
img=double(img);
img=reshape(img,m,1);
t=[t,img];
end
end
x=double(x);
t=double(t);
x_max=max(max(x));
x_min=min(min(x));
t_max=max(max(t));
t_min=min(min(t));
for i=1:p
x(:,i)=(x(:,i)-x_min)/(x_max-x_min);
end
for i=1:q
t(:,i)=(t(:,i)-t_min)/(t_max-t_min);
end
d=zeros(train_num,n);
for i=1:train_num
for j=1:n
d(i,j)=i;
end
end
d=d';
d=reshape(d,train_num*n,1);
d=d';
y=zeros(train_num,train_num*n);
for i=1:train_num*n
y(d(i),i)=1;
end
v=rand(70,m+1);
w=rand(train_num,71);
eta=0.01;
for epoch=1:1000
epoch
for i=1:train_num*n
xx=[x(:,i)',1]';
v=[v(:,1:m),ones(70,1)];
b=v*xx;
z=1./(1+exp(-b));
z=[z',1]';
c=w*z;
y1=1./(1+exp(-c));
delta1=y1.*(1-y1).*(y(:,i)-y1);
delta2=z.*(1-z).*(w'*delta1);
delta2=delta2(1:end-1);
w=w+eta*delta1*z';
v=v+eta*delta2*xx';
end
end
correct=0;
for i=1:test_num*n
xx=[t(:,i)',1]';
b=v*xx;
z=1./(1+exp(-b));
z=[z',1]';
c=w*z;
y1=1./(1+exp(-c));
[M,I]=max(y1);
if(I==ceil(i/n))
correct=correct+1;
end
end
correct
```
此代码假设您有一个名为`orl_faces`的文件夹,其中包含用于训练和测试的人脸图像。您可以从`AT&T ORL Database of Faces`网站上下载该数据集。
请注意,这只是一个基本示例代码,您可能需要对其进行修改以适应您的特定需求。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)