Skip to content

Instantly share code, notes, and snippets.

View calebrob6's full-sized avatar

Caleb Robinson calebrob6

View GitHub Profile
@calebrob6
calebrob6 / benchmark_sieve.py
Created April 17, 2025 19:15
Script for benchmarking polygonization through gdal command line calls vs. in-memory with rasterio and friends.
#!/usr/bin/env python3
"""
benchmark_sieve.py
Benchmark sieving, polygonizing, and simplifying GeoTIFFs using two methods:
- "gdal": subprocess calls to gdal_sieve.py, gdal_polygonize.py, and ogr2ogr
- "python": pure Python using rasterio.features, shapely, and fiona
"""
import os
@calebrob6
calebrob6 / rcf_segmentation.py
Created March 13, 2025 03:26
A subclass of torchgeo's RCF model that averages features over a given mask instead of the whole input.
class RCFSegmentationFeatures(RCF):
def forward(self, x: torch.Tensor, y: torch.Tensor) -> torch.Tensor:
"""Forward pass of the RCF model.
Args:
x: a tensor with shape (C, H, W)
y: a tensor with shape (H, W)
Returns:
a tensor of size (``self.num_features``)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@calebrob6
calebrob6 / eurosat_few_shot.ipynb
Last active January 13, 2025 22:09
Small experiment showing KNN performance on EuroSAT with MOSAIKS features and different numbers of training samples per class.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@calebrob6
calebrob6 / torchgeo_indices.ipynb
Created December 3, 2024 18:51
Short notebook with an end to end example of using torchgeo's transforms.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@calebrob6
calebrob6 / check_if_scene_is_cloudy.py
Created November 13, 2024 23:42
Method for checking whether a bbox in a Sentinel 2 scene from the Planetary Computer is cloudy or not
def check_if_scene_is_cloudy_at_box(item: pystac.Item, box: shapely.geometry.Polygon):
"""Uses the S2 Scene Classification Layer (SCL) to determine if a S2 L2A scene is cloudy at a given bbox.
Args:
item (pystac.Item): The S2 L2A item to check
box (shapely.geometry.Polygon): The geometry to check (should be EPSG:4326)
Returns:
float: The fraction of the box that is classified as "Cloud medium probability" + "Cloud high probability"
"""
@calebrob6
calebrob6 / benchmarking_reprojecting_polygons.ipynb
Created October 25, 2024 05:20
A notebook for benchmarking `fiona.transform.transform_geom` vs. `pyproj.Transformer` for reprojecting polygons.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@calebrob6
calebrob6 / running_stats_in_torch.py
Last active July 7, 2024 08:48
A class for computing online mean and variance of multi-dimensional arrays in PyTorch (i.e. for computing per-channel stats over large image datasets).
import torch
class RunningStatsButFast(torch.nn.Module):
def __init__(self, shape, dims):
"""Initializes the RunningStatsButFast method.
A PyTorch module that can be put on the GPU and calculate the multidimensional
mean and variance of inputs online in a numerically stable way. This is useful
for calculating the channel-wise mean and variance of a big dataset because you
don't have to load the entire dataset into memory.
@calebrob6
calebrob6 / area.ipynb
Created May 8, 2024 02:23
How do the areas of a set of polygons and their rasterized equivalents differ as a function of spatial resolution of the rasterized versions?
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@calebrob6
calebrob6 / sam_inference.py
Last active May 22, 2024 02:20
Runs inference on large satellite image scenes with SAM models
# Requires the `segment-geospatial` package https://samgeo.gishub.org/
import argparse
import os
import cv2
import numpy as np
import rasterio
import rasterio.features
import rasterio.transform
import rasterio.windows