Response must not contain any NaNs??
Show older comments
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
Accepted Answer
More Answers (0)
Categories
Find more on Grid Lines, Tick Values, and Labels 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!