Download cat_and_dog Dataset(kaggle dataset)
After downloading the dataset to locally, then extract here, then you will find the dataset structure is like this
Only two directories are included: train and test, The train directory constains all the images cats and dogs. Here we want split all the train images to two directory './train/cat/' or './train/dog/' respectively by they target. Here is the python scripts
import os
path = r'/home/haku/Documents/cat_dog_dataset/train/'
print(os.path.abspath(path))
image_lists = os.listdir(path)
print(len(image_lists))
cat_dir = './train/cat'
print(os.path.abspath(cat_dir))
dog_dir = './train/dog'
print(os.path.abspath(dog_dir))
if not os.path.exists(cat_dir):
os.makedirs(cat_dir)
if not os.path.exists(dog_dir):
os.makedirs(dog_dir)
for image in image_lists:
label = image.split('.')[0]
if label == 'cat':
os.system("cp {}{} {}".format(path,image,cat_dir))
if label == 'dog':
os.system("cp {}{} {}".format(path,image,dog_dir))
print("final")