MATLAB Answers

Response must not contain any NaNs??

56 views (last 30 days)
Sho Wright
Sho Wright on 3 Jan 2021
Commented: Sho Wright on 15 Jan 2021
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, ...
[X, Y] = imds2array(imds);
layers = [
imageInputLayer([25 25 1])
%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', ...
%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} );
Y = imds.Labels;


Sign in to comment.

Accepted Answer

Athul Prakash
Athul Prakash on 6 Jan 2021
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!

  1 Comment

Sho Wright
Sho Wright on 15 Jan 2021
Hi Athul,
Thank you for the helpful response, I used NaN breakpoints to pinpoint the error. It turns out the error was pointing towards my line:
"Y = imds.Labels"
Which is not a vector, matrix or 4D array numerical response, but instead sets the response to a categorical datatype. I resolved the issue by changing Y to a 4D array, and setting the appropriate dimensions throughout.
Many thanks,

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by