unrecognized method property or field Labels for class augmentdatastore?

12 次查看(过去 30 天)
I am tring to train the model on .mat dataset. i have train the model sucessfully but when i tried to find the accuracy i got the error.
imds = imageDatastore('D:\yellow\img-data\iqmat\', 'FileExtensions', '.mat', 'IncludeSubfolders',true, ...
'LabelSource','foldernames',...
'ReadFcn',@matReader);
[imdsTrain,imdsValidation] = splitEachLabel(imds,0.7, 'randomized');
inputSize = lgraph_1.Layers(1).InputSize;
[learnableLayer,classLayer] = findLayersToReplace(lgraph_1);
numClasses = numel(categories(imdsTrain.Labels));
if isa(learnableLayer,'nnet.cnn.layer.FullyConnectedLayer')
newLearnableLayer = fullyConnectedLayer(numClasses, ...
'Name','new_fc', ...
'WeightLearnRateFactor',10, ...
'BiasLearnRateFactor',10);
elseif isa(learnableLayer,'nnet.cnn.layer.Convolution2DLayer')
newLearnableLayer = convolution2dLayer(1,numClasses, ...
'Name','new_conv', ...
'WeightLearnRateFactor',10, ...
'BiasLearnRateFactor',10);
end
lgraph_1 = replaceLayer(lgraph_1,learnableLayer.Name,newLearnableLayer);
newClassLayer = classificationLayer('Name','new_classoutput');
lgraph_1 = replaceLayer(lgraph_1,classLayer.Name,newClassLayer);
imdsTrain = augmentedImageDatastore([224,224],imdsTrain);
imdsValidation = augmentedImageDatastore([224,224],imdsValidation);
miniBatchSize =8;
valFrequency = floor(numel(imdsTrain.Files)/miniBatchSize);
checkpointPath = pwd;
options = trainingOptions('sgdm', ...
'MiniBatchSize',miniBatchSize, ...
'MaxEpochs',100, ...
'InitialLearnRate',1e-4, ...
'Shuffle','every-epoch', ...
'ValidationData',imdsValidation, ...
'ValidationFrequency',valFrequency, ...
'Verbose',false, ...
'Plots','training-progress', ...
'CheckpointPath',checkpointPath,...
'ExecutionEnvironment','gpu');
net = trainNetwork(imdsTrain,lgraph_1,options);
[YPred,probs] = classify(net,imdsValidation);
accuracy = mean(YPred == imdsValidation.Labels)
error:
unrecognized method property or field Labels for class augmentdatastore

采纳的回答

Walter Roberson
Walter Roberson 2021-12-14
augmentedImageDatastore() does not record the labels of the input data store.
You currently have
imdsValidation = augmentedImageDatastore([224,224],imdsValidation);
which takes imdsValidation (an image data store that has labels) as input, and you write to the same variable... but augmentedImageDatastore does not carry the labels.
If you wrote to a different variable, then when you got to
accuracy = mean(YPred == imdsValidation.Labels)
you could be referring to the unaugmented data store that still has the labels.
  6 个评论
Walter Roberson
Walter Roberson 2021-12-15
imds = imageDatastore('D:\yellow\img-data\iqmat\', 'FileExtensions', '.mat', 'IncludeSubfolders',true, ...
'LabelSource','foldernames',...
'ReadFcn',@matReader);
[imdsTrain,imdsValidation] = splitEachLabel(imds,0.7, 'randomized');
inputSize = lgraph_1.Layers(1).InputSize;
[learnableLayer,classLayer] = findLayersToReplace(lgraph_1);
numClasses = numel(categories(imdsTrain.Labels));
if isa(learnableLayer,'nnet.cnn.layer.FullyConnectedLayer')
newLearnableLayer = fullyConnectedLayer(numClasses, ...
'Name','new_fc', ...
'WeightLearnRateFactor',10, ...
'BiasLearnRateFactor',10);
elseif isa(learnableLayer,'nnet.cnn.layer.Convolution2DLayer')
newLearnableLayer = convolution2dLayer(1,numClasses, ...
'Name','new_conv', ...
'WeightLearnRateFactor',10, ...
'BiasLearnRateFactor',10);
end
lgraph_1 = replaceLayer(lgraph_1,learnableLayer.Name,newLearnableLayer);
newClassLayer = classificationLayer('Name','new_classoutput');
lgraph_1 = replaceLayer(lgraph_1,classLayer.Name,newClassLayer);
imdsTrain = augmentedImageDatastore([224,224],imdsTrain);
imdsValidation_aug = augmentedImageDatastore([224,224],imdsValidation); %HERE
miniBatchSize =8;
valFrequency = floor(numel(imdsTrain.Files)/miniBatchSize);
checkpointPath = pwd;
options = trainingOptions('sgdm', ...
'MiniBatchSize',miniBatchSize, ...
'MaxEpochs',100, ...
'InitialLearnRate',1e-4, ...
'Shuffle','every-epoch', ...
'ValidationData',imdsValidation_aug, ... %HERE
'ValidationFrequency',valFrequency, ...
'Verbose',false, ...
'Plots','training-progress', ...
'CheckpointPath',checkpointPath,...
'ExecutionEnvironment','gpu');
net = trainNetwork(imdsTrain,lgraph_1,options);
[YPred,probs] = classify(net,imdsValidation_aug);
accuracy = mean(YPred == imdsValidation.Labels)

请先登录,再进行评论。

更多回答(0 个)

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by