A way to set images in the right folder before training


I have images of dogs and cats in the same folder. what I’m trying to do is to set the images like the bottom. is there a sample code that I could try using os path and os rename??
I’m not familiar with this technique. thanks and have a nice day!

One easy way would be to use glob or os to get all *.jpg files.
Once you have all image paths, you could split them simply by using a condition and finally copy them to the destination folder. Here is some pseudo code:

image_paths = glob.glob('./dogsandcats/train/*.jpg')
dog_paths = []
cat_paths = []
for p in image_paths:
    if 'dog' in p:
    elif 'cat' in p:
        print('Unknown image: {}'.format(p))

# Split paths to train/val
nb_dogs = len(dog_paths)
dog_paths_train = dog_paths[:int(nb_dogs*0.8)]
dog_paths_val = dog_paths[int(nb_dogs*0.8):]
# same for cat

for p in dog_paths_train:
    shutil.copy2(p, './dogsandcats/train/dog/')
# same for val and cat
1 Like

thank you so much. learning a lot from you!

1 Like