-
Notifications
You must be signed in to change notification settings - Fork 5
/
data_loader.py
137 lines (121 loc) · 5.33 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
# original code is from https://github.com/aaron-xichen/pytorch-playground
import torch
import os
import numpy.random as nr
import numpy as np
import bisect
from torchvision import datasets, transforms
from torch.utils.data import DataLoader
from torch.utils.data.sampler import SubsetRandomSampler
def get_raw_data(target_data_loader, num_classes, num_val):
flag = 0
total_data, total_label = 0, 0
validation_index = []
test_index = []
label_count = np.empty(num_classes)
label_count.fill(num_val)
for data, target in target_data_loader:
data, target = data.cuda(), target.cuda()
if flag == 0:
total_label = target
total_data = data
else:
total_label = torch.cat((total_label, target), 0)
total_data = torch.cat((total_data, data), 0)
flag = 1
for index in range(0, total_data.size(0)):
label = total_label[index]
if label_count[label] > 0:
validation_index.append(index)
label_count[label] -= 1
else:
test_index.append(index)
return total_data, total_label, validation_index, test_index
def get_validation(total_data, total_label, validation_index, test_index):
output = []
output.append(total_data.index_select(0, torch.LongTensor(validation_index).cuda()))
output.append(total_label.index_select(0, torch.LongTensor(validation_index).cuda()))
output.append(total_data.index_select(0, torch.LongTensor(test_index).cuda()))
output.append(total_label.index_select(0, torch.LongTensor(test_index).cuda()))
return output
def getSVHN(batch_size, TF, data_root='/tmp/public_dataset/pytorch', train_shuffle=True, train=True, val=True, **kwargs):
data_root = os.path.expanduser(os.path.join(data_root, 'svhn-data'))
num_workers = kwargs.setdefault('num_workers', 1)
kwargs.pop('input_size', None)
print("Building SVHN data loader with {} workers".format(num_workers))
ds = []
if train:
train_loader = torch.utils.data.DataLoader(
datasets.SVHN(
root=data_root, split='train', download=True,
transform=TF,
#target_transform=target_transform,
),
batch_size=batch_size, shuffle=train_shuffle, **kwargs)
ds.append(train_loader)
if val:
test_loader = torch.utils.data.DataLoader(
datasets.SVHN(
root=data_root, split='test', download=True,
transform=TF,
#target_transform=target_transform
),
batch_size=batch_size, shuffle=False, **kwargs)
ds.append(test_loader)
ds = ds[0] if len(ds) == 1 else ds
return ds
def getCIFAR10(batch_size, TF, data_root='/tmp/public_dataset/pytorch', train_shuffle=True, train=True, val=True, **kwargs):
data_root = os.path.expanduser(os.path.join(data_root, 'cifar10-data'))
num_workers = kwargs.setdefault('num_workers', 1)
kwargs.pop('input_size', None)
print("Building CIFAR-10 data loader with {} workers".format(num_workers))
ds = []
if train:
train_loader = torch.utils.data.DataLoader(
datasets.CIFAR10(
root=data_root, train=True, download=True,
transform=TF),
batch_size=batch_size, shuffle=train_shuffle, **kwargs)
ds.append(train_loader)
if val:
test_loader = torch.utils.data.DataLoader(
datasets.CIFAR10(
root=data_root, train=False, download=True,
transform=TF),
batch_size=batch_size, shuffle=False, **kwargs)
ds.append(test_loader)
ds = ds[0] if len(ds) == 1 else ds
return ds
def getCIFAR100(batch_size, TF, data_root='/tmp/public_dataset/pytorch', train_shuffle=True, train=True, val=True, **kwargs):
data_root = os.path.expanduser(os.path.join(data_root, 'cifar100-data'))
num_workers = kwargs.setdefault('num_workers', 1)
kwargs.pop('input_size', None)
print("Building CIFAR-100 data loader with {} workers".format(num_workers))
ds = []
if train:
train_loader = torch.utils.data.DataLoader(
datasets.CIFAR100(
root=data_root, train=True, download=True,
transform=TF),
batch_size=batch_size, shuffle=train_shuffle, **kwargs)
ds.append(train_loader)
if val:
test_loader = torch.utils.data.DataLoader(
datasets.CIFAR100(
root=data_root, train=False, download=True,
transform=TF),
batch_size=batch_size, shuffle=False, **kwargs)
ds.append(test_loader)
ds = ds[0] if len(ds) == 1 else ds
return ds
def getTargetDataSet(data_type, batch_size, input_TF, dataroot, train_shuffle = True):
if data_type == 'cifar10':
train_loader, test_loader \
= getCIFAR10(batch_size=batch_size, TF=input_TF, data_root=dataroot, train_shuffle=train_shuffle, num_workers=1)
elif data_type == 'svhn':
train_loader, test_loader \
= getSVHN(batch_size=batch_size, TF=input_TF, data_root=dataroot, train_shuffle=train_shuffle, num_workers=1)
elif data_type == 'cifar100':
train_loader, test_loader \
= getCIFAR100(batch_size=batch_size, TF=input_TF, data_root=dataroot, train_shuffle=train_shuffle, num_workers=1)
return train_loader, test_loader