필터 지우기
필터 지우기

How to Train CNN on Custom dataset in matrix form

조회 수: 30 (최근 30일)
Med Future
Med Future 2022년 2월 14일
댓글: yanqi liu 2022년 2월 17일
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);

채택된 답변

yanqi liu
yanqi liu 2022년 2월 14일
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
Med Future
Med Future 2022년 2월 17일
@yanqi liu Can you please share 1D CNN network for this?
yanqi liu
yanqi liu 2022년 2월 17일
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];

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Image Data Workflows에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by