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

ObjectDetectionTask: increase test coverage for torchvision 0.14+ #1739

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
40 changes: 37 additions & 3 deletions tests/trainers/test_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,14 @@ def load(url: str, *args: Any, **kwargs: Any) -> dict[str, Any]:
return state_dict


def plot(*args: Any, **kwargs: Any) -> None:
def plot_no_rgb(*args: Any, **kwargs: Any) -> None:
raise ValueError


def no_plot_method(*args: Any, **kwargs: Any) -> None:
return None


class TestClassificationTask:
@pytest.mark.parametrize(
"name",
Expand Down Expand Up @@ -181,7 +185,21 @@ def test_invalid_loss(self) -> None:
ClassificationTask(model="resnet18", loss="invalid_loss")

def test_no_rgb(self, monkeypatch: MonkeyPatch, fast_dev_run: bool) -> None:
monkeypatch.setattr(EuroSATDataModule, "plot", plot)
monkeypatch.setattr(EuroSATDataModule, "plot", plot_no_rgb)
datamodule = EuroSATDataModule(
root="tests/data/eurosat", batch_size=1, num_workers=0
)
model = ClassificationTask(model="resnet18", in_channels=13, num_classes=10)
trainer = Trainer(
accelerator="cpu",
fast_dev_run=fast_dev_run,
log_every_n_steps=1,
max_epochs=1,
)
trainer.validate(model=model, datamodule=datamodule)

def test_no_plot_method(self, monkeypatch: MonkeyPatch, fast_dev_run: bool) -> None:
monkeypatch.setattr(EuroSATDataModule, "plot", no_plot_method)
datamodule = EuroSATDataModule(
root="tests/data/eurosat", batch_size=1, num_workers=0
)
Expand Down Expand Up @@ -258,7 +276,23 @@ def test_invalid_loss(self) -> None:
MultiLabelClassificationTask(model="resnet18", loss="invalid_loss")

def test_no_rgb(self, monkeypatch: MonkeyPatch, fast_dev_run: bool) -> None:
monkeypatch.setattr(BigEarthNetDataModule, "plot", plot)
monkeypatch.setattr(BigEarthNetDataModule, "plot", plot_no_rgb)
datamodule = BigEarthNetDataModule(
root="tests/data/bigearthnet", batch_size=1, num_workers=0
)
model = MultiLabelClassificationTask(
model="resnet18", in_channels=14, num_classes=19, loss="bce"
)
trainer = Trainer(
accelerator="cpu",
fast_dev_run=fast_dev_run,
log_every_n_steps=1,
max_epochs=1,
)
trainer.validate(model=model, datamodule=datamodule)

def test_no_plot_method(self, monkeypatch: MonkeyPatch, fast_dev_run: bool) -> None:
monkeypatch.setattr(BigEarthNetDataModule, "plot", no_plot_method)
datamodule = BigEarthNetDataModule(
root="tests/data/bigearthnet", batch_size=1, num_workers=0
)
Expand Down
27 changes: 24 additions & 3 deletions tests/trainers/test_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,27 @@ def forward(self, images: Any, targets: Any = None) -> Any:
else: # eval mode
output = []
for i in range(batch_size):
boxes = torch.zeros(10, 4, dtype=torch.float)
# Create xmax, ymax larger than 0.0
boxes[:, 2:4] = torch.FloatTensor(10, 2).uniform_(0.1, 0.9)
output.append(
{
"boxes": torch.rand(10, 4),
"boxes": boxes,
adamjstewart marked this conversation as resolved.
Show resolved Hide resolved
"labels": torch.randint(0, 2, (10,)),
"scores": torch.rand(10),
}
)
return output


def plot(*args: Any, **kwargs: Any) -> None:
def plot_no_rgb(*args: Any, **kwargs: Any) -> None:
raise ValueError


def no_plot_method(*args: Any, **kwargs: Any) -> None:
return None


class TestObjectDetectionTask:
@pytest.mark.parametrize("name", ["nasa_marine_debris"])
@pytest.mark.parametrize("model_name", ["faster-rcnn", "fcos", "retinanet"])
Expand Down Expand Up @@ -114,7 +121,21 @@ def test_pretrained_backbone(self) -> None:
ObjectDetectionTask(backbone="resnet18", weights=True)

def test_no_rgb(self, monkeypatch: MonkeyPatch, fast_dev_run: bool) -> None:
monkeypatch.setattr(NASAMarineDebrisDataModule, "plot", plot)
monkeypatch.setattr(NASAMarineDebrisDataModule, "plot", plot_no_rgb)
datamodule = NASAMarineDebrisDataModule(
root="tests/data/nasa_marine_debris", batch_size=1, num_workers=0
)
model = ObjectDetectionTask(backbone="resnet18", num_classes=2)
trainer = Trainer(
accelerator="cpu",
fast_dev_run=fast_dev_run,
log_every_n_steps=1,
max_epochs=1,
)
trainer.validate(model=model, datamodule=datamodule)

def test_no_plot_method(self, monkeypatch: MonkeyPatch, fast_dev_run: bool) -> None:
monkeypatch.setattr(NASAMarineDebrisDataModule, "plot", no_plot_method)
datamodule = NASAMarineDebrisDataModule(
root="tests/data/nasa_marine_debris", batch_size=1, num_workers=0
)
Expand Down
22 changes: 20 additions & 2 deletions tests/trainers/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,14 @@ def load(url: str, *args: Any, **kwargs: Any) -> dict[str, Any]:
return state_dict


def plot(*args: Any, **kwargs: Any) -> None:
def plot_no_rgb(*args: Any, **kwargs: Any) -> None:
raise ValueError


def no_plot_method(*args: Any, **kwargs: Any) -> None:
return None


class TestRegressionTask:
@classmethod
def create_model(*args: Any, **kwargs: Any) -> Module:
Expand Down Expand Up @@ -153,7 +157,21 @@ def test_weight_str_download(self, weights: WeightsEnum) -> None:
)

def test_no_rgb(self, monkeypatch: MonkeyPatch, fast_dev_run: bool) -> None:
monkeypatch.setattr(TropicalCycloneDataModule, "plot", plot)
monkeypatch.setattr(TropicalCycloneDataModule, "plot", plot_no_rgb)
datamodule = TropicalCycloneDataModule(
root="tests/data/cyclone", batch_size=1, num_workers=0
)
model = RegressionTask(model="resnet18")
trainer = Trainer(
accelerator="cpu",
fast_dev_run=fast_dev_run,
log_every_n_steps=1,
max_epochs=1,
)
trainer.validate(model=model, datamodule=datamodule)

def test_no_plot_method(self, monkeypatch: MonkeyPatch, fast_dev_run: bool) -> None:
monkeypatch.setattr(TropicalCycloneDataModule, "plot", no_plot_method)
datamodule = TropicalCycloneDataModule(
root="tests/data/cyclone", batch_size=1, num_workers=0
)
Expand Down
24 changes: 22 additions & 2 deletions tests/trainers/test_segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,14 @@ def load(url: str, *args: Any, **kwargs: Any) -> dict[str, Any]:
return state_dict


def plot(*args: Any, **kwargs: Any) -> None:
def plot_no_rgb(*args: Any, **kwargs: Any) -> None:
raise ValueError


def no_plot_method(*args: Any, **kwargs: Any) -> None:
return None


class TestSemanticSegmentationTask:
@pytest.mark.parametrize(
"name",
Expand Down Expand Up @@ -180,7 +184,23 @@ def test_ignoreindex_with_jaccard(self) -> None:
SemanticSegmentationTask(loss="jaccard", ignore_index=0)

def test_no_rgb(self, monkeypatch: MonkeyPatch, fast_dev_run: bool) -> None:
monkeypatch.setattr(SEN12MSDataModule, "plot", plot)
monkeypatch.setattr(SEN12MSDataModule, "plot", plot_no_rgb)
datamodule = SEN12MSDataModule(
root="tests/data/sen12ms", batch_size=1, num_workers=0
)
model = SemanticSegmentationTask(
backbone="resnet18", in_channels=15, num_classes=6
)
trainer = Trainer(
accelerator="cpu",
fast_dev_run=fast_dev_run,
log_every_n_steps=1,
max_epochs=1,
)
trainer.validate(model=model, datamodule=datamodule)

def test_no_plot_method(self, monkeypatch: MonkeyPatch, fast_dev_run: bool) -> None:
monkeypatch.setattr(SEN12MSDataModule, "plot", no_plot_method)
datamodule = SEN12MSDataModule(
root="tests/data/sen12ms", batch_size=1, num_workers=0
)
Expand Down
Loading