Created
March 21, 2018 08:01
-
-
Save sam-thecoder/04d91da2c669f3de7e7f7c54b05bc2d7 to your computer and use it in GitHub Desktop.
distribute images to right sub folder for training
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import os | |
train = pd.read_csv('train.csv') | |
landmarks = list(set(train['landmark_id'].tolist())) #use set to have only unique id's and turn it back to a list type again | |
#creates sub folders of landmark id in train and validation folder | |
train_folder = 'training_images' | |
validation_folder = 'validation_images' | |
for landmark in landmarks: | |
train_dir = "{0}/{1}".format(train_folder, landmark) | |
val_dir = "{0}/{1}".format(validation_folder, landmark) | |
if not os.path.exists(train_dir): | |
os.makedirs(train_dir) | |
if not os.path.exists(val_dir): | |
os.makedirs(val_dir) | |
#now you need to match all the images downloaded to training_images to the right subfolder | |
#create image names and get landmarks | |
images_ids = ["{0}.jpg".format(x) for x in train['id'].tolist()] | |
full_landmarks = train['landmark_id'].tolist() | |
for image_name, landmark in zip(images_ids, full_landmarks): | |
old_filename = "{0}/{1}".format(train_folder, image_name) | |
new_filename = "{0}/{1}/{2}".format(train_folder, landmark, imagename) | |
try: | |
os.rename(old_filename, new_filename) | |
except Exception as e: | |
print ( "Failed to move file {0} error {1}".format(old_filename, e) ) | |
#move 20% of the images to the validation folder | |
for folder in landmarks: | |
files = [f for f in os.listdir('{0}/{1}'.format(train_folder,folder)) if os.path.isfile(join('{0}/{1}'.format(train_folder, folder), f))] | |
sample_files = files[:len(files)/5] | |
for image in small_slice: | |
try: | |
os.rename('{0}/{1}/{2}'.format(train_folder, folder, image), '{0}/{1}/{2}'.format(validation_folder, folder, image)) | |
except Exception as e: | |
print "Failed to move file {0} error {1}".format(image, e) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment