split_data_in_k_folds_1.py 1.3 KB

1234567891011121314151617181920212223
  1. def populate_kfold_directories(data_dir, K_FOLDS):
  2. alarmed_images = os.listdir(f"{data_dir}/Alarmed")
  3. annoyed_images = os.listdir(f"{data_dir}/Annoyed")
  4. curious_images = os.listdir(f"{data_dir}/Curious")
  5. relaxed_images = os.listdir(f"{data_dir}/Relaxed")
  6. for i in range(K_FOLDS):
  7. validation_range = (i*20, i*20 + 20)
  8. for j in range(0, 100):
  9. if validation_range[0] <= j < validation_range[1]:
  10. shutil.copy(f"{data_dir}/Alarmed/{alarmed_images[j]}", f"folds/fold{i}/validation/Alarmed/")
  11. shutil.copy(f"{data_dir}/Annoyed/{annoyed_images[j]}", f"folds/fold{i}/validation/Annoyed/")
  12. shutil.copy(f"{data_dir}/Curious/{curious_images[j]}", f"folds/fold{i}/validation/Curious/")
  13. shutil.copy(f"{data_dir}/Relaxed/{relaxed_images[j]}", f"folds/fold{i}/validation/Relaxed/")
  14. else:
  15. shutil.copy(f"{data_dir}/Alarmed/{alarmed_images[j]}", f"folds/fold{i}/train/Alarmed/")
  16. shutil.copy(f"{data_dir}/Annoyed/{annoyed_images[j]}", f"folds/fold{i}/train/Annoyed/")
  17. shutil.copy(f"{data_dir}/Curious/{curious_images[j]}", f"folds/fold{i}/train/Curious/")
  18. shutil.copy(f"{data_dir}/Relaxed/{relaxed_images[j]}", f"folds/fold{i}/train/Relaxed/")