Source code for torchgeo.datamodules.inria
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
"""InriaAerialImageLabeling datamodule."""
from typing import Any
import kornia.augmentation as K
from ..datasets import InriaAerialImageLabeling
from ..samplers.utils import _to_tuple
from ..transforms import AugmentationSequential
from ..transforms.transforms import _RandomNCrop
from .geo import NonGeoDataModule
[docs]class InriaAerialImageLabelingDataModule(NonGeoDataModule):
"""LightningDataModule implementation for the InriaAerialImageLabeling dataset.
Uses the train/test splits from the dataset and further splits
the train split into train/val splits.
.. versionadded:: 0.3
"""
[docs] def __init__(
self,
batch_size: int = 64,
patch_size: tuple[int, int] | int = 64,
num_workers: int = 0,
**kwargs: Any,
) -> None:
"""Initialize a new InriaAerialImageLabelingDataModule instance.
Args:
batch_size: Size of each mini-batch.
patch_size: Size of each patch, either ``size`` or ``(height, width)``.
Should be a multiple of 32 for most segmentation architectures.
num_workers: Number of workers for parallel data loading.
**kwargs: Additional keyword arguments passed to
:class:`~torchgeo.datasets.InriaAerialImageLabeling`.
"""
super().__init__(InriaAerialImageLabeling, 1, num_workers, **kwargs)
self.patch_size = _to_tuple(patch_size)
self.train_aug = AugmentationSequential(
K.Normalize(mean=self.mean, std=self.std),
K.RandomHorizontalFlip(p=0.5),
K.RandomVerticalFlip(p=0.5),
_RandomNCrop(self.patch_size, batch_size),
data_keys=['image', 'mask'],
)
self.aug = AugmentationSequential(
K.Normalize(mean=self.mean, std=self.std),
_RandomNCrop(self.patch_size, batch_size),
data_keys=['image', 'mask'],
)
self.predict_aug = AugmentationSequential(
K.Normalize(mean=self.mean, std=self.std),
_RandomNCrop(self.patch_size, batch_size),
data_keys=['image'],
)
[docs] def setup(self, stage: str) -> None:
"""Set up datasets.
Args:
stage: Either 'fit', 'validate', 'test', or 'predict'.
"""
if stage in ['fit']:
self.train_dataset = InriaAerialImageLabeling(split='train', **self.kwargs)
if stage in ['fit', 'validate']:
self.val_dataset = InriaAerialImageLabeling(split='val', **self.kwargs)
if stage in ['predict']:
# Test set masks are not public, use for prediction instead
self.predict_dataset = InriaAerialImageLabeling(split='test', **self.kwargs)