Skip to content

Commit

Permalink
Adding tiny_imagenet clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
rjha18 committed Jan 3, 2024
1 parent 431108d commit 6071e7f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
36 changes: 36 additions & 0 deletions modules/base_utils/fix_val.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import os

DATA_DIR = 'data/tiny-imagenet-200/'
VALID_DIR = DATA_DIR + 'val'

# Create separate validation subfolders for the validation images based on
# their labels indicated in the val_annotations txt file
val_img_dir = os.path.join(VALID_DIR, 'images')
fp = open(os.path.join(VALID_DIR, 'val_annotations.txt'), 'r')
data = fp.readlines()

# Create dictionary to store img filename (word 0) and corresponding
# label (word 1) for every line in the txt file (as key value pair)
val_img_dict = {}
for line in data:
words = line.split('\t')
val_img_dict[words[0]] = words[1]
fp.close()

# Create subfolders (if not present) for validation images based on label ,
# and move images into the respective folders
for img, folder in val_img_dict.items():
newpath = (os.path.join(val_img_dir, folder))
if not os.path.exists(newpath):
os.makedirs(newpath)
if os.path.exists(os.path.join(val_img_dir, img)):
os.rename(os.path.join(val_img_dir, img), os.path.join(newpath, img))

# Save class names (for corresponding labels) as dict from words.txt file
class_to_name_dict = dict()
fp = open(os.path.join(DATA_DIR, 'words.txt'), 'r')
data = fp.readlines()
for line in data:
words = line.strip('\n').split('\t')
class_to_name_dict[words[0]] = words[1].split(',')[0]
fp.close()
3 changes: 2 additions & 1 deletion modules/base_utils/tiny_imagenet_setup.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
wget -nc http:https://cs231n.stanford.edu/tiny-imagenet-200.zip
unzip tiny-imagenet-200.zip -d data/
unzip tiny-imagenet-200.zip -d data/
python modules/base_utils/fix_val.py

0 comments on commit 6071e7f

Please sign in to comment.