Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to only catch intentionally raised exceptions #36

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions nonechucks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ def _get_pytorch_version():
from nonechucks.dataset import SafeDataset
from nonechucks.sampler import SafeSampler
from nonechucks.dataloader import SafeDataLoader
from nonechucks.utils import NoneChucksSkipException
7 changes: 6 additions & 1 deletion nonechucks/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@
import torch.utils.data

from nonechucks.utils import memoize
from nonechucks.utils import NoneChucksSkipException


class SafeDataset(torch.utils.data.Dataset):
"""A wrapper around a torch.utils.data.Dataset that allows dropping
samples dynamically.
"""

def __init__(self, dataset, eager_eval=False):
def __init__(self, dataset, eager_eval=False, catch_all = True):
"""Creates a `SafeDataset` wrapper around `dataset`."""
self.dataset = dataset
self.eager_eval = eager_eval
self.catch_all = catch_all
# These will contain indices over the original dataset. The indices of
# the safe samples will go into _safe_indices and similarly for unsafe
# samples.
Expand Down Expand Up @@ -41,6 +43,9 @@ def _safe_get_item(self, idx):
self._safe_indices.append(idx)
return sample
except Exception as e:
if not self.catch_all:
if not isinstance(e, NoneChucksSkipException):
raise
if isinstance(e, IndexError):
if invalid_idx:
raise
Expand Down
4 changes: 4 additions & 0 deletions nonechucks/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
from torch._six import string_classes


class NoneChucksSkipException(Exception):
None # ...Chucks


class memoize(object):
"""cache the return value of a method

Expand Down