-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepare_tiny.py
More file actions
33 lines (27 loc) · 1.24 KB
/
prepare_tiny.py
File metadata and controls
33 lines (27 loc) · 1.24 KB
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
32
33
import os
# Create separate validation subfolders for the validation images based on
# their labels indicated in the val_annotations txt file
DATA_DIR = '../tiny-imagenet-200' # Original images come in shapes of [3,64,64]
TRAIN_DIR = os.path.join(DATA_DIR, 'train')
VALID_DIR = os.path.join(DATA_DIR, 'val')
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))