Last active
February 5, 2020 08:49
-
-
Save xjtu-blacksmith/4102eda77cd4254a48d466d8929995ea to your computer and use it in GitHub Desktop.
Processing tiny-imagenet-200 datasets to pytorch.dataset.ImageFolders
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
# the file is at: data/tiny-imagenet-200/train/ | |
import os | |
dirs = os.listdir('.') # get categoris | |
for the_dir in dirs: | |
if os.path.isdir(the_dir): # only work for folders | |
for _, _, images in os.walk(os.path.join(the_dir, 'images')): | |
for image in images: | |
os.rename( | |
os.path.join(the_dir, 'images', image), | |
os.path.join(the_dir, image) | |
) # extract the images from 'images' folder to category root | |
os.rmdir(os.path.join(the_dir, 'images')) # remove 'images' folder | |
os.remove(os.path.join(the_dir, the_dir + '_boxes.txt')) # remove the boxes file |
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
# the file is at: data/tiny-imagenet-200/val/ | |
import os | |
sheet_path = 'val_annotations.txt' # reference | |
with open(sheet_path, 'r') as f: | |
lines = f.readlines() # read the file | |
for line in lines: | |
filename, catogory = line.split('\t')[:2] # the first two words from each line | |
if os.path.isdir(catogory) == False: | |
os.mkdir(catogory) # make dir in advance | |
os.rename('images/' + filename, catogory + '/' + filename) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment