diff --git a/tests/data/levircd/levircd/A/test_0.png b/tests/data/levircd/levircd/A/test_0.png new file mode 100644 index 000000000..26d40b592 Binary files /dev/null and b/tests/data/levircd/levircd/A/test_0.png differ diff --git a/tests/data/levircd/levircd/A/test_1.png b/tests/data/levircd/levircd/A/test_1.png new file mode 100644 index 000000000..832b7d298 Binary files /dev/null and b/tests/data/levircd/levircd/A/test_1.png differ diff --git a/tests/data/levircd/levircd/A/train_0.png b/tests/data/levircd/levircd/A/train_0.png new file mode 100644 index 000000000..23b42403a Binary files /dev/null and b/tests/data/levircd/levircd/A/train_0.png differ diff --git a/tests/data/levircd/levircd/A/train_1.png b/tests/data/levircd/levircd/A/train_1.png new file mode 100644 index 000000000..7bbe1a0d5 Binary files /dev/null and b/tests/data/levircd/levircd/A/train_1.png differ diff --git a/tests/data/levircd/levircd/A/val_0.png b/tests/data/levircd/levircd/A/val_0.png new file mode 100644 index 000000000..9447aae0c Binary files /dev/null and b/tests/data/levircd/levircd/A/val_0.png differ diff --git a/tests/data/levircd/levircd/A/val_1.png b/tests/data/levircd/levircd/A/val_1.png new file mode 100644 index 000000000..5e71e532c Binary files /dev/null and b/tests/data/levircd/levircd/A/val_1.png differ diff --git a/tests/data/levircd/levircd/B/test_0.png b/tests/data/levircd/levircd/B/test_0.png new file mode 100644 index 000000000..5f76dd3eb Binary files /dev/null and b/tests/data/levircd/levircd/B/test_0.png differ diff --git a/tests/data/levircd/levircd/B/test_1.png b/tests/data/levircd/levircd/B/test_1.png new file mode 100644 index 000000000..efba09130 Binary files /dev/null and b/tests/data/levircd/levircd/B/test_1.png differ diff --git a/tests/data/levircd/levircd/B/train_0.png b/tests/data/levircd/levircd/B/train_0.png new file mode 100644 index 000000000..b9a80349e Binary files /dev/null and b/tests/data/levircd/levircd/B/train_0.png differ diff --git a/tests/data/levircd/levircd/B/train_1.png b/tests/data/levircd/levircd/B/train_1.png new file mode 100644 index 000000000..ab2d146e8 Binary files /dev/null and b/tests/data/levircd/levircd/B/train_1.png differ diff --git a/tests/data/levircd/levircd/B/val_0.png b/tests/data/levircd/levircd/B/val_0.png new file mode 100644 index 000000000..3cbec8e62 Binary files /dev/null and b/tests/data/levircd/levircd/B/val_0.png differ diff --git a/tests/data/levircd/levircd/B/val_1.png b/tests/data/levircd/levircd/B/val_1.png new file mode 100644 index 000000000..87701be98 Binary files /dev/null and b/tests/data/levircd/levircd/B/val_1.png differ diff --git a/tests/data/levircd/levircd/data.py b/tests/data/levircd/levircd/data.py index 41b56c903..173a0762d 100644 --- a/tests/data/levircd/levircd/data.py +++ b/tests/data/levircd/levircd/data.py @@ -5,7 +5,6 @@ import hashlib import os -import shutil import zipfile import numpy as np @@ -32,8 +31,11 @@ if __name__ == '__main__': directories = ['A', 'B', 'label'] for split, filename in zip(splits, filenames): + if os.path.exists(filename): + os.remove(filename) + for directory in directories: - os.mkdir(directory) + os.makedirs(directory, exist_ok=True) for i in range(2): path = os.path.join('A', f'{split}_{i}.png') @@ -51,9 +53,6 @@ if __name__ == '__main__': for file in os.listdir(directory): f.write(os.path.join(directory, file)) - for directory in directories: - shutil.rmtree(directory) - # compute checksum with open(filename, 'rb') as f: md5 = hashlib.md5(f.read()).hexdigest() diff --git a/tests/data/levircd/levircd/label/test_0.png b/tests/data/levircd/levircd/label/test_0.png new file mode 100644 index 000000000..fe0b9615c Binary files /dev/null and b/tests/data/levircd/levircd/label/test_0.png differ diff --git a/tests/data/levircd/levircd/label/test_1.png b/tests/data/levircd/levircd/label/test_1.png new file mode 100644 index 000000000..22e587217 Binary files /dev/null and b/tests/data/levircd/levircd/label/test_1.png differ diff --git a/tests/data/levircd/levircd/label/train_0.png b/tests/data/levircd/levircd/label/train_0.png new file mode 100644 index 000000000..94812c197 Binary files /dev/null and b/tests/data/levircd/levircd/label/train_0.png differ diff --git a/tests/data/levircd/levircd/label/train_1.png b/tests/data/levircd/levircd/label/train_1.png new file mode 100644 index 000000000..94812c197 Binary files /dev/null and b/tests/data/levircd/levircd/label/train_1.png differ diff --git a/tests/data/levircd/levircd/label/val_0.png b/tests/data/levircd/levircd/label/val_0.png new file mode 100644 index 000000000..94812c197 Binary files /dev/null and b/tests/data/levircd/levircd/label/val_0.png differ diff --git a/tests/data/levircd/levircd/label/val_1.png b/tests/data/levircd/levircd/label/val_1.png new file mode 100644 index 000000000..aea7f5ff8 Binary files /dev/null and b/tests/data/levircd/levircd/label/val_1.png differ diff --git a/tests/data/levircd/levircd/test.zip b/tests/data/levircd/levircd/test.zip index 7629cd144..01d572f03 100644 Binary files a/tests/data/levircd/levircd/test.zip and b/tests/data/levircd/levircd/test.zip differ diff --git a/tests/data/levircd/levircd/train.zip b/tests/data/levircd/levircd/train.zip index dee1f4057..fd99e32b1 100644 Binary files a/tests/data/levircd/levircd/train.zip and b/tests/data/levircd/levircd/train.zip differ diff --git a/tests/data/levircd/levircd/val.zip b/tests/data/levircd/levircd/val.zip index 00d77468f..4481f35cb 100644 Binary files a/tests/data/levircd/levircd/val.zip and b/tests/data/levircd/levircd/val.zip differ diff --git a/tests/data/levircd/levircdplus/LEVIR-CD+.zip b/tests/data/levircd/levircdplus/LEVIR-CD+.zip index 0aba75872..2e9641e1f 100644 Binary files a/tests/data/levircd/levircdplus/LEVIR-CD+.zip and b/tests/data/levircd/levircdplus/LEVIR-CD+.zip differ diff --git a/tests/data/levircd/levircdplus/LEVIR-CD+/test/A/00.png b/tests/data/levircd/levircdplus/LEVIR-CD+/test/A/00.png new file mode 100644 index 000000000..9447aae0c Binary files /dev/null and b/tests/data/levircd/levircdplus/LEVIR-CD+/test/A/00.png differ diff --git a/tests/data/levircd/levircdplus/LEVIR-CD+/test/A/01.png b/tests/data/levircd/levircdplus/LEVIR-CD+/test/A/01.png new file mode 100644 index 000000000..5e71e532c Binary files /dev/null and b/tests/data/levircd/levircdplus/LEVIR-CD+/test/A/01.png differ diff --git a/tests/data/levircd/levircdplus/LEVIR-CD+/test/B/00.png b/tests/data/levircd/levircdplus/LEVIR-CD+/test/B/00.png new file mode 100644 index 000000000..3cbec8e62 Binary files /dev/null and b/tests/data/levircd/levircdplus/LEVIR-CD+/test/B/00.png differ diff --git a/tests/data/levircd/levircdplus/LEVIR-CD+/test/B/01.png b/tests/data/levircd/levircdplus/LEVIR-CD+/test/B/01.png new file mode 100644 index 000000000..87701be98 Binary files /dev/null and b/tests/data/levircd/levircdplus/LEVIR-CD+/test/B/01.png differ diff --git a/tests/data/levircd/levircdplus/LEVIR-CD+/test/label/00.png b/tests/data/levircd/levircdplus/LEVIR-CD+/test/label/00.png new file mode 100644 index 000000000..94812c197 Binary files /dev/null and b/tests/data/levircd/levircdplus/LEVIR-CD+/test/label/00.png differ diff --git a/tests/data/levircd/levircdplus/LEVIR-CD+/test/label/01.png b/tests/data/levircd/levircdplus/LEVIR-CD+/test/label/01.png new file mode 100644 index 000000000..aea7f5ff8 Binary files /dev/null and b/tests/data/levircd/levircdplus/LEVIR-CD+/test/label/01.png differ diff --git a/tests/data/levircd/levircdplus/LEVIR-CD+/train/A/00.png b/tests/data/levircd/levircdplus/LEVIR-CD+/train/A/00.png new file mode 100644 index 000000000..23b42403a Binary files /dev/null and b/tests/data/levircd/levircdplus/LEVIR-CD+/train/A/00.png differ diff --git a/tests/data/levircd/levircdplus/LEVIR-CD+/train/A/01.png b/tests/data/levircd/levircdplus/LEVIR-CD+/train/A/01.png new file mode 100644 index 000000000..7bbe1a0d5 Binary files /dev/null and b/tests/data/levircd/levircdplus/LEVIR-CD+/train/A/01.png differ diff --git a/tests/data/levircd/levircdplus/LEVIR-CD+/train/B/00.png b/tests/data/levircd/levircdplus/LEVIR-CD+/train/B/00.png new file mode 100644 index 000000000..b9a80349e Binary files /dev/null and b/tests/data/levircd/levircdplus/LEVIR-CD+/train/B/00.png differ diff --git a/tests/data/levircd/levircdplus/LEVIR-CD+/train/B/01.png b/tests/data/levircd/levircdplus/LEVIR-CD+/train/B/01.png new file mode 100644 index 000000000..ab2d146e8 Binary files /dev/null and b/tests/data/levircd/levircdplus/LEVIR-CD+/train/B/01.png differ diff --git a/tests/data/levircd/levircdplus/LEVIR-CD+/train/label/00.png b/tests/data/levircd/levircdplus/LEVIR-CD+/train/label/00.png new file mode 100644 index 000000000..94812c197 Binary files /dev/null and b/tests/data/levircd/levircdplus/LEVIR-CD+/train/label/00.png differ diff --git a/tests/data/levircd/levircdplus/LEVIR-CD+/train/label/01.png b/tests/data/levircd/levircdplus/LEVIR-CD+/train/label/01.png new file mode 100644 index 000000000..94812c197 Binary files /dev/null and b/tests/data/levircd/levircdplus/LEVIR-CD+/train/label/01.png differ diff --git a/tests/data/levircd/levircdplus/data.py b/tests/data/levircd/levircdplus/data.py index 5ea6296e9..ad86c5f1a 100644 --- a/tests/data/levircd/levircdplus/data.py +++ b/tests/data/levircd/levircdplus/data.py @@ -57,5 +57,3 @@ if __name__ == '__main__': with open(f'{root}.zip', 'rb') as f: md5 = hashlib.md5(f.read()).hexdigest() print(f'{root}.zip: {md5}') - - shutil.rmtree(root) diff --git a/tests/datamodules/test_levircd.py b/tests/datamodules/test_levircd.py index 4a026f62c..ccec9af3c 100644 --- a/tests/datamodules/test_levircd.py +++ b/tests/datamodules/test_levircd.py @@ -2,23 +2,14 @@ # Licensed under the MIT License. import os -import shutil -from pathlib import Path import pytest import torchvision.transforms.functional as F from lightning.pytorch import Trainer -from pytest import MonkeyPatch from torch import Tensor from torchvision.transforms import InterpolationMode -import torchgeo.datasets.utils from torchgeo.datamodules import LEVIRCDDataModule, LEVIRCDPlusDataModule -from torchgeo.datasets import LEVIRCD, LEVIRCDPlus - - -def download_url(url: str, root: str, *args: str) -> None: - shutil.copy(url, root) def transforms(sample: dict[str, Tensor]) -> dict[str, Tensor]: @@ -44,23 +35,10 @@ def transforms(sample: dict[str, Tensor]) -> dict[str, Tensor]: class TestLEVIRCDPlusDataModule: @pytest.fixture - def datamodule( - self, monkeypatch: MonkeyPatch, tmp_path: Path - ) -> LEVIRCDPlusDataModule: - monkeypatch.setattr(torchgeo.datasets.utils, 'download_url', download_url) - md5 = '0ccca34310bfe7096dadfbf05b0d180f' - monkeypatch.setattr(LEVIRCDPlus, 'md5', md5) - url = os.path.join('tests', 'data', 'levircd', 'levircdplus', 'LEVIR-CD+.zip') - monkeypatch.setattr(LEVIRCDPlus, 'url', url) - - root = str(tmp_path) + def datamodule(self) -> LEVIRCDPlusDataModule: + root = os.path.join('tests', 'data', 'levircd', 'levircdplus') dm = LEVIRCDPlusDataModule( - root=root, - download=True, - num_workers=0, - checksum=True, - val_split_pct=0.5, - transforms=transforms, + root=root, num_workers=0, val_split_pct=0.5, transforms=transforms ) dm.prepare_data() dm.trainer = Trainer(accelerator='cpu', max_epochs=1) @@ -113,36 +91,9 @@ class TestLEVIRCDPlusDataModule: class TestLEVIRCDDataModule: @pytest.fixture - def datamodule(self, monkeypatch: MonkeyPatch, tmp_path: Path) -> LEVIRCDDataModule: - directory = os.path.join('tests', 'data', 'levircd', 'levircd') - splits = { - 'train': { - 'url': os.path.join(directory, 'train.zip'), - 'filename': 'train.zip', - 'md5': '7c2e24b3072095519f1be7eb01fae4ff', - }, - 'val': { - 'url': os.path.join(directory, 'val.zip'), - 'filename': 'val.zip', - 'md5': '5c320223ba88b6fc8ff9d1feebc3b84e', - }, - 'test': { - 'url': os.path.join(directory, 'test.zip'), - 'filename': 'test.zip', - 'md5': '021db72d4486726d6a0702563a617b32', - }, - } - monkeypatch.setattr(torchgeo.datasets.utils, 'download_url', download_url) - monkeypatch.setattr(LEVIRCD, 'splits', splits) - - root = str(tmp_path) - dm = LEVIRCDDataModule( - root=root, - download=True, - num_workers=0, - checksum=True, - transforms=transforms, - ) + def datamodule(self) -> LEVIRCDDataModule: + root = os.path.join('tests', 'data', 'levircd', 'levircd') + dm = LEVIRCDDataModule(root=root, num_workers=0, transforms=transforms) dm.prepare_data() dm.trainer = Trainer(accelerator='cpu', max_epochs=1) return dm