Skip to content

Instantly share code, notes, and snippets.

@martinetmayank
Last active September 4, 2021 15:24
Show Gist options
  • Save martinetmayank/e0d3a9a14f52751e5de91847aa7ef0d6 to your computer and use it in GitHub Desktop.
Save martinetmayank/e0d3a9a14f52751e5de91847aa7ef0d6 to your computer and use it in GitHub Desktop.
Copying images to training, testing and validation directories
DATASET_DIR = 'your-dataset-directory'
BASE_DIR = os.getcwd() + '\\trainset'
os.mkdir(BASE_DIR)
TRAIN_DIR = os.path.join(BASE_DIR, 'train')
TRAIN_DOGS_DIR = os.path.join(TRAIN_DIR, 'dogs')
TRAIN_CATS_DIR = os.path.join(TRAIN_DIR, 'cats')
os.mkdir(TRAIN_DIR)
os.mkdir(TRAIN_DOGS_DIR)
os.mkdir(TRAIN_CATS_DIR)
VALIDATION_DIR = os.path.join(BASE_DIR, 'validation')
VALIDATION_CATS_DIR = os.path.join(VALIDATION_DIR, 'cats')
VALIDATION_DOGS_DIR = os.path.join(VALIDATION_DIR, 'dogs')
os.mkdir(VALIDATION_DIR)
os.mkdir(VALIDATION_CATS_DIR)
os.mkdir(VALIDATION_DOGS_DIR)
TEST_DIR = os.path.join(BASE_DIR, 'test')
TEST_DOGS_DIR = os.path.join(TEST_DIR, 'dogs')
TEST_CATS_DIR = os.path.join(TEST_DIR, 'cats')
os.mkdir(TEST_DIR)
os.mkdir(TEST_DOGS_DIR)
os.mkdir(TEST_CATS_DIR)
def copy_cat_img():
fnames = ['cat.{}.jpg'.format(i) for i in range(0, 1000)]
for fname in fnames:
src = os.path.join(DATASET_DIR, fname)
dst = os.path.join(TRAIN_CATS_DIR, fname)
shutil.copy(src, dst)
fnames = ['cat.{}.jpg'.format(i) for i in range(1000, 1500)]
for fname in fnames:
src = os.path.join(DATASET_DIR, fname)
dst = os.path.join(VALIDATION_CATS_DIR, fname)
shutil.copy(src, dst)
fnames = ['cat.{}.jpg'.format(i) for i in range(1500, 2000)]
for fname in fnames:
src = os.path.join(DATASET_DIR, fname)
dst = os.path.join(TEST_CATS_DIR, fname)
shutil.copy(src, dst)
def copy_dog_img():
fnames = ['dog.{}.jpg'.format(i) for i in range(0, 1000)]
for fname in fnames:
src = os.path.join(DATASET_DIR, fname)
dst = os.path.join(TRAIN_DOGS_DIR, fname)
shutil.copy(src, dst)
fnames = ['dog.{}.jpg'.format(i) for i in range(1000, 1500)]
for fname in fnames:
src = os.path.join(DATASET_DIR, fname)
dst = os.path.join(VALIDATION_DOGS_DIR, fname)
shutil.copy(src, dst)
fnames = ['dog.{}.jpg'.format(i) for i in range(1500, 2000)]
for fname in fnames:
src = os.path.join(DATASET_DIR, fname)
dst = os.path.join(TEST_DOGS_DIR, fname)
shutil.copy(src, dst)
copy_cat_img()
copy_dog_img()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment