what should i do with this error on cross validation?

1 回表示 (過去 30 日間)
Amir
Amir 2023 年 7 月 12 日
回答済み: Aniketh 2023 年 7 月 12 日
i newer to matlab and i want to perform cross validation on my image dataset and i use below code for it:
path_to_images = "*\Rice_Image_Dataset";
image_datastore = imageDatastore(path_to_images, "IncludeSubfolders", true, "LabelSource", "foldernames");
% Split the data into train, validation, and test sets
[train, validation, test] = splitEachLabel(image_datastore, 0.6, 0.2, 0.2, 'randomized');
numFolds = 5;
cv = cvpartition(train.Labels, 'KFold', numFolds);
nets = cell(1, numFolds);
accuracies = zeros(1, numFolds);
for i = 1:numFolds
trainIdx = training(cv, i);
valIdx = test(cv, i);
trainData = subset(train, trainIdx);
valData = subset(train, valIdx);
rsz_train = augmentedImageDatastore([224 224 3], trainData);
rsz_val = augmentedImageDatastore([224 224 3], valData);
augmentedTrainDatastore = augmentedImageDatastore([224 224 3], trainData, 'ColorPreprocessing', 'gray2rgb');
augmentedValDatastore = augmentedImageDatastore([224 224 3], valData, 'ColorPreprocessing', 'gray2rgb');
opts = trainingOptions("sgdm", ...
"ExecutionEnvironment", "auto", ...
"InitialLearnRate", 0.01, ...
"MaxEpochs", 5, ...
"MiniBatchSize", 64, ...
"Shuffle", "every-epoch", ...
"ValidationFrequency", 70, ...
"Plots", "training-progress", ...
"ValidationData", rsz_val, ...
"Momentum", 0.9);
[net, traininfo] = trainNetwork(augmentedTrainDatastore, lgraph_1, opts);
nets{i} = net;
true_val_labels = valData.Labels;
pred_val_labels = classify(net, augmentedValDatastore);
accuracies(i) = mean(true_val_labels == pred_val_labels);
end
% Compute average accuracy over all folds
averageAccuracy = mean(accuracies);
but i recieved the following error:
Array formation and parentheses-style indexing with objects of class 'matlab.io.datastore.ImageDatastore' is not allowed. Use objects of class
'matlab.io.datastore.ImageDatastore' only as scalars or use a cell array.
could you plz help me with that.

採用された回答

Aniketh
Aniketh 2023 年 7 月 12 日
Hi Amir, to resolve the error, modify the following lines in your code:
trainData = subset(train, trainIdx);
valData = subset(train, valIdx);
to
trainData = subset(train, trainIdx);
valData = subset(train, valIdx);
By using image_datastore instead of train, you ensure that the subset function operates on the imageDatastore object as intended.
Hope that helped your case!

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMaterial Sciences についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by