Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

create_train_test_split.py 986 B

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  1. # script used for creating the train/test sets from the images
  2. import glob
  3. import os
  4. from random import sample, seed
  5. import shutil
  6. from config import *
  7. if __name__ == "__main__":
  8. seed(42)
  9. for class_name in CLASSES:
  10. files = glob.glob(f"{RAW_IMAGES_DIR}/{class_name}/*.jpg")
  11. sampled_indices = sample(range(len(files)), N_TRAIN + N_TEST)
  12. indices_dict = {
  13. "train": sampled_indices[:N_TRAIN],
  14. "test": sampled_indices[N_TRAIN:]
  15. }
  16. for dataset in ["train", "test"]:
  17. print(f"Creating {dataset} set for class {class_name}")
  18. for i in indices_dict[dataset]:
  19. f = os.path.basename(files[i])
  20. src = os.path.join(RAW_IMAGES_DIR, class_name, f)
  21. dst = os.path.join(PROCESSED_IMAGES_DIR, dataset, class_name, f)
  22. os.makedirs(os.path.dirname(dst), exist_ok=True)
  23. shutil.copy(src, dst)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...