Skip to content

Instantly share code, notes, and snippets.

@xjtu-blacksmith
Last active February 5, 2020 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xjtu-blacksmith/4102eda77cd4254a48d466d8929995ea to your computer and use it in GitHub Desktop.
Save xjtu-blacksmith/4102eda77cd4254a48d466d8929995ea to your computer and use it in GitHub Desktop.
Processing tiny-imagenet-200 datasets to pytorch.dataset.ImageFolders
# 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
# 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