trainNetwork - Number of observations in X and Y disagree

Hi,
I'm training a network that has an imageInputLayer with image size of [498,13]. I have 1600 instances of these 498x13 images in my training data set.
The dimensions of the variables I'm passing to the trainNetwork function are the following:
Xtrain: 498x13x1600
Labels: 1600x1
These dimensions are as reccomended in the documentation:
Dimensions for X: hxwxN & Dimensions for Y: Nx1 are as specified in the answer to a question on this error asked previously.
But I'm still getting the error "Number of observations in X and Y disagree" when I call trainNetwork(Xtrain, Labels, layers, options)
Do not understand why I'm getting the error.
Thanks,
Nikhil

2 comentarios

Just to be clear, replace this line
trainNetwork(Xtrain, Labels, layers, options)
with these lines
whos Xtrain
whos Labels
trainNetwork(Xtrain, Labels, layers, options)
and tell us what you see in the command window.
Here's what I get I run the code below:
whos XtrainShaped
whos TTrain
trainedNet = trainNetwork(XtrainShaped,TTrain,layers,options);

Iniciar sesión para comentar.

Respuestas (2)

Rahul
Rahul el 12 de Oct. de 2022
"XtrainShaped" dimension should be [img_height, img_width, img_planes, num_samples]. "img_planes" denotes whether is image is RGB (img_planes = 3) or grayscale (img_planes = 1). In your dataset, the network is thinking the number of planes are 1600 with only one sample whereas length of the Labels categorical vector is 1600. Hence, you are receiving the error. You can reshape your "XtrainShaped" matrix using the link hyperlink given.
The shape of your XtrainShaped should be . The 1 represents the number of planes in an image denoting whether the image is grayscale or RGB. Please refer the below code for more information. This way, you will not receive any syntax error.
Important Note: The accuracy and other metrics depend on your CNN model, training options, and other hyperparameters. Below code is just for demo purpose to avoid the error that you are receiving.
imdsTrain = rand(498, 13, 1600); % generating random samples (your dataset)
% RESHAPE YOUR DATA
imdsTrain_reshaped = reshape(imdsTrain, [498, 13, 1, 1600]); % reshaping to [height, width , num_planes, num_samples]
labels = randi([0,9],1600, 1); % generating vector having 10 classes
labels = categorical(labels); % converting to categorical vector
[img_height, img_width , num_planes, num_samples] = size(imdsTrain_reshaped);
num_classes = 10;
% CNN layers
layers = [ ...
imageInputLayer([img_height, img_width , num_planes]) % input layer
convolution2dLayer(3,20) % 2D convolutional layer
reluLayer % activation function
maxPooling2dLayer(2,'Stride',2) % 2D max-pooling layer
fullyConnectedLayer(num_classes) % Fully connected later with output neurons 10
softmaxLayer % activation function
classificationLayer]; % Classification layer
% training options
options = trainingOptions('sgdm', ...
'MaxEpochs',5,...
'InitialLearnRate',1e-4, ...
'Verbose',true, ...
'Plots','training-progress');
% training the network
net = trainNetwork(imdsTrain_reshaped, labels, layers, options);

11 comentarios

Thank you for explaining how the image dimension should be specified for imageInputLayer.
I reshaped the input to be a 498x13x1x1600 vector, but still getting the same error message.
whos XtrainShaped
whos TTrain
trainedNet = trainNetwork(XtrainShaped,TTrain,layers,options);
Rahul
Rahul el 13 de Oct. de 2022
Can we have a screen sharing session as per your convenience? Suggest a suitable time. We can google meet.
Check "imageinputlayer" in "layers" and check the dimesions.
imageInputLayer([img_height, img_width , num_planes]) % input layer
I'm setting as below:
[img_height, img_width , num_planes, num_samples] = size(XtrainShaped)
%Define layers of the network
layers = [
imageInputLayer([img_height, img_width , num_planes]) % input layer
I get the following
I get the following values for the image dimensions:
But, still getting the error when I run the code below:
whos XtrainShaped
whos TTrain
trainedNet = trainNetwork(XtrainShaped,TTrain,layers,options);
Rahul
Rahul el 17 de Oct. de 2022
Please contact MathWorks technical support to resolve this issue.
Rahul
Rahul el 17 de Oct. de 2022
I am using MATLAB R2022a and I did not encounter any such error. If possible, please upgrade the MATLAB software to R2022a or later to avoid this error. If this is not possible, please let me know the MATLAB release that you are using and I will try to reproduce the issue at my end. We can also schedule a screen sharing session at your availability.
I'm also using R2022a.
Please let me know your availability for a screen sharing session this weekend.
Thanks, Nikhil
Rahul
Rahul el 19 de Oct. de 2022
Editada: Rahul el 19 de Oct. de 2022
Saturday 22nd, 2022 after 05:30 PM IST or Sunday 23rd, 2022 anytime. Please suggest a suitable time as per your convenience and I shall mail you the google meet link details.
Does 8:00 PM or 8:30 PM IST on Sunday 23rd, work?
Thanks, Nikhil
Rahul
Rahul el 19 de Oct. de 2022
Sure 8:00 PM IST works. I have mailed you the google meet link details on your gmail account. Feel free to reply.
Did you get the solution for that? I am currently having the same problem. Thanks

Iniciar sesión para comentar.

伟 王
伟 王 el 1 de Dic. de 2022
Did you solve this problem, I am also experiencing the same situation

Categorías

Más información sobre Deep Learning Toolbox en Centro de ayuda y File Exchange.

Productos

Versión

R2022a

Preguntada:

el 9 de Oct. de 2022

Comentada:

el 2 de Mayo de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by