-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_class_sets.py
31 lines (25 loc) · 1.12 KB
/
create_class_sets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import os
VALID_DIR = '../datasets/tiny-imagenet-200/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')
# Open and read val annotations text file
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()
# Display first 10 entries of resulting val_img_dict dictionary
#{k: val_img_dict[k] for k in list(val_img_dict)[:10]}
# 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))