myasin commited on
Commit
92b2916
·
1 Parent(s): 3cc12b1

Update cocosplit.py

Browse files

- Added measures to ensure that when the user decides to split on unannotated images as well, the ratio of annotated to unannotated remains the same in both the splits (i.e. Train and Test).
- Altered variable names for clarity.

Files changed (1) hide show
  1. utils/cocosplit.py +26 -8
utils/cocosplit.py CHANGED
@@ -38,24 +38,42 @@ def main(annotation_path,
38
 
39
  number_of_images = len(images)
40
 
41
- images_with_annotations = funcy.lmap(lambda a: int(a['image_id']), annotations)
 
 
 
 
 
 
 
 
 
 
42
 
43
  if having_annotations:
44
- images = funcy.lremove(lambda i: i['id'] not in images_with_annotations, images)
 
 
 
 
 
45
 
46
- x, y = train_test_split(images, train_size=split_ratio, random_state=random_state)
47
 
48
  # Train Data
49
- coco.update({'images': x,
50
- 'annotations': filter_annotations(annotations, x)})
51
  save_coco(train_save_path, coco)
52
 
53
  # Test Data
54
- coco.update({'images': y,
55
- 'annotations': filter_annotations(annotations, y)})
56
  save_coco(test_save_path, coco)
57
 
58
- print("Saved {} entries in {} and {} in {}".format(len(x), train_save_path, len(y), test_save_path))
 
 
 
59
 
60
 
61
  if __name__ == "__main__":
 
38
 
39
  number_of_images = len(images)
40
 
41
+ ids_with_annotations = funcy.lmap(lambda a: int(a['image_id']), annotations)
42
+
43
+ # Images with annotations
44
+ img_ann = funcy.lremove(lambda i: i['id'] not in ids_with_annotations, images)
45
+ tr_ann, ts_ann = train_test_split(img_ann, train_size=split_ratio,
46
+ random_state=random_state)
47
+
48
+ # Images without annotations
49
+ img_wo_ann = funcy.lremove(lambda i: i['id'] in ids_with_annotations, images)
50
+ tr_wo_ann, ts_wo_ann = train_test_split(img_wo_ann, train_size=split_ratio,
51
+ random_state=random_state)
52
 
53
  if having_annotations:
54
+ tr, ts = tr_ann, ts_ann
55
+
56
+ else:
57
+ # Merging the 2 image lists (i.e. with and without annotation)
58
+ tr_ann.extend(tr_wo_ann)
59
+ ts_ann.extend(ts_wo_ann)
60
 
61
+ tr, ts = tr_ann, ts_ann
62
 
63
  # Train Data
64
+ coco.update({'images': tr,
65
+ 'annotations': filter_annotations(annotations, tr)})
66
  save_coco(train_save_path, coco)
67
 
68
  # Test Data
69
+ coco.update({'images': ts,
70
+ 'annotations': filter_annotations(annotations, ts)})
71
  save_coco(test_save_path, coco)
72
 
73
+ print("Saved {} entries in {} and {} in {}".format(len(tr),
74
+ train_save_path,
75
+ len(ts),
76
+ test_save_path))
77
 
78
 
79
  if __name__ == "__main__":