|
| 1 | +# Copyright contributors to the Terratorch project |
| 2 | + |
| 3 | +import os |
| 4 | + |
| 5 | +import pytest |
| 6 | +import torch |
| 7 | + |
| 8 | +from terratorch.models import SMPModelFactory |
| 9 | +from terratorch.models.backbones.prithvi_vit import PRETRAINED_BANDS |
| 10 | + |
| 11 | +# from terratorch.models.backbones.prithvi_vit import default_cfgs as vit_default_cfgs |
| 12 | + |
| 13 | +NUM_CHANNELS = 6 |
| 14 | +NUM_CLASSES = 2 |
| 15 | +EXPECTED_SEGMENTATION_OUTPUT_SHAPE = (1, NUM_CLASSES, 224, 224) |
| 16 | +EXPECTED_REGRESSION_OUTPUT_SHAPE = (1, 224, 224) |
| 17 | +EXPECTED_CLASSIFICATION_OUTPUT_SHAPE = (1, NUM_CLASSES) |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture(scope="session") |
| 21 | +def model_factory() -> SMPModelFactory: |
| 22 | + return SMPModelFactory() |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture(scope="session") |
| 26 | +def model_input() -> torch.Tensor: |
| 27 | + return torch.ones((1, NUM_CHANNELS, 224, 224)) |
| 28 | + |
| 29 | + |
| 30 | +@pytest.mark.parametrize("backbone", ["timm-regnetx_002"]) |
| 31 | +@pytest.mark.parametrize("model", ["Unet", "DeepLabV3"]) |
| 32 | +def test_create_segmentation_model(backbone, model, model_factory: SMPModelFactory, model_input): |
| 33 | + model = model_factory.build_model( |
| 34 | + "segmentation", |
| 35 | + backbone=backbone, |
| 36 | + model=model, |
| 37 | + in_channels=NUM_CHANNELS, |
| 38 | + bands=PRETRAINED_BANDS, |
| 39 | + pretrained=False, |
| 40 | + num_classes=NUM_CLASSES, |
| 41 | + ) |
| 42 | + model.eval() |
| 43 | + |
| 44 | + with torch.no_grad(): |
| 45 | + assert model(model_input).output.shape == EXPECTED_SEGMENTATION_OUTPUT_SHAPE |
| 46 | + |
| 47 | + |
| 48 | +@pytest.mark.parametrize("backbone", ["timm-regnetx_002"]) |
| 49 | +@pytest.mark.parametrize("model", ["Unet", "DeepLabV3"]) |
| 50 | +def test_create_segmentation_model_no_in_channels(backbone, model, model_factory: SMPModelFactory, model_input): |
| 51 | + model = model_factory.build_model( |
| 52 | + "segmentation", |
| 53 | + backbone=backbone, |
| 54 | + model=model, |
| 55 | + bands=PRETRAINED_BANDS, |
| 56 | + pretrained=False, |
| 57 | + num_classes=NUM_CLASSES, |
| 58 | + ) |
| 59 | + model.eval() |
| 60 | + |
| 61 | + with torch.no_grad(): |
| 62 | + assert model(model_input).output.shape == EXPECTED_SEGMENTATION_OUTPUT_SHAPE |
| 63 | + |
| 64 | + |
| 65 | +@pytest.mark.parametrize("backbone", ["timm-regnetx_002"]) |
| 66 | +@pytest.mark.parametrize("model", ["Unet", "DeepLabV3"]) |
| 67 | +def test_create_model_with_extra_bands(backbone, model, model_factory: SMPModelFactory): |
| 68 | + model = model_factory.build_model( |
| 69 | + "segmentation", |
| 70 | + backbone=backbone, |
| 71 | + model=model, |
| 72 | + in_channels=NUM_CHANNELS + 1, |
| 73 | + bands=[*PRETRAINED_BANDS, 7], # add an extra band |
| 74 | + pretrained=False, |
| 75 | + num_classes=NUM_CLASSES, |
| 76 | + ) |
| 77 | + model.eval() |
| 78 | + model_input = torch.ones((1, NUM_CHANNELS + 1, 224, 224)) |
| 79 | + with torch.no_grad(): |
| 80 | + assert model(model_input).output.shape == EXPECTED_SEGMENTATION_OUTPUT_SHAPE |
0 commit comments