-
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
4479fe0
commit 6991d7c
Showing
5 changed files
with
76 additions
and
43 deletions.
There are no files selected for viewing
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 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
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]] | ||
TRANSFORM_DICT = {0: 0, 1: 0, 2: 1, 3: 1, 4: 2, 5: 2, 6: 3, 7: 3, 8: 4, 9: 4} | ||
|
||
|
||
class LabelTransformDataset(Dataset): | ||
""" | ||
Meant to replace: GroupLabelDataset, CorruptLabelDataset | ||
""" | ||
|
||
def __init__(self, dataset, transform_dict=None): | ||
self.dataset = dataset | ||
self.inverse_transform = dataset.inverse_transform | ||
if transform_dict is None: | ||
transform_dict = TRANSFORM_DICT | ||
self.transform_dict = transform_dict | ||
self.inv_transform_dict = self.invert_labels_dict(transform_dict) | ||
self.class_labels = list(self.inv_transform_dict.keys()) | ||
|
||
def __getitem__(self, index): | ||
x, y = self.dataset[index] | ||
g = self.transform_dict[y] | ||
return x, (g, y) | ||
|
||
def __len__(self): | ||
return len(self.dataset) | ||
|
||
@staticmethod | ||
def invert_labels_dict(labels_dict): | ||
return {v: [k for k in labels_dict if labels_dict[k] == v] for v in set(labels_dict.values())} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import torch | ||
|
||
|
||
def mark_image_contour_and_square(x): | ||
mask = torch.zeros_like(x[0]) | ||
mid = int(x.shape[-1] / 2) | ||
mask[mid - 3 : mid + 3, mid - 3 : mid + 3] = 1.0 | ||
mask[:2, :] = 1.0 | ||
mask[-2:, :] = 1.0 | ||
mask[:, -2:] = 1.0 | ||
mask[:, :2] = 1.0 | ||
x[0] = torch.ones_like(x[0]) * mask + x[0] * (1 - mask) | ||
if x.shape[0] > 1: | ||
x[1:] = torch.zeros_like(x[1:]) * mask + x[1:] * (1 - mask) | ||
return x.numpy().transpose(1, 2, 0) | ||
|
||
|
||
def mark_image_middle_square(x): | ||
mask = torch.zeros_like(x[0]) | ||
mid = int(x.shape[-1] / 2) | ||
mask[(mid - 4) : (mid + 4), (mid - 4) : (mid + 4)] = 1.0 | ||
x[0] = torch.ones_like(x[0]) * mask + x[0] * (1 - mask) | ||
if x.shape[0] > 1: | ||
x[1:] = torch.zeros_like(x[1:]) * mask + x[1:] * (1 - mask) | ||
return x.numpy().transpose(1, 2, 0) | ||
|
||
|
||
def mark_image_contour(x): | ||
# TODO: make controur, middle square and combined masks a constant somewhere else | ||
mask = torch.zeros_like(x[0]) | ||
mask[:2, :] = 1.0 | ||
mask[-2:, :] = 1.0 | ||
mask[:, -2:] = 1.0 | ||
mask[:, :2] = 1.0 | ||
x[0] = torch.ones_like(x[0]) * mask + x[0] * (1 - mask) | ||
if x.shape[0] > 1: | ||
x[1:] = torch.zeros_like(x[1:]) * mask + x[1:] * (1 - mask) | ||
|
||
return x.numpy().transpose(1, 2, 0) |