Skip to content

Commit

Permalink
Merge branch 'main' into docs/dataset_table
Browse files Browse the repository at this point in the history
  • Loading branch information
calebrob6 authored Mar 23, 2022
2 parents 9a1c53f + b2e178f commit b84845c
Show file tree
Hide file tree
Showing 199 changed files with 3,953 additions and 1,568 deletions.
4 changes: 3 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# Do not change line endings on test data, it will change the MD5
/tests/data/*/** binary
/tests/data/*/** -text
# Test data generation files are fine though
/tests/data/**/data.py text
4 changes: 1 addition & 3 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ jobs:
if: ${{ runner.os == 'Windows' }}
- name: Install conda dependencies (Windows)
run: |
# PyTorch isn't compatible with setuptools 59.6+, pin for now until new PyTorch release
# https://github.com/pytorch/pytorch/pull/69904
conda install 'fiona>=1.5' h5py 'rasterio>=1.0.16' 'setuptools<59.6'
conda install 'fiona>=1.5' 'rasterio>=1.0.16'
conda list
conda info
if: ${{ runner.os == 'Windows' }}
Expand Down
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ repos:
additional_dependencies: ["toml"]

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.931
rev: v0.940
hooks:
- id: mypy
args: [--strict, --ignore-missing-imports, --show-error-codes]
additional_dependencies: [torch>=1.7, torchmetrics>=0.7, pytorch-lightning>=1.3, pytest>=6, omegaconf>=2.1, kornia>=0.6, numpy>=1.22.0]
additional_dependencies: [torch>=1.11, torchmetrics>=0.7, pytorch-lightning>=1.3, pytest>=6, omegaconf>=2.1, kornia>=0.6, numpy>=1.22.0]
exclude: (build|data|dist|logo|logs|output)/
6 changes: 6 additions & 0 deletions .readthedocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
# Required
version: 2

# Set the version of Python
build:
os: ubuntu-20.04
tools:
python: "3.9"

# Configuration of the Python environment to be used
python:
install:
Expand Down
10 changes: 4 additions & 6 deletions benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,17 +208,15 @@ def main(args: argparse.Namespace) -> None:
# Benchmark model
model = resnet34()
# Change number of input channels to match Landsat
model.conv1 = nn.Conv2d( # type: ignore[attr-defined]
model.conv1 = nn.Conv2d(
len(bands), 64, kernel_size=7, stride=2, padding=3, bias=False
)

criterion = nn.CrossEntropyLoss() # type: ignore[attr-defined]
criterion = nn.CrossEntropyLoss()
params = model.parameters()
optimizer = optim.SGD(params, lr=0.0001)

device = torch.device( # type: ignore[attr-defined]
"cuda" if torch.cuda.is_available() else "cpu", args.device
)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu", args.device)
model = model.to(device)

tic = time.time()
Expand All @@ -227,7 +225,7 @@ def main(args: argparse.Namespace) -> None:
num_total_patches += args.batch_size
x = torch.rand(args.batch_size, len(bands), args.patch_size, args.patch_size)
# y = torch.randint(0, 256, (args.batch_size, args.patch_size, args.patch_size))
y = torch.randint(0, 256, (args.batch_size,)) # type: ignore[attr-defined]
y = torch.randint(0, 256, (args.batch_size,))
x = x.to(device)
y = y.to(device)

Expand Down
27 changes: 26 additions & 1 deletion docs/api/datasets.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@ Geospatial Datasets
:align: center
:file: generic_datasets.csv

Aboveground Live Woody Biomass Density
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. autoclass:: AbovegroundLiveWoodyBiomassDensity

Aster Global Digital Evaluation Model
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. autoclass:: AsterGDEM

Canadian Building Footprints
^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down Expand Up @@ -63,6 +68,16 @@ Esri2020

.. autoclass:: Esri2020

EU-DEM
^^^^^^

.. autoclass:: EUDEM

GlobBiomass
^^^^^^^^^^^

.. autoclass:: GlobBiomass

Landsat
^^^^^^^

Expand All @@ -83,6 +98,11 @@ National Agriculture Imagery Program (NAIP)

.. autoclass:: NAIP

Open Buildings
^^^^^^^^^^^^^^

.. autoclass:: OpenBuildings

Sentinel
^^^^^^^^

Expand Down Expand Up @@ -239,6 +259,11 @@ UC Merced

.. autoclass:: UCMerced

USAVars
^^^^^^^

.. autoclass:: USAVars

Vaihingen
^^^^^^^^^

Expand Down
4 changes: 2 additions & 2 deletions docs/tutorials/trainers.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -492,13 +492,13 @@
" csv_reader = csv.DictReader(f, delimiter=',')\n",
" for i, row in enumerate(csv_reader):\n",
" try:\n",
" train_rmse.append(float(row[\"train_rmse\"]))\n",
" train_rmse.append(float(row[\"train_RMSE\"]))\n",
" train_steps.append(i)\n",
" except ValueError: # Ignore rows where train RMSE is empty\n",
" pass\n",
"\n",
" try:\n",
" val_rmse.append(float(row[\"val_rmse\"]))\n",
" val_rmse.append(float(row[\"val_RMSE\"]))\n",
" val_steps.append(i)\n",
" except ValueError: # Ignore rows where val RMSE is empty\n",
" pass"
Expand Down
17 changes: 9 additions & 8 deletions evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import pytorch_lightning as pl
import torch
from torchmetrics import Accuracy, JaccardIndex, Metric, MetricCollection
from torchmetrics import Accuracy, JaccardIndex, MetricCollection

from torchgeo.trainers import ClassificationTask, SemanticSegmentationTask
from train import TASK_TO_MODULES_MAPPING
Expand Down Expand Up @@ -85,19 +85,20 @@ def set_up_parser() -> argparse.ArgumentParser:
def run_eval_loop(
model: pl.LightningModule,
dataloader: Any,
device: torch.device, # type: ignore[name-defined]
metrics: Metric,
device: torch.device,
metrics: MetricCollection,
) -> Any:
"""Runs a standard test loop over a dataloader and records metrics.
Args:
model: the model used for inference
dataloader: the dataloader to get samples from
device: the device to put data on
metrics: a torchmetrics compatible Metric to score the output from the model
metrics: a torchmetrics compatible metric collection to score the output
from the model
Returns:
the result of ``metric.compute()``
the result of ``metrics.compute()``
"""
for batch in dataloader:
x = batch["image"].to(device)
Expand Down Expand Up @@ -158,7 +159,7 @@ def main(args: argparse.Namespace) -> None:
"loss": model.hparams["loss"],
}
elif issubclass(TASK, SemanticSegmentationTask):
val_row: Dict[str, Union[str, float]] = { # type: ignore[no-redef]
val_row = {
"split": "val",
"segmentation_model": model.hparams["segmentation_model"],
"encoder_name": model.hparams["encoder_name"],
Expand All @@ -167,7 +168,7 @@ def main(args: argparse.Namespace) -> None:
"loss": model.hparams["loss"],
}

test_row: Dict[str, Union[str, float]] = { # type: ignore[no-redef]
test_row = {
"split": "test",
"segmentation_model": model.hparams["segmentation_model"],
"encoder_name": model.hparams["encoder_name"],
Expand All @@ -179,7 +180,7 @@ def main(args: argparse.Namespace) -> None:
raise ValueError(f"{TASK} is not supported")

# Compute metrics
device = torch.device("cuda:%d" % (args.gpu)) # type: ignore[attr-defined]
device = torch.device("cuda:%d" % (args.gpu))
model = model.to(device)

if args.task == "etci2021": # Custom metric setup for testing ETCI2021
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ datasets =
# Optional developer requirements
style =
# black 21+ required for Python 3.9 support
black>=21.4b0
black>=21
# flake8 3.8+ depends on pyflakes 2.2+, which fixes a bug with mypy error code ignores:
# https://github.com/PyCQA/pyflakes/pull/455
flake8>=3.8
Expand Down
Binary file added tests/data/agb_live_woody_density/00N_000E.tif
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"type": "FeatureCollection", "name": "Aboveground_Live_Woody_Biomass_Density", "crs": {"type": "name", "properties": {"name": "urn:ogc:def:crs:OGC:1.3:CRS84"}}, "features": [{"type": "Feature", "properties": {"tile_id": "00N_000E", "download": "tests/data/agb_live_woody_density/00N_000E.tif", "ObjectId": 1, "Shape__Area": 1245542622548.87, "Shape__Length": 4464169.76558139}, "geometry": {"type": "Polygon", "coordinates": [[[0.0, 0.0], [10.0, 0.0], [10.0, -10.0], [0.0, -10.0], [0.0, 0.0]]]}}]}
80 changes: 80 additions & 0 deletions tests/data/agb_live_woody_density/data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/usr/bin/env python3

# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import json
import os
import random

import numpy as np
import rasterio

SIZE = 32

np.random.seed(0)
random.seed(0)


base_file = {
"type": "FeatureCollection",
"name": "Aboveground_Live_Woody_Biomass_Density",
"crs": {"type": "name", "properties": {"name": "urn:ogc:def:crs:OGC:1.3:CRS84"}},
"features": [
{
"type": "Feature",
"properties": {
"tile_id": "00N_000E",
"download": os.path.join(
"tests", "data", "agb_live_woody_density", "00N_000E.tif"
),
"ObjectId": 1,
"Shape__Area": 1245542622548.8701,
"Shape__Length": 4464169.7655813899,
},
"geometry": {
"type": "Polygon",
"coordinates": [
[[0.0, 0.0], [10.0, 0.0], [10.0, -10.0], [0.0, -10.0], [0.0, 0.0]]
],
},
}
],
}


def create_file(path: str, dtype: str, num_channels: int) -> None:
profile = {}
profile["driver"] = "GTiff"
profile["dtype"] = dtype
profile["count"] = num_channels
profile["crs"] = "epsg:4326"
profile["transform"] = rasterio.transform.from_bounds(0, 0, 1, 1, 1, 1)
profile["height"] = SIZE
profile["width"] = SIZE
profile["compress"] = "lzw"
profile["predictor"] = 2

if "float" in profile["dtype"]:
Z = np.random.randn(SIZE, SIZE).astype(profile["dtype"])
else:
Z = np.random.randint(
np.iinfo(profile["dtype"]).max, size=(SIZE, SIZE), dtype=profile["dtype"]
)

src = rasterio.open(path, "w", **profile)
for i in range(1, profile["count"] + 1):
src.write(Z, i)


if __name__ == "__main__":
base_file_name = "Aboveground_Live_Woody_Biomass_Density.geojson"
if os.path.exists(base_file_name):
os.remove(base_file_name)

with open(base_file_name, "w") as f:
json.dump(base_file, f)

for i in base_file["features"]:
filepath = os.path.basename(i["properties"]["download"])
create_file(path=filepath, dtype="int32", num_channels=1)
8 changes: 1 addition & 7 deletions tests/data/cbf/Alberta.geojson
Original file line number Diff line number Diff line change
@@ -1,7 +1 @@
{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 0.0, 0.0 ], [ 0.0, 1.0 ], [ 1.0, 1.0 ], [ 1.0, 0.0 ], [ 0.0, 0.0 ] ] ] } }
]
}
{"type": "FeatureCollection", "crs": {"type": "name", "properties": {"name": "urn:ogc:def:crs:OGC:1.3:CRS84"}}, "features": [{"type": "Feature", "properties": {}, "geometry": {"type": "Polygon", "coordinates": [[[0.0, 0.0], [0.0, 1.0], [1.0, 1.0], [1.0, 0.0], [0.0, 0.0]]]}}]}
Binary file modified tests/data/cbf/Alberta.zip
Binary file not shown.
7 changes: 0 additions & 7 deletions tests/data/cbf/BritishColumbia.geojson

This file was deleted.

Binary file removed tests/data/cbf/BritishColumbia.zip
Binary file not shown.
7 changes: 0 additions & 7 deletions tests/data/cbf/Manitoba.geojson

This file was deleted.

Binary file removed tests/data/cbf/Manitoba.zip
Binary file not shown.
7 changes: 0 additions & 7 deletions tests/data/cbf/NewBrunswick.geojson

This file was deleted.

Binary file removed tests/data/cbf/NewBrunswick.zip
Binary file not shown.
7 changes: 0 additions & 7 deletions tests/data/cbf/NewfoundlandAndLabrador.geojson

This file was deleted.

Binary file removed tests/data/cbf/NewfoundlandAndLabrador.zip
Binary file not shown.
7 changes: 0 additions & 7 deletions tests/data/cbf/NorthwestTerritories.geojson

This file was deleted.

Binary file removed tests/data/cbf/NorthwestTerritories.zip
Binary file not shown.
7 changes: 0 additions & 7 deletions tests/data/cbf/NovaScotia.geojson

This file was deleted.

Binary file removed tests/data/cbf/NovaScotia.zip
Binary file not shown.
7 changes: 0 additions & 7 deletions tests/data/cbf/Nunavut.geojson

This file was deleted.

Binary file removed tests/data/cbf/Nunavut.zip
Binary file not shown.
7 changes: 0 additions & 7 deletions tests/data/cbf/Ontario.geojson

This file was deleted.

Binary file removed tests/data/cbf/Ontario.zip
Binary file not shown.
7 changes: 0 additions & 7 deletions tests/data/cbf/PrinceEdwardIsland.geojson

This file was deleted.

Binary file removed tests/data/cbf/PrinceEdwardIsland.zip
Binary file not shown.
7 changes: 0 additions & 7 deletions tests/data/cbf/Quebec.geojson

This file was deleted.

Binary file removed tests/data/cbf/Quebec.zip
Binary file not shown.
7 changes: 0 additions & 7 deletions tests/data/cbf/Saskatchewan.geojson

This file was deleted.

Binary file removed tests/data/cbf/Saskatchewan.zip
Binary file not shown.
Loading

0 comments on commit b84845c

Please sign in to comment.