1234567891011121314151617181920212223242526272829303132333435 |
- def split_train_val():
- tmp_dir = os.path.join(ROOT_DIR, "tmp")
-
- os.makedirs(os.path.join(ROOT_DIR, "train"))
- train_dir = os.path.join(ROOT_DIR, "train")
- os.makedirs(os.path.join(train_dir, "images"))
-
- os.makedirs(os.path.join(ROOT_DIR, "val"))
- val_dir = os.path.join(ROOT_DIR, "val")
- os.makedirs(os.path.join(val_dir, "images"))
- # Opening JSON file
- with open(os.path.join(tmp_dir, 'annotations.json')) as json_file:
- data = json.load(json_file)
- # 0.1765 is 15% van 100% omdat test al 20 % is (niet helemaal)
- val_keys = random.sample(list(data), round(len(data) * 0.1765))
- validation = {k: v for k, v in data.items() if k in val_keys}
- train = {k: v for k, v in data.items() if k not in val_keys}
- with open(os.path.join(val_dir, SAVE_FILE), 'w') as output_json_file:
- json.dump(validation, output_json_file)
- with open(os.path.join(train_dir, SAVE_FILE), 'w') as output_json_file:
- json.dump(train, output_json_file)
-
- for key, _ in validation.items():
- shutil.move(os.path.join(tmp_dir, "images", key), os.path.join(val_dir, "images", key))
- for key, _ in train.items():
- shutil.move(os.path.join(tmp_dir, "images", key), os.path.join(train_dir, "images"))
- shutil.rmtree(tmp_dir)
|