-
Notifications
You must be signed in to change notification settings - Fork 1
/
TinyImageNetDataset.py
160 lines (134 loc) · 5.19 KB
/
TinyImageNetDataset.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import imageio
import numpy as np
import os
from collections import defaultdict
from torch.utils.data import Dataset
from tqdm.autonotebook import tqdm
from PIL import Image
def _add_channels(img, total_channels=3):
while len(img.shape) < 3: # third axis is the channels
img = np.expand_dims(img, axis=-1)
while(img.shape[-1]) < 3:
img = np.concatenate([img, img[:, :, -1:]], axis=-1)
return img
class TinyImageNetPaths:
def __init__(self, root_dir, download=False):
if download: download_and_unzip('http://cs231n.stanford.edu/tiny-imagenet-200.zip',
root_dir)
train_path = os.path.join(root_dir, 'train')
val_path = os.path.join(root_dir, 'val')
test_path = os.path.join(root_dir, 'test')
wnids_path = os.path.join(root_dir, 'wnids.txt')
words_path = os.path.join(root_dir, 'words.txt')
self._make_paths(train_path, val_path, test_path,
wnids_path, words_path)
def _make_paths(self, train_path, val_path, test_path,
wnids_path, words_path):
self.ids = []
with open(wnids_path, 'r') as idf:
for nid in idf:
nid = nid.strip()
self.ids.append(nid)
self.nid_to_words = defaultdict(list)
with open(words_path, 'r') as wf:
for line in wf:
nid, labels = line.split('\t')
labels = list(map(lambda x: x.strip(), labels.split(',')))
self.nid_to_words[nid].extend(labels)
self.paths = {
'train': [], # [img_path, id, nid, box]
'val': [], # [img_path, id, nid, box]
'test': [] # img_path
}
# Get the test paths
self.paths['test'] = list(map(lambda x: os.path.join(test_path, x),
os.listdir(test_path)))
# Get the validation paths and labels
with open(os.path.join(val_path, 'val_annotations.txt')) as valf:
for line in valf:
fname, nid, x0, y0, x1, y1 = line.split()
fname = os.path.join(val_path, 'images', fname)
bbox = int(x0), int(y0), int(x1), int(y1)
label_id = self.ids.index(nid)
self.paths['val'].append((fname, label_id, nid, bbox))
# Get the training paths
train_nids = os.listdir(train_path)
for nid in train_nids:
anno_path = os.path.join(train_path, nid, nid+'_boxes.txt')
imgs_path = os.path.join(train_path, nid, 'images')
label_id = self.ids.index(nid)
with open(anno_path, 'r') as annof:
for line in annof:
fname, x0, y0, x1, y1 = line.split()
fname = os.path.join(imgs_path, fname)
bbox = int(x0), int(y0), int(x1), int(y1)
self.paths['train'].append((fname, label_id, nid, bbox))
"""Datastructure for the tiny image dataset.
Args:
root_dir: Root directory for the data
mode: One of "train", "test", or "val"
preload: Preload into memory
load_transform: Transformation to use at the preload time
transform: Transformation to use at the retrieval time
download: Download the dataset
Members:
tinp: Instance of the TinyImageNetPaths
img_data: Image data
label_data: Label data
"""
class TinyImageNetDataset(Dataset):
def __init__(self, root_dir, mode='train', preload=True, load_transform=None,
transform=None, download=False, max_samples=None):
tinp = TinyImageNetPaths(root_dir, download)
self.mode = mode
self.label_idx = 1 # from [image, id, nid, box]
self.preload = preload
self.transform = transform
self.transform_results = dict()
self.IMAGE_SHAPE = (64, 64, 3)
self.img_data = []
self.label_data = []
self.max_samples = max_samples
self.samples = tinp.paths[mode]
self.samples_num = len(self.samples)
if self.max_samples is not None:
self.samples_num = min(self.max_samples, self.samples_num)
self.samples = np.random.permutation(self.samples)[:self.samples_num]
if self.preload:
load_desc = "Preloading {} data...".format(mode)
self.img_data = np.zeros((self.samples_num,) + self.IMAGE_SHAPE,
dtype=np.float32)
self.label_data = np.zeros((self.samples_num,), dtype=np.int)
for idx in tqdm(range(self.samples_num), desc=load_desc):
s = self.samples[idx]
img = imageio.imread(s[0])
img = _add_channels(img)
self.img_data[idx] = img
if mode != 'test':
self.label_data[idx] = s[self.label_idx]
if load_transform:
for lt in load_transform:
result = lt(self.img_data, self.label_data)
self.img_data, self.label_data = result[:2]
if len(result) > 2:
self.transform_results.update(result[2])
def __len__(self):
return self.samples_num
def __getitem__(self, idx):
if self.preload:
img = self.img_data[idx]
lbl = None if self.mode == 'test' else self.label_data[idx]
else:
s = self.samples[idx]
img = imageio.imread(s[0])
img = _add_channels(img)
lbl = None if self.mode == 'test' else s[self.label_idx]
# sample = {'image': img, 'label': lbl}
# if self.transform:
# print(sample)
# sample['im'] = self.transform(sample['image'])
# return sample
img = Image.fromarray(img)
if self.transform:
img = self.transform(img)
return img, lbl