1.软件版本
matlab2021a
2.本算法理论知识
GRNN,即General Regression Neural Network,中文全称为广义回归神经网络,是由The Lockheed Palo Alto研究实验室在1991年提出的。GRNN是一种新型的基于非线性回归理论的神经网络模型[43,44]。GRNN是建立在非参数核回归基础之上的,该神经网络是以测试样本为后验条件,并从观测样本中计算得到自变量和因变量之间的概率密度函数,然后在计算出因变量关于自变量的回归值。由于GRNN不需要规定模型的类型,只需要设置神经网络的光滑因子参数,GRNN神经网络的光滑因子参数的取值对神经网络的输出影响较大,当光滑因子参数较大的时候,其对应的神经元所覆盖的输入区域就越大;当光滑因子参数较小的时候,神经网络对应的径向基函数曲线较陡,因此神经网络输出结果更接近期望值,但此时光滑度越差。由于GRNN广义回归神经网络是基于非线性核回归分析的神经网络,因此,对于任意一个非独立变量y,其相对于独立变量x的回归分析的过程是计算具有最大概率值y。现假设随机变量x和y的联合概率密度函数为f (x ,y),已知x的观测值为X,则y相对于X的回归,即条件均值为:
从图的结构图可知,GRNN神经网络的输入层神经元数目和输入样本的维度是相同的,即每一个神经元将输入信号直接传递给GRNN神经网络的隐含层中。GRNN神经网络的模式层的神经元数目和学习训练样本的数目相同,即每一个神经元都分别对应着一个不同的学习训练样本,模式层中神经元的传递函数为:
3.核心代码
clc;
clear;
close all;
warning off;
addpath 'func\'
addpath 'func\Grnn\'
addpath 'func\PCA\'
%是否需要重新训练,1,重新训练,0不训练,直接测试
SEL = 1;
nPerson = 9;
nExpressionPerPerson = 7;
TEST_DATA = 2;%1:选择测试样本集合1,2:选择测试样本集合2
DIM = 120;
SET = [];
for flag = 1:3
flag
if SEL == 1
display('读入训练集合...');
if TEST_DATA == 1
%第一组样本
TrainData = readfaceFeature(nExpressionPerPerson, nPerson,0,130);
trainLabel = [1,1,2,2,3,3,3,4,4,4,5,5,6,6,7,7,1,1,2,2,2,3,3,4,4,5,5,6,6,7,7,1,1,2,3,3,4,4,4,5,5,6,6,6,7,7,1,1,2,2,3,3,4,4,5,5,6,6,7,7,1,1,2,2,3,3,4,4,4,5,5,6,6,7,1,1,2,3,3,4,4,5,5,6,6,7,7,1,1,2,2,3,3,4,4,5,5,6,6,7,7,1,1,2,2,3,3,4,4,5,5,6,6,7,7,1,1,2,2,3,3,3,4,4,5,5,6,6,7,7]';
%降低维度
pcaFaces = fastPCA(TrainData,DIM); % 主成分分析PCA
end
if TEST_DATA == 2
%第二组样本jaffe
TrainData = readfaceFeature2(nExpressionPerPerson, nPerson,0,140,flag);
trainLabel1 = [1,2,3,4,5,6,7,...
1,2,3,4,5,6,7,...
1,2,3,4,5,6,7,...
1,2,3,4,5,6,7,...
1,2,3,4,5,6,7,...
1,2,3,4,5,6,7,...
1,2,3,4,5,6,7,...
1,2,3,4,5,6,7,...
1,2,3,4,5,6,7,...
1,2,3,4,5,6,7]';
trainLabel = [trainLabel1;trainLabel1];
%降低维度
pcaFaces = fastPCA(TrainData,DIM); % 主成分分析PCA
end
X = pcaFaces;
TrainData = X;
[X,A0,B0] = scaling(X);
%替换算法,使用GRNN神经网络,否则原来的SVM我仔细研究下来,缺陷太大,几次改下来,性能也没到50%,所以选择pass
%但为了你方便写论文,我在文档中将GRNN神经网络的理论都给你写了,然后测试大概也写了,
net = multiGRNNTrain(TrainData',trainLabel',0.0015);
save NN_model.mat net A0 B0
display('..............................');
display('训练结束。');
end
display('读入测试集合...');
%降低维度
load NN_model.mat
load PCA.mat
if TEST_DATA == 1
%第一组样本
TestData0 = readfaceFeature(nExpressionPerPerson, nPerson,1,130);
TestLabel = [1,1,2,2,3,3,3,4,4,4,5,5,6,6,7,7,1,1,2,2,2,3,3,4,4,5,5,6,6,7,7,1,1,2,3,3,4,4,4,5,5,6,6,6,7,7,1,1,2,2,3,3,4,4,5,5,6,6,7,7,1,1,2,2,3,3,4,4,4,5,5,6,6,7,1,1,2,3,3,4,4,5,5,6,6,7,7,1,1,2,2,3,3,4,4,5,5,6,6,7,7,1,1,2,2,3,3,4,4,5,5,6,6,7,7,1,1,2,2,3,3,3,4,4,5,5,6,6,7,7]';
[m,n] = size(TestData0);
TestData = (TestData0-repmat(meanVec, m, 1))*V; % 经过pca变换降维
end
if TEST_DATA == 2
%第二组样本jaffe
TestData0 = readfaceFeature2(nExpressionPerPerson, nPerson,1,70,flag);
TestLabel = [1,2,3,4,5,6,7,...
1,2,3,4,5,6,7,...
1,2,3,4,5,6,7,...
1,2,3,4,5,6,7,...
1,2,3,4,5,6,7,...
1,2,3,4,5,6,7,...
1,2,3,4,5,6,7,...
1,2,3,4,5,6,7,...
1,2,3,4,5,6,7,...
1,2,3,4,5,6,7]';
[m,n] = size(TestData0);
TestData = (TestData0-repmat(meanVec, m, 1))*V; % 经过pca变换降维
end
classes = multiGRNNClassify(TestData',net);
nError = sum(classes ~= TestLabel);
accuracy = 1 - nError/length(TestLabel);
N1=0;
N2=0;
N3=0;
N4=0;
N5=0;
N6=0;
N7=0;
for i = 1:length(TestLabel)
if TestLabel(i) == 1 & classes(i) == 1
N1 = N1 + 1;
end
if TestLabel(i) == 2 & classes(i) == 2
N2 = N2 + 1;
end
if TestLabel(i) == 3 & classes(i) == 3
N3 = N3 + 1;
end
if TestLabel(i) == 4 & classes(i) == 4
N4 = N4 + 1;
end
if TestLabel(i) == 5 & classes(i) == 5
N5 = N5 + 1;
end
if TestLabel(i) == 6 & classes(i) == 6
N6 = N6 + 1;
end
if TestLabel(i) == 7 & classes(i) == 7
N7 = N7 + 1;
end
end
SET = [SET;[N1,N2,N3,N4,N5,N6,N7]/10];
end
fprintf('\n')
display(['对于测试集人脸样本的生气识别率为', num2str(mean(SET(:,1))*100), '%']);fprintf('\n')
display(['对于测试集人脸样本的厌恶识别率为', num2str(mean(SET(:,2))*100), '%']);fprintf('\n')
display(['对于测试集人脸样本的害怕识别率为', num2str(mean(SET(:,3))*100), '%']);fprintf('\n')
display(['对于测试集人脸样本的开心识别率为', num2str(mean(SET(:,4))*100), '%']);fprintf('\n')
display(['对于测试集人脸样本的中性识别率为', num2str(mean(SET(:,5))*100), '%']);fprintf('\n')
display(['对于测试集人脸样本的悲伤识别率为', num2str(mean(SET(:,6))*100), '%']);fprintf('\n')
display(['对于测试集人脸样本的惊讶识别率为', num2str(mean(SET(:,7))*100), '%']);fprintf('\n')
display(['整体识别率为', num2str(mean2(SET)*100), '%']);fprintf('\n')
4.操作步骤与仿真结论
5.参考文献
[1]王镇镇. 基于人脸子区域加权和LDA的表情识别算法[D]. 大连海事大学, 2017.
A05-45
6.完整源码获得方式
方式1:微信或者QQ联系博主
方式2:订阅MATLAB/FPGA教程,免费获得教程案例以及任意2份完整源码