Number of observations in X and Y disagree. - Error training a U-Net for classification

20 views (last 30 days)
I am trying to train a U-Net for classification aim. Each of the 8x8 central pixels of my input matrix 48x48 (I have a 4-D matrix as input: 48x48x4xN) have to be classified in 8 different classes. I built a U-Net with the 'ConvolutionPadding' option set as 'valid' (this is the reason why starting from a 48x48 the last layer output is 8x8) and the 'pixelClassificationLayer' as classification layer.
Follwing my code where A and B (input and responses, respectively) are not the real data but are used only as an example.
A=rand(48,48,4,100);%Input
B=ones(8,8,1,100);%Responses
B=categorical(B(:));
EncoderDepth=2;
NFEF=[16,32,64];
sz=size(A);
MaxEpoch=200;%Maximum number of epochs
ILR=1e-06;%Initial learn rate value
SGF=[0.9,0.99,0.999];
MiniBatch=2000;
L2R=[1e-05,1e-04,1e-03];
for i=3%:numel(NFEF)
lgraph=unetLayers([48,48,4],2,'EncoderDepth',EncoderDepth,'NumFirstEncoderFilters',NFEF(i),'ConvolutionPadding','valid');
lgraph=removeLayers(lgraph,'Final-ConvolutionLayer');
lgraph=removeLayers(lgraph,'Softmax-Layer');
lgraph=removeLayers(lgraph,'Segmentation-Layer');
lgraph=addLayers(lgraph,convolution2dLayer([1,1],1,'name','Final-ConvolutionLayer'));
lgraph=addLayers(lgraph,softmaxLayer('name','Softmax-Layer'));
lgraph=addLayers(lgraph,pixelClassificationLayer('name','classificationLayer'));
lgraph=connectLayers(lgraph,'Decoder-Stage-2-ReLU-2','Final-ConvolutionLayer');
lgraph=connectLayers(lgraph,'Final-ConvolutionLayer','Softmax-Layer');
lgraph=connectLayers(lgraph,'Softmax-Layer','classificationLayer');
for j=1:numel(ILR)
for k=1%:numel(SGF)
options = trainingOptions('rmsprop','InitialLearnRate',ILR(j),...
'MiniBatchSize',MiniBatch,'Shuffle','every-epoch',...
'SquaredGradientDecayFactor',0.99,...
'MaxEpochs',MaxEpoch,...
...%'L2Regularization',L2R(j),...
...%'ValidationData',{MSGdata_VAL_V11,DPRGMI_ValDataset_DeepLearn}, ...
...%'ValidationFrequency',floor(sz(4)/MiniBatch), ...
'ExecutionEnvironment','cpu','Plots','training-progress');
[net,info]=trainNetwork(A,B,lgraph,options);
UNET.net=net;
UNET.info=info;
EpochSTR=num2str(MaxEpoch);
MiniBatchSTR=num2str(MiniBatch);
ILRstr=num2str(ILR(j));
NFEFstr=num2str(NFEF(i));
% SGFstr=num2str(SGF(k));
% L2Rstr=num2str(L2R(j));
save([PathOut,filesep,'UNET_NoPadding_MaxPool_',EpochSTR,'Epochs_',MiniBatchSTR,'MBS_',ILRstr,'ILR_',...
NFEFstr,'NFEF_OversampledData_NoParallax_NoBN_ValDataset_PRthre',PRthre,'mmh_',VERin,'_',VERout,'.mat'],'UNET');
end
end
end
When I run the code I get the following error message:
Error using trainNetwork
Number of observations in X and Y disagree.
Error in untitled2 (line 42)
[net,info]=trainNetwork(A,B,lgraph,options);
I would appreciate any help to fix this problem.

Answers (1)

Matt J
Matt J on 10 Feb 2023
Edited: Matt J on 10 Feb 2023
You have 100 training images. Therefore, B should be 100x1, not 6400x1.
  4 Comments
Leo Pio D'Adderio
Leo Pio D'Adderio on 13 Feb 2023
Matt, what I learned is that pixelLabelDatastore works reading image files (e.g. jpeg, png, tiff, ecc.). I do not have these files, but netCDf files that I read and extract my numerical matrices (that I save in .mat files). So, I talk about images because they come from satellite images, but I work with numerical matrices.
I am still facing the problem to classify each pixel of my matrix.
Matt J
Matt J on 13 Feb 2023
Edited: Matt J on 13 Feb 2023
I do not have these files, but netCDf files that I read and extract my numerical matrices (that I save in .mat files)
The type of file you store your responses in should not be an issue. You can use the ReadFcn parameter to allow a pixelLabelDataStore to read any kind of file:

Sign in to comment.

Categories

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

Community Treasure Hunt

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

Start Hunting!