-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasets.py
77 lines (59 loc) · 2.55 KB
/
datasets.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
import os
import numpy as np
import torchvision.transforms as transforms
from torch.utils.data import Dataset
from PIL import Image
'''
The original source code can be found in
https://github.com/aimagelab/mammoth/blob/master/datasets/seq_tinyimagenet.py
'''
class TinyImagenet(Dataset):
"""
Defines Tiny Imagenet as for the others pytorch datasets.
"""
def __init__(self, root: str, train: bool=True, transform: transforms=None,
target_transform: transforms=None, download: bool=False) -> None:
self.not_aug_transform = transforms.Compose([transforms.ToTensor()])
self.root = root
self.train = train
self.transform = transform
self.target_transform = target_transform
self.download = download
if download:
if os.path.isdir(root) and len(os.listdir(root)) > 0:
print('Download not needed, files already on disk.')
else:
from google_drive_downloader import GoogleDriveDownloader as gdd
# https://drive.google.com/file/d/1Sy3ScMBr0F4se8VZ6TAwDYF-nNGAAdxj/view
print('Downloading dataset')
gdd.download_file_from_google_drive(
file_id='1Sy3ScMBr0F4se8VZ6TAwDYF-nNGAAdxj',
dest_path=os.path.join(root, 'tiny-imagenet-processed.zip'),
unzip=True)
self.data = []
for num in range(20):
self.data.append(np.load(os.path.join(
root, 'processed/x_%s_%02d.npy' %
('train' if self.train else 'val', num+1))))
self.data = np.concatenate(np.array(self.data))
self.targets = []
for num in range(20):
self.targets.append(np.load(os.path.join(
root, 'processed/y_%s_%02d.npy' %
('train' if self.train else 'val', num+1))))
self.targets = np.concatenate(np.array(self.targets))
def __len__(self):
return len(self.data)
def __getitem__(self, index):
img, target = self.data[index], self.targets[index]
# doing this so that it is consistent with all other datasets
# to return a PIL Image
img = Image.fromarray(np.uint8(255 * img))
original_img = img.copy()
if self.transform is not None:
img = self.transform(img)
if self.target_transform is not None:
target = self.target_transform(target)
if hasattr(self, 'logits'):
return img, target, original_img, self.logits[index]
return img, target