Split images based on patient folders for cross validation

Hi,
I need to split my images to validation and train set in cross validation, in a way that images of each patient be either in train or validation. each patient has about 8-11 images.
root
|___patien_1 im1
| | __ im2 …
|
|___patient_2 im1
| | __ im2 …
|
.
.
.

how I can split images based on patient folders?

If you have all target values for all images, you could create a corresponding group for each patient and use sklearn.model_selection.GroupShuffleSplit.

1 Like

this is a binary classification task [0,1]. but how I can make the group of patients?

Each patient would get an index (similar to a target) and you could pass it to the GroupShuffleSplit to get the indices for the train and validation split based on the patients.

E.g. if patient_1 has 1 image and patient_2 2 images, you could pass the groups as: [0, 1, 1].

1 Like

Thanks for explanation!
I have 300 patients, this means that my group range should be 0-299? or I need to make the groups based on target 0-1 ?

Each patient should belong to a unique group, so the range should be [0, 299].

1 Like