-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathdataloader_utils.py
117 lines (93 loc) · 4.73 KB
/
dataloader_utils.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
"""Utils for loading the dataset in pytorch.Currently supported - MNIST, CIFAR-10"""
import os
import numpy as np
import torch
import torchvision.datasets as dsets
import torchvision.transforms as transforms
from torch.utils.data.sampler import SubsetRandomSampler
def load_dataset(mode, name, dataset_params):
train_loader, val_loader = None, None
train_transform = None
if dataset_params.aug == "on":
train_transform = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.247, 0.243, 0.261))
])
else:
train_transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.247, 0.243, 0.261))
])
val_transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.247, 0.243, 0.261))
])
if name == 'mnist':
root = './data_mnist'
train_set = dsets.MNIST(root=root, train=True, download=True, transform=train_transform)
val_set = dsets.MNIST(root=root, train=False, download=True, transform=val_transform)
train_loader = torch.utils.data.DataLoader(train_set, batch_size=dataset_params.batch_size, shuffle=True, num_workers=dataset_params.num_workers, pin_memory=dataset_params.cuda)
val_loader = torch.utils.data.DataLoader(val_set, batch_size=dataset_params.batch_size, shuffle=True, num_workers=dataset_params.num_workers, pin_memory=dataset_params.cuda)
else:
root = './data_cifar10'
if dataset_params.aug == "on":
train_transform = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.247, 0.243, 0.261))
])
else:
train_transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.247, 0.243, 0.261))
])
train_set = dsets.CIFAR10(root=root, train=True, download=True, transform=train_transform)
val_set = dsets.CIFAR10(root=root, train=False, download=True, transform=val_transform)
train_loader = torch.utils.data.DataLoader(train_set, batch_size=dataset_params.batch_size, shuffle=True, num_workers=dataset_params.num_workers, pin_memory=dataset_params.cuda)
val_loader = torch.utils.data.DataLoader(val_set, batch_size=dataset_params.batch_size, shuffle=True, num_workers=dataset_params.num_workers, pin_memory=dataset_params.cuda)
ch_dataset = None
if mode=='train':
ch_dataset = train_loader
else:
ch_dataset = val_loader
return ch_dataset
def load_subsampled_dataset(mode, name, dataset_params):
"""
Currently only for CIFAR10 dataset
"""
if name=='mnist':
raise ValueError("MNIST is not supported for random subset sampling")
root = './data_cifar10'
if dataset_params["aug"] == "on":
train_transform = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.247, 0.243, 0.261))
])
else:
train_transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.247, 0.243, 0.261))
])
val_transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.247, 0.243, 0.261))
])
train_set = dsets.CIFAR10(root=root, train=True, download=True, transform=train_transform)
val_set = dsets.CIFAR10(root=root, train=False, download=True, transform=val_transform)
len_train = len(train_set)
indices = list(range(len_train))
split = int(np.floor(dataset_params["subset_percent"]*len_train))
np.random.seed(912) # Heh
np.random.shuffle(indices)
train_loader = torch.utils.data.DataLoader(train_set, batch_size=dataset_params["batch_size"], shuffle=True, sampler=SubsetRandomSampler(indices[:split]), num_workers=dataset_params["num_workers"],pin_memory=dataset_params["cuda"])
val_loader = torch.utils.data.DataLoader(val_set, batch_size=dataset_params["batch_size"], shuffle=False, num_workers=dataset_params["num_workers"],pin_memory=dataset_params["cuda"])
ch_dataset = None
if mode == "train":
ch_dataset = train_loader
else:
ch_dataset = val_loader
return ch_dataset