Why I can't use trainetwork for the CNN3D using convolution3DLayer

Here is my code:
clc;clear all;
%%
X_train = zeros(9,9,4,20);
Y_train = categorical(randi(2,[20,1]));
%%
trainD=X_train;
targetD=Y_train;
[row collum di]=size(trainD(:,:,:,1));
%% Define Network Architecture
% Define the convolutional neural network architecture.
lgraph = layerGraph;
layers = [
image3dInputLayer([row collum di 1],'Name','input') % 9x9x4x1 refers to number of features per sample
convolution3dLayer([2 2 2],5,'stride',[1 1 1],'padding','same','Name','conv_1')
batchNormalizationLayer('Name','BN_1')
reluLayer('Name','Relu_1')
dropoutLayer(0.5,'Name','Dropout1')
fullyConnectedLayer(1024,'Name','Fc1')
fullyConnectedLayer(2,'Name','Fc2')
softmaxLayer('Name','Softmax')
classificationLayer('Name','ClassOutput')];
options = trainingOptions('adam',...
'MaxEpochs',1000, ...
'Shuffle','every-epoch', ...
'InitialLearnRate',1.0000e-04, ...
'L2Regularization',0.5,...
'Verbose',true,...
'VerboseFrequency',5,...
'Plots','training-progress',...
'OutputFcn',@(info)stopIfAccuracyNotImproving(info,3));
%%
net = trainNetwork(trainD,targetD,layers,options);
predictedLabels = classify(net,trainD)';
%%
%caculate accuracy (need change code a litte (do later))
YPred = classify(net,valD);%fix this add our eeg
YValidation = vallab;%fix this add our label
accuracy = sum(YPred == YValidation)/numel(YValidation)*100
The error is:
Error using trainNetwork (line 165)
Number of observations in X and Y disagree.
Error in test_CNN3D (line 48)
net = trainNetwork(trainD,targetD,layers,options);
I need some help as soon as possible please.
Thanks

3 Comments

modify train data, X_train = zeros(9,9,4,1,20);
Thanks for your help. I change it and it work now but can you explane me why i need to set the train data to 5 dimensions.
According to the documentation on “image3dInputLayer()”, the layer expects an input data of the form where h, w, d and c correspond to the height, width, depth and number of channels respectively.
Here the “X_train" is initialized to have a size of 9x9x4X20 and I am assuming that your input data is a 3-D grayscale image. So the “trainNetwork()”function expects each sample of the training data to be of the format or (depending on grayscale or color images) and the training data to be of the form
With the original initializations, the no. of channels would be treated as 20 which may trigger the internal input argument validation processes. Adding a singleton dimension as the fourth index and shifting the number of samples to the fifth index changes the training data into the appropriate format and thereby removes the error message.

Sign in to comment.

Answers (0)

Categories

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

Products

Release

R2019a

Asked:

on 12 Nov 2019

Commented:

on 21 Nov 2019

Community Treasure Hunt

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

Start Hunting!