glenn-jocher
commited on
Commit
•
354109c
1
Parent(s):
4798e66
Autosplit (#1488)
Browse files- utils/datasets.py +5 -4
utils/datasets.py
CHANGED
@@ -904,18 +904,19 @@ def flatten_recursive(path='../coco128'):
|
|
904 |
shutil.copyfile(file, new_path / Path(file).name)
|
905 |
|
906 |
|
907 |
-
def autosplit(path='../coco128', weights=(0.9, 0.1, 0.0)): # from utils.datasets import *; autosplit()
|
908 |
-
""" Autosplit a dataset into train/val/test splits and save *.txt files
|
909 |
# Arguments
|
910 |
path: Path to images directory
|
911 |
weights: Train, val, test weights (list)
|
912 |
"""
|
913 |
path = Path(path) # images dir
|
914 |
files = list(path.rglob('*.*'))
|
915 |
-
|
|
|
916 |
txt = ['autosplit_train.txt', 'autosplit_val.txt', 'autosplit_test.txt'] # 3 txt files
|
917 |
[(path / x).unlink() for x in txt if (path / x).exists()] # remove existing
|
918 |
-
for i, img in tqdm(zip(indices, files)):
|
919 |
if img.suffix[1:] in img_formats:
|
920 |
with open(path / txt[i], 'a') as f:
|
921 |
f.write(str(img) + '\n') # add image to txt file
|
|
|
904 |
shutil.copyfile(file, new_path / Path(file).name)
|
905 |
|
906 |
|
907 |
+
def autosplit(path='../coco128', weights=(0.9, 0.1, 0.0)): # from utils.datasets import *; autosplit('../coco128')
|
908 |
+
""" Autosplit a dataset into train/val/test splits and save path/autosplit_*.txt files
|
909 |
# Arguments
|
910 |
path: Path to images directory
|
911 |
weights: Train, val, test weights (list)
|
912 |
"""
|
913 |
path = Path(path) # images dir
|
914 |
files = list(path.rglob('*.*'))
|
915 |
+
n = len(files) # number of files
|
916 |
+
indices = random.choices([0, 1, 2], weights=weights, k=n) # assign each image to a split
|
917 |
txt = ['autosplit_train.txt', 'autosplit_val.txt', 'autosplit_test.txt'] # 3 txt files
|
918 |
[(path / x).unlink() for x in txt if (path / x).exists()] # remove existing
|
919 |
+
for i, img in tqdm(zip(indices, files), total=n):
|
920 |
if img.suffix[1:] in img_formats:
|
921 |
with open(path / txt[i], 'a') as f:
|
922 |
f.write(str(img) + '\n') # add image to txt file
|