Skip to content

Instantly share code, notes, and snippets.

@lharries
Created August 1, 2018 08:46
Show Gist options
  • Save lharries/ba4b543dba30ae7bb7998e33ea58fd75 to your computer and use it in GitHub Desktop.
Save lharries/ba4b543dba30ae7bb7998e33ea58fd75 to your computer and use it in GitHub Desktop.
Divide folder of images into train and test
def split_train_test(categories, PATH, percent_test=0.2):
for category in categories:
print(len(os.listdir(PATH+category)))
files = os.listdir(PATH+category)
for file in files:
if np.random.rand(1) < percent_test:
shutil.copyfile(PATH + category +"/" + file, PATH + "test/" +category +"/" + file)
else:
shutil.copyfile(PATH + category +"/" + file, PATH + "train/"+ category +"/" + file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment