MATLAB Answers

I have started using Deep Learning Toolbox and getting error "Error using trainNetwork (line 183) Number of observations in X and Y disagree."

47 views (last 30 days)
Tanu Shree
Tanu Shree on 14 Dec 2020
Answered: Srivardhan Gadila on 17 Dec 2020
I am using below code. It works perfectly fine when I have the 4 D image. When I change it to 3D it starts giving the error. Any help would be highly appreciated.
load('C:\Users\myROIFC_GroupTwo.mat');
load('C:\Users\myROIFC_GroupZero.mat');
trainingLabels = [zeros(30,1);2*ones(23,1)];
trainingLabels = categorical(trainingLabels);
trainingImages=[myROIfc_GroupZero;myROIfc_GroupTwo];
size(myROIfc_GroupZero)
size(trainingImages)
testImages = trainingImages;
trainingImagesNew = shiftdim(trainingImages,1);
size(trainingImagesNew)
testImagesNew = shiftdim(testImages,1);
trainingImagesNew= (reshape(trainingImagesNew,116,116,53));
testImagesNew= (reshape(testImagesNew,116,116,53));
numTrainingImages=size(trainingImagesNew,3);
idx = randperm(size(trainingImagesNew,3),floor(numTrainingImages/2));
XValidation = trainingImagesNew(:,:,idx);
trainingImagesNew(:,:,idx) = [];
YValidation = trainingLabels(idx);
trainingLabels(idx) = [];
opts = trainingOptions('sgdm', 'Plots', 'training-progress', ...
'Momentum', 0.93, ...
'InitialLearnRate', 0.01, ... %0.001
'LearnRateSchedule', 'piecewise', ...
'LearnRateDropFactor', 0.1, ... %0.1
'LearnRateDropPeriod', 8, ... %8
'L2Regularization', 0.004, ...
'ValidationFrequency',15,...
'MaxEpochs',80 , ... %40
'MiniBatchSize', 24, ...
'ValidationData',{XValidation,YValidation}, ...
'Verbose', true);
[height,width,numChannels, ~] = size(trainingImagesNew);
imageSize = [height width numChannels];
inputLayer = imageInputLayer(imageSize);
numImageCategories = length(unique(trainingLabels));
filterSize = [11 11];
numFilters = 64;
middleLayers = [
convolution2dLayer(filterSize,numFilters,'Padding',3)
reluLayer()
maxPooling2dLayer(3,'Stride',2)
convolution2dLayer(filterSize,numFilters,'Padding',3) %2
reluLayer()
maxPooling2dLayer(3, 'Stride',2)
convolution2dLayer(filterSize,2 * numFilters,'Padding',2)
reluLayer()
maxPooling2dLayer(3,'Stride',2)
]
finalLayers = [
fullyConnectedLayer(64)
reluLayer
fullyConnectedLayer(numImageCategories)
softmaxLayer
classificationLayer
]
layers = [
inputLayer
middleLayers
finalLayers
]
layers(2).Weights = 0.0001 * randn([filterSize numChannels numFilters]);
tic
GWI_RCNNv0 = trainNetwork(trainingImagesNew, trainingLabels, layers, opts);
toc

  0 Comments

Sign in to comment.

Answers (1)

Srivardhan Gadila
Srivardhan Gadila on 17 Dec 2020
As you are using the following syntax of the trainNetwork function: net = trainNetwork(X,Y,layers,options), you can refer to the corresponding explanation of the format of the X & Y to understand the cause of the error.
Image or feature data, specified as a numeric array. The size of the array depends on the type of input:
Input: Description
2-D images: A h-by-w-by-c-by-N numeric array, where h, w, and c are the height, width, and number of channels of the images, respectively, and N is the number of images.
3-D images: A h-by-w-by-d-by-c-by-N numeric array, where h, w, d, and c are the height, width, depth, and number of channels of the images, respectively, and N is the number of images.
Features: A N-by-numFeatures numeric array, where N is the number of observations and numFeatures is the number of features of the input data.
Y — Responses (categorical vector of labels | numeric array | cell array of categorical sequences | cell array of numeric sequences)
Responses, specified as a categorical vector of labels, a numeric array, a cell array of categorical sequences, or cell array of numeric sequences. The format of Y depends on the type of task. Responses must not contain NaNs.
Task: Format
Image or feature classification or Sequence-to-label classification: N-by-1 categorical vector of labels, where N is the number of observations.
Sequence-to-sequence classification: N-by-1 cell array of categorical sequences of labels, where N is the number of observations. Each sequence must have the same number of time steps as the corresponding predictor sequence.
For sequence-to-sequence classification tasks with one observation, sequences can also be a vector. In this case, Y must be a categorical sequence of labels.

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by