MATLAB: Response must not contain any NaNs?

cnndeep learningnanregression

I am trying to make a image to number CNN with a regression layer, and keep getting the error: "Error using trainNetwork (line 183) Invalid training data. For regression tasks, responses must be a vector, a matrix, or a 4-D array of numeric responses. Responses must not contain NaNs."
I'm attempting to use the imageDatastore function, and convert it into 4-D array using imds2array, and I'm not sure how I set it up incorrectly, here's my code so far:
Why is it "Not a Number"? What should I be changing/adding to get past this error?
%Loading Dataset
imds = imageDatastore('PlaceLocationHere', ...
'IncludeSubfolders',true, ...
'LabelSource','foldernames','FileExtensions','.jpeg');
[X, Y] = imds2array(imds);
layers = [
imageInputLayer([25 25 1])
convolution2dLayer(3,8,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(3,16,'Padding','same')
batchNormalizationLayer
reluLayer
maxPooling2dLayer(2,'Stride',2)
convolution2dLayer(3,32,'Padding','same')
batchNormalizationLayer
reluLayer
convolution2dLayer(3,32,'Padding','same')
batchNormalizationLayer
reluLayer
dropoutLayer(0.2)
fullyConnectedLayer(1)
regressionLayer];
%Network Options
miniBatchSize = 128;
validationFrequency = floor(numel(Y)/miniBatchSize);
options = trainingOptions('sgdm', ...
'MiniBatchSize',miniBatchSize, ...
'MaxEpochs',8, ...
'InitialLearnRate',1e-3, ...
'LearnRateSchedule','piecewise', ...
'LearnRateDropFactor',0.1, ...
'LearnRateDropPeriod',20, ...
'Shuffle','every-epoch', ...
'ValidationData',{X,Y}, ...
'Plots','training-progress', ...
'Verbose',false);
%Training the network
net=trainNetwork(X, Y, layers, options); %What should I put as the input?
function [X, Y] = imds2array(imds)
% X - Input data as an H-by-W-by-C-by-N array, where H is the
% height and W is the width of the images, C is the number of
% channels, and N is the number of images.
% Y - Categorical vector containing the labels for each observation.
imagesCellArray = imds.readall();
numImages = numel( imagesCellArray );
[h, w, c] = size( imagesCellArray{1} );
X = zeros( 1365, 2048, 3, 16); % size of images in practice folder (h,w,c,n)
for i=1:numImages
X(:,:,:,i) = im2double( imagesCellArray{i} );
end
Y = imds.Labels;
end

Best Answer

Hi Sho,
NaN is a special floating-point value which is used as a placeholder in cases where a 'double' type number was expected but could not be obtained. Sometimes, NaN is deliberately set to indicate a soft error. You can learn more about NaN in this doc:Missing Data in MATLAB.
There are many workflows for which the input data cannot be a NaN, such as for trainNetwork as in your case.
It seems like some of the data, from the datastore you have creted, could not be converted to array values and were set to NaN instead. You may inspect the variable 'X' to find where are the NaNs.
isnan(X) % You can start with isnan() to locate the NaN values.
Additionally, settings breakpoints in your script and inspecting the input data as it is being processed should help you to identify where the NaNs are originating. Set BreakPoints in MATLAB
Hope it Helps!
Related Question