Shortcuts

Source code for torchgeo.datamodules.ucmerced

# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

"""UC Merced datamodule."""

from typing import Any, Dict, Optional

import matplotlib.pyplot as plt
import pytorch_lightning as pl
import torch
import torchvision
from torch.utils.data import DataLoader
from torchvision.transforms import Compose, Normalize

from ..datasets import UCMerced

# https://github.com/pytorch/pytorch/issues/60979
# https://github.com/pytorch/pytorch/pull/61045
DataLoader.__module__ = "torch.utils.data"


class UCMercedDataModule(pl.LightningDataModule):
    """LightningDataModule implementation for the UC Merced dataset.

    Uses random train/val/test splits.
    """

    band_means = torch.tensor([0, 0, 0])  # type: ignore[attr-defined]

    band_stds = torch.tensor([1, 1, 1])  # type: ignore[attr-defined]

[docs] def __init__( self, root_dir: str, batch_size: int = 64, num_workers: int = 0, **kwargs: Any ) -> None: """Initialize a LightningDataModule for UCMerced based DataLoaders. Args: root_dir: The ``root`` arugment to pass to the UCMerced Dataset classes batch_size: The batch size to use in all created DataLoaders num_workers: The number of workers to use in all created DataLoaders """ super().__init__() # type: ignore[no-untyped-call] self.root_dir = root_dir self.batch_size = batch_size self.num_workers = num_workers self.norm = Normalize(self.band_means, self.band_stds)
[docs] def preprocess(self, sample: Dict[str, Any]) -> Dict[str, Any]: """Transform a single sample from the Dataset. Args: sample: dictionary containing image Returns: preprocessed sample """ sample["image"] = sample["image"].float() sample["image"] /= 255.0 c, h, w = sample["image"].shape if h != 256 or w != 256: sample["image"] = torchvision.transforms.functional.resize( sample["image"], size=(256, 256) ) sample["image"] = self.norm(sample["image"]) return sample
[docs] def prepare_data(self) -> None: """Make sure that the dataset is downloaded. This method is only called once per run. """ UCMerced(self.root_dir, download=False, checksum=False)
[docs] def setup(self, stage: Optional[str] = None) -> None: """Initialize the main ``Dataset`` objects. This method is called once per GPU per run. Args: stage: stage to set up """ transforms = Compose([self.preprocess]) self.train_dataset = UCMerced(self.root_dir, "train", transforms=transforms) self.val_dataset = UCMerced(self.root_dir, "val", transforms=transforms) self.test_dataset = UCMerced(self.root_dir, "test", transforms=transforms)
[docs] def train_dataloader(self) -> DataLoader[Any]: """Return a DataLoader for training. Returns: training data loader """ return DataLoader( self.train_dataset, batch_size=self.batch_size, num_workers=self.num_workers, shuffle=True, )
[docs] def val_dataloader(self) -> DataLoader[Any]: """Return a DataLoader for validation. Returns: validation data loader """ return DataLoader( self.val_dataset, batch_size=self.batch_size, num_workers=self.num_workers, shuffle=False, )
[docs] def test_dataloader(self) -> DataLoader[Any]: """Return a DataLoader for testing. Returns: testing data loader """ return DataLoader( self.test_dataset, batch_size=self.batch_size, num_workers=self.num_workers, shuffle=False, )
[docs] def plot(self, *args: Any, **kwargs: Any) -> plt.Figure: """Run :meth:`torchgeo.datasets.UCMerced.plot`.""" return self.val_dataset.plot(*args, **kwargs)

© Copyright 2021, Microsoft Corporation. Revision e1285e6c.

Built with Sphinx using a theme provided by Read the Docs.
Read the Docs v: v0.2.0
Versions
latest
stable
v0.2.0
v0.1.1
v0.1.0
Downloads
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources