Your image_datasets are a dict with a dataset for train and val, so the train_indices will have a length of 2.
If you want to use leave-one-out cv, you probably don’t need a separate test set, as the one sample will act as the test sample.
Could you try to use list(range(len(image_datasets['train']))) or change your dataset?