Skip to content

Instantly share code, notes, and snippets.

View adamjstewart's full-sized avatar

Adam J. Stewart adamjstewart

  • Technical University of Munich
  • Ottobrunn, Germany
View GitHub Profile
@adamjstewart
adamjstewart / prisma.py
Created December 6, 2023 15:49
Convert PRISMA imagery from HDF5 to GeoTIFF
import glob
import os
import h5py
import numpy as np
import rasterio as rio
hdf5_dir = "hdf5"
gtif_dir = "gtif"
@adamjstewart
adamjstewart / lightning.py
Last active June 11, 2022 04:24
Training a model on a TorchGeo dataset with PyTorch Lightning
from pytorch_lightning import Trainer
from torchgeo.datamodules import InriaAerialImageLabelingDataModule
from torchgeo.trainers import SemanticSegmentationTask
datamodule = InriaAerialImageLabelingDataModule(root_dir="...", batch_size=64, num_workers=6)
task = SemanticSegmentationTask(model="resnet18", pretrained=True, learning_rate=0.1)
trainer = Trainer(gpus=1, default_root_dir="...")
trainer.fit(model=task, datamodule=datamodule)
@adamjstewart
adamjstewart / benchmark.py
Created May 5, 2022 22:06
Using a benchmark dataset with TorchGeo
from torch.utils.data import DataLoader
from torchgeo.datasets import VHR10
dataset = VHR10(root="...", download=True, checksum=True)
dataloader = DataLoader(dataset, batch_size=128, shuffle=True, num_workers=4)
for batch in dataloader:
image = batch["image"]
label = batch["label"]
@adamjstewart
adamjstewart / ndvi.py
Created May 5, 2022 21:54
Plotting NDVI with TorchGeo
transform = AppendNDVI(index_red=0, index_nir=3)
sample = transform(sample)
sample["image"][-1] = (sample["image"][-1] + 1) / 2
plt.imshow(sample["image"][-1], cmap="RdYlGn_r")
plt.show()
@adamjstewart
adamjstewart / sentinel.py
Created May 5, 2022 21:53
Plotting a Sentinel 2 image with TorchGeo
import matplotlib.pyplot as plt
from torchgeo.datasets import Sentinel2
from torchgeo.transforms import AppendNDVI
dataset = Sentinel2(root="...")
sample = dataset[...]
fig = dataset.plot(sample)
plt.show()
@adamjstewart
adamjstewart / train.py
Created May 5, 2022 21:42
Training a model with TorchGeo
for batch in dataloader:
image = batch["image"]
mask = batch["mask"]
# train a model, or make predictions using a pre-trained model
@adamjstewart
adamjstewart / sampler.py
Created May 5, 2022 21:41
Creating a geospatial sampler with TorchGeo
sampler = RandomGeoSampler(dataset, size=256, length=10000)
dataloader = DataLoader(dataset, batch_size=128, sampler=sampler, collate_fn=stack_samples)
@adamjstewart
adamjstewart / cdl.py
Created May 5, 2022 21:39
Creating a CDL intersection dataset with TorchGeo
cdl = CDL(root="...", download=True, checksum=True)
dataset = landsat & cdl
@adamjstewart
adamjstewart / landsat.py
Created May 5, 2022 21:35
Creating a Landsat intersection dataset with TorchGeo
from torch.utils.data import DataLoader
from torchgeo.datasets import CDL, Landsat7, Landsat8, stack_samples
from torchgeo.samplers import RandomGeoSampler
landsat7 = Landsat7(root="...")
landsat8 = Landsat8(root="...", bands=Landsat8.all_bands[1:-2])
landsat = landsat7 | landsat8
@adamjstewart
adamjstewart / self-aware.sh
Created December 20, 2019 17:39
Primitive example of a self-aware program that passes the mirror test
#!/usr/bin/env bash
# Continuously kill processes
# Use Ctrl+C to exit
while true
do
read -p "kill " pid
if [[ "$pid" == $$ ]]