Skip to content

Commit

Permalink
Merge pull request #124 from ENSTA-U2IS-AI/dev
Browse files Browse the repository at this point in the history
👕 Change character per line limit to 100 & remove explicit dependencies from pyproject
  • Loading branch information
alafage authored Nov 19, 2024
2 parents d482189 + cf5e9e3 commit c7d870d
Show file tree
Hide file tree
Showing 149 changed files with 758 additions and 2,217 deletions.
6 changes: 2 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,16 @@ keywords = [
]
classifiers = [
"License :: OSI Approved :: Apache Software License",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3 :: Only",
]
dependencies = [
"timm",
"lightning[pytorch-extra]>=2.0",
"torchvision>=0.16",
"einops",
"matplotlib",
"rich>=10.2.2",
"seaborn",
]

Expand Down Expand Up @@ -82,7 +80,7 @@ repository = "https://github.com/ENSTA-U2IS-AI/torch-uncertainty.git"
name = "torch_uncertainty"

[tool.ruff]
line-length = 80
line-length = 100
target-version = "py310"
lint.extend-select = [
"A",
Expand Down
10 changes: 3 additions & 7 deletions tests/_dummies/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ def __init__(
else:
self.data = self.data.transpose((0, 2, 3, 1)) # convert to HWC

self.targets = torch.randint(
low=0, high=num_classes, size=(num_images,)
)
self.targets = torch.randint(low=0, high=num_classes, size=(num_images,))
self.targets = torch.arange(start=0, end=num_classes).repeat(
num_images // (num_classes) + 1
)[:num_images]
Expand Down Expand Up @@ -123,10 +121,8 @@ def __init__(
self.targets = []

input_shape = (num_samples, in_features)
if out_features != 1:
output_shape = (num_samples, out_features)
else:
output_shape = (num_samples,)

output_shape = (num_samples, out_features) if out_features != 1 else (num_samples,)

self.data = torch.rand(
size=input_shape,
Expand Down
4 changes: 1 addition & 3 deletions tests/_dummies/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ def __init__(
self.in_channels = in_channels
self.num_classes = num_classes
self.image_size = image_size
self.conv = nn.Conv2d(
in_channels, num_classes, kernel_size=3, padding=1
)
self.conv = nn.Conv2d(in_channels, num_classes, kernel_size=3, padding=1)
self.dropout = nn.Dropout(p=dropout_rate)
self.last_layer = last_layer

Expand Down
4 changes: 1 addition & 3 deletions tests/baselines/test_deep_ensembles.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ class TestDeepEnsembles:
"""Testing the Deep Ensembles baseline class."""

def test_failure(self):
with pytest.raises(
ValueError, match="Models must not be an empty list."
):
with pytest.raises(ValueError, match="Models must not be an empty list."):
DeepEnsemblesBaseline(
log_path=".",
checkpoint_ids=[],
Expand Down
32 changes: 13 additions & 19 deletions tests/datamodules/classification/test_cifar10.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ def test_cifar10_main(self):
auto_augment="rand-m9-n2-mstd0.5",
)

with pytest.raises(
ValueError, match="CIFAR-H can only be used in testing."
):
with pytest.raises(ValueError, match="CIFAR-H can only be used in testing."):
dm = CIFAR10DataModule(
root="./data/",
batch_size=128,
Expand All @@ -100,25 +98,21 @@ def test_cifar10_main(self):

def test_cifar10_cv(self):
dm = CIFAR10DataModule(root="./data/", batch_size=128)
dm.dataset = (
lambda root, train, download, transform: DummyClassificationDataset(
root,
train=train,
download=download,
transform=transform,
num_images=20,
)
dm.dataset = lambda root, train, download, transform: DummyClassificationDataset(
root,
train=train,
download=download,
transform=transform,
num_images=20,
)
dm.make_cross_val_splits(2, 1)

dm = CIFAR10DataModule(root="./data/", batch_size=128, val_split=0.1)
dm.dataset = (
lambda root, train, download, transform: DummyClassificationDataset(
root,
train=train,
download=download,
transform=transform,
num_images=20,
)
dm.dataset = lambda root, train, download, transform: DummyClassificationDataset(
root,
train=train,
download=download,
transform=transform,
num_images=20,
)
dm.make_cross_val_splits(2, 1)
36 changes: 14 additions & 22 deletions tests/datamodules/classification/test_cifar100.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,35 +58,27 @@ def test_cifar100(self):
randaugment=True,
)

dm = CIFAR100DataModule(
root="./data/", batch_size=128, randaugment=True
)
dm = CIFAR100DataModule(root="./data/", batch_size=128, randaugment=True)

dm = CIFAR100DataModule(
root="./data/", batch_size=128, auto_augment="rand-m9-n2-mstd0.5"
)
dm = CIFAR100DataModule(root="./data/", batch_size=128, auto_augment="rand-m9-n2-mstd0.5")

def test_cifar100_cv(self):
dm = CIFAR100DataModule(root="./data/", batch_size=128)
dm.dataset = (
lambda root, train, download, transform: DummyClassificationDataset(
root,
train=train,
download=download,
transform=transform,
num_images=20,
)
dm.dataset = lambda root, train, download, transform: DummyClassificationDataset(
root,
train=train,
download=download,
transform=transform,
num_images=20,
)
dm.make_cross_val_splits(2, 1)

dm = CIFAR100DataModule(root="./data/", batch_size=128, val_split=0.1)
dm.dataset = (
lambda root, train, download, transform: DummyClassificationDataset(
root,
train=train,
download=download,
transform=transform,
num_images=20,
)
dm.dataset = lambda root, train, download, transform: DummyClassificationDataset(
root,
train=train,
download=download,
transform=transform,
num_images=20,
)
dm.make_cross_val_splits(2, 1)
24 changes: 6 additions & 18 deletions tests/datamodules/classification/test_imagenet.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ def test_imagenet(self):
dm.prepare_data()
dm.setup()

path = (
Path(__file__).parent.resolve() / "../../assets/dummy_indices.yaml"
)
path = Path(__file__).parent.resolve() / "../../assets/dummy_indices.yaml"
dm = ImageNetDataModule(root="./data/", batch_size=128, val_split=path)
dm.dataset = DummyClassificationDataset
dm.ood_dataset = DummyClassificationDataset
Expand Down Expand Up @@ -55,22 +53,16 @@ def test_imagenet(self):
dm.setup("other")

for test_alt in ["r", "o", "a"]:
dm = ImageNetDataModule(
root="./data/", batch_size=128, test_alt=test_alt
)
dm = ImageNetDataModule(root="./data/", batch_size=128, test_alt=test_alt)

with pytest.raises(ValueError):
dm.setup()

with pytest.raises(ValueError):
dm = ImageNetDataModule(
root="./data/", batch_size=128, test_alt="x"
)
dm = ImageNetDataModule(root="./data/", batch_size=128, test_alt="x")

for ood_ds in ["inaturalist", "imagenet-o", "textures", "openimage-o"]:
dm = ImageNetDataModule(
root="./data/", batch_size=128, ood_ds=ood_ds
)
dm = ImageNetDataModule(root="./data/", batch_size=128, ood_ds=ood_ds)
if ood_ds == "inaturalist":
dm.eval_ood = True
dm.dataset = DummyClassificationDataset
Expand All @@ -80,9 +72,7 @@ def test_imagenet(self):
dm.test_dataloader()

with pytest.raises(ValueError):
dm = ImageNetDataModule(
root="./data/", batch_size=128, ood_ds="other"
)
dm = ImageNetDataModule(root="./data/", batch_size=128, ood_ds="other")

for procedure in ["ViT", "A3"]:
dm = ImageNetDataModule(
Expand All @@ -93,9 +83,7 @@ def test_imagenet(self):
)

with pytest.raises(ValueError):
dm = ImageNetDataModule(
root="./data/", batch_size=128, procedure="A2"
)
dm = ImageNetDataModule(root="./data/", batch_size=128, procedure="A2")

with pytest.raises(FileNotFoundError):
dm._verify_splits(split="test")
Expand Down
12 changes: 3 additions & 9 deletions tests/datamodules/classification/test_tiny_imagenet.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ def test_tiny_imagenet(self):
)

with pytest.raises(ValueError):
TinyImageNetDataModule(
root="./data/", batch_size=128, ood_ds="other"
)
TinyImageNetDataModule(root="./data/", batch_size=128, ood_ds="other")

dm.dataset = DummyClassificationDataset
dm.ood_dataset = DummyClassificationDataset
Expand All @@ -52,9 +50,7 @@ def test_tiny_imagenet(self):
dm.setup("test")
dm.test_dataloader()

dm = TinyImageNetDataModule(
root="./data/", batch_size=128, ood_ds="svhn"
)
dm = TinyImageNetDataModule(root="./data/", batch_size=128, ood_ds="svhn")
dm.dataset = DummyClassificationDataset
dm.ood_dataset = DummyClassificationDataset
dm.shift_dataset = DummyClassificationDataset
Expand All @@ -70,9 +66,7 @@ def test_tiny_imagenet_cv(self):
)
dm.make_cross_val_splits(2, 1)

dm = TinyImageNetDataModule(
root="./data/", batch_size=128, val_split=0.1
)
dm = TinyImageNetDataModule(root="./data/", batch_size=128, val_split=0.1)
dm.dataset = lambda root, split, transform: DummyClassificationDataset(
root, split=split, transform=transform, num_images=20
)
Expand Down
8 changes: 2 additions & 6 deletions tests/datamodules/segmentation/test_camvid.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@ class TestCamVidDataModule:
"""Testing the CamVidDataModule datamodule."""

def test_camvid_main(self):
dm = CamVidDataModule(
root="./data/", batch_size=128, group_classes=False
)
dm = CamVidDataModule(
root="./data/", batch_size=128, basic_augment=False
)
dm = CamVidDataModule(root="./data/", batch_size=128, group_classes=False)
dm = CamVidDataModule(root="./data/", batch_size=128, basic_augment=False)

assert dm.dataset == CamVid

Expand Down
4 changes: 1 addition & 3 deletions tests/datamodules/segmentation/test_cityscapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ class TestCityscapesDataModule:

def test_camvid_main(self):
dm = CityscapesDataModule(root="./data/", batch_size=128)
dm = CityscapesDataModule(
root="./data/", batch_size=128, basic_augment=False
)
dm = CityscapesDataModule(root="./data/", batch_size=128, basic_augment=False)

assert dm.dataset == Cityscapes

Expand Down
8 changes: 2 additions & 6 deletions tests/datamodules/test_abstract_datamodule.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ def test_cv_main(self):
dm.train = ds
dm.val = ds
dm.test = ds
cv_dm = CrossValDataModule(
"root", [0], [1], dm, 128, 0.0, 4, True, True
)
cv_dm = CrossValDataModule("root", [0], [1], dm, 128, 0.0, 4, True, True)

cv_dm.setup()
cv_dm.setup("test")
Expand All @@ -54,9 +52,7 @@ def test_errors(self):
dm.train = ds
dm.val = ds
dm.test = ds
cv_dm = CrossValDataModule(
"root", [0], [1], dm, 128, 0.0, 4, True, True
)
cv_dm = CrossValDataModule("root", [0], [1], dm, 128, 0.0, 4, True, True)
with pytest.raises(NotImplementedError):
cv_dm.setup()
cv_dm._get_train_data()
Expand Down
4 changes: 1 addition & 3 deletions tests/datamodules/test_depth.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ class TestMUADDataModule:
"""Testing the MUADDataModule datamodule."""

def test_muad_main(self):
dm = MUADDataModule(
root="./data/", min_depth=0, max_depth=100, batch_size=128
)
dm = MUADDataModule(root="./data/", min_depth=0, max_depth=100, batch_size=128)

assert dm.dataset == MUAD

Expand Down
4 changes: 1 addition & 3 deletions tests/datamodules/test_uci_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ class TestUCIRegressionDataModule:
"""Testing the UCIRegressionDataModule datamodule class."""

def test_uci_regression(self):
dm = UCIRegressionDataModule(
dataset_name="kin8nm", root="./data/", batch_size=128
)
dm = UCIRegressionDataModule(dataset_name="kin8nm", root="./data/", batch_size=128)

dm.dataset = partial(DummyRegressionDataset, num_samples=64)
dm.prepare_data()
Expand Down
24 changes: 6 additions & 18 deletions tests/layers/test_bayesian.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@ def test_conv1(self, feat_input_odd: torch.Tensor) -> None:
assert out.shape == torch.Size([2, 10])

def test_conv1_even(self, feat_input_even: torch.Tensor) -> None:
layer = BayesConv1d(
8, 2, kernel_size=1, sigma_init=0, padding_mode="reflect"
)
layer = BayesConv1d(8, 2, kernel_size=1, sigma_init=0, padding_mode="reflect")
print(layer)
out = layer(feat_input_even)
assert out.shape == torch.Size([2, 10])
Expand All @@ -94,9 +92,7 @@ def test_conv1_even(self, feat_input_even: torch.Tensor) -> None:

def test_error(self):
with pytest.raises(ValueError):
BayesConv1d(
8, 2, kernel_size=1, sigma_init=0, padding_mode="random"
)
BayesConv1d(8, 2, kernel_size=1, sigma_init=0, padding_mode="random")


class TestBayesConv2d:
Expand All @@ -115,9 +111,7 @@ def test_conv2(self, img_input_odd: torch.Tensor) -> None:
layer.sample()

def test_conv2_even(self, img_input_even: torch.Tensor) -> None:
layer = BayesConv2d(
10, 2, kernel_size=1, sigma_init=0, padding_mode="reflect"
)
layer = BayesConv2d(10, 2, kernel_size=1, sigma_init=0, padding_mode="reflect")
print(layer)
out = layer(img_input_even)
assert out.shape == torch.Size([8, 2, 3, 3])
Expand All @@ -141,9 +135,7 @@ def test_conv3(self, cube_input_odd: torch.Tensor) -> None:
assert out.shape == torch.Size([1, 2, 3, 3, 3])

def test_conv3_even(self, cube_input_even: torch.Tensor) -> None:
layer = BayesConv3d(
10, 2, kernel_size=1, sigma_init=0, padding_mode="reflect"
)
layer = BayesConv3d(10, 2, kernel_size=1, sigma_init=0, padding_mode="reflect")
print(layer)
out = layer(cube_input_even)
assert out.shape == torch.Size([2, 2, 3, 3, 3])
Expand Down Expand Up @@ -192,17 +184,13 @@ def test_conv2(self, img_input_odd: torch.Tensor) -> None:
out = layer(img_input_odd.repeat(4, 1, 1, 1))
assert out.shape == torch.Size([5 * 4, 2, 3, 3])

layer = LPBNNConv2d(
10, 2, kernel_size=1, num_estimators=4, bias=False, gamma=False
)
layer = LPBNNConv2d(10, 2, kernel_size=1, num_estimators=4, bias=False, gamma=False)
layer = layer.eval()
out = layer(img_input_odd.repeat(4, 1, 1, 1))
assert out.shape == torch.Size([5 * 4, 2, 3, 3])

def test_conv2_even(self, img_input_even: torch.Tensor) -> None:
layer = LPBNNConv2d(
10, 2, kernel_size=1, num_estimators=4, padding_mode="reflect"
)
layer = LPBNNConv2d(10, 2, kernel_size=1, num_estimators=4, padding_mode="reflect")
print(layer)
out = layer(img_input_even.repeat(4, 1, 1, 1))
assert out.shape == torch.Size([8 * 4, 2, 3, 3])
Expand Down
Loading

0 comments on commit c7d870d

Please sign in to comment.