-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.py
48 lines (39 loc) · 1.48 KB
/
dataset.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
import os
import torchvision.transforms as transforms
from torch.utils.data import Subset
from torchvision.datasets import ImageNet
def imagenet_1k():
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
dataset_dir = os.path.expanduser('~/Datasets/imagenet')
dataset_train = ImageNet(
dataset_dir, split='train',
transform=transforms.Compose([
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize,
]))
dataset_val = ImageNet(
dataset_dir, split='val',
transform=transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
normalize,
]))
return dataset_train, dataset_val
def imagenet_subset(num_categories: int, *, random: bool):
if random:
raise NotImplementedError
ds_trn, ds_val = imagenet_1k()
accept_categories = list(range(num_categories))
subset_idx = [i for i in range(len(ds_trn)) if ds_trn.targets[i] in accept_categories]
ds_trn = Subset(ds_trn, subset_idx)
subset_idx = [i for i in range(len(ds_val)) if ds_val.targets[i] in accept_categories]
ds_val = Subset(ds_val, subset_idx)
return ds_trn, ds_val
def imagenet_100():
return imagenet_subset(100, random=False)
def imagenet_10():
return imagenet_subset(10, random=False)