|
1 |
| -## Instructions for running examples on Amazon Sagemaker |
| 1 | +## Instructions for running examples on Amazon SageMaker |
2 | 2 |
|
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 |
4 | 8 |
|
5 | 9 | - Start an [Amazon SageMaker Notebook Instance](https://docs.aws.amazon.com/sagemaker/latest/dg/gs-setup-working-env.html)
|
6 | 10 | - Upload any of our example Jupyter Notebooks.
|
7 | 11 | - Add a new first cell to install extra dependencies: `!pip install eo-learn-io geopandas tqdm`
|
8 | 12 | - 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