Response must not contain any NaNs??

15 ビュー (過去 30 日間)
Sho Wright
Sho Wright 2021 年 1 月 3 日
コメント済み: Noel Png 2021 年 6 月 24 日
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

採用された回答

Athul Prakash
Athul Prakash 2021 年 1 月 6 日
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!
  2 件のコメント
Sho Wright
Sho Wright 2021 年 1 月 15 日
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,
Sho
Noel Png
Noel Png 2021 年 6 月 24 日
Hi, could you elaborate on your solution ?
Im facing a similar problem

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeImage Data Workflows についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by