-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.py
110 lines (94 loc) · 3.57 KB
/
data.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
import torch.utils.data
import torchvision.transforms as transforms
from torchvision.datasets import CIFAR10, CIFAR100
valid_datasets = ['cifar10', 'cifar100']
def _verify_dataset(dataset):
if dataset not in valid_datasets:
msg = "Unknown dataset \'{}\'. ".format(dataset)
msg += "Valid datasets are {}.".format(", ".join(valid_datasets))
raise ValueError(msg)
return dataset
def cifar10_loader(batch_size, num_workers, datapath, cuda):
normalize = transforms.Normalize(
mean=[0.4914, 0.4822, 0.4465],
std=[0.2023, 0.1994, 0.2010])
transform_train = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize,
])
transform_val = transforms.Compose([
transforms.ToTensor(),
normalize,
])
trainset = CIFAR10(
root=datapath, train=True, download=True,
transform=transform_train)
valset = CIFAR10(
root=datapath, train=False, download=True,
transform=transform_val)
if cuda:
train_loader = torch.utils.data.DataLoader(
trainset,
batch_size=batch_size, shuffle=True,
num_workers=num_workers, pin_memory=True)
val_loader = torch.utils.data.DataLoader(
valset,
batch_size=batch_size, shuffle=False,
num_workers=num_workers, pin_memory=True)
else:
train_loader = torch.utils.data.DataLoader(
trainset,
batch_size=batch_size, shuffle=True,
num_workers=num_workers, pin_memory=False)
val_loader = torch.utils.data.DataLoader(
valset,
batch_size=batch_size, shuffle=False,
num_workers=num_workers, pin_memory=False)
return train_loader, val_loader
def cifar100_loader(batch_size, num_workers, datapath, cuda):
normalize = transforms.Normalize(
mean=[0.4914, 0.4822, 0.4465],
std=[0.2023, 0.1994, 0.2010])
transform_train = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize,
])
transform_val = transforms.Compose([
transforms.ToTensor(),
normalize,
])
trainset = CIFAR100(
root=datapath, train=True, download=True,
transform=transform_train)
valset = CIFAR100(
root=datapath, train=False, download=True,
transform=transform_val)
if cuda:
train_loader = torch.utils.data.DataLoader(
trainset,
batch_size=batch_size, shuffle=True,
num_workers=num_workers, pin_memory=True)
val_loader = torch.utils.data.DataLoader(
valset,
batch_size=batch_size, shuffle=False,
num_workers=num_workers, pin_memory=True)
else:
train_loader = torch.utils.data.DataLoader(
trainset,
batch_size=batch_size, shuffle=True,
num_workers=num_workers, pin_memory=False)
val_loader = torch.utils.data.DataLoader(
valset,
batch_size=batch_size, shuffle=False,
num_workers=num_workers, pin_memory=False)
return train_loader, val_loader
def DataLoader(batch_size, num_workers, dataset='cifar10', datapath='../data', cuda=True):
DataSet = _verify_dataset(dataset)
if DataSet == 'cifar10':
return cifar10_loader(batch_size, num_workers, datapath, cuda)
elif DataSet == 'cifar100':
return cifar100_loader(batch_size, num_workers, datapath, cuda)