split_data_in_k_folds.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import os
  2. import shutil
  3. def populate_kfold_directories(data_dir, K_FOLDS):
  4. alarmed_images = os.listdir(f"{data_dir}/Alarmed")
  5. annoyed_images = os.listdir(f"{data_dir}/Annoyed")
  6. curious_images = os.listdir(f"{data_dir}/Curious")
  7. relaxed_images = os.listdir(f"{data_dir}/Relaxed")
  8. for i in range(K_FOLDS):
  9. validation_range = (i*20, i*20 + 20)
  10. for j in range(0, 100):
  11. if validation_range[0] <= j < validation_range[1]:
  12. shutil.copy(f"{data_dir}/Alarmed/{alarmed_images[j]}", f"folds/fold{i}/validation/Alarmed/")
  13. shutil.copy(f"{data_dir}/Annoyed/{annoyed_images[j]}", f"folds/fold{i}/validation/Annoyed/")
  14. shutil.copy(f"{data_dir}/Curious/{curious_images[j]}", f"folds/fold{i}/validation/Curious/")
  15. shutil.copy(f"{data_dir}/Relaxed/{relaxed_images[j]}", f"folds/fold{i}/validation/Relaxed/")
  16. else:
  17. shutil.copy(f"{data_dir}/Alarmed/{alarmed_images[j]}", f"folds/fold{i}/train/Alarmed/")
  18. shutil.copy(f"{data_dir}/Annoyed/{annoyed_images[j]}", f"folds/fold{i}/train/Annoyed/")
  19. shutil.copy(f"{data_dir}/Curious/{curious_images[j]}", f"folds/fold{i}/train/Curious/")
  20. shutil.copy(f"{data_dir}/Relaxed/{relaxed_images[j]}", f"folds/fold{i}/train/Relaxed/")
  21. def create_kfold_directories(K_FOLDS):
  22. try:
  23. os.mkdir("folds")
  24. except:
  25. print("Directory 'folds' already exists")
  26. for i in range(K_FOLDS):
  27. try:
  28. os.mkdir(f"folds/fold{i}/")
  29. os.mkdir(f"folds/fold{i}/train")
  30. os.mkdir(f"folds/fold{i}/validation")
  31. os.mkdir(f"folds/fold{i}/train/Alarmed")
  32. os.mkdir(f"folds/fold{i}/train/Annoyed")
  33. os.mkdir(f"folds/fold{i}/train/Curious")
  34. os.mkdir(f"folds/fold{i}/train/Relaxed")
  35. os.mkdir(f"folds/fold{i}/validation/Alarmed")
  36. os.mkdir(f"folds/fold{i}/validation/Annoyed")
  37. os.mkdir(f"folds/fold{i}/validation/Curious")
  38. os.mkdir(f"folds/fold{i}/validation/Relaxed")
  39. except:
  40. print("Can't create directory because it already exists")