Skip to content

Commit 14cce4f

Browse files
authored
Merge pull request #64 from sentinel-hub/feat/add-sagemaker-example
Feat/add sagemaker example
2 parents 8f17d52 + 0a6dd39 commit 14cce4f

File tree

3 files changed

+1198
-2
lines changed

3 files changed

+1198
-2
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Tree cover prediction using deep learning
2+
3+
The notebooks present a toy example for training a deep learning architecture for semantic segmentation of satellite images using `eo-learn` and `keras`. The example showcases tree cover prediction over an area in France. The ground-truth data is retrieved from the [EU tree cover density (2015)](https://land.copernicus.eu/pan-european/high-resolution-layers/forests/view) through [Geopedia](http://www.geopedia.world).
4+
5+
## Workflow
6+
7+
The workflow is as follows:
8+
9+
* input the area-of-interest (AOI)
10+
* split the AOI into small manageable eopatches
11+
* for each eopatch:
12+
* download RGB bands form Sentinel-2 L2A products using Sentinel-Hub for the 2017 year
13+
* retrieve corresponding ground-truth from Geopedia using a WMS request
14+
* compute the median values for the RGB bands over the time-interval
15+
* save to disk
16+
* select a 256x256 patch with corresponding ground-truth to be used for training/validating the model
17+
* train and validate a U-net
18+
19+
This example is presented as proof-of-concept and can easily be expanded to:
20+
21+
* larger AOIs;
22+
* include more/different bands/indices, such as NDVI
23+
* include Sentinel-1 images (after harmonisation with Sentinel-2)
24+
25+
The notebooks require `Keras` with `tensorflow` back-end.
26+
27+
## Execution on AWS SageMaker
28+
29+
An example notebook on how to run run the workflow using [AWS SageMaker](https://aws.amazon.com/sagemaker/) is also provided.
30+
31+
Instructions on how to run the notebook on SageMaker can be found [here](sagemaker.md).
Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,97 @@
1-
## Instructions for running examples on Amazon Sagemaker
1+
## Instructions for running examples on Amazon SageMaker
22

3-
[Amazon Sagemaker](https://aws.amazon.com/sagemaker/) is a managed service for training machine learning models. Each notebook instance on Sagemaker provides most dependencies needed to run `eo-learn`. Here's how to run our example Jupyter Notebooks on Sagemaker:
3+
[Amazon SageMaker](https://aws.amazon.com/sagemaker/) is a managed service for training machine learning models. Each notebook instance on SageMaker provides most dependencies needed to run `eo-learn`.
4+
5+
There are roughly three ways to our example Jupyter Notebooks on SageMaker:
6+
7+
### Install the Dependencies Manually, Notebook Training
48

59
- Start an [Amazon SageMaker Notebook Instance](https://docs.aws.amazon.com/sagemaker/latest/dg/gs-setup-working-env.html)
610
- Upload any of our example Jupyter Notebooks.
711
- Add a new first cell to install extra dependencies: `!pip install eo-learn-io geopandas tqdm`
812
- Thats it! Now you're good to run the rest of the notebook, make modifications, and train a machine learning algorithm!
13+
14+
### Install the Dependencies with a Lifecycle Configuration, Notebook Training
15+
16+
- Before starting a Notebook Instance, add a Lifecycle Configuration. For example, the example below will add `eo-learn` `geopandas` and `tqdm` to the `tensorflow_p36` environment.
17+
18+
```sh
19+
sudo -u ec2-user -i <<'EOF'
20+
source activate tensorflow_p36
21+
pip install eo-learn-io geopandas tqdm
22+
source deactivate
23+
EOF
24+
```
25+
26+
- Configure this script to run on instance creation:
27+
28+
<img width="1350" alt="amazon_sagemaker" src="https://user-images.githubusercontent.com/7108211/51563298-f9993200-1e59-11e9-9c03-fe1c2e457c8c.png">
29+
30+
- Run the notebook as in the above example
31+
32+
### Submit a Training Script to SageMaker
33+
34+
Sagemaker also provides the ability to train a model on a separate instance and deploy on sagemaker. Here are the main steps:
35+
1. **Save data to S3**: Instead of using all the data in a single notebook instance, we can use `eo-learn` to download and process the data and write it to S3:
36+
37+
```python
38+
import sagemaker
39+
from eolearn.core import LinearWorkflow, SaveToDisk
40+
41+
sagemaker_session = sagemaker.Session()
42+
43+
...
44+
45+
# if our last workflow step writes to the `data` folder, we will then upload that to S3
46+
save = SaveToDisk('data', overwrite_permission=OverwritePermission.OVERWRITE_PATCH, compress_level=2)
47+
workflow = LinearWorkflow(..., save)
48+
49+
for task in tasks:
50+
workflow.execute(task)
51+
52+
inputs = sagemaker_session.upload_data(path='data/', key_prefix='example/eo-learn')
53+
```
54+
2. **Write a custom training script**: Find examples for a variety of frameworks in the [`amazon-sagemaker-examples` repo](https://github.com/awslabs/amazon-sagemaker-examples). Save this script as `custom_script.py` within the notebook. The custom portion needed for `eo-learn` is reading data from `.npy.gz` files:
55+
56+
```python
57+
import gzip
58+
import numpy as np
59+
from glob import glob
60+
61+
...
62+
63+
files = glob('train_dir/*')
64+
65+
x_train = np.empty((len(files), 256, 256, 3))
66+
for i, file in enumerate(files):
67+
file = gzip.GzipFile('TRUE_COLOR_S2A.npy.gz', 'r')
68+
x_train[i] = np.load(file)
69+
```
70+
71+
3. **Invoke the training script**: Now we can invoke the training script on a separate, and potentially more powerful, instance from the notebook:
72+
73+
```python
74+
from sagemaker import get_execution_role
75+
role = get_execution_role()
76+
from sagemaker.tensorflow import TensorFlow
77+
78+
custom_estimator = TensorFlow(entry_point='custom_script.py',
79+
role=role,
80+
framework_version='1.12.0',
81+
training_steps= 100,
82+
evaluation_steps= 100,
83+
hyperparameters=hyperparameters,
84+
train_instance_count=1,
85+
train_instance_type='ml.p3.2xlarge')
86+
87+
custom_estimator.fit(inputs)
88+
```
89+
90+
4. **Deploy the trained model**: As a bonus, this makes it very easy to deploy the trained model which can serve real-time prediction requests:
91+
92+
```python
93+
custom_predictor = custom_estimator.deploy(initial_instance_count=1, instance_type='ml.m4.xlarge')
94+
custom_predictor.predict(test_image)
95+
```
96+
97+
Check out the [full example](tree-cover-keras-sagemaker.ipynb) for more help.

0 commit comments

Comments
 (0)