-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from BloodAxe/develop
PyTorch Toolbelt 0.4.3
- Loading branch information
Showing
24 changed files
with
700 additions
and
536 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
from __future__ import absolute_import | ||
|
||
__version__ = "0.4.2" | ||
__version__ = "0.4.3" |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from .common import * | ||
from .classification import * | ||
from .segmentation import * | ||
from .wrappers import * |
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
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,55 @@ | ||
import random | ||
from typing import Any | ||
|
||
from torch.utils.data import Dataset | ||
import numpy as np | ||
|
||
__all__ = ["RandomSubsetDataset", "RandomSubsetWithMaskDataset"] | ||
|
||
|
||
class RandomSubsetDataset(Dataset): | ||
""" | ||
Wrapper to get desired number of samples from underlying dataset | ||
""" | ||
|
||
def __init__(self, dataset, num_samples: int): | ||
self.dataset = dataset | ||
self.num_samples = num_samples | ||
|
||
def __len__(self) -> int: | ||
return self.num_samples | ||
|
||
def __getitem__(self, _) -> Any: | ||
index = random.randrange(len(self.dataset)) | ||
return self.dataset[index] | ||
|
||
|
||
class RandomSubsetWithMaskDataset(Dataset): | ||
""" | ||
Wrapper to get desired number of samples from underlying dataset only considering | ||
samples P for which mask[P] equals True | ||
""" | ||
|
||
def __init__(self, dataset: Dataset, mask: np.ndarray, num_samples: int): | ||
if ( | ||
not isinstance(mask, np.ndarray) | ||
or mask.dtype != np.bool | ||
or len(mask.shape) != 1 | ||
or len(mask) != len(dataset) | ||
): | ||
raise ValueError("Mask must be boolean 1-D numpy array") | ||
|
||
if not mask.any(): | ||
raise ValueError("Mask must have at least one positive value") | ||
|
||
self.dataset = dataset | ||
self.mask = mask | ||
self.num_samples = num_samples | ||
self.indexes = np.flatnonzero(self.mask) | ||
|
||
def __len__(self) -> int: | ||
return self.num_samples | ||
|
||
def __getitem__(self, _) -> Any: | ||
index = random.choice(self.indexes) | ||
return self.dataset[index] |
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
Oops, something went wrong.