-
Notifications
You must be signed in to change notification settings - Fork 351
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
Add ChangeDetectionTask #2422
Draft
keves1
wants to merge
5
commits into
microsoft:main
Choose a base branch
from
keves1:change-detection-task
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Add ChangeDetectionTask #2422
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a66d7c4
starting from PR #1760
keves1 a392676
changed from image1, image2 to stacked images.
keves1 a8e1f0a
fixed mypy and ruff issues
keves1 4513e84
adding tests. some still need work.
keves1 7e5ba82
making Kornia transforms work with added temporal dimension.
keves1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
model: | ||
class_path: ChangeDetectionTask | ||
init_args: | ||
loss: 'ce' | ||
model: 'unet' | ||
backbone: 'resnet18' | ||
in_channels: 13 | ||
num_classes: 2 | ||
ignore_index: 0 | ||
data: | ||
class_path: OSCDDataModule | ||
init_args: | ||
batch_size: 2 | ||
patch_size: 16 | ||
val_split_pct: 0.5 | ||
dict_kwargs: | ||
root: 'tests/data/oscd' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
import os | ||
from pathlib import Path | ||
from typing import Any, cast | ||
|
||
import pytest | ||
import segmentation_models_pytorch as smp | ||
import timm | ||
import torch | ||
import torch.nn as nn | ||
from lightning.pytorch import Trainer | ||
from pytest import MonkeyPatch | ||
from torch.nn.modules import Module | ||
from torchvision.models._api import WeightsEnum | ||
|
||
from torchgeo.datamodules import MisconfigurationException, SEN12MSDataModule | ||
from torchgeo.datasets import RGBBandsMissingError | ||
from torchgeo.main import main | ||
from torchgeo.models import ResNet18_Weights | ||
from torchgeo.trainers import ChangeDetectionTask | ||
|
||
|
||
class ChangeDetectionTestModel(Module): | ||
def __init__(self, in_channels: int = 3, classes: int = 3, **kwargs: Any) -> None: | ||
super().__init__() | ||
self.conv1 = nn.Conv2d( | ||
in_channels=in_channels, out_channels=classes, kernel_size=1, padding=0 | ||
) | ||
|
||
def forward(self, x: torch.Tensor) -> torch.Tensor: | ||
return cast(torch.Tensor, self.conv1(x)) | ||
|
||
|
||
def create_model(**kwargs: Any) -> Module: | ||
return ChangeDetectionTestModel(**kwargs) | ||
|
||
|
||
def plot(*args: Any, **kwargs: Any) -> None: | ||
return None | ||
|
||
|
||
def plot_missing_bands(*args: Any, **kwargs: Any) -> None: | ||
raise RGBBandsMissingError() | ||
|
||
|
||
class TestChangeDetectionTask: | ||
@pytest.mark.parametrize('name', ['oscd']) | ||
def test_trainer( | ||
self, monkeypatch: MonkeyPatch, name: str, fast_dev_run: bool | ||
) -> None: | ||
config = os.path.join('tests', 'conf', name + '.yaml') | ||
|
||
monkeypatch.setattr(smp, 'Unet', create_model) | ||
monkeypatch.setattr(smp, 'DeepLabV3Plus', create_model) | ||
|
||
args = [ | ||
'--config', | ||
config, | ||
'--trainer.accelerator', | ||
'cpu', | ||
'--trainer.fast_dev_run', | ||
str(fast_dev_run), | ||
'--trainer.max_epochs', | ||
'1', | ||
'--trainer.log_every_n_steps', | ||
'1', | ||
] | ||
|
||
main(['fit', *args]) | ||
try: | ||
main(['test', *args]) | ||
except MisconfigurationException: | ||
pass | ||
try: | ||
main(['predict', *args]) | ||
except MisconfigurationException: | ||
pass | ||
|
||
@pytest.fixture | ||
def weights(self) -> WeightsEnum: | ||
return ResNet18_Weights.SENTINEL2_ALL_MOCO | ||
|
||
@pytest.fixture | ||
def mocked_weights( | ||
self, | ||
tmp_path: Path, | ||
monkeypatch: MonkeyPatch, | ||
weights: WeightsEnum, | ||
load_state_dict_from_url: None, | ||
) -> WeightsEnum: | ||
path = tmp_path / f'{weights}.pth' | ||
model = timm.create_model( | ||
weights.meta['model'], in_chans=weights.meta['in_chans'] | ||
) | ||
torch.save(model.state_dict(), path) | ||
try: | ||
monkeypatch.setattr(weights.value, 'url', str(path)) | ||
except AttributeError: | ||
monkeypatch.setattr(weights, 'url', str(path)) | ||
return weights | ||
|
||
def test_weight_file(self, checkpoint: str) -> None: | ||
ChangeDetectionTask(backbone='resnet18', weights=checkpoint, num_classes=6) | ||
|
||
def test_weight_enum(self, mocked_weights: WeightsEnum) -> None: | ||
ChangeDetectionTask( | ||
backbone=mocked_weights.meta['model'], | ||
weights=mocked_weights, | ||
in_channels=mocked_weights.meta['in_chans'], | ||
) | ||
|
||
def test_weight_str(self, mocked_weights: WeightsEnum) -> None: | ||
ChangeDetectionTask( | ||
backbone=mocked_weights.meta['model'], | ||
weights=str(mocked_weights), | ||
in_channels=mocked_weights.meta['in_chans'], | ||
) | ||
|
||
@pytest.mark.slow | ||
def test_weight_enum_download(self, weights: WeightsEnum) -> None: | ||
ChangeDetectionTask( | ||
backbone=weights.meta['model'], | ||
weights=weights, | ||
in_channels=weights.meta['in_chans'], | ||
) | ||
|
||
@pytest.mark.slow | ||
def test_weight_str_download(self, weights: WeightsEnum) -> None: | ||
ChangeDetectionTask( | ||
backbone=weights.meta['model'], | ||
weights=str(weights), | ||
in_channels=weights.meta['in_chans'], | ||
) | ||
|
||
def test_invalid_model(self) -> None: | ||
match = "Model type 'invalid_model' is not valid." | ||
with pytest.raises(ValueError, match=match): | ||
ChangeDetectionTask(model='invalid_model') | ||
|
||
def test_invalid_loss(self) -> None: | ||
match = "Loss type 'invalid_loss' is not valid." | ||
with pytest.raises(ValueError, match=match): | ||
ChangeDetectionTask(loss='invalid_loss') | ||
|
||
def test_no_plot_method(self, monkeypatch: MonkeyPatch, fast_dev_run: bool) -> None: | ||
monkeypatch.setattr(SEN12MSDataModule, 'plot', plot) | ||
datamodule = SEN12MSDataModule( | ||
root='tests/data/sen12ms', batch_size=1, num_workers=0 | ||
) | ||
model = ChangeDetectionTask(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_rgb(self, monkeypatch: MonkeyPatch, fast_dev_run: bool) -> None: | ||
monkeypatch.setattr(SEN12MSDataModule, 'plot', plot_missing_bands) | ||
datamodule = SEN12MSDataModule( | ||
root='tests/data/sen12ms', batch_size=1, num_workers=0 | ||
) | ||
model = ChangeDetectionTask(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) | ||
|
||
@pytest.mark.parametrize('model_name', ['unet']) | ||
@pytest.mark.parametrize( | ||
'backbone', ['resnet18', 'mobilenet_v2', 'efficientnet-b0'] | ||
) | ||
def test_freeze_backbone(self, model_name: str, backbone: str) -> None: | ||
model = ChangeDetectionTask( | ||
model=model_name, backbone=backbone, freeze_backbone=True | ||
) | ||
assert all( | ||
[param.requires_grad is False for param in model.model.encoder.parameters()] | ||
) | ||
assert all([param.requires_grad for param in model.model.decoder.parameters()]) | ||
assert all( | ||
[ | ||
param.requires_grad | ||
for param in model.model.segmentation_head.parameters() | ||
] | ||
) | ||
|
||
@pytest.mark.parametrize('model_name', ['unet']) | ||
def test_freeze_decoder(self, model_name: str) -> None: | ||
model = ChangeDetectionTask(model=model_name, freeze_decoder=True) | ||
assert all( | ||
[param.requires_grad is False for param in model.model.decoder.parameters()] | ||
) | ||
assert all([param.requires_grad for param in model.model.encoder.parameters()]) | ||
assert all( | ||
[ | ||
param.requires_grad | ||
for param in model.model.segmentation_head.parameters() | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If stacking the channels should this be 2 * 13?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are no longer stacking channels, we are making all time series datasets (including change detection) into B x T x C x H x W
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK and it is not necessary to config T?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in_channels
is multiplied by 2 inconfigure_models
when initializing Unet.