How to Train CNN on Custom dataset in matrix form

39 visualizzazioni (ultimi 30 giorni)
Hi everyone, i hope you are doing well
i have the following dataset myFile.txt includes 102x5,in which first 4 coloums are the Number of Observation and the last column are the Discrete labels/Classes for the dataset. I want to train simple CNN on this dataset
How i can randomly split the data into train/validation/ test and reshape the data to train simple CNN
when i run numel(trainlabel) it shows 102 Classes but i have only 5 Classes
I have got this error when i run the below code
Caused by:
Layer 2: Input size mismatch. Size of input to this layer is different from the expected input size.
Inputs to this layer:
from layer 1 (size 102(S) × 4(S) × 1(C) × 1(B))
%myFile.txt includes 102x5,in which first 4 coloums are the
% Number of Observation and the last 5 column are the Discrete labels/Classes for
% the dataset
dataset=readmatrix('myFile.txt');
traindata=dataset(:,1:4);
trainlabel=categorical(dataset(:,5));
% The following show 102 Classes but i have only 5 Classes
numClasses = numel(trainlabel)
%How to Split Data Randomly into Train,Validation and Test
% The below code Split the data 80/20 in Train and Test but not randomly
PD = 0.80 ; % percentage 80%
Ptrain = traindata(1:round(PD*length(traindata)),:) ; Ttrain = trainlabel(1:round(PD*length(traindata))) ;
Ptest = traindata(round(PD*length(traindata)):end,:) ;Ttest = trainlabel(round(PD*length(traindata)):end) ;
% I have defined the following Network for training,
layers = [ ...
imageInputLayer([102 4 1])
convolution2dLayer(5,20)
reluLayer
maxPooling2dLayer(2,'Stride',2)
fullyConnectedLayer(5)
softmaxLayer
classificationLayer];
options = trainingOptions('sgdm', ...
'MaxEpochs',20,...
'InitialLearnRate',1e-4, ...
'Verbose',false, ...
'Plots','training-progress');
net = trainNetwork(traindata,trainlabel,layers,options);

Risposta accettata

yanqi liu
yanqi liu il 14 Feb 2022
yes,sir,may be reshape your data,such as
sz = size(dataset);
dataset = dataset(randperm(sz(1)),:);
traindata=dataset(:,1:4);
trainlabel=categorical(dataset(:,5));
classes = unique(trainlabel)
numClasses = numel(unique(trainlabel))
PD = 0.80 ;
Ptrain = []; Ttrain = [];
Ptest = []; Ttest = [];
for i = 1 : length(classes)
indi = find(trainlabel==classes(i));
indi = indi(randperm(length(indi)));
indj = round(length(indi)*PD);
Ptrain = [Ptrain; traindata(indi(1:indj),:)]; Ttrain = [Ttrain; trainlabel(indi(1:indj),:)];
Ptest = [Ptest; traindata(indi(1+indj:end),:)]; Ttest = [Ttest; trainlabel(indi(1+indj:end),:)];
end
Ptrain=(reshape(Ptrain', [4,1,1,size(Ptrain,1)]));
Ptest=(reshape(Ptest', [4,1,1,size(Ptest,1)]));
layers = [imageInputLayer([4 1 1])
convolution2dLayer([3 1],3,'Stride',1)
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2,'Padding',[0 0 0 1])
dropoutLayer
fullyConnectedLayer(numClasses)
softmaxLayer
classificationLayer];
options = trainingOptions('adam', ...
'MaxEpochs',3000, ...
'Shuffle','every-epoch', ...
'Plots','training-progress', ...
'Verbose',false, ...
'ValidationData',{Ptest,Ttest},...
'ExecutionEnvironment', 'cpu', ...
'ValidationPatience',Inf);
net = trainNetwork(Ptrain,Ttrain,layers,options);
  10 Commenti
Med Future
Med Future il 17 Feb 2022
@yanqi liu Can you please share 1D CNN network for this?
yanqi liu
yanqi liu il 17 Feb 2022
yes,sir,may be make model structure like this
layers = [ ...
sequenceInputLayer(4)
lstmLayer(100,'OutputMode','sequence')
dropoutLayer(0.3)
lstmLayer(50,'OutputMode','sequence')
dropoutLayer(0.2)
fullyConnectedLayer(numClasses)
softmaxLayer
classificationLayer];

Accedi per commentare.

Più risposte (0)

Prodotti


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by