-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c68616c
commit 945f752
Showing
13 changed files
with
216 additions
and
202 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import random | ||
|
||
import torch | ||
from torch.utils.data.dataset import Dataset | ||
|
||
from utils.cache import IndicesCache as IC | ||
|
||
|
||
class CorruptLabelDataset(Dataset): | ||
def __init__( | ||
self, | ||
dataset: Dataset, | ||
dataset_id: str, | ||
class_labels, | ||
classes, | ||
inverse_transform, | ||
cache_path="./datasets", | ||
p=0.3, | ||
): | ||
super().__init__() | ||
self.dataset = dataset | ||
self.class_labels = class_labels | ||
self.classes = classes | ||
self.inverse_transform = inverse_transform | ||
self.p = p | ||
|
||
if IC.exists(path=cache_path, file_id=f"{dataset_id}_corrupt_ids"): | ||
self.corrupt_indices = IC.load(path=cache_path, file_id=f"{dataset_id}_corrupt_ids") | ||
else: | ||
self.corrupt_indices = self.get_corrupt_sample_ids() | ||
IC.save(path=cache_path, file_id=f"{dataset_id}_corrupt_ids", indices=self.corrupt_indices) | ||
|
||
self.corrupt_labels = [self.corrupt_label(self.dataset[i][1]) for i in self.corrupt_indices] | ||
IC.save(path=cache_path, file_id=f"{dataset_id}_corrupt_labels", indices=self.corrupt_labels) | ||
|
||
def get_corrupt_sample_ids(self): | ||
corrupt = torch.rand(len(self.dataset)) | ||
return torch.where(corrupt < self.p)[0] | ||
|
||
def __getitem__(self, item): | ||
x, y_true = self.dataset[item] | ||
y = y_true | ||
if item in self.corrupt_indices: | ||
y = int( | ||
self.corrupt_labels[torch.squeeze((self.corrupt_indices == item).nonzero())] | ||
) # TODO: not the most elegant solution | ||
return x, (y, y_true) | ||
|
||
def __len__(self): | ||
return len(self.dataset) | ||
|
||
def corrupt_label(self, y): | ||
classes = [cls for cls in self.classes if cls != y] | ||
random.seed(27) | ||
corrupted_class = random.choice(classes) | ||
return corrupted_class |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from torch.utils.data.dataset import Dataset | ||
|
||
CLASS_GROUP_BY = [[0, 1], [2, 3], [4, 5], [6, 7], [8, 9]] | ||
|
||
|
||
class GroupLabelDataset(Dataset): | ||
def __init__(self, dataset, class_groups=None): | ||
self.dataset = dataset | ||
self.class_labels = [i for i in range(len(class_groups))] | ||
self.inverse_transform = dataset.inverse_transform | ||
if class_groups is None: | ||
class_groups = CLASS_GROUP_BY | ||
self.class_groups = class_groups | ||
self.inverted_class_groups = self.invert_class_groups(class_groups) | ||
|
||
def __getitem__(self, index): | ||
x, y = self.dataset[index] | ||
g = self.inverted_class_groups[y] | ||
return x, (g, y) | ||
|
||
def __len__(self): | ||
return len(self.dataset) | ||
|
||
@staticmethod | ||
def invert_class_groups(groups): | ||
inverted_class_groups = {} | ||
for g, group in enumerate(groups): | ||
intersection = inverted_class_groups.keys() & group | ||
if len(intersection) > 0: | ||
raise ValueError("Class indices %s are present in multiple groups." % (str(intersection))) | ||
inverted_class_groups.update({cls: g for cls in group}) | ||
return inverted_class_groups |
9 changes: 3 additions & 6 deletions
9
src/datasets/restricted_dataset.py → src/utils/datasets/indexed_subset.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.