The TinyImageNet classification dataset, with 200 classes and 500 images per class. See https://tiny-imagenet.herokuapp.com/. It produces [image, label] where image is a 64x64x3(BGR) image, label is an integer in [0, 200).
| 291 | |
| 292 | |
| 293 | class TinyImageNet(RNGDataFlow): |
| 294 | """ |
| 295 | The TinyImageNet classification dataset, with 200 classes and 500 images |
| 296 | per class. See https://tiny-imagenet.herokuapp.com/. |
| 297 | |
| 298 | It produces [image, label] where image is a 64x64x3(BGR) image, label is an |
| 299 | integer in [0, 200). |
| 300 | """ |
| 301 | def __init__(self, dir, name, shuffle=None): |
| 302 | """ |
| 303 | Args: |
| 304 | dir (str): a directory |
| 305 | name (str): one of 'train' or 'val' |
| 306 | shuffle (bool): shuffle the dataset. |
| 307 | Defaults to True if name=='train'. |
| 308 | """ |
| 309 | assert name in ['train', 'val'], name |
| 310 | dir = Path(os.path.expanduser(dir)) |
| 311 | assert os.path.isdir(dir), dir |
| 312 | self.full_dir = dir / name |
| 313 | if shuffle is None: |
| 314 | shuffle = name == 'train' |
| 315 | self.shuffle = shuffle |
| 316 | |
| 317 | with open(dir / "wnids.txt") as f: |
| 318 | wnids = [x.strip() for x in f.readlines()] |
| 319 | cls_to_id = {name: id for id, name in enumerate(wnids)} |
| 320 | assert len(cls_to_id) == 200 |
| 321 | |
| 322 | self.imglist = [] |
| 323 | if name == 'train': |
| 324 | for clsid, cls in enumerate(wnids): |
| 325 | cls_dir = self.full_dir / cls / "images" |
| 326 | for img in cls_dir.iterdir(): |
| 327 | self.imglist.append((str(img), clsid)) |
| 328 | else: |
| 329 | with open(self.full_dir / "val_annotations.txt") as f: |
| 330 | for line in f: |
| 331 | line = line.strip().split() |
| 332 | img, cls = line[0], line[1] |
| 333 | img = self.full_dir / "images" / img |
| 334 | clsid = cls_to_id[cls] |
| 335 | self.imglist.append((str(img), clsid)) |
| 336 | |
| 337 | def __len__(self): |
| 338 | return len(self.imglist) |
| 339 | |
| 340 | def __iter__(self): |
| 341 | idxs = np.arange(len(self.imglist)) |
| 342 | if self.shuffle: |
| 343 | self.rng.shuffle(idxs) |
| 344 | for k in idxs: |
| 345 | fname, label = self.imglist[k] |
| 346 | im = cv2.imread(fname, cv2.IMREAD_COLOR) |
| 347 | assert im is not None, fname |
| 348 | yield [im, label] |
| 349 | |
| 350 |