Error using trainingOptions (line 269) The value of 'ValidationData' is invalid. The datastore used for 'ValidationData' must return a 2-column table or an M-by-2 cell array.

2 次查看(过去 30 天)
clear;clc;close all
% Load the Image Dataset of Normal and Malignant WBC
imdsTrain = imageDatastore('D:\Project\DB1\train','IncludeSubfolders',true,'LabelSource','foldernames');
imdsTest = imageDatastore('D:\Project\DB1\test','IncludeSubfolders',true,'LabelSource','foldernames');
%Perform Cross-Validation using Hold-out method with a percentage split of 70% training and 30% testing
%[imdsTrain,imdsValidation] = splitEachLabel(imds,0.7,'randomized');
%%
%%
for i=1:numel(imdsTrain.Files)
a=(imdsTrain.Files(i));
targetSize = [299,299];
imdsReSz = transform(imdsTrain,@(x) imresize(x,targetSize));
%imgReSz1 = read(imdsReSz);
%a = imread(char(a));
%a = imresize(a,[299 299]);
end
for i=1:numel(imdsTest.Files)
a=(imdsTest.Files(i));
targetSize1 = [299,299];
imdsReSz1 = transform(imdsTest,@(x) imresize(x,targetSize1));
%a = imread(char(a));
%a = imresize(a,[299 299]);
end
load('HW');
%%
%Select the Test images and save in Y_test
Y_test = imdsReSz1.UnderlyingDatastore.Labels;
%%
% optimzation techniques selection and hyperparamter selection
options = trainingOptions('adam', ...
'MiniBatchSize',16, ...
'MaxEpochs',20, ...
'InitialLearnRate',1e-4, ...
'Shuffle','every-epoch', ...
'ValidationData',imdsReSz1, ...
'ValidationFrequency',3, ...
'Verbose',false, ...
'Plots','training-progress');
%%
%CNN model training
netTransfer = trainNetwork(imdsReSz,HW,options);
%%
% for i=1:numel(imdsValidation.Files)
% a=[imdsValidation.Files(i)];
% a = imread(char(a));
% % featuresTest22 = activations(net,a,layer,'OutputAs','rows');
% YPred(i) = classify(netTransfer,a);
% imshow(a),title(char(YPred));
% i
% end
%%
% CNN Model validation
YPred = classify(netTransfer,imdsReSz1);
%Performance evaluation of Deep Learning Trained Model
plotconfusion(Y_test,YPred)
Error using trainingOptions (line 269)
The value of 'ValidationData' is invalid. The datastore used for 'ValidationData' must return a 2-column
table or an M-by-2 cell array.
Error in cnn (line 38)
options = trainingOptions('adam', ...

回答(1 个)

T.Nikhil kumar
T.Nikhil kumar 2024-4-19
Hello,
The error message states that the ‘ValidationData’ parameter in the ‘trainingOptions’ function expects the validation data to be either a 2-column table or an M-by-2 cell array, where the first column contains the data (images in your case) and the second column contains the labels. For image data, I would suggest you to specify the validation data as an ‘imageDatastore’ and use ‘augmentedImageDatastore’ for efficient preprocessing of images for deep learning including image resizing.
You can consider the below mentioned points to resolve your error:
  1. Instead of resizing within a loop and trying to use the test set as validation, you can have a validation set split from your training data. It looks like you've commented out the line that does this. You can uncomment and use the ‘splitEachLabel’ method to create one.
  2. You should use the ‘augmentedImageDatastore’ to resize the images for both training and validation.
Here is a modified version of your code:
clear; clc; close all;
% Load the Image Dataset of Normal and Malignant WBC
imds = imageDatastore('D:\Project\DB1\train','IncludeSubfolders',true,'LabelSource','foldernames');
mdsTest = imageDatastore('D:\Project\DB1\test','IncludeSubfolders',true,'LabelSource','foldernames');
[imdsTrain,imdsValidation] = splitEachLabel(imds,0.7,'randomized');
% Define target size for resizing
targetSize = [299,299];
% Resize training and validation datasets
augimdsTrain = augmentedImageDatastore(targetSize, imdsTrain);
augimdsValidation = augmentedImageDatastore(targetSize, imdsValidation);
load('HW');
% optimzation techniques selection and hyperparamter selection
options = trainingOptions('adam', ...
'MiniBatchSize',16, ...
'MaxEpochs',20, ...
'InitialLearnRate',1e-4, ...
'Shuffle','every-epoch', ...
'ValidationData',augimdsValidation, ... % Use the augmented validation set
'ValidationFrequency',3, ...
'Verbose',false, ...
'Plots','training-progress');
%CNN model training
netTransfer = trainNetwork(augimdsTrain, HW, options);
% Assuming imdsTest is your test set and has been loaded correctly
augimdsTest = augmentedImageDatastore(targetSize, imdsTest);
Y_test = imdsTest.Labels; % Get the true labels from the test set
% Classify test images
YPred = classify(netTransfer, augimdsTest);
% Performance evaluation
plotconfusion(Y_test, YPred);
Refer to the following documentation to understand about the type of validation data accepted by the ‘trainingOptions’ function in MATLAB R2019a - here
I hope this gets you going.

类别

Help CenterFile Exchange 中查找有关 Image Data Workflows 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by