-
Notifications
You must be signed in to change notification settings - Fork 11
/
dataset.py
127 lines (109 loc) · 5.62 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
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
import torch
from torchvision import datasets, transforms
MNIST_PATH = '/home/dingxiaohan/datasets/torch_mnist/'
CIFAR10_PATH = '/home/dingxiaohan/datasets/cifar-10-batches-py/'
CH_PATH = '/home/dingxiaohan/datasets/torch_ch/'
SVHN_PATH = '/home/dingxiaohan/datasets/torch_svhn/'
def load_cuda_data(data_loader, dataset_name):
if dataset_name == 'imagenet':
data_dict = next(data_loader)
data = data_dict['data']
label = data_dict['label']
data = torch.from_numpy(data).cuda()
label = torch.from_numpy(label).type(torch.long).cuda()
else:
data, label = next(data_loader)
data = data.cuda()
label = label.cuda()
return data, label
class InfiniteDataLoader(torch.utils.data.DataLoader):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Initialize an iterator over the dataset.
self.dataset_iterator = super().__iter__()
def __iter__(self):
return self
def __next__(self):
try:
batch = next(self.dataset_iterator)
except StopIteration:
# Dataset exhausted, use a new fresh iterator.
self.dataset_iterator = super().__iter__()
batch = next(self.dataset_iterator)
return batch
def create_dataset(dataset_name, subset, batch_size):
assert dataset_name in ['imagenet', 'cifar10', 'ch', 'svhn', 'mnist']
assert subset in ['train', 'val']
if dataset_name == 'imagenet':
from ntools.megtools.classification.config import DpflowProviderMaker, DataproProviderMaker
if subset == 'train':
with open('imagenet_train_conn.txt', 'r') as f:
conn = f.readline().strip()
return DpflowProviderMaker(conn=conn,
entry_names=['image', 'label'],
output_names=['data', 'label'],
descriptor={'data': {'shape': [batch_size, 3, 224, 224]}, 'label': {'shape': [batch_size]}},
buffer_size=16,
group_id = None,
enable_multiprocessing=False)()
else:
return DataproProviderMaker(config_file='provider_config_val.txt',
provider_name='provider_cfg_val',
entry_names=['image_val', 'label'],
output_names=['data', 'label'])()
# copied from https://github.com/pytorch/examples/blob/master/mnist/main.py
elif dataset_name == 'mnist':
if subset == 'train':
return InfiniteDataLoader(datasets.MNIST(MNIST_PATH, train=True, download=True,
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))])), batch_size=batch_size, shuffle=True)
else:
return InfiniteDataLoader(datasets.MNIST(MNIST_PATH, train=False, transform=transforms.Compose([
transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,))])),
batch_size=batch_size, shuffle=False)
elif dataset_name == 'cifar10':
if subset == 'train':
return InfiniteDataLoader(datasets.CIFAR10(CIFAR10_PATH, train=True, download=False,
transform=transforms.Compose([
transforms.Pad(padding=(4, 4, 4, 4)),
transforms.RandomCrop(32),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])),
batch_size=batch_size, shuffle=True)
else:
return InfiniteDataLoader(datasets.CIFAR10(CIFAR10_PATH, train=False,
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])),
batch_size=batch_size, shuffle=False)
elif dataset_name == 'ch':
if subset == 'train':
return InfiniteDataLoader(datasets.CIFAR100(CH_PATH, train=True, download=True,
transform=transforms.Compose([
transforms.Pad(padding=(4, 4, 4, 4)),
transforms.RandomCrop(32),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])),
batch_size=batch_size, shuffle=True)
else:
return InfiniteDataLoader(datasets.CIFAR100(CH_PATH, train=False,
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])),
batch_size=batch_size, shuffle=False)
else:
assert False
def num_train_examples_per_epoch(dataset_name):
if dataset_name == 'imagenet':
return 1281167
elif dataset_name == 'mnist':
return 60000
elif dataset_name in ['cifar10', 'ch']:
return 50000
else:
assert False
def num_iters_per_epoch(cfg):
return num_train_examples_per_epoch(cfg.dataset_name) // cfg.global_batch_size