-
Notifications
You must be signed in to change notification settings - Fork 1
/
load_data.py
98 lines (76 loc) · 3.25 KB
/
load_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
import torch
import numpy as np
from torchvision import datasets, transforms
# # https://github.com/aaron-xichen/pytorch-playground/blob/master/svhn/train.py
# def target_transform(target):
# return int(target[0]) - 1
data_path = "./data"
batch_size = None
def load_svhn_help():
train_dataset = datasets.SVHN(
root=data_path, split='train', download=True,
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
]),
#target_transform=target_transform,
)
test_dataset = datasets.SVHN(
root=data_path, split='test', download=True,
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
]),
#target_transform=target_transform
)
return train_dataset, test_dataset
def load_mnist_help():
train_dataset = datasets.MNIST(root=data_path,
train=True,
transform=transforms.ToTensor(),
download=True)
test_dataset = datasets.MNIST(root=data_path,
train=False,
transform=transforms.ToTensor())
return train_dataset, test_dataset
def load_cifar10_help():
transform_train = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
num_classes = 10
trainset = datasets.CIFAR10(root=data_path, train=True, download=True, transform=transform_train)
testset = datasets.CIFAR10(root=data_path, train=False, download=False, transform=transform_test)
return trainset, testset
def load_data(dataset, batch_size, data_path, n_train = None, n_test=None):
"""
return the first n_train training data and first n_test testing data
"""
batch_size = batch_size
data_path = data_path
if dataset == "MNIST":
train_dataset, test_dataset = load_mnist_help()
elif dataset == "SVHN":
train_dataset, test_dataset = load_svhn_help()
elif dataset == "CIFAR10":
train_dataset, test_dataset = load_cifar10_help()
else:
raise fNotImplementedError
# Data loader
if n_train:
train_dataset = Subset(train_dataset, range(n_train))
if n_test:
test_dataset = Subset(train_dataset, range(n_test))
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
batch_size=batch_size,
shuffle=True)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
batch_size=batch_size,
shuffle=False)
return train_loader, test_loader