本人深度学习小白,想用matlab做两组传感器数据的神经网络特征融合,不知道我的想法对不对,我在工具箱里这么编写了一个基于AlexNet的神经网络模型,两组数据经过相同的卷积层,再通过一个additionLayer连接层绑两组数据连接起来,再通过三个全连接层,最后输出三个分类。
代码是通过深度学习工具箱生成的,如下
lgraph = layerGraph();
tempLayers = [
imageInputLayer([227 227 3],"Name","data1")
convolution2dLayer([11 11],96,"Name","conv1_1","BiasLearnRateFactor",2,"Stride",[4 4])
reluLayer("Name","relu1_1")
crossChannelNormalizationLayer(5,"Name","norm1_1","K",1)
maxPooling2dLayer([3 3],"Name","pool1_1","Stride",[2 2])
groupedConvolution2dLayer([5 5],128,2,"Name","conv1_2","BiasLearnRateFactor",2,"Padding",[2 2 2 2])
reluLayer("Name","relu1_2")
crossChannelNormalizationLayer(5,"Name","norm1_2","K",1)
maxPooling2dLayer([3 3],"Name","pool1_2","Stride",[2 2])
convolution2dLayer([3 3],384,"Name","conv1_3","BiasLearnRateFactor",2,"Padding",[1 1 1 1])
reluLayer("Name","relu1_3")
groupedConvolution2dLayer([3 3],192,2,"Name","conv1_4","BiasLearnRateFactor",2,"Padding",[1 1 1 1])
reluLayer("Name","relu1_4")
groupedConvolution2dLayer([3 3],128,2,"Name","conv1_5","BiasLearnRateFactor",2,"Padding",[1 1 1 1])
reluLayer("Name","relu1_5")
maxPooling2dLayer([3 3],"Name","pool1_5","Stride",[2 2])];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
imageInputLayer([227 227 3],"Name","data2")
convolution2dLayer([11 11],96,"Name","conv2_1","BiasLearnRateFactor",2,"Stride",[4 4])
reluLayer("Name","relu2_1")
crossChannelNormalizationLayer(5,"Name","norm2_1","K",1)
maxPooling2dLayer([3 3],"Name","pool2_1","Stride",[2 2])
groupedConvolution2dLayer([5 5],128,2,"Name","conv2_2","BiasLearnRateFactor",2,"Padding",[2 2 2 2])
reluLayer("Name","relu2_2")
crossChannelNormalizationLayer(5,"Name","norm2_2","K",1)
maxPooling2dLayer([3 3],"Name","pool2_2","Stride",[2 2])
convolution2dLayer([3 3],384,"Name","conv2_3","BiasLearnRateFactor",2,"Padding",[1 1 1 1])
reluLayer("Name","relu2_3")
groupedConvolution2dLayer([3 3],192,2,"Name","conv2_4","BiasLearnRateFactor",2,"Padding",[1 1 1 1])
reluLayer("Name","relu2_4")
groupedConvolution2dLayer([3 3],128,2,"Name","conv2_5","BiasLearnRateFactor",2,"Padding",[1 1 1 1])
reluLayer("Name","relu2_5")
maxPooling2dLayer([3 3],"Name","pool2_5","Stride",[2 2])];
lgraph = addLayers(lgraph,tempLayers);
tempLayers = [
additionLayer(2,"Name","addition")
fullyConnectedLayer(4096,"Name","fc6","BiasLearnRateFactor",2)
reluLayer("Name","relu6")
dropoutLayer(0.5,"Name","drop6")
fullyConnectedLayer(4096,"Name","fc7","BiasLearnRateFactor",2)
reluLayer("Name","relu7")
dropoutLayer(0.5,"Name","drop7")
fullyConnectedLayer(3,"Name","fc8","BiasLearnRateFactor",2)
softmaxLayer("Name","prob")
classificationLayer("Name","classoutput")];
lgraph = addLayers(lgraph,tempLayers);
% 清理辅助变量
clear tempLayers;
lgraph = connectLayers(lgraph,"pool1_5","addition/in1");
lgraph = connectLayers(lgraph,"pool2_5","addition/in2");
figure
plot(lgraph);
layers = lgraph.Layers;
%% 训练数据存储;
allImages1 = imageDatastore("E:\Data\old-new\CWT\1\",...
'IncludeSubfolders',true,...
'LabelSource','foldernames');
allImages2= imageDatastore("E:\Data\old-new\CWT\2\",...
'IncludeSubfolders',true,...
'LabelSource','foldernames');
%% 数据处理;
rng default
[imgsTrain1,imgsValidation1] = splitEachLabel(allImages1,0.8,'randomized');%按比例拆分 ImageDatastore 标签
disp(['Number of training images: ',num2str(numel(imgsTrain1.Files))]);
disp(['Number of validation images: ',num2str(numel(imgsValidation1.Files))]);
[imgsTrain2,imgsValidation2] = splitEachLabel(allImages2,0.8,'randomized');%按比例拆分 ImageDatastore 标签
disp(['Number of training images: ',num2str(numel(imgsTrain2.Files))]);
disp(['Number of validation images: ',num2str(numel(imgsValidation2.Files))]);
%% 处理输入的图片;
inputSize = layers(1).InputSize;
augimgsTrain1 = augmentedImageDatastore(inputSize(1:2),imgsTrain1); % 调整图像大小以匹配网络输入层
augimgsValidation1 = augmentedImageDatastore(inputSize(1:2),imgsValidation1);
augimgsTrain2 = augmentedImageDatastore(inputSize(1:2),imgsTrain2); % 调整图像大小以匹配网络输入层
augimgsValidation2 = augmentedImageDatastore(inputSize(1:2),imgsValidation2);
augimgsTrain = combine(augimgsTrain1,augimgsTrain2);
% augimgsValidation = combine(augimgsValidation1,augimgsValidation2);
但是训练时会一直报错,ValidationData设置为空因为我不知道怎么把augimgsValidation1 augimgsValidation2两个结构体的验证集放到一块,‘opts =’ 这行会报错误,所以上面也就注掉了
训练的代码如下
%% 设置参数进行训练
rng default
mbSize = 40;
mxEpochs = 40;
ilr = 1e-4;
plt = 'training-progress';
opts = trainingOptions('sgdm',...
'InitialLearnRate',ilr, ...
'MaxEpochs',mxEpochs ,...
'MiniBatchSize',mbSize, ...
'ValidationData',[],...
'ExecutionEnvironment','multi-gpu',...
'Plots',plt);
[trainedAN,info2] = trainNetwork(augimgsTrain,layers,opts);
有没有会用matlab做深度学习的大佬,帮我看看这个,万分感谢!