【Detailed Explanation of Practical Cases of MATLAB Image Processing (20)】—Using BP Neural Network to Realize Face Orientation Judgment

1. Problem description

The BP neural network uses the error after the output to estimate the error of the direct leading layer of the output layer, and then uses this error to estimate the error of the previous layer, so that the layer-by-layer back propagation can obtain the errors of all other layers. estimate.

The BP algorithm uses the error backpropagation algorithm of the multi-layer perceptron. Its basic idea is that the learning process consists of two processes: the forward propagation of the signal and the backpropagation of the error.

During forward propagation, the input samples are passed in from the input layer, processed layer by layer by each hidden layer, and passed to the output layer. If the actual output of the output layer does not match the expected output, it will turn to the reverse transmission stage of the error.

Error backpropagation is to pass the output error back to the input layer by layer through the hidden layer in some form, and distribute the error to all the units of each layer, so as to obtain the error signal of each layer of units, and the error signal is used as the correction of each unit. Basis for weight.

The basic flow chart of BP neural network is as follows:
insert image description here

The goal of this paper is to use BP neural network to predict the direction of the face and locate the direction of the face reasonably for the face database image.

2. Algorithm steps

2.1 Read in data and extract features

code show as below:

for j=1:N_train
      for i=1:M_train
        %读取图像,连接字符串形成图像的文件名。
        str=strcat('Images\',num2str(i),'_',num2str(j),'.bmp'); 
        img= imread(str);  
        [rows cols]= size(img);%获得图像的行和列值。
        img_edge=edge(img,'Sobel');

        %由于在分割图片中,人脸的眼睛部分位置变化比较大,边缘检测效果好
        sub_rows=floor(rows/6);%最接近的最小整数,分成6行
        sub_cols=floor(cols/8);%最接近的最小整数,分成8列
        sample_num=M_train*N_train;%前5个是第一幅人脸的5个角度

        sample_number=sample_number+1;
        for subblock_i=1:8 %因为这还在i,j的循环中,所以不可以用i 
            block_num=subblock_i;
            pixel_value(sample_number,block_num)=0;  
            for ii=sub_rows:(2*sub_rows)
                for jj=(subblock_i-1)*sub_cols+1:subblock_i*sub_cols
                    pixel_value(sample_number,block_num)=pixel_value(sample_number,block_num)+img_edge(ii,jj);          
                end
            end     
        end  
    end
end

2.2 Create a neural network and train it

code show as below:

%  创建一个新的前向神经网络  
net_1=newff(minmax(P),[10,3],{
    
    'tansig','purelin'},'traingdm')
%  调用 TRAINGDM 算法训练 BP 网络
[net_1,tr]=train(net_1,P,T); 

2.3 Testing

code show as below:

%  对 BP 网络进行仿真
A = sim(net_1,P); 
%  计算仿真误差  
E = T - A; 
MSE=mse(E) 

x=[0.14 0 1 1 0 1 1 1.2]';
sim(net_1,x)

3. Results Analysis

The result of the operation is as follows:
insert image description here

root mean square error:
insert image description here

It can be seen from the results that using the BP neural network to predict the direction of the face, the prediction error is very small, the root mean square error is 0.00099931, and the predicted result is close to the real result. Therefore, the BP neural network has better data prediction and fitting ability, and has a wide range of applications. BP neural network has the following advantages:
(1) Non-linear mapping ability: BP neural network essentially realizes a mapping function from input to output. Mathematical theory proves that a three-layer neural network can approximate any nonlinear continuous function with arbitrary precision. . This makes it particularly suitable for solving problems with complex internal mechanisms, that is, the BP neural network has strong nonlinear mapping capabilities.
(2) Self-learning and self-adaptive ability: During training, the BP neural network can automatically extract the "reasonable rules" between output and output data through learning, and adaptively memorize the learning content in the weight of the network. That is, the BP neural network has a high degree of self-learning and self-adaptive ability.
(3) Generalization ability: The so-called generalization ability means that when designing a pattern classifier, it is necessary to consider that the network can ensure the correct classification of the required classification objects, and also care about whether the network can classify the unseen objects after training. , or noise-polluted patterns, for correct classification. That is to say, the BP neural network has the ability to apply the learning results to new knowledge.
(4) Fault tolerance: BP neural network will not have a great impact on the overall training results after its local or partial neurons are damaged, that is to say, the system can still work normally even when it is partially damaged. That is, the BP neural network has a certain fault tolerance.


Due to the limitation of the article, the complete face orientation data set and BP neural network code can be downloaded here: https://download.csdn.net/download/didi_ya/87741778 .

Guess you like

Origin blog.csdn.net/didi_ya/article/details/130438268