-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfilter_scratch.py
More file actions
executable file
·43 lines (36 loc) · 1.58 KB
/
Copy pathfilter_scratch.py
File metadata and controls
executable file
·43 lines (36 loc) · 1.58 KB
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
#! ./venv/bin/python
from RESNET.train import CifarData
import random
from torch.utils.data import Subset
from torch.utils.data import DataLoader
class NewCifarData(CifarData):
@staticmethod
def random_indices_in_range(num_to_remove, num_labels):
"""
param: num_to_remove: the number of labels
to remove from the dataset
param: num_labels: the number of labels in
the dataset
"""
indices = random.sample(range(0, num_labels-1), num_to_remove)
return indices
def filter_dataset(self, batch_size, num_to_remove):
trainset, valset = self.get_dataset()
labels_to_remove = self.random_indices_in_range(num_to_remove, 100)
# get the indices to remove from the torch dataset
# given the labels to remove
train_indices_to_remove = [idx for idx, target in enumerate(trainset.targets) if target not in labels_to_remove]
val_indices_to_remove = [idx for idx, target in enumerate(valset.targets) if target not in labels_to_remove]
trainloader = DataLoader(Subset(trainset, train_indices_to_remove),
batch_size=batch_size,
shuffle=True,
num_workers=2,
)
valloader = DataLoader(Subset(trainset, val_indices_to_remove),
batch_size=1,
shuffle=True,)
return trainloader, valloader
cd = NewCifarData()
trn, val = cd.get_data_loader()
trainloader, valloader = cd.filter_dataset(16, 99)
print(len(valloader))