确实是个好问题。
据我所知,ImageDataGenerator() 中没有内置的多标签分层。
我将建议两种可能的方法:
-
您可以将Sequence() 类子类化,以便能够准确控制您在网络中的每一步提供的内容。您可以覆盖__getitem__() 方法,并确保在每批中按比例采样数据。
-
您可以使用外部库,在将数据输入网络之前对其进行预处理。通过这种方式,您可以预处理数据并使用tf.data.Dataset() 管道将数据提供给您的网络。
(2)的一个例子是这个:
from iterstrat.ml_stratifiers import MultilabelStratifiedKFold
import numpy as np
X = np.array([[1,2], [3,4], [1,2], [3,4], [1,2], [3,4], [1,2], [3,4]])
y = np.array([[0,0], [0,0], [0,1], [0,1], [1,1], [1,1], [1,0], [1,0]])
mskf = MultilabelStratifiedKFold(n_splits=2, shuffle=True, random_state=0)
for train_index, test_index in mskf.split(X, y):
print("TRAIN:", train_index, "TEST:", test_index)
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]