-
Notifications
You must be signed in to change notification settings - Fork 13
/
data_loader.py
162 lines (136 loc) · 6.92 KB
/
data_loader.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
161
162
"""
CIFAR-10 CIFAR-100, Tiny-ImageNet data loader
"""
import random
import os
import numpy as np
import torch
import torchvision
import torchvision.transforms as transforms
from torch.utils.data.sampler import SubsetRandomSampler
def fetch_dataloader(types, params):
"""
Fetch and return train/dev dataloader with hyperparameters (params.subset_percent = 1.)
"""
# using random crops and horizontal flip for train set
if params.augmentation:
train_transformer = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(), # randomly flip image horizontally
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.247, 0.243, 0.261))])
# data augmentation can be turned off
else:
train_transformer = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.247, 0.243, 0.261))])
# transformer for dev set
dev_transformer = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.247, 0.243, 0.261))])
# ************************************************************************************
if params.dataset == 'cifar10':
trainset = torchvision.datasets.CIFAR10(root='./data/data-cifar10', train=True,
download=True, transform=train_transformer)
devset = torchvision.datasets.CIFAR10(root='./data/data-cifar10', train=False,
download=True, transform=dev_transformer)
# ************************************************************************************
elif params.dataset == 'cifar100':
trainset = torchvision.datasets.CIFAR100(root='./data/data-cifar100', train=True,
download=True, transform=train_transformer)
devset = torchvision.datasets.CIFAR100(root='./data/data-cifar100', train=False,
download=True, transform=dev_transformer)
# ************************************************************************************
elif params.dataset == 'tiny_imagenet':
data_dir = './data/tiny-imagenet-200/'
data_transforms = {
'train': transforms.Compose([
transforms.RandomRotation(20),
transforms.RandomHorizontalFlip(0.5),
transforms.ToTensor(),
transforms.Normalize([0.4802, 0.4481, 0.3975], [0.2302, 0.2265, 0.2262]),
]),
'val': transforms.Compose([
transforms.ToTensor(),
transforms.Normalize([0.4802, 0.4481, 0.3975], [0.2302, 0.2265, 0.2262]),
])
}
train_dir = data_dir + 'train/'
test_dir = data_dir + 'val/'
trainset = torchvision.datasets.ImageFolder(train_dir, data_transforms['train'])
devset = torchvision.datasets.ImageFolder(test_dir, data_transforms['val'])
trainloader = torch.utils.data.DataLoader(trainset, batch_size=params.batch_size,
shuffle=True, num_workers=params.num_workers)
devloader = torch.utils.data.DataLoader(devset, batch_size=params.batch_size,
shuffle=False, num_workers=params.num_workers)
if types == 'train':
dl = trainloader
else:
dl = devloader
return dl
def fetch_subset_dataloader(types, params):
"""
Use only a subset of dataset for KD training, depending on params.subset_percent
"""
# using random crops and horizontal flip for train set
if params.augmentation:
train_transformer = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(), # randomly flip image horizontally
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.247, 0.243, 0.261))])
# data augmentation can be turned off
else:
train_transformer = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.247, 0.243, 0.261))])
# transformer for dev set
dev_transformer = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.247, 0.243, 0.261))])
# ************************************************************************************
if params.dataset == 'cifar10':
trainset = torchvision.datasets.CIFAR10(root='./data/data-cifar10', train=True,
download=True, transform=train_transformer)
devset = torchvision.datasets.CIFAR10(root='./data/data-cifar10', train=False,
download=True, transform=dev_transformer)
# ************************************************************************************
elif params.dataset == 'cifar100':
trainset = torchvision.datasets.CIFAR100(root='./data/data-cifar100', train=True,
download=True, transform=train_transformer)
devset = torchvision.datasets.CIFAR100(root='./data/data-cifar100', train=False,
download=True, transform=dev_transformer)
# ************************************************************************************
elif params.dataset == 'tiny_imagenet':
data_dir = './data/tiny-imagenet-200/'
data_transforms = {
'train': transforms.Compose([
transforms.RandomRotation(20),
transforms.RandomHorizontalFlip(0.5),
transforms.ToTensor(),
transforms.Normalize([0.4802, 0.4481, 0.3975], [0.2302, 0.2265, 0.2262]),
]),
'val': transforms.Compose([
transforms.ToTensor(),
transforms.Normalize([0.4802, 0.4481, 0.3975], [0.2302, 0.2265, 0.2262]),
])
}
train_dir = data_dir + 'train/'
test_dir = data_dir + 'val/'
trainset = torchvision.datasets.ImageFolder(train_dir, data_transforms['train'])
devset = torchvision.datasets.ImageFolder(test_dir, data_transforms['val'])
trainset_size = len(trainset)
indices = list(range(trainset_size))
split = int(np.floor(params.subset_percent * trainset_size))
np.random.seed(230)
np.random.shuffle(indices)
train_sampler = SubsetRandomSampler(indices[:split])
trainloader = torch.utils.data.DataLoader(trainset, batch_size=params.batch_size,
sampler=train_sampler, num_workers=params.num_workers, pin_memory=params.cuda)
devloader = torch.utils.data.DataLoader(devset, batch_size=params.batch_size,
shuffle=False, num_workers=params.num_workers, pin_memory=params.cuda)
if types == 'train':
dl = trainloader
else:
dl = devloader
return dl