如何在MATLAB中拆分图像数据存储区以进行交叉验证? [英] How to split an image datastore for cross-validation in MATLAB?
问题描述
在MATLAB中,imageDatastore
对象的方法splitEachLabel
将图像数据存储区按每个类别标签分成多个比例.如何使用交叉验证和trainImageCategoryCalssifier
类拆分图像数据存储以进行训练?
In MATLAB the method splitEachLabel
of an imageDatastore
object splits an image data store into proportions per category label. How can one split an image data store for training using cross-validation and using the trainImageCategoryCalssifier
class?
即可以很容易地将其拆分为N个分区,但是需要某种_mergeEachLabel_
功能,以便能够使用交叉验证来训练分类器.
I.e. it's easy to split it in N partitions, but then some sort of _mergeEachLabel_
functionality is needed to be able to train a classifier using cross-validation.
还是有另一种方式来实现这一目标?
Or is there another way of achieving that?
关于, 埃琳娜(Elena)
Regards, Elena
推荐答案
以下代码应适用于基本的交叉验证,当然,您需要适当更改 k 的值和数据存储区选项
The following code should work for basic cross validation, of course you will need to change the value of k and the datastore options appropriately.
k = 5; % number of folds
datastore = imageDatastore(fullfile('.'), 'IncludeSubfolders', true, 'LabelSource', 'foldernames');
partStores{k} = [];
for i = 1:k
temp = partition(datastore, k, i);
partStores{i} = temp.Files;
end
% this will give us some randomization
% though it is still advisable to randomize the data before hand
idx = crossvalind('Kfold', k, k);
for i = 1:k
test_idx = (idx == i);
train_idx = ~test_idx;
test_Store = imageDatastore(partStores{test_idx}, 'IncludeSubfolders', true, 'LabelSource', 'foldernames');
train_Store = imageDatastore(cat(1, partStores{train_idx}), 'IncludeSubfolders', true, 'LabelSource', 'foldernames');
% do your training and predictions here, maybe pre-allocate them before the loop, too
%net{i} = trainNetwork(train_Store, layers options);
%pred{i} = classify(net, test_Store);
end
这篇关于如何在MATLAB中拆分图像数据存储区以进行交叉验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!