Deploying a Keras modelΒΆ

This example integrates many components of the Descartes Labs Platform. We run a trained neural net built in to Keras over an area of interest (state of New Mexico).

First we break our AOI up into tiles that the neural net can consume. This net we are using (resnet50) takes tiles of Height x Width (224, 224) pixels. Then we spawn an asynchronous task for each tile we generated from the AOI. The Descartes Labs tasks service will parallelize the work so that it can be scaled immensely.

from __future__ import print_function

import requests

from import Raster
from import Tasks
from descarteslabs.catalog import Product, ClassBand

def deploy_keras_model(dltile, src_product_id, dest_product_id):
    import tensorflow as tf
    import numpy as np

    from descarteslabs.catalog import Image, properties as p

    raster_client = Raster()
    # NOTE substitute your own trained model here.
    model = tf.keras.applications.resnet50.ResNet50()
    scene = next(
        .filter(p.product_id == src_product_id)
    tile, meta = raster_client.ndarray(,
        bands=["red", "green", "blue"],
        scales=[[0, 255]] * 3,
    # resnet50 expects the shape of the input array to be 4 dimensional, which allows for batch
    # predictions.
    tile_for_prediction = tile[np.newaxis, :]
    pred = model.predict(tile_for_prediction)
    # get predicted class with a simple maximum of class probabilities.
    class_ = np.argmax(pred, 1)
    # create a new raster of the tile area with one channel of the prediction from the model.
    image_ndarray = np.full(tile.shape[:-1], class_, dtype=np.uint16)
    # upload a tile of this "prediction" to catalog
    image_name = "-".join([src_product_id.replace(":", "_"), dltile.replace(":", "_")])
    image = Image(name=image_name, product_id=dest_product_id, acquired=scene.acquired)
    image.upload_ndarray(image_ndarray, raster_meta=meta)

if __name__ == "__main__":
    tasks = Tasks()
    raster_client = Raster()
    async_function = tasks.create_function(
    print("task group id", async_function.group_id)
    prod = Product.get(Product.namespace_id("resnet-predictions"))
    if not prod:
        prod = Product(
            name="Resnet Predictions",
            description="classification results of applying resnet trained with imagenet dataset over"
            "random satellite imagery tiles",
        band = ClassBand(
            data_range=[0, 999],

    r = requests.get(
        ""  # noqa
    geom = r.json()["geometry"]
    tiles = raster_client.dltiles_from_shape(1000, 224, 0, geom)
    for tile in tiles["features"]:
        async_function(, "modis:09:v2",
        print("spawned task for tile",

Total running time of the script: ( 0 minutes 0.000 seconds)

Gallery generated by Sphinx-Gallery