当前位置:网站首页>Analysis of 43 cases of MATLAB neural network: Chapter 27 prediction of LVQ Neural Network - face orientation recognition

Analysis of 43 cases of MATLAB neural network: Chapter 27 prediction of LVQ Neural Network - face orientation recognition

2022-06-21 17:37:00 mozun2020

《MATLAB neural network 43 A case study 》: The first 27 Chapter LVQ Neural network prediction —— Face orientation recognition

1. Preface

《MATLAB neural network 43 A case study 》 yes MATLAB Technology Forum (www.matlabsky.com) planning , Led by teacher wangxiaochuan ,2013 Beijing University of Aeronautics and Astronautics Press MATLAB A book for tools MATLAB Example teaching books , Is in 《MATLAB neural network 30 A case study 》 On the basis of modification 、 Complementary , Adhering to “ Theoretical explanation — case analysis — Application extension ” This feature , Help readers to be more intuitive 、 Learn neural networks vividly .

《MATLAB neural network 43 A case study 》 share 43 Chapter , The content covers common neural networks (BP、RBF、SOM、Hopfield、Elman、LVQ、Kohonen、GRNN、NARX etc. ) And related intelligent algorithms (SVM、 Decision tree 、 Random forests 、 Extreme learning machine, etc ). meanwhile , Some chapters also cover common optimization algorithms ( Genetic algorithm (ga) 、 Ant colony algorithm, etc ) And neural network . Besides ,《MATLAB neural network 43 A case study 》 It also introduces MATLAB R2012b New functions and features of neural network toolbox in , Such as neural network parallel computing 、 Custom neural networks 、 Efficient programming of neural network, etc .

In recent years, with the rise of artificial intelligence research , The related direction of neural network has also ushered in another upsurge of research , Because of its outstanding performance in the field of signal processing , The neural network method is also being applied to various applications in the direction of speech and image , This paper combines the cases in the book , It is simulated and realized , It's a relearning , I hope I can review the old and know the new , Strengthen and improve my understanding and practice of the application of neural network in various fields . I just started this book on catching more fish , Let's start the simulation example , Mainly to introduce the source code application examples in each chapter , This paper is mainly based on MATLAB2015b(32 position ) Platform simulation implementation , This is Chapter 27 of the book LVQ An example of neural network prediction , Don't talk much , Start !

2. MATLAB Simulation example 1

open MATLAB, Click on “ Home page ”, Click on “ open ”, Find the sample file
 Insert picture description here
Choose chapter27_lvq.m, Click on “ open ”

chapter27_lvq.m Source code is as follows :

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function : LVQ Neural network prediction —— Face recognition 
% Environmental Science :Win7,Matlab2015b
%Modi: C.S
% Time :2022-06-20
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% LVQ Neural network prediction —— Face recognition 

%%  Clear environment variables 
clear all
clc
tic
%%  Face feature vector extraction  
%  The number of 
M = 10;
%  Number of face orientation categories 
N = 5; 
%  Feature vector extraction 
pixel_value = feature_extraction(M,N);

%%  Training set / Test set generation 
%  Generate a random sequence of image sequence numbers 
rand_label = randperm(M*N);  
%  The face faces toward the label 
direction_label = repmat(1:N,1,M);
%  Training set 
train_label = rand_label(1:30);
P_train = pixel_value(train_label,:)';
Tc_train = direction_label(train_label);
T_train = ind2vec(Tc_train);
%  Test set 
test_label = rand_label(31:end);
P_test = pixel_value(test_label,:)';
Tc_test = direction_label(test_label);

%%  establish LVQ The Internet 
for i = 1:5
    rate{
    i} = length(find(Tc_train == i))/30;
end
net = newlvq(minmax(P_train),20,cell2mat(rate),0.01,'learnlv1');
%  Set training parameters 
net.trainParam.epochs = 100;
net.trainParam.goal = 0.001;
net.trainParam.lr = 0.1;

%%  Training network 
net = train(net,P_train,T_train);

%%  Face recognition test 
T_sim = sim(net,P_test);
Tc_sim = vec2ind(T_sim);
result = [Tc_test;Tc_sim]

%%  Results show 
%  Training set face label 
strain_label = sort(train_label);
htrain_label = ceil(strain_label/N);
%  Training set face orientation label 
dtrain_label = strain_label - floor(strain_label/N)*N;
dtrain_label(dtrain_label == 0) = N;
%  Displays the training set image sequence number 
disp(' The training set image is :' );
for i = 1:30 
    str_train = [num2str(htrain_label(i)) '_'...
               num2str(dtrain_label(i)) ' '];
    fprintf('%s',str_train)
    if mod(i,5) == 0
        fprintf('\n');
    end
end
%  Test set face label 
stest_label = sort(test_label);
htest_label = ceil(stest_label/N);
%  Test set face orientation label 
dtest_label = stest_label - floor(stest_label/N)*N;
dtest_label(dtest_label == 0) = N;
%  Displays the test set image sequence number 
disp(' The test set image is :');
for i = 1:20 
    str_test = [num2str(htest_label(i)) '_'...
              num2str(dtest_label(i)) ' '];
    fprintf('%s',str_test)
    if mod(i,5) == 0
        fprintf('\n');
    end
end
%  Display recognition error image 
error = Tc_sim - Tc_test;
location = {
    ' left side ' ' Left front ' ' In front of the ' ' Right front ' ' right '};
for i = 1:length(error)
    if error(i) ~= 0
        %  Recognize the face label of the wrong image 
        herror_label = ceil(test_label(i)/N);
        %  Identify the face orientation label of the wrong image 
        derror_label = test_label(i) - floor(test_label(i)/N)*N;
        derror_label(derror_label == 0) = N;
        %  The original orientation of the image 
        standard = location{
    Tc_test(i)};
        %  The result of image recognition is towards 
        identify = location{
    Tc_sim(i)};
        str_err = strcat([' Images ' num2str(herror_label) '_'...
                        num2str(derror_label) ' Recognition error .']);
        disp([str_err '( Correct result : toward ' standard...
                      '; Recognition result : toward ' identify ')']);
    end
end
%  Display the recognition rate 
disp([' The recognition rate is :' num2str(length(find(error == 0))/20*100) '%']);

toc

Add completed , Click on “ function ”, Start emulating , The output simulation results are as follows :


result =

  1  to  19  Column 

     2     4     3     3     1     1     1     5     2     2     4     2     5     4     1     1     5     3     2
     2     4     3     3     4     1     4     5     2     2     4     2     5     4     1     1     5     3     2

  20  Column 

     1
     1

 The training set image is :
1_5  2_1  2_3  2_5  3_1  
3_2  3_3  3_4  3_5  4_1  
4_3  4_4  5_2  5_3  5_4  
5_5  6_1  6_3  6_5  7_2  
7_3  7_4  8_2  8_4  9_2  
9_4  9_5  10_3  10_4  10_5  
 The test set image is :
1_1  1_2  1_3  1_4  2_2  
2_4  4_2  4_5  5_1  6_2  
6_4  7_1  7_5  8_1  8_3  
8_5  9_1  9_3  10_1  10_2  
 Images 5_1 Recognition error .( Correct result : Towards the left ; Recognition result : Towards the front right )
 Images 9_1 Recognition error .( Correct result : Towards the left ; Recognition result : Towards the front right )
 The recognition rate is :90%
 Time has passed  4.401417  second .

 Insert picture description here
In turn, click Plots Medium Performance,Training State,Confusion,Receiver Operating Characteristic The following graphical results are obtained :
 Insert picture description here
 Insert picture description here
 Insert picture description here
 Insert picture description here

3. MATLAB Simulation example 2

Click... In the current folder view box chapter27_bp.m
 Insert picture description here
open chapter27_bp.m Source code is as follows :

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function : BP Neural network prediction —— Face recognition 
% Environmental Science :Win7,Matlab2015b
%Modi: C.S
% Time :2022-06-20
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% BP Neural network prediction —— Face recognition 

%%  Clear environment variables 
clear all
clc
tic
%%  Face feature vector extraction  
%  The number of 
M = 10;
%  Number of face orientation categories 
N = 5; 
%  Feature vector extraction 
pixel_value = feature_extraction(M,N);

%%  Training set / Test set generation 
%  Generate a random sequence of image sequence numbers 
rand_label = randperm(M*N);  
%  The face faces toward the label 
direction_label = [1 0 0;1 1 0;0 1 0;0 1 1;0 0 1];
%  Training set 
train_label = rand_label(1:30);
P_train = pixel_value(train_label,:)';
dtrain_label = train_label - floor(train_label/N)*N;
dtrain_label(dtrain_label == 0) = N;
T_train = direction_label(dtrain_label,:)';
%  Test set 
test_label = rand_label(31:end);
P_test = pixel_value(test_label,:)';
dtest_label = test_label - floor(test_label/N)*N;
dtest_label(dtest_label == 0) = N;
T_test = direction_label(dtest_label,:)';

%%  establish BP The Internet 
net = newff(minmax(P_train),[10,3],{
    'tansig','purelin'},'trainlm');
%  Set training parameters 
net.trainParam.epochs = 1000;
net.trainParam.show = 10;
net.trainParam.goal = 1e-3;
net.trainParam.lr = 0.1;

%%  Network training 
net = train(net,P_train,T_train);

%%  The simulation test 
T_sim = sim(net,P_test);
for i = 1:3
    for j = 1:20
        if T_sim(i,j) < 0.5
            T_sim(i,j) = 0;
        else
            T_sim(i,j) = 1;
        end
    end
end
T_sim
T_test
toc

Click on “ function ”, Start emulating , The output simulation results are as follows :


T_sim =

  1  to  19  Column 

     0     0     0     0     0     0     1     0     1     0     0     1     0     0     0     1     1     0     0
     1     0     0     0     0     1     0     1     1     1     1     0     0     1     1     1     1     0     0
     1     0     0     1     1     0     0     0     0     1     1     0     1     1     0     1     0     1     0

  20  Column 

     1
     1
     0


T_test =

  1  to  19  Column 

     0     0     1     0     0     0     1     0     0     0     0     1     0     0     0     1     1     0     1
     1     0     0     1     0     1     0     1     1     1     0     0     0     1     1     1     1     0     0
     1     1     0     1     1     0     0     0     0     1     1     0     1     1     0     0     0     1     0

  20  Column 

     1
     1
     0

 Time has passed  3.215773  second .

 Insert picture description here
In turn, click Plots Medium Performance,Training State,Regression The following figure is available :
 Insert picture description here
 Insert picture description here
 Insert picture description here

4. MATLAB Simulation example 3

Click... In the current folder view box chapter_svm.m
 Insert picture description here
open chapter_svm.m Source code is as follows :

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function : SVM The forecast —— Face recognition 
% Environmental Science :Win7,Matlab2015b
%Modi: C.S
% Time :2022-06-20
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% SVM The forecast —— Face recognition 

%%  Clear environment variables 
clear all
clc
warning off
tic
%%  Face feature vector extraction  
%  The number of 
M = 10;
%  Number of face orientation categories 
N = 5; 
%  Feature vector extraction 
pixel_value = feature_extraction(M,N);
%  normalization 
pixel_value = premnmx(pixel_value);

%%  Training set / Test set generation 
%  Generate a random sequence of image sequence numbers 
rand_label = randperm(M*N);  
%  The face faces toward the label 
direction_label = repmat(1:N,1,M);
%  Training set 
rand_train = rand_label(1:30);
Train = pixel_value(rand_train,:);
Train_label = direction_label(rand_train)';
%  Test set 
rand_test = rand_label(31:end);
Test = pixel_value(rand_test,:);
Test_label = direction_label(rand_test)';
% SVM Model 
model = svmtrain(Train_label,Train,'-c 2 -g 0.05');
%  The simulation test 
[predict_label,accuracy] = svmpredict(Test_label,Test,model);
result_svm = [Test_label';predict_label']
toc

Click on “ function ”, Start emulating , The output simulation results are as follows :

Accuracy = 100% (20/20) (classification)

result_svm =

  1  to  19  Column 

     5     2     4     3     5     3     4     4     3     2     4     4     3     5     3     5     1     5     2
     5     2     4     3     5     3     4     4     3     2     4     4     3     5     3     5     1     5     2

  20  Column 

     4
     4

 Time has passed  0.832052  second .

The sample image folder is as follows :
 Insert picture description here

5. Summary

This chapter continues with LVQ Neural network for prediction , Simultaneous adoption BP Neural networks and SVM Contrast , The result is shown in the figure above , You can see the prediction effect LVQ neural network <BP neural network <SVM, Thought of other LVQ Examples of neural networks , You can go back to the previous chapter of this column to learn about . Interested in the content of this chapter or want to fully learn and understand , It is suggested to study the contents of chapter 27 in the book . Some of these knowledge points will be supplemented on the basis of their own understanding in the later stage , Welcome to study and exchange together .

The article in the previous chapter :《MATLAB neural network 43 A case study 》: The first 26 Chapter LVQ Classification of neural networks —— Breast tumor diagnosis

原网站

版权声明
本文为[mozun2020]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206211528535593.html