필터 지우기
필터 지우기

what should i do with this error on cross validation?

조회 수: 2 (최근 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 CenterFile Exchange에서 Image Data Workflows에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by