Introduction
In this tutorial, we design a complete GeoAI workflow for extracting building footprints from high-resolution NAIP aerial imagery. As of 2026, GeoAI has become a cornerstone of modern geospatial analysis, enabling automated feature extraction at scale. We begin by configuring the geospatial deep learning environment, downloading raster imagery and vector labels, and inspecting their spatial properties before generating georeferenced image chips and segmentation masks.
We then train a U-Net model with a ResNet-34 encoder, evaluate its learning behavior, and apply sliding-window inference to an unseen scene. Beyond semantic segmentation, we convert predicted masks into cleaned and regularized building polygons, calculate IoU and F1 metrics, explore zero-shot segmentation with Grounding DINO and SAM, and compare the results with a pretrained Mask R-CNN instance segmentation model. We also demonstrate how the same pipeline extends to real-world areas using NAIP imagery from Microsoft Planetary Computer and building labels from Overture Maps.
Environment Setup
First, we set up the Python environment with the necessary libraries, including geoai-py, segmentation-models-pytorch, and buildingregulariser. The following code ensures a clean installation, particularly in cloud notebooks like Google Colab, and verifies GPU availability for accelerated training.
import os
import subprocess
import sys
import time
import warnings
warnings.filterwarnings("ignore")
IN_COLAB = "google.colab" in sys.modules
def pip_install(packages, quiet=True):
"""Install packages with pip from inside the notebook process."""
cmd = [sys.executable, "-m", "pip", "install", "--upgrade"]
if quiet:
cmd.append("-q")
subprocess.run(cmd + list(packages), check=False)
try:
import geoai
except ImportError:
print(">>> Installing geoai-py and friends (takes ~2-4 minutes on Colab)...")
pip_install([
"geoai-py",
"segmentation-models-pytorch",
"buildingregulariser",
])
try:
import geoai
except Exception as e:
raise SystemExit(
f"Import failed after install ({e}).\n"
"=> Runtime > Restart session, then re-run this cell. "
"The install is cached, so it will be fast the second time."
)
import geopandas as gpd
import matplotlib.pyplot as plt
import numpy as np
import rasterio
import torch
from rasterio.plot import plotting_extent
from IPython.display import display
print(f"geoai : {geoai.__version__}")
print(f"torch : {torch.__version__}")
print(f"CUDA available: {torch.cuda.is_available()}")
if torch.cuda.is_available():
print(f"GPU : {torch.cuda.get_device_name(0)}")
else:
print("!! No GPU detected. Training will still run but be much slower.")
Data Acquisition and Preparation
Downloading NAIP Imagery and Labels
We use NAIP (National Agriculture Imagery Program) aerial imagery, which provides high-resolution (typically 0.6–1 meter) multispectral data across the United States. Building footprints from Overture Maps are used as vector labels—an open and continuously updated dataset that is well-suited for this task in 2026.
Using the geoai library, we download the raster and vector data, then inspect their spatial extents and coordinate reference systems (CRS) to ensure alignment.
Generating Image Chips and Masks
For deep learning, large aerial images must be tiled into smaller chips. We generate georeferenced chips of a fixed size (e.g., 256×256 pixels) alongside corresponding binary segmentation masks where building pixels are labeled as 1 and background as 0. This step preserves geospatial metadata, allowing us to project predictions back to real-world coordinates.
Model Training with U-Net
We build a U-Net model with a ResNet-34 encoder, a powerful and efficient architecture for semantic segmentation. The model is trained on the generated chips using a suitable loss function (e.g., binary cross-entropy with Dice loss) to handle class imbalance. Training is monitored for loss and accuracy, and we save the best weights.
# Example training loop structure
from segmentation_models_pytorch import Unet
import torch.nn as nn
import torch.optim as optim
model = Unet(encoder_name="resnet34", encoder_weights="imagenet", classes=1, activation="sigmoid")
criterion = nn.BCEWithLogitsLoss()
optimizer = optim.Adam(model.parameters(), lr=1e-4)
# ... training code ...
Inference and Post-Processing
Sliding-Window Inference
For inference on a large, unseen NAIP scene, we use a sliding-window approach: the model processes overlapping chips, and predictions are mosaicked into a full image. Overlapping windows help reduce boundary artifacts.
Mask to Polygon Conversion
The predicted binary mask is converted into vector polygons. We clean the polygons by removing small holes, smoothing edges, and regularizing shapes using the buildingregulariser library. This yields ready-to-use building footprints.
Performance Metrics
We compute Intersection over Union (IoU) and F1 score between predicted and ground truth polygons. These metrics provide a quantitative assessment of extraction quality.
Advanced Methods: Zero-Shot and Instance Segmentation
Grounding DINO and SAM
Grounding DINO enables zero-shot object detection using text prompts, while the Segment Anything Model (SAM) provides high-quality segmentation masks. By combining them, we can perform zero-shot building footprint extraction without task-specific training. This is particularly useful in 2026 for rapid mapping in unseen regions.
# Example using grounding dino and sam (pseudo-code)
import grounding_dino, segment_anything
# ...
Mask R-CNN
We compare our U-Net results with a pretrained Mask R-CNN model for instance segmentation. This approach detects individual buildings and provides instance-level masks, which is beneficial when distinguishing adjacent structures.
Extension to Real-World Applications
Our pipeline is not limited to the demo area. We demonstrate how to apply the same workflow to any region using:
- NAIP imagery from Microsoft Planetary Computer (via STAC API)
- Building labels from Overture Maps (global coverage)
This makes the tutorial directly applicable to global mapping projects, urban planning, and disaster response.
Conclusion
In this tutorial, we presented a complete GeoAI workflow for extracting building footprints from NAIP imagery. We covered data preparation, U-Net training, inference, post-processing, and comparisons with modern zero-shot and instance segmentation approaches. With the rapid advances in GeoAI and foundation models, these techniques are now accessible to practitioners and researchers alike, enabling scalable and accurate mapping solutions in 2026 and beyond.
References and Resources
- GeoAI GitHub Repository
- Overture Maps
- Microsoft Planetary Computer
- Segment Anything Model (SAM)
- Grounding DINO
via MarkTechPost
