-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdata.py
69 lines (53 loc) · 1.94 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
import torch
from torchvision import datasets, transforms
from torch.utils.data import Dataset
from torch.utils.data.sampler import SubsetRandomSampler
import numpy as np
def load_data(train, batch_size):
"""Helper function used to load the train/test data.
Args:
train[boolean]: Indicates whether its train/test data.
batch_size[int]: Batch size
"""
loader = torch.utils.data.DataLoader(
datasets.MNIST(
"../data",
train=train,
download=True,
transform=transforms.Compose(
[transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,))]
),
),
batch_size=batch_size,
shuffle=True,
)
return loader
class NoisyDataset(Dataset):
"""Dataset with targets predicted by ensemble of teachers.
Args:
dataloader (torch dataloader): The original torch dataloader.
model(torch model): Teacher model to make predictions.
transform (callable, optional): Optional transform to be applied on a sample.
"""
def __init__(self, dataloader, predictionfn, transform=None):
self.dataloader = dataloader
self.predictionfn = predictionfn
self.transform = transform
self.noisy_data = self.process_data()
def process_data(self):
"""
Replaces original targets with targets predicted by ensemble of teachers.
Returns:
noisy_data[torch tensor]: Dataset with labels predicted by teachers
"""
noisy_data = []
for data, _ in self.dataloader:
noisy_data.append([data, torch.tensor(self.predictionfn(data)["predictions"])])
return noisy_data
def __len__(self):
return len(self.dataloader)
def __getitem__(self, idx):
sample = self.noisy_data[idx]
if self.transform:
sample = self.transform(sample)
return sample