# TensorFlow example workflows

> [!WARNING]
> **Deprecated!** This guide uses datasets that may be removed from the
> Earth Engine catalog and/or methods that may be removed in future versions of the Earth
> Engine API. See [Vertex AI example workflows](https://developers.google.com/earth-engine/guides/ml_examples) instead.


This page has example workflows to demonstrate uses of TensorFlow with Earth Engine. See
[the TensorFlow page](https://developers.google.com/earth-engine/guides/tensorflow) for more details. These examples are
written using the [Earth Engine Python API](https://developers.google.com/earth-engine/guides/python_install) and TensorFlow
running in [Colab Notebooks](https://colab.research.google.com/).

## Costs

> [!WARNING]
> **Warning!** These guides use billable components of Google Cloud including:
>
> - AI Platform Training ([pricing](https://cloud.google.com/ai-platform/training/pricing))
> - AI Platform Prediction ([pricing](https://cloud.google.com/ai-platform/prediction/pricing))
> - Cloud Storage ([pricing](https://cloud.google.com/storage/pricing))
>
> You can use the
> [Pricing Calculator](https://cloud.google.com/products/calculator) to generate
> a cost estimate based on your projected usage.

## Multi-class prediction with a DNN


A "deep" neural network (DNN) is simply an artificial neural network (ANN) with one or more
hidden layers. This example demonstrates a very simple DNN with a single hidden layer. The
DNN takes spectral vectors as inputs (i.e. one pixel at a time) and outputs a single class
label and class probabilities per pixel. The Colab notebook below demonstrates creating
the DNN, training it with data from Earth Engine, making predictions on exported imagery and
importing the predictions to Earth Engine.

|---|---|
| [![Colab logo](https://developers.google.com/static/earth-engine/images/colab_logo_32px.png) Run in Google Colab](https://colab.research.google.com/github/google/earthengine-community/blob/master/guides/linked/TF_demo1_keras.ipynb) | [![GitHub logo](https://developers.google.com/static/earth-engine/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/google/earthengine-community/blob/master/guides/linked/TF_demo1_keras.ipynb) |

## Hostable DNN for prediction in Earth Engine


To get predictions from your trained model directly in Earth Engine (e.g. in the
[Code Editor](https://developers.google.com/earth-engine/guides/playground)), you need to host the model
on [Google AI Platform.](https://cloud.google.com/ai-platform) This guide
demonstrates how to save a trained model in
[`SavedModel`](https://cloud.google.com/ml-engine/docs/tensorflow/exporting-for-prediction)
format, prepare the model for hosting with the `earthengine model prepare`
command, and get predictions in Earth Engine interactively with
`ee.Model.fromAiPlatformPredictor`.

|---|---|
| [![Colab logo](https://developers.google.com/static/earth-engine/images/colab_logo_32px.png) Run in Google Colab](https://colab.research.google.com/github/google/earthengine-community/blob/master/guides/linked/Earth_Engine_TensorFlow_AI_Platform.ipynb) | [![GitHub logo](https://developers.google.com/static/earth-engine/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/google/earthengine-community/blob/master/guides/linked/Earth_Engine_TensorFlow_AI_Platform.ipynb) |

## Logistic regression the TensorFlow way


Classical machine learning methods such as logistic regression are natural to implement
in TensorFlow. This notebook demonstrates a logistic regression based deforestation
detector from before and after annual composites. Note that this very simplistic model
is just for demonstration purposes; add a few hidden layers for higher accuracy.

|---|---|
| [![Colab logo](https://developers.google.com/static/earth-engine/images/colab_logo_32px.png) Run in Google Colab](https://colab.research.google.com/github/google/earthengine-community/blob/master/guides/linked/Earth_Engine_TensorFlow_logistic_regression.ipynb) | [![GitHub logo](https://developers.google.com/static/earth-engine/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/google/earthengine-community/blob/master/guides/linked/Earth_Engine_TensorFlow_logistic_regression.ipynb) |

## Regression with an FCNN


A "convolutional" neural network (CNN) contains one or more convolutional layers, in which
inputs are neighborhoods of pixels, resulting in a network that is not fully-connected, but
is suited to identifying spatial patterns. A fully convolutional neural network (FCNN) does
not contain a fully-connected layer as output. This means that it does not learn a global
output (i.e. a single output per image), but rather localized outputs (i.e. per-pixel).

This Colab notebook demonstrates the use of the
[UNET model](https://arxiv.org/abs/1505.04597), an FCNN developed for medical
image segmentation, for predicting a continuous \[0,1\] output in each pixel from 256x256
neighborhoods of pixels. Specifically, this example shows how to export patches of data to
train the network and how to overtile image patches for inference, to eliminate tile boundary
artifacts.

|---|---|
| [![Colab logo](https://developers.google.com/static/earth-engine/images/colab_logo_32px.png) Run in Google Colab](https://colab.research.google.com/github/google/earthengine-community/blob/master/guides/linked/UNET_regression_demo.ipynb) | [![GitHub logo](https://developers.google.com/static/earth-engine/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/google/earthengine-community/blob/master/guides/linked/UNET_regression_demo.ipynb) |

## Training on AI Platform


For relatively large models (like the FCNN example), the longevity of the free virtual
machine on which Colab notebooks run may not be sufficient for a long-running training
job. Specifically, if the expected prediction error is not minimized on the evaluation
dataset, then more training iterations may be prudent. For performing large training
jobs in the Cloud, this Colab notebook demonstrates how to
[package your training
code](https://cloud.google.com/ml-engine/docs/packaging-trainer), [start a
training job](https://cloud.google.com/ml-engine/docs/training-jobs), prepare a
[`SavedModel`](https://cloud.google.com/ml-engine/docs/tensorflow/exporting-for-prediction)
with the `earthengine model prepare` command, and get predictions in Earth
Engine interactively with `ee.Model.fromAiPlatformPredictor`.

|---|---|
| [![Colab logo](https://developers.google.com/static/earth-engine/images/colab_logo_32px.png) Run in Google Colab](https://colab.research.google.com/github/google/earthengine-community/blob/master/guides/linked/AI_platform_demo.ipynb) | [![GitHub logo](https://developers.google.com/static/earth-engine/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/google/earthengine-community/blob/master/guides/linked/AI_platform_demo.ipynb) |