-
Notifications
You must be signed in to change notification settings - Fork 189
/
dataloader.py
127 lines (104 loc) · 5.4 KB
/
dataloader.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
"""Pytorch dataset object that loads MNIST dataset as bags."""
import numpy as np
import torch
import torch.utils.data as data_utils
from torchvision import datasets, transforms
class MnistBags(data_utils.Dataset):
def __init__(self, target_number=9, mean_bag_length=10, var_bag_length=2, num_bag=250, seed=1, train=True):
self.target_number = target_number
self.mean_bag_length = mean_bag_length
self.var_bag_length = var_bag_length
self.num_bag = num_bag
self.train = train
self.r = np.random.RandomState(seed)
self.num_in_train = 60000
self.num_in_test = 10000
if self.train:
self.train_bags_list, self.train_labels_list = self._create_bags()
else:
self.test_bags_list, self.test_labels_list = self._create_bags()
def _create_bags(self):
if self.train:
loader = data_utils.DataLoader(datasets.MNIST('../datasets',
train=True,
download=True,
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))])),
batch_size=self.num_in_train,
shuffle=False)
else:
loader = data_utils.DataLoader(datasets.MNIST('../datasets',
train=False,
download=True,
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))])),
batch_size=self.num_in_test,
shuffle=False)
for (batch_data, batch_labels) in loader:
all_imgs = batch_data
all_labels = batch_labels
bags_list = []
labels_list = []
for i in range(self.num_bag):
bag_length = np.int(self.r.normal(self.mean_bag_length, self.var_bag_length, 1))
if bag_length < 1:
bag_length = 1
if self.train:
indices = torch.LongTensor(self.r.randint(0, self.num_in_train, bag_length))
else:
indices = torch.LongTensor(self.r.randint(0, self.num_in_test, bag_length))
labels_in_bag = all_labels[indices]
labels_in_bag = labels_in_bag == self.target_number
bags_list.append(all_imgs[indices])
labels_list.append(labels_in_bag)
return bags_list, labels_list
def __len__(self):
if self.train:
return len(self.train_labels_list)
else:
return len(self.test_labels_list)
def __getitem__(self, index):
if self.train:
bag = self.train_bags_list[index]
label = [max(self.train_labels_list[index]), self.train_labels_list[index]]
else:
bag = self.test_bags_list[index]
label = [max(self.test_labels_list[index]), self.test_labels_list[index]]
return bag, label
if __name__ == "__main__":
train_loader = data_utils.DataLoader(MnistBags(target_number=9,
mean_bag_length=10,
var_bag_length=2,
num_bag=100,
seed=1,
train=True),
batch_size=1,
shuffle=True)
test_loader = data_utils.DataLoader(MnistBags(target_number=9,
mean_bag_length=10,
var_bag_length=2,
num_bag=100,
seed=1,
train=False),
batch_size=1,
shuffle=False)
len_bag_list_train = []
mnist_bags_train = 0
for batch_idx, (bag, label) in enumerate(train_loader):
len_bag_list_train.append(int(bag.squeeze(0).size()[0]))
mnist_bags_train += label[0].numpy()[0]
print('Number positive train bags: {}/{}\n'
'Number of instances per bag, mean: {}, max: {}, min {}\n'.format(
mnist_bags_train, len(train_loader),
np.mean(len_bag_list_train), np.max(len_bag_list_train), np.min(len_bag_list_train)))
len_bag_list_test = []
mnist_bags_test = 0
for batch_idx, (bag, label) in enumerate(test_loader):
len_bag_list_test.append(int(bag.squeeze(0).size()[0]))
mnist_bags_test += label[0].numpy()[0]
print('Number positive test bags: {}/{}\n'
'Number of instances per bag, mean: {}, max: {}, min {}\n'.format(
mnist_bags_test, len(test_loader),
np.mean(len_bag_list_test), np.max(len_bag_list_test), np.min(len_bag_list_test)))