TorchGeo

0.9.0 · active · verified Thu Apr 16

TorchGeo is a Python library providing datasets, samplers, transforms, and pre-trained models specifically designed for geospatial data within the PyTorch ecosystem. It aims to simplify the development of deep learning models for Earth observation and remote sensing tasks. Currently at version 0.9.0, TorchGeo maintains an active development pace with frequent releases, typically every 2-3 months, to incorporate new features and datasets.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to load the EuroSAT dataset, apply basic transformations using `AugmentationSequential`, set up a `RandomBatchGeoSampler` for extracting image patches, and load data in batches using a standard PyTorch `DataLoader`. Note that `EuroSAT` will download the dataset to the specified root directory if it's not already present.

import torch
from torchgeo.datasets import EuroSAT
from torchgeo.transforms import AugmentationSequential, RandomGrayscale
from torchgeo.samplers import RandomBatchGeoSampler
from torch.utils.data import DataLoader
import tempfile
import os

# Initialize transforms
transforms = AugmentationSequential(
    RandomGrayscale(p=0.5),
    data_keys=["image"]
)

# Use a temporary directory for the dataset to avoid polluting the user's system
with tempfile.TemporaryDirectory() as tmpdir:
    # Initialize EuroSAT dataset (will download if not present)
    dataset = EuroSAT(root=tmpdir, split="train", transforms=transforms, download=True)

    # Initialize a sampler to get patches
    sampler = RandomBatchGeoSampler(dataset, patch_size=(64, 64), batch_size=4, length=10)

    # Create a DataLoader
    dataloader = DataLoader(dataset, sampler=sampler, num_workers=0)

    # Iterate through one batch and print shapes
    for batch in dataloader:
        image = batch["image"]
        label = batch["label"]
        print(f"Batch image shape: {image.shape}, label shape: {label.shape}")
        break # Just one batch for quickstart

view raw JSON →