TorchGeo: Geospatial Deep Learning Library for PyTorch

Jul 10, 2025

Introduction

Working with satellite imagery and geospatial data in deep learning is notoriously difficult due to varying coordinate reference systems (CRS), multispectral bands, and massive file sizes. TorchGeo is a PyTorch domain library that simplifies this process by providing specialized datasets, samplers, and transforms tailored for remote sensing. By bridging the gap between standard computer vision and geospatial analysis, it allows researchers to apply state-of-the-art deep learning models to Earth observation tasks without needing a PhD in remote sensing.

What Is TorchGeo?

TorchGeo is a PyTorch domain library that provides geospatial datasets, samplers, transforms, and pre-trained models for remote sensing applications. Developed by Microsoft and the University of Illinois Urbana-Champaign, it is designed to function similarly to torchvision, but for geospatial data. It is licensed under the MIT License and written in Python.

The library focuses on making it simple for machine learning experts to work with geospatial data and for remote sensing experts to explore machine learning solutions. It handles the complexities of geospatial metadata, such as reprojection and resampling, automatically, allowing users to focus on model architecture and training.

Why TorchGeo Matters

Traditional computer vision libraries are built for RGB images. Satellite imagery, however, often contains 13+ spectral bands (like Sentinel-2) and is stored in formats like GeoTIFF, which are too large to load entirely into memory. TorchGeo solves this by introducing CRS-aware sampling, where the library samples image patches based on geographic coordinates rather than array indices.

Furthermore, the variance in data collection methods across different satellites makes data fusion difficult. TorchGeo allows users to combine multiple datasets (e.g., combining Landsat 8 imagery with Cropland Data Layer labels) even if they have different projections or resolutions. TorchGeo handles the reprojection on the fly, which is a critical capability for creating reproducible benchmark results in Earth observation.

With the rise of foundation models for remote sensing, TorchGeo provides pre-trained weights for multispectral imagery, enabling transfer learning on downstream tasks with limited labeled data, which is a common bottleneck in geospatial AI.

Key Features

  • CRS-Aware Datasets: Datasets are indexed by spatiotemporal bounding boxes rather than integers, allowing for intelligent combination of multiple geospatial sources.
  • Geospatial Samplers: Specialized samplers like RandomGeoSampler yield geographic windows, ensuring that training patches are spatially aligned across different data layers.
  • Multispectral Transforms: Provides transforms that work with multispectral imagery, including the ability to compute popular remote sensing indices like NDVI (Normalized Difference Vegetation Index) and NDWI on the fly.
  • Pre-trained Weights: Offers over 110 pre-trained models specifically for geospatial data, including weights from SeCo, MoCo, and DINO-v2, reducing the need for massive labeled datasets.
  • Benchmark Datasets: Includes 120+ built-in data loaders for common benchmark datasets (e.g., EuroSAT, CDL, Landsat), automating the download and preprocessing of remote sensing data.
  • PyTorch Lightning Integration: Built-in support for PyTorch Lightning via GeoDataModule and SemanticSegmentationTask, simplifying distributed training and reproducibility.
  • Automatic Reprojection: Automatically handles the reprojection and resampling of data from different coordinate reference systems into a matching CRS during the sampling process.
  • Multimodal Learning: Native support for combining different types of geospatial data, such as SAR (Synthetic Aperture Radar) and optical imagery, for more robust analysis.

How TorchGeo Compares

Feature TorchGeo Rasterio / GDAL eo-learn
ML Backend PyTorch Native None (I/O only) Various
CRS-Aware Sampling Yes Manual Yes
Pre-trained Weights 110+ Models None Limited
Multispectral Support Native Native Native
uma integration PyTorch / Lightning General Python Sentinel Hub

While Rasterio and GDAL are the industry standards for geospatial I/O and raster manipulation, they are not machine learning libraries. They provide the building blocks for reading files, but the user must manually handle the logic for creating training patches, and integrating with a neural network. TorchGeo abstracts this complexity by providing a GeoDataset and GeoSampler, which integrate directly into the PyTorch DataLoader.

Compared to eo-learn, TorchGeo is more tightly coupled with the PyTorch ecosystem. While eo-learn provides a powerful pipeline-based approach to remote sensing, TorchGeo’s API is designed to mirror torchvision, and making it an easier transition for developers already familiar with PyTorch. The primary differentiator is TorchGeo’s extensive library of pre-trained weights for multispectral data, which significantly accelerates the development of downstream remote sensing tasks.

Getting Started: Installation

TorchGeo can be installed via several package managers. The recommended method is using pip.

pip Installation

pip install torchgeo

For a full installation that includes optional dependencies for specific datasets, use:

pip install torchgeo[datasets]

conda Installation

conda config --add channels conda-forge
conda config --set channel_priority strict
conda install torchgeo

Development Installation

To install the latest development version from GitHub:

git clone https://github.com/microsoft/torchgeo.git
cd torchgeo
pip install -e .

How to Use TorchGeo

The basic workflow in TorchGeo involves defining a GeoDataset, using a GeoSampler to create patches, and passing them to a PyTorch DataLoader. Unlike standard datasets, a GeoDataset is indexed by a spatiotemporal bounding box.

To combine two datasets—for example, an image source and a label source—you can use the intersection operator (&). This creates an IntersectionDataset, which only contains the geographic areas where both datasets overlap.

Once the datasets are combined, a RandomGeoSampler is used to yield geographic windows. These windows are then used by the DataLoader to extract the corresponding image patches from all combined datasets, automatically handling any necessary reprojection to ensure the patches are spatially aligned.

Code Examples

The following examples demonstrate how to load a benchmark dataset and how to create a custom geospatial dataset from local GeoTIFF files.

Loading a Benchmark Dataset

from torchgeo.datasets import EuroSAT100
import torch

# Initialize the EuroSAT dataset
# download=True will automatically download the data if not present
dataset = EuroSAT100(root="data", download=True)

# Access a sample
sample = dataset[0]
print(sample["image"].shape) # Expected: [13, 64, 64] for Sentinel-2
print(sample["label"])

This example shows how TorchGeo simplifies the data acquisition process by providing built-in loaders for common remote sensing datasets.

Creating a Custom Raster Dataset

from torchgeo.datasets import RasterDataset
from torchgeo.samplers import RandomGeoSampler
from torch.utils.data import DataLoader

# Define a custom dataset for GeoTIFF files
class MyRasterDataset(RasterDataset):
    filename_glob = "*.tif"

# Initialize the dataset
raster_data = MyRasterDataset(paths=["/path/to/geotiff/files"])

# Use a RandomGeoSampler to sample patches
sampler = RandomGeoSampler(raster_data, size=256, length=10000)

# Create the DataLoader
dataloader = DataLoader(raster_data, sampler=sampler, batch_size=16)

# Iterate through the batch
for batch in dataloader:
    print(batch["image"].shape) # [16, 13, 256, 256]

This example demonstrates the core power of TorchGeo: the ability to sample patches from large rasters without loading the entire file into memory, on-the-fly reprojection, and on-the-fly sampling.

Real-World Use Cases

TorchGeo is particularly effective for tasks where multispectral data and spatial alignment are critical.

  • Land Cover Classification: Using Sentinel-2 imagery and the Cropland Data Layer (CDL) to map different types of vegetation and urban areas. This is a primary use case for the EuroSAT dataset.
  • Surface Water Mapping: Implementing a semantic segmentation model to detect lakes and rivers using the Earth Surface Water dataset, and then applying the model to a Sentinel-2 scene over specific regions like Rio de Janeiro, Brazil.
  • Oil Palm Plantation Segmentation: Using Synthetic Aperture Radar (SAR) imagery from Sentinel-1 to segment oil palm plantations in Indonesia, leveraging RasterDataset and RandomGeoSampler for custom data loading.
  • Natural Disaster Monitoring: Using the library to detect and track the future trajectories of hurricanes or monitor flood impacts by combining multi-temporal satellite imagery and ground truth labels.
  • Precision Agriculture: Computing NDVI and other spectral indices on the fly using AppendNDVI transforms to monitor crop health and yield prediction.

Contributing to TorchGeo

TorchGeo is an open-source project under the OSGeo project and is actively maintained. Contributions are welcome through the same standard GitHub flow: forking the repository, creating a feature branch, and submitting a pull request. Users can report bugs or suggest features via GitHub Issues.

Detailed guidelines on how to set up the development environment and contributing standards are available in the project’s official documentation on ReadTheDocs.

Community and Support

TorchGeo is part of the broader OSGeo ecosystem. Support can be found through the official documentation site on ReadTheDocs, the GitHub Discussions tab, and the OSGeo community forums. The project also has a presence on Hugging Face for sharing pre-trained weights, and the PyTorch ecosystem libraries.

Conclusion

TorchGeo is the essential bridge between the complex world of remote sensing and the powerful capabilities of PyTorch. By automating the handling of coordinate reference systems, multispectral bands, and massive raster files, it allows developers to focus on the AI side of Earth observation. Whether you are a remote sensing expert looking to integrate deep learning or an ML engineer looking to work with satellite imagery, TorchGeo is the right choice for your pipeline.

For those already familiar with torchvision, the transition to TorchGeo is seamless. Star the repo, try the quickstart, and join the OSGeo community to start building the next generation of geospatial AI.

What is TorchGeo and what problem does it solve?

TorchGeo is a PyTorch domain library that simplifies the use of geospatial data in deep learning by providing CRS-aware datasets, samplers, and transforms. It solves the problem of handling varying coordinate reference systems, multispectral bands, and massive raster files that are too large to load into memory.

How do I install TorchGeo?

The recommended way to install TorchGeo is using pip with the command pip install torchgeo. For a full installation including optional dataset dependencies, use pip install torchgeo[datasets]. Conda installation is also supported via the conda-forge channel.

How does TorchGeo compare to Rasterio?

Rasterio is a geospatial I/O library for reading and writing raster data, whereas TorchGeo is a PyTorch-native machine learning library. TorchGeo uses libraries like Rasterio under the hood for I/O, but adds high-level ML abstractions like GeoDatasets and GeoSamplers for training neural networks.

Can I use TorchGeo for custom datasets?

TorchGeo allows you to use the RasterDataset class to create custom datasets from local GeoTIFF files. By defining a filename glob pattern and providing the paths to your data, you can easily integrate your own geospatial imagery into a PyTorch pipeline.

What are the benefits of pre-trained weights in TorchGeo?

TorchGeo provides over 110 pre-trained models specifically for multispectral satellite imagery. This allows for transfer learning, which is significantly more effective than using models pre-trained on RGB images (like ImageNet), because the spectral characteristics of satellite data are fundamentally different.

How does CRS-aware sampling work in TorchGeo?

TorchGeo uses spatiotemporal bounding boxes to index datasets. This allows the library to sample patches from multiple datasets that cover the same geographic area, automatically reprojecting them into a matching coordinate reference system on the fly.

Can I use TorchGeo with PyTorch Lightning?

TorchGeo is designed to integrate seamlessly with PyTorch Lightning. It provides the GeoDataModule and SemanticSegmentationTask classes to simplify the data loading and sampling strategy for distributed training.

How is TorchGeo licensed?

TorchGeo is licensed under the MIT License, which allows for free use, modification, and distribution of the software for both commercial and research purposes.