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

Remove dataset-specific trainers #286

Merged
merged 18 commits into from
Jan 1, 2022
Merged
Show file tree
Hide file tree
Changes from 16 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
2 changes: 1 addition & 1 deletion conf/task_defaults/eurosat.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ experiment:
learning_rate_schedule_patience: 6
weights: "random"
in_channels: 13
num_classes: 10
num_classes: 2
adamjstewart marked this conversation as resolved.
Show resolved Hide resolved
datamodule:
root_dir: "tests/data/eurosat"
batch_size: 1
Expand Down
2 changes: 1 addition & 1 deletion conf/task_defaults/resisc45.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ experiment:
learning_rate_schedule_patience: 6
weights: "random"
in_channels: 3
num_classes: 45
num_classes: 3
datamodule:
root_dir: "tests/data/resisc45"
batch_size: 1
Expand Down
2 changes: 1 addition & 1 deletion conf/task_defaults/ucmerced.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ experiment:
learning_rate: 1e-3
learning_rate_schedule_patience: 6
in_channels: 3
num_classes: 21
num_classes: 2
datamodule:
root_dir: "tests/data/ucmerced"
batch_size: 1
Expand Down
1 change: 1 addition & 0 deletions docs/api/datasets.rst
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,4 @@ Collation Functions
.. autofunction:: stack_samples
.. autofunction:: concat_samples
.. autofunction:: merge_samples
.. autofunction:: unbind_samples
27 changes: 27 additions & 0 deletions tests/datamodules/test_landcoverai.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import os
from typing import Any, Dict, cast

import pytest
from omegaconf import OmegaConf

from torchgeo.datamodules import LandCoverAIDataModule


class TestLandCoverAIDataModule:
@pytest.fixture(scope="class")
def datamodule(self) -> LandCoverAIDataModule:
conf = OmegaConf.load(os.path.join("conf", "task_defaults", "landcoverai.yaml"))
kwargs = OmegaConf.to_object(conf.experiment.datamodule)
kwargs = cast(Dict[str, Any], kwargs)

datamodule = LandCoverAIDataModule(**kwargs)
datamodule.prepare_data()
datamodule.setup()
return datamodule

def test_undefined_attribute(self, datamodule: LandCoverAIDataModule) -> None:
batch = next(iter(datamodule.train_dataloader()))
batch = datamodule.on_after_batch_transfer(batch, 0)
27 changes: 27 additions & 0 deletions tests/datamodules/test_resisc45.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import os
from typing import Any, Dict, cast

import pytest
from omegaconf import OmegaConf

from torchgeo.datamodules import RESISC45DataModule


class TestRESISC45DataModule:
@pytest.fixture(scope="class")
def datamodule(self) -> RESISC45DataModule:
conf = OmegaConf.load(os.path.join("conf", "task_defaults", "resisc45.yaml"))
kwargs = OmegaConf.to_object(conf.experiment.datamodule)
kwargs = cast(Dict[str, Any], kwargs)

datamodule = RESISC45DataModule(**kwargs)
datamodule.prepare_data()
datamodule.setup()
return datamodule

def test_undefined_attribute(self, datamodule: RESISC45DataModule) -> None:
batch = next(iter(datamodule.train_dataloader()))
batch = datamodule.on_after_batch_transfer(batch, 0)
22 changes: 20 additions & 2 deletions tests/datasets/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
merge_samples,
percentile_normalization,
stack_samples,
unbind_samples,
working_dir,
)

Expand Down Expand Up @@ -457,7 +458,7 @@ def samples(self) -> List[Dict[str, Any]]:
},
]

def test_stack_samples(self, samples: List[Dict[str, Any]]) -> None:
def test_stack_unbind_samples(self, samples: List[Dict[str, Any]]) -> None:
sample = stack_samples(samples)
assert sample["image"].size() == torch.Size( # type: ignore[attr-defined]
[2, 3]
Expand All @@ -468,6 +469,13 @@ def test_stack_samples(self, samples: List[Dict[str, Any]]) -> None:
)
assert sample["crs"] == [CRS.from_epsg(2000), CRS.from_epsg(2001)]

new_samples = unbind_samples(sample)
for i in range(2):
assert torch.allclose( # type: ignore[attr-defined]
samples[i]["image"], new_samples[i]["image"]
)
assert samples[i]["crs"] == new_samples[i]["crs"]

def test_concat_samples(self, samples: List[Dict[str, Any]]) -> None:
sample = concat_samples(samples)
assert sample["image"].size() == torch.Size([6]) # type: ignore[attr-defined]
Expand Down Expand Up @@ -500,7 +508,7 @@ def samples(self) -> List[Dict[str, Any]]:
},
]

def test_stack_samples(self, samples: List[Dict[str, Any]]) -> None:
def test_stack_unbind_samples(self, samples: List[Dict[str, Any]]) -> None:
sample = stack_samples(samples)
assert sample["image"].size() == torch.Size( # type: ignore[attr-defined]
[1, 3]
Expand All @@ -515,6 +523,16 @@ def test_stack_samples(self, samples: List[Dict[str, Any]]) -> None:
assert sample["crs1"] == [CRS.from_epsg(2000)]
assert sample["crs2"] == [CRS.from_epsg(2001)]

new_samples = unbind_samples(sample)
assert torch.allclose( # type: ignore[attr-defined]
samples[0]["image"], new_samples[0]["image"]
)
assert samples[0]["crs1"] == new_samples[0]["crs1"]
assert torch.allclose( # type: ignore[attr-defined]
samples[1]["mask"], new_samples[0]["mask"]
)
assert samples[1]["crs2"] == new_samples[0]["crs2"]

def test_concat_samples(self, samples: List[Dict[str, Any]]) -> None:
sample = concat_samples(samples)
assert sample["image"].size() == torch.Size([3]) # type: ignore[attr-defined]
Expand Down
64 changes: 0 additions & 64 deletions tests/trainers/test_chesapeake.py

This file was deleted.

36 changes: 36 additions & 0 deletions tests/trainers/test_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,23 @@ def test_trainer(self, name: str, classname: Type[LightningDataModule]) -> None:
trainer.fit(model=model, datamodule=datamodule)
trainer.test(model=model, datamodule=datamodule)

def test_no_logger(self) -> None:
conf = OmegaConf.load(os.path.join("conf", "task_defaults", "ucmerced.yaml"))
conf_dict = OmegaConf.to_object(conf.experiment)
conf_dict = cast(Dict[Any, Dict[Any, Any]], conf_dict)

# Instantiate datamodule
datamodule_kwargs = conf_dict["datamodule"]
datamodule = UCMercedDataModule(**datamodule_kwargs)

# Instantiate model
model_kwargs = conf_dict["module"]
model = ClassificationTask(**model_kwargs)

# Instantiate trainer
trainer = Trainer(logger=None, fast_dev_run=True, log_every_n_steps=1)
trainer.fit(model=model, datamodule=datamodule)

@pytest.fixture
def model_kwargs(self) -> Dict[Any, Any]:
return {
Expand Down Expand Up @@ -120,6 +137,25 @@ def test_trainer(self, name: str, classname: Type[LightningDataModule]) -> None:
trainer.fit(model=model, datamodule=datamodule)
trainer.test(model=model, datamodule=datamodule)

def test_no_logger(self) -> None:
conf = OmegaConf.load(
os.path.join("conf", "task_defaults", "bigearthnet_s1.yaml")
)
conf_dict = OmegaConf.to_object(conf.experiment)
conf_dict = cast(Dict[Any, Dict[Any, Any]], conf_dict)

# Instantiate datamodule
datamodule_kwargs = conf_dict["datamodule"]
datamodule = BigEarthNetDataModule(**datamodule_kwargs)

# Instantiate model
model_kwargs = conf_dict["module"]
model = MultiLabelClassificationTask(**model_kwargs)

# Instantiate trainer
trainer = Trainer(logger=None, fast_dev_run=True, log_every_n_steps=1)
trainer.fit(model=model, datamodule=datamodule)

@pytest.fixture
def model_kwargs(self) -> Dict[Any, Any]:
return {
Expand Down
67 changes: 0 additions & 67 deletions tests/trainers/test_landcoverai.py

This file was deleted.

55 changes: 0 additions & 55 deletions tests/trainers/test_naipchesapeake.py

This file was deleted.

Loading