如何在MATLAB中拆分图像数据存储区以进行交叉验证? [英] How to split an image datastore for cross-validation in MATLAB?

查看:139
本文介绍了如何在MATLAB中拆分图像数据存储区以进行交叉验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MATLAB中,imageDatastore对象的方法splitEachLabel将图像数据存储区按每个类别标签分成多个比例.如何使用交叉验证和trainImageCategoryCalssifier类拆分图像数据存储以进行训练?

In MATLAB the method splitEachLabelof 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆