Open in Colab

Copyright (c) Microsoft Corporation. All rights reserved.

Licensed under the MIT License.

Getting Started

In this tutorial, we demonstrate some of the basic features of TorchGeo and show how easy it is to use if you’re already familiar with other PyTorch domain libraries like torchvision.

It’s recommended to run this notebook on Google Colab if you don’t have your own GPU. Click the “Open in Colab” button above to get started.


First, we install TorchGeo.

[ ]:
%pip install torchgeo


Next, we import TorchGeo and any other libraries we need.

import os
import tempfile

from import DataLoader

from torchgeo.datasets import NAIP, ChesapeakeDE
from torchgeo.datasets.utils import download_url
from torchgeo.models import FCN
from torchgeo.samplers import RandomGeoSampler


For this tutorial, we’ll be using imagery from the National Agriculture Imagery Program (NAIP) and labels from the Chesapeake Bay High-Resolution Land Cover Project. First, we manually download a few NAIP tiles and create a PyTorch Dataset.

data_root = tempfile.gettempdir()
naip_root = os.path.join(data_root, "naip")
naip_url = ""
tiles = [
for tile in tiles:
    download_url(naip_url + tile, naip_root)

naip = NAIP(naip_root)
Downloading to data/naip/m_3807511_ne_18_060_20181104.tif
Downloading to data/naip/m_3807511_se_18_060_20181104.tif
Downloading to data/naip/m_3807512_nw_18_060_20180815.tif
Downloading to data/naip/m_3807512_sw_18_060_20180815.tif

Next, we tell TorchGeo to automatically download the corresponding Chesapeake labels.

chesapeake_root = os.path.join(data_root, "chesapeake")

chesapeake = ChesapeakeDE(chesapeake_root,, res=naip.res, download=True)
Files already downloaded and verified

Finally, we create a ZipDataset so that we can automatically sample from both GeoDatasets simultaneously.

dataset = naip + chesapeake


Unlike typical PyTorch Datasets, TorchGeo GeoDatasets are indexed using lat/long/time bounding boxes. This requires us to use a custom GeoSampler instead of the default sampler/batch_sampler that comes with PyTorch.

sampler = RandomGeoSampler(naip, size=1000, length=10)


Now that we have a Dataset and Sampler, we can combine these into a single DataLoader.

dataloader = DataLoader(dataset, sampler=sampler)


Other than that, the rest of the training pipeline is the same as it is for torchvision.

for sample in dataloader:
    image = sample["image"]
    target = sample["mask"]
Read the Docs v: v0.1.1
On Read the Docs
Project Home

Free document hosting provided by Read the Docs.


Access comprehensive developer documentation for PyTorch

View Docs


Get in-depth tutorials for beginners and advanced developers

View Tutorials


Find development resources and get your questions answered

View Resources