卷积层的结构如下所示:
具体可以看如下的网址:
这个是传统的卷积神经网络。我们按这个进行设计。仿真结果如下:
他的识别率为88.89%。
然后我这里介绍下我们的改进思路,改进思路如下所示:
此外, 影响卷积网络的参数主要为学习率的设置,然后我们通过WOA优化算法计算最优的学习率。关于WOA的原理,请参考文献:
这个部分,将得到的最优的学习率为:0.0408
训练过程为:
通过测试可以知道,
GRNN识别率为80%
CNN的识别率为88.89%
基于改进CNN,识别率为:95.56%
部分核心代码如下:
clc;
clear;
close all;
warning off;
addpath 'subfunc\'
rng(1);
digitDatasetPath = ['步态能量图\'];
imds = imageDatastore(digitDatasetPath,'IncludeSubfolders', true, 'LabelSource', 'foldernames');
%划分数据为训练集合验证集,训练集中每个类别包含1张图像,验证集包含其余图像的标签
numTrainFiles = 1;%设置每个类别的训练个数
[imdsTrain, imdsValidation] = splitEachLabel(imds, numTrainFiles, 'randomize');
%定义卷积神经网络的基础结构
layers = [
imageInputLayer([400 150 1]);%注意,400,150为能量图的大小,不能改
%第1个卷积层
convolution2dLayer(3, 8, 'Padding', 'same');%第一个卷积层
batchNormalizationLayer;
%第1个激励层
reluLayer;
%第1个池化层
maxPooling2dLayer(2, 'Stride', 2);
%第2个卷积层
convolution2dLayer(3, 8, 'padding', 'same');
batchNormalizationLayer;
%第2个激励层
reluLayer;
%第2个池化层
maxPooling2dLayer(2, 'Stride', 2);
%第3个卷积层
convolution2dLayer(3, 16, 'Padding', 'same');
batchNormalizationLayer;
%第3个激励层
reluLayer;
%第3个池化层
maxPooling2dLayer(2, 'Stride', 2);
%全连接层1
fullyConnectedLayer(5);
%softmax
softmaxLayer;
%输出分类结果
classificationLayer;];
%设置训练参数
Num = 10; %搜索数量
Iters = 10; %迭代次数
D = 1; %搜索空间维数
woa_idx = zeros(1,D);
woa_get = inf;
%初始化种群的个体
%初始化种群的个体
Xmin = 0;
Xmax = 1;
xwoa=rand(Num,D);
for t=1:Iters
t
for i=1:Num
%目标函数更新
[pa(i)] = fitness(xwoa(i,:),imdsTrain, imdsValidation,layers);
Fitout = pa(i);
%更新
if Fitout < woa_get
woa_get = Fitout;
woa_idx = xwoa(i,:);
end
end
%调整参数
c1 = 2-t*((1)/Iters);
c2 =-1+t*((-1)/Iters);
% w = 0.1+0.8*(cos(std(pa)));
%位置更新
for i=1:Num
rng(i);
r1 = rand();
r2 = rand();
K1 = 2*c1*r1-c1;
K2 = 2*r2;
l =(c2-1)*rand + 1;
rand_flag = rand();
if rand_flag<0.5
if abs(K1)>=1
RLidx = floor(Num*rand()+1);
X_rand = xwoa(RLidx, :);
D_X_rand = abs(K2*X_rand(1:D)-xwoa(i,1:D));
xwoa(i,1:D)= X_rand(1:D)-K1*D_X_rand;
else
D_Leader = abs(K2*woa_idx(1:D)-xwoa(i,1:D));
xwoa(i,1:D)= woa_idx(1:D)-K1*D_Leader;
end
else
distLeader = abs(woa_idx(1:D)-xwoa(i,1:D));
xwoa(i,1:D) = distLeader*exp(l).*cos(l.*2*pi)+woa_idx(1:D);
end
end
[pb] = fitness(woa_idx,imdsTrain, imdsValidation,layers);
end
rng(1);
options = trainingOptions('sgdm', ...
'InitialLearnRate', abs(woa_idx/5), ...
'MaxEpochs', 200, ...
'Shuffle', 'every-epoch', ...
'ValidationData', imdsValidation, ...
'ValidationFrequency', 10, ...
'Verbose', false, ...
'Plots', 'training-progress');
%使用训练集训练网络
net = trainNetwork(imdsTrain, layers, options);
%对验证图像进行分类并计算精度
YPred = classify(net, imdsValidation);
YValidation = imdsValidation.Labels;
accuracy = sum(YPred == YValidation) / numel(YValidation);
accuracy
运行结果如下:
A05-77