images classification with cnn error

Hi all, I run cnn with some images but I get an error what is wrong?
error:
Error using trainNetwork (line 154)
Invalid training data. X and Y must have the same number of observations.
Error in cnnmatlb (line 35)
net = trainNetwork(trainD,targetD',layers,options);
Caused by:
Error using nnet.internal.cnn.util.NetworkDataValidator/assertXAndYHaveSameNumberOfObservations (line 142)
Invalid training data. X and Y must have the same number of observations.
clear all
clc
Path = 'D:\eegdata\*.jpg';
Files = dir(Path);
for i=1:length(Files)
fn = [Path(1:end-5) Files(i,1).name];
I=imread(fn);
I=imresize(I,[28,28]);
I(:,:,3)=rgb2gray(I);
[m,n,o]=size(I);
if i<=30
trainD(:,:,:,1)= I(:,:,:);
end
if i>=31
trainD(:,:,:,2)=I(:,:,:);
end
targetD=categorical([1;2]);
%% Define Network Architecture
% Define the convolutional neural network architecture.
layers = [
imageInputLayer([m n o]) % 22X5X1 refers to number of features per sample
convolution2dLayer(n,16,'Padding','same')
reluLayer
fullyConnectedLayer(384) % 384 refers to number of neurons in next FC hidden layer
fullyConnectedLayer(384) % 384 refers to number of neurons in next FC hidden layer
fullyConnectedLayer(2) % 6 refers to number of neurons in next output layer (number of output classes)
softmaxLayer
classificationLayer];
options = trainingOptions('sgdm',...
'MaxEpochs',5000, ...
'Plots','training-progress');
net = trainNetwork(trainD,targetD',layers,options);
predictedLabels = classify(net,trainD)'
end

3 Comments

There's not anyone can help me in this issue???
What does this show in the command window
whos trainD
whos targetD
Hi,trainD is two class image and targetD is the prediction of two class
ero cnn.png

Sign in to comment.

Answers (0)

Categories

Find more on Deep Learning Toolbox in Help Center and File Exchange

Asked:

on 23 Dec 2019

Commented:

on 24 Dec 2019

Community Treasure Hunt

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

Start Hunting!