Jules Musquin commited on
Commit
cb6da6a
·
1 Parent(s): e1ac534

[update] update on split.py and Readme

Browse files
Files changed (3) hide show
  1. .gitignore +2 -1
  2. README.md +2 -2
  3. split.py +42 -15
.gitignore CHANGED
@@ -29,4 +29,5 @@ lib64/
29
 
30
  #website
31
  generated_html/
32
- annotations/
 
 
29
 
30
  #website
31
  generated_html/
32
+ annotations/
33
+ dataset/
README.md CHANGED
@@ -34,8 +34,8 @@ GenHisDoc
34
  ├── labels
35
  ├── classes.txt
36
  ├── origin.csv #Show you the dataset origin of a specific image
37
- ├── split.py # split the dataset between a test, train and val set.
38
- └── html.py # will create a html page with updated statistic about the dataset and a full annotation display on all images.
39
  ```
40
 
41
  The annotations are in YOLO format using the classes below.
 
34
  ├── labels
35
  ├── classes.txt
36
  ├── origin.csv #Show you the dataset origin of a specific image
37
+ ├── split.py # split the dataset between a test, train and val as shown in the .txt files
38
+ └── html.py # will create a html page with updated statistic about the dataset and a full annotation display on all images in the annotations/ folder
39
  ```
40
 
41
  The annotations are in YOLO format using the classes below.
split.py CHANGED
@@ -1,18 +1,21 @@
1
  import glob
2
  import os
3
- import numpy as np
4
  import random
 
5
 
6
  # Training Yolo for Object Detection in PyTorch with Your Custom Dataset — The Simple Way
7
  # https://medium.com/data-science/training-yolo-for-object-detection-in-pytorch-with-your-custom-dataset-the-simple-way-1aa6f56cf7d9
8
 
9
- current_dir = "./images"
 
 
 
10
  val_pct = 10 # 10% validation
11
  test_pct = 10 # 10% test
12
 
13
  # Récupère toutes les images (en gérant .jpg et .JPG)
14
- images = glob.glob(os.path.join(current_dir, "*.jpg")) + \
15
- glob.glob(os.path.join(current_dir, "*.JPG"))
16
 
17
  random.seed(42) # pour un split reproductible
18
  random.shuffle(images)
@@ -25,16 +28,40 @@ val_images = images[:n_val]
25
  test_images = images[n_val:n_val + n_test]
26
  train_images = images[n_val + n_test:]
27
 
28
- def write_list(filename, image_list):
29
- with open(filename, "w") as f:
30
- for path in image_list:
31
- f.write(path.replace("\\", "/") + "\n")
32
 
33
- write_list("train.txt", train_images)
34
- write_list("val.txt", val_images)
35
- write_list("test.txt", test_images)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
- print(f"Total images : {n_total}")
38
- print(f"Train : {len(train_images)} ({len(train_images)/n_total*100:.1f}%)")
39
- print(f"Val : {len(val_images)} ({len(val_images)/n_total*100:.1f}%)")
40
- print(f"Test : {len(test_images)} ({len(test_images)/n_total*100:.1f}%)")
 
1
  import glob
2
  import os
 
3
  import random
4
+ import shutil
5
 
6
  # Training Yolo for Object Detection in PyTorch with Your Custom Dataset — The Simple Way
7
  # https://medium.com/data-science/training-yolo-for-object-detection-in-pytorch-with-your-custom-dataset-the-simple-way-1aa6f56cf7d9
8
 
9
+ images_dir = "./images"
10
+ labels_dir = "./labels"
11
+ output_dir = "./dataset"
12
+
13
  val_pct = 10 # 10% validation
14
  test_pct = 10 # 10% test
15
 
16
  # Récupère toutes les images (en gérant .jpg et .JPG)
17
+ images = glob.glob(os.path.join(images_dir, "*.jpg")) + \
18
+ glob.glob(os.path.join(images_dir, "*.JPG"))
19
 
20
  random.seed(42) # pour un split reproductible
21
  random.shuffle(images)
 
28
  test_images = images[n_val:n_val + n_test]
29
  train_images = images[n_val + n_test:]
30
 
 
 
 
 
31
 
32
+ def copy_split(split_name, image_list):
33
+ split_images_dir = os.path.join(output_dir, split_name, "images")
34
+ split_labels_dir = os.path.join(output_dir, split_name, "labels")
35
+ os.makedirs(split_images_dir, exist_ok=True)
36
+ os.makedirs(split_labels_dir, exist_ok=True)
37
+
38
+ copied = 0
39
+ missing_labels = 0
40
+
41
+ for image_path in image_list:
42
+ filename = os.path.basename(image_path)
43
+ identifier, ext = os.path.splitext(filename)
44
+ label_path = os.path.join(labels_dir, identifier + ".txt")
45
+
46
+ # Copie l'image
47
+ shutil.copy2(image_path, os.path.join(split_images_dir, filename))
48
+
49
+ # Copie le label correspondant, s'il existe
50
+ if os.path.isfile(label_path):
51
+ shutil.copy2(label_path, os.path.join(split_labels_dir, identifier + ".txt"))
52
+ copied += 1
53
+ else:
54
+ print(f"Label manquant pour {filename}")
55
+ missing_labels += 1
56
+
57
+ return copied, missing_labels
58
+
59
+
60
+ train_copied, train_missing = copy_split("train", train_images)
61
+ val_copied, val_missing = copy_split("val", val_images)
62
+ test_copied, test_missing = copy_split("test", test_images)
63
 
64
+ print(f"\nTotal images : {n_total}")
65
+ print(f"Train : {len(train_images)} images ({len(train_images)/n_total*100:.1f}%), {train_missing} labels manquants")
66
+ print(f"Val : {len(val_images)} images ({len(val_images)/n_total*100:.1f}%), {val_missing} labels manquants")
67
+ print(f"Test : {len(test_images)} images ({len(test_images)/n_total*100:.1f}%), {test_missing} labels manquants")