Skip to content

🐛 Fix MNIST test dataloader for shifted data #135

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

Merged
merged 7 commits into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 3 additions & 1 deletion torch_uncertainty/datamodules/classification/mnist.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,12 @@ def test_dataloader(self) -> list[DataLoader]:

Return:
list[DataLoader]: Dataloaders of the MNIST test set (in
distribution data) and FashionMNIST test split
distribution data), MNISTC (shifted data), and FashionMNIST test split
(out-of-distribution data).
"""
dataloader = [self._data_loader(self.test)]
if self.eval_ood:
dataloader.append(self._data_loader(self.ood))
if self.eval_shift:
dataloader.append(self._data_loader(self.shift))
return dataloader
8 changes: 8 additions & 0 deletions torch_uncertainty/datasets/classification/mnist_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class MNISTC(VisionDataset):
takes in the target and transforms it. Defaults to None.
subset (str): The subset to use, one of ``all`` or the keys in
``mnistc_subsets``.
shift_severity (int): The shift_severity of the corruption, between 1 and 5.
download (bool, optional): If True, downloads the dataset from the
internet and puts it in root directory. If dataset is already
downloaded, it is not downloaded again. Defaults to False.
Expand Down Expand Up @@ -70,6 +71,7 @@ def __init__(
target_transform: Callable | None = None,
split: Literal["train", "test"] = "test",
subset: str = "all",
shift_severity: int = 1,
download: bool = False,
) -> None:
self.root = Path(root)
Expand All @@ -90,6 +92,12 @@ def __init__(
raise ValueError(f"The subset '{subset}' does not exist in MNIST-C.")
self.subset = subset

self.shift_severity = shift_severity
if shift_severity not in list(range(1, 6)):
raise ValueError(
"Corruptions shift_severity should be chosen between 1 and 5 " "included."
)

if split not in ["train", "test"]:
raise ValueError(f"The split '{split}' should be either 'train' or 'test'.")
self.split = split
Expand Down
Loading