Updated
I've uploaded a dummy data set, link here. The df.head():
It has 4 class in total and df.object.value_counts():
human 23
car 13
cat 5
dog 3
I want to do properly K-Fold validation splits over a multi-class object detection data set.
Initial Approach
To achieve proper k-fold validation splits, I took the object counts and the number of bounding box into account. I understand, the K-fold splitting strategies mostly depends on the data set (meta information). But for now with these dataset, I've tried something like as follows:
skf = StratifiedKFold(n_splits=3, shuffle=True, random_state=101)
df_folds = main_df[['image_id']].copy()
df_folds.loc[:, 'bbox_count'] = 1
df_folds = df_folds.groupby('image_id').count()
df_folds.loc[:, 'object_count'] = main_df.groupby('image_id')['object'].nunique()
df_folds.loc[:, 'stratify_group'] = np.char.add(
df_folds['object_count'].values.astype(str),
df_folds['bbox_count'].apply(lambda x: f'_{x // 15}').values.astype(str)
)
df_folds.loc[:, 'fold'] = 0
for fold_number, (train_index, val_index) in enumerate(skf.split(X=df_folds.index, y=df_folds['stratify_group'])):
df_folds.loc[df_folds.iloc[val_index].index, 'fold'] = fold_number
After the splitting, I've checked to ensure if it's working. And it seems Ok so far.
All the folds contain stratified k-fold samples, len(df_folds[df_folds['fold'] == fold_number].index) and no intersection to each other, set(A).intersection(B) where A and B are the index value (image_id) of two folds. But the issue seems like:
Fold 0 has total: 18 + 2 + 3 = 23 bbox
Fold 1 has total: 2 + 11 = 13 bbox
Fold 2 has total: 5 + 3 = 8 bbox
Concern
However, I couldn't ensure whether it's the proper way for this type of task in general. I want some advice. Is the above approach OK? or any issue? or there is some better approach! Any sorts of suggestions would be appreciated. Thanks.

