The value of 'ValidationData' is invalid. Validation data must be a table, a datastore, or a cell array with input data and responses.

52 次查看(过去 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)];
a = imread(char(a));
a = imresize(a,[299 299]);
end
a1=a;
for i=1:numel(imdsTest.Files)
a=[imdsTest.Files(i)];
a = imread(char(a));
a = imresize(a,[299 299]);
end
a2=a;
load('HW');
%%
%Select the Test images and save in Y_test
Y_test = imdsTest.Labels;
%%
% optimzation techniques selection and hyperparamter selection
options = trainingOptions('adam', ...
'MiniBatchSize',16, ...
'MaxEpochs',20, ...
'InitialLearnRate',1e-4, ...
'Shuffle','every-epoch', ...
'ValidationData',a2, ...
'ValidationFrequency',3, ...
'Verbose',false, ...
'Plots','training-progress');
%%
%CNN model training
netTransfer = trainNetwork(a1,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,a2);
%Performance evaluation of Deep Learning Trained Model
plotconfusion(Y_test,YPred)
Error using trainingOptions (line 269)
The value of 'ValidationData' is invalid. Validation data must be a table, a datastore, or a cell array
with input data and responses.
Error in cnn (line 32)
options = trainingOptions('adam', ...
>>

采纳的回答

Walter Roberson
Walter Roberson 2022-1-6
for i=1:numel(imdsTrain.Files)
a=[imdsTrain.Files(i)];
a = imread(char(a));
a = imresize(a,[299 299]);
end
a1=a;
for i=1:numel(imdsTest.Files)
a=[imdsTest.Files(i)];
a = imread(char(a));
a = imresize(a,[299 299]);
end
In those two loops, you process each of a number of files, but you throw away the result of the processing as soon as you start the next loop iteration.
You should consider either using a custom read function for your datastore, or else use a transform store; https://www.mathworks.com/help/matlab/ref/matlab.io.datastore.transform.html
'ValidationData',a2, ...
Your a2 is the result of transforming the very last of your test files only
Use a transform store.
  2 个评论
sun rise
sun rise 2022-1-9
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.
sun rise
sun rise 2022-1-10
Dear Walter Roberson,
Thanks for your advice
Although I tried to use this method, it still throws following error..
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.
In addition, my images are grayscale image. In this case, how can i modify my code to apply into inception_v3?
However, I'm very new to programming and not sure how to apply a method to resizing and RGB to grayscale into my code...
Thanks for you in advance!

请先登录,再进行评论。

更多回答(0 个)

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by