且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

keras模型的分割图像数据集.fit_generator

更新时间:2023-12-01 22:11:28

使用glob获取文件路径迭代器.

然后,您可以使用scikit-learn的train-test拆分来获取火车和测试数据路径(使用stratify参数来获得与整个数据集中的测试/火车相同的类分布).

结果将是两个路径列表,您可以将其写入适当的测试/培训文件夹,然后可以应用生成器的flow_from_directory方法.

第二种方法是不使用flow_from_directory,而是加载训练/测试集(加载所有内容并使用scikit-learn方法或使用我之前描述的方法),然后使用生成器的flow方法. /p>

还请注意,您可能不希望将生成器用于测试/验证数据,因为由于您没有固定的验证/测试集,这会使比较准确性变得困难.

>

I have single directory, dataset, which contains sub-folders(labels/classes) of images.
Here's the Sub-folders of animal images in dataset:

I want to split the dataset into train and test set for model.fit_generotar().

How can I do that?

Use glob to get file paths iterator.

You can then use scikit-learn's train-test split to get train and test data paths (use stratify parameter to get the same class distribution in test/train as in whole dataset).

The result would be two lists of paths, which you can write to appropriate test/train folders, and then you can apply generator's flow_from_directory method.

EDIT:

The second way would be to not use flow_from_directory, but load train/test sets (either load everything and use scikit-learn method or use what I've described before) and then use generator's flow method.

Also note that you might not want to use generators for test/validation data, since it would make comparing accuracy hard, since you won't have a fixed valid/test set.