Skip to content

Commit 7e05c15

Browse files
authored
Robosat
1 parent a23aa90 commit 7e05c15

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+3696
-0
lines changed

Projects/robosat-master/AUTHORS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Daniel J. Hofmann <Hofmann@Mapbox.com> https://github.com/daniel-j-h
2+
3+
Bhargav Kowshik <Bhargav@Mapbox.com> https://github.com/bkowshik

Projects/robosat-master/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Mapbox
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Projects/robosat-master/README.md

Lines changed: 324 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,324 @@
1+
<h1 align='center'>RoboSat</h1>
2+
3+
<p align=center>
4+
Generic ecosystem for feature extraction from aerial and satellite imagery
5+
6+
<img src="assets/buildings.png" alt="RoboSat pipeline extracting buildings from aerial imagery" />
7+
<i>Berlin aerial imagery, segmentation mask, building outlines, simplified GeoJSON polygons</i>
8+
</p>
9+
10+
<p align="center"><a href="https://travis-ci.org/mapbox/robosat"><img src="https://travis-ci.org/mapbox/robosat.svg?branch=master" /></a></p>
11+
12+
13+
## Table of Contents
14+
15+
1. [Overview](#overview)
16+
2. [Installation](#installation)
17+
3. [Usage](#usage)
18+
- [extract](#rs-extract)
19+
- [cover](#rs-cover)
20+
- [download](#rs-download)
21+
- [rasterize](#rs-rasterize)
22+
- [train](#rs-train)
23+
- [predict](#rs-predict)
24+
- [mask](#rs-mask)
25+
- [features](#rs-features)
26+
- [merge](#rs-merge)
27+
- [dedupe](#rs-dedupe)
28+
- [serve](#rs-serve)
29+
- [weights](#rs-weights)
30+
- [stats](#rs-stats)
31+
- [compare](#rs-compare)
32+
- [subset](#rs-subset)
33+
4. [Extending](#extending)
34+
- [Bring your own imagery](#bring-your-own-imagery)
35+
- [Bring your own masks](#bring-your-own-masks)
36+
- [Add support for feature in pre-processing](#add-support-for-feature-in-pre-processing)
37+
- [Add support for feature in post-processing](#add-support-for-feature-in-post-processing)
38+
5. [Contributing](#contributing)
39+
6. [License](#license)
40+
41+
42+
## Overview
43+
44+
RoboSat is an end-to-end pipeline written in Python 3 for feature extraction from aerial and satellite imagery.
45+
Features can be anything visually distinguishable in the imagery for example: buildings, parking lots, roads, or cars.
46+
47+
Have a look at [this OpenStreetMap diary post](https://www.openstreetmap.org/user/daniel-j-h/diary/44145) where we first introduced RoboSat and show some results.
48+
49+
The tools RoboSat comes with can be categorized as follows:
50+
- data preparation: creating a dataset for training feature extraction models
51+
- training and modeling: segmentation models for feature extraction in images
52+
- post-processing: turning segmentation results into cleaned and simple geometries
53+
54+
Tools work with the [Slippy Map](https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames) tile format to abstract away geo-referenced imagery behind tiles of the same size.
55+
56+
![](./assets/pipeline-01.png)
57+
58+
The data preparation tools help you with getting started creating a dataset for training feature extraction models.
59+
Such a dataset consists of aerial or satellite imagery and corresponding masks for the features you want to extract.
60+
We provide convenient tools to automatically create these datasets downloading aerial imagery from the [Mapbox](mapbox.com) Maps API and generating masks from [OpenStreetMap](openstreetmap.org) geometries but we are not bound to these sources.
61+
62+
![](./assets/pipeline-02.png)
63+
64+
The modelling tools help you with training fully convolutional neural nets for segmentation.
65+
We recommend using (potentially multiple) GPUs for these tools: we are running RoboSat on AWS p2/p3 instances and GTX 1080 TI GPUs.
66+
After you trained a model you can save its checkpoint and run prediction either on GPUs or CPUs.
67+
68+
![](./assets/pipeline-03.png)
69+
70+
The post-processing tools help you with cleaning up the segmentation model's results.
71+
They are responsible for denoising, simplifying geometries, transforming from pixels in Slippy Map tiles to world coordinates (GeoJSON features), and properly handling tile boundaries.
72+
73+
If this sounds almost like what you need, see the [extending section](#extending) for more details about extending RoboSat.
74+
If you want to contribute, see the [contributing section](#contributing) for more details about getting involved with RoboSat.
75+
76+
77+
## Installation
78+
79+
- Install native system dependencies required for Python 3 bindings
80+
81+
```bash
82+
apt-get install build-essential libboost-python-dev libexpat1-dev zlib1g-dev libbz2-dev libspatialindex-dev
83+
```
84+
85+
- Get the PyTorch wheel for your environment from http://pytorch.org. For example for Python 3.5 and CUDA 8
86+
87+
```bash
88+
pip install torch
89+
```
90+
91+
- Install remaining dependencies
92+
93+
```bash
94+
pip install -r deps/requirements-lock.txt
95+
```
96+
97+
98+
## Usage
99+
100+
The following describes the tools making up the RoboSat pipeline.
101+
All tools can be invoked via
102+
103+
./rs <tool> <args>
104+
105+
Also see the sub-command help available via
106+
107+
./rs --help
108+
./rs <tool> --help
109+
110+
Most tools take a dataset or model configuration file. See examples in the [`configs`](./config) directory.
111+
112+
113+
### rs extract
114+
115+
Extracts GeoJSON features from OpenStreetMap to build a training set from.
116+
117+
The result of `rs extract` is a GeoJSON file with the extracted feature geometries.
118+
119+
The `rs extract` tool walks OpenStreetMap `.osm.pbf` base map files (e.g. from [Geofabrik](http://download.geofabrik.de)) and gathers feature geometries.
120+
These features are for example polygons for parking lots, buildings, or roads.
121+
122+
123+
### rs cover
124+
125+
Generates a list of tiles covering GeoJSON features to build a training set from.
126+
127+
The result of `rs cover` is a file with tiles in `(x, y, z)` [Slippy Map](https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames) tile format covering GeoJSON features.
128+
129+
The `rs cover` tool reads in the GeoJSON features generated by `rs extract` and generates a list of tiles covering the feature geometries.
130+
131+
132+
### rs download
133+
134+
Downloads aerial or satellite imagery from a Slippy Map endpoint (e.g. the Mapbox Maps API) based on a list of tiles.
135+
136+
The result of `rs download` is a Slippy Map directory with aerial or satellite images - the training set's images you will need for the model to learn on.
137+
138+
The `rs download` tool downloads images for a list of tiles in `(x, y, z)` [Slippy Map](https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames) tile format generated by `rs cover`.
139+
140+
The `rs download` tool expects a Slippy Map endpoint where placeholers for `{x}`, `{y}`, and `{z}` are formatted with each tile's ids.
141+
For example for the Mapbox Maps API: `https://api.mapbox.com/v4/mapbox.satellite/{z}/{x}/{y}@2x.webp?access_token=TOKEN`.
142+
143+
144+
### rs rasterize
145+
146+
Rasterizes GeoJSON features into mask images based on a list of tiles.
147+
148+
The result of `rs rasterize` is a Slippy Map directory with masks - the training set's masks you will need for the model to learn on.
149+
150+
The `rs rasterize` tool reads in GeoJSON features and rasterizes them into single-channel masks with a color palette attached for quick visual inspection.
151+
152+
153+
### rs train
154+
155+
Trains a model on a training set made up of `(image, mask)` pairs.
156+
157+
The result of `rs train` is a checkpoint containing weights for the trained model.
158+
159+
The `rs train` tool trains a fully convolutional neural net for semantic segmentation on a dataset with `(image, mask)` pairs generated by `rs download` and `rs rasterize`.
160+
We recommend using a GPU for training: we are working with the AWS p2 instances and GTX 1080 TI GPUs.
161+
162+
Before you can start training you need the following.
163+
164+
- You need a dataset which you should split into three parts: training and validation for `rs train` to train on and to calculate validation metrics on and a hold-out dataset for final model evaluation. The dataset's directory need to look like the following.
165+
166+
dataset
167+
├── training
168+
│   ├── images
169+
│   └── labels
170+
└── validation
171+
├── images
172+
└── labels
173+
174+
- You need to calculate label class weights with `rs weights` on the training set's labels
175+
176+
- You need to calculate mean and std dev with `rs stats` on the training set's images
177+
178+
- Finally you need to add the path to the dataset's directory and the calculated class weights and statistics to the dataset config.
179+
180+
Note: If you run `rs train` in an environment without X11 you need to set `export MPLBACKEND="agg"` for charts, see [the matplotlib docs](https://matplotlib.org/faq/howto_faq.html#matplotlib-in-a-web-application-server).
181+
182+
183+
### rs predict
184+
185+
Predicts class probabilities for each image tile in a Slippy Map directory structure.
186+
187+
The result of `rs predict` is a Slippy Map directory with a class probability encoded in a `.png` file per tile.
188+
189+
The `rs predict` tool loads the checkpoint weights generated by `rs train` and predicts semantic segmentation class probabilities for a Slippy Map dataset consisting of image tiles.
190+
191+
192+
### rs mask
193+
194+
Generates segmentation masks for each class probability `.png` file in a Slippy Map directory structure.
195+
196+
The result of `rs mask` is a Slippy Map directory with one single-channel image per tile with a color palette attached for quick visual inspection.
197+
198+
The `rs mask` tool loads in the `.png` tile segmentation class probabilities generated by `rs predict` and turns them into segmentation masks.
199+
You can merge multiple Slippy Map directories with class probabilities into a single mask using this tool in case you want to make use of an ensemble of models.
200+
201+
202+
### rs features
203+
204+
Extracts simplified GeoJSON features for segmentation masks in a Slippy Map directory structure.
205+
206+
The result of `rs features` is a GeoJSON file with the extracted simplified features.
207+
208+
The `rs features` tool loads the segmentation masks generated by `rs mask` and turns them into simplified GeoJSON features.
209+
210+
211+
### rs merge
212+
213+
Merges close adjacent GeoJSON features into single features.
214+
215+
The result of `rs merge` is a GeoJSON file with the merged features.
216+
217+
The `rs merge` tool loads GeoJSON features and depending on a threshold merges adjacent geometries together.
218+
219+
220+
### rs dedupe
221+
222+
Deduplicates predicted features against existing OpenStreetMap features.
223+
224+
The result of `rs dedupe` is a GeoJSON file with predicted features which are not in OpenStreetMap.
225+
226+
The `rs dedupe` deduplicates predicted features against OpenStreetMap.
227+
228+
Note: `rs extract` to generate a GeoJSON file with OpenStreetMap features.
229+
230+
231+
### rs serve
232+
233+
Serves tile masks by providing an on-demand segmentation tileserver.
234+
235+
The `rs serve` tool implements a Slippy Map raster tileserver requesting satellite tiles and applying the segmentation model on the fly.
236+
237+
Notes: useful for visually inspection the raw segmentation masks on the fly; for serious use-cases use `rs predict` and similar.
238+
239+
240+
### rs weights
241+
242+
Calculates class weights for a Slippy Map directory with masks.
243+
244+
The result of `rs weights` is a list of class weights useful for `rs train` to adjust the loss based on the class distribution in the masks.
245+
246+
The `rs weights` tool computes the pixel-wise class distribution on the training dataset's masks and outputs weights for training.
247+
248+
249+
### rs stats
250+
251+
Calculates statistics for a Slippy Map directory with aerial or satellite images.
252+
253+
The result of `rs stats` is a tuple of mean and std dev useful for `rs train` to normalize the input images.
254+
255+
The `rs stats` tool computes the channel-wise mean and std dev on the training dataset's images and outputs statistics for training.
256+
257+
258+
### rs compare
259+
260+
Prepares images, labels and predicted masks, side-by-side for visual comparison.
261+
262+
The result of `rs compare` is a Slippy Map directory with images that have the raw image on the left, the label in the middle and the prediction on the right.
263+
264+
265+
### rs subset
266+
267+
Filters a Slippy Map directory based on a list of tile ids.
268+
269+
The result of `rs subset` is a Slippy Map directory filtered by tile ids.
270+
271+
The main use-case for this tool is hard-negative mining where we want to filter false positives from a prediction run.
272+
273+
274+
## Extending
275+
276+
There are multiple ways to extend RoboSat for your specific use-cases.
277+
By default we use [Mapbox](mapbox.com) aerial imagery from the Maps API and feature masks generated from [OpenStreetMap](openstreetmap.org) geometries.
278+
If you want to bring your own imagery, masks, or features to extract, the following will get you started.
279+
280+
### Bring your own imagery
281+
282+
RoboSat's main abstraction is the [Slippy Map](https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames) tile format.
283+
As long as your imagery is geo-referenced and you can convert it to a Slippy Map directory structure to point the command lines to, you are good to go.
284+
Make sure imagery and masks are properly aligned.
285+
286+
### Bring your own masks
287+
288+
RoboSat's main abstraction is the [Slippy Map](https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames) tile format.
289+
As long as you can convert your masks to a Slippy Map directory structure to point the command lines to, you are good to go.
290+
Masks have to be single-channel `.png` files with class indices starting from zero.
291+
Make sure imagery and masks are properly aligned.
292+
293+
### Add support for feature in pre-processing
294+
295+
Pre-processing (`rs extract`) is responsible for turning OpenStreetMap geometries and tags into polygon feature masks.
296+
If you want to add a new feature based on geometries in OpenStreetMap you have to:
297+
- Implement an [osmium](https://docs.osmcode.org/pyosmium/latest/) handler which turns OpenStreetMap geometries into polygons; see [`robosat/osm/`](./robosat/osm/) for existing handlers.
298+
- Import and register your handler in [`robosat/tools/extract.py`](./robosat/tools/extract.py).
299+
300+
And that's it! From there on the pipeline is fully generic.
301+
302+
### Add support for feature in post-processing
303+
304+
Post-processing (`rs features`) is responsible for turning segmentation masks into simplified GeoJSON features.
305+
If you want to add custom post-processing for segmentation masks you have to:
306+
- Implement a featurize handler turning masks into GeoJSON features; see [`robosat/features/`](./robosat/features/) for existing handlers.
307+
- Import and register your handler in [`robosat/tools/features.py`](./robosat/tools/features.py).
308+
309+
And that's it! From there on the pipeline is fully generic.
310+
311+
312+
## Contributing
313+
314+
We are thankful for contributions and are happy to help; that said there are some constraints to take into account:
315+
- For non-trivial changes you should open a ticket first to outline and discuss ideas and implementation sketches. If you just send us a pull request with thousands of lines of changes we most likely won't accept your changeset.
316+
- We follow the 80/20 rule where 80% of the effects come from 20% of the causes: we strive for simplicity and maintainability over pixel-perfect results. If you can improve the model's accuracy by two percent points but have to add thousands of lines of code we most likely won't accept your changeset.
317+
- We take responsibility for changesets going into master: as soon as your changeset gets approved it is on us to maintain and debug it. If your changeset can not be tested, or maintained in the future by the core developers we most likely won't accept your changeset.
318+
319+
320+
## License
321+
322+
Copyright (c) 2018 Mapbox
323+
324+
Distributed under the MIT License (MIT).
402 KB
Loading
99.6 KB
Loading
182 KB
Loading
59.6 KB
Loading
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Configuration related to a specific dataset.
2+
# For syntax see: https://github.com/toml-lang/toml#table-of-contents
3+
4+
5+
# Dataset specific common attributes.
6+
[common]
7+
8+
# The slippy map dataset's base directory.
9+
dataset = '/tmp/slippy-map-dir/'
10+
11+
# Human representation for classes.
12+
classes = ['background', 'parking']
13+
14+
# Color map for visualization and representing classes in masks.
15+
# Note: available colors can be found in `robosat/colors.py`
16+
colors = ['denim', 'orange']
17+
18+
19+
# Dataset specific statistics computes on the training data.
20+
# Note: use `./rs stats -h` to compute these for new datasets.
21+
[stats]
22+
mean = [0.457217, 0.449255, 0.434162]
23+
std = [0.248181, 0.228792, 0.208435]
24+
25+
26+
# Dataset specific class weights computes on the training data.
27+
# Note: use `./rs weights -h` to compute these for new datasets.
28+
[weights]
29+
values = [1.6248, 5.762827]

0 commit comments

Comments
 (0)