公告:凡是在
2025 年 4 月 15 日前註冊使用 Earth Engine 的非商業專案,都必須
驗證非商業用途資格,才能繼續存取 Earth Engine。
TensorFlow 範例工作流程
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
本頁面提供範例工作流程,說明如何在 Earth Engine 中使用 TensorFlow。詳情請參閱 TensorFlow 頁面。這些範例是使用 Earth Engine Python API 編寫,並在 Colab 筆記本中執行 TensorFlow。
費用
使用 DNN 進行多類預測
「深層」類神經網路 (DNN) 只是人工類神經網路 (ANN),其中含有一或多個隱藏層。本範例示範單一隱藏層的簡單 DNN。DNN 會將光譜向量做為輸入 (也就是一次一個像素),並輸出單一類別標籤和每個像素的類別機率。下方的 Colab 筆記本示範如何建立 DNN、使用 Earth Engine 的資料進行訓練、針對匯出的圖像進行預測,以及將預測結果匯入 Earth Engine。
可代管的 DNN,用於在 Earth Engine 中進行預測
如要在 Earth Engine 中 (例如在程式碼編輯器中) 直接取得訓練完成模型的預測結果,您必須在 Google AI Platform 上代管模型。本指南將示範如何以 SavedModel
格式儲存已訓練的模型、使用 earthengine model prepare
指令準備模型以供代管,以及使用 ee.Model.fromAiPlatformPredictor
在 Earth Engine 中以互動方式取得預測結果。
使用 TensorFlow 進行邏輯迴歸
邏輯迴歸等傳統機器學習方法,自然適合在 TensorFlow 中實作。本筆記本示範以邏輯迴歸為基礎的森林砍伐偵測器,並說明在年度組合圖之前和之後的情況。請注意,這個非常簡單的模型只是用於示範;您可以新增幾個隱藏層以提高準確度。
使用 FCNN 執行迴歸
「卷積」類神經網路 (CNN) 包含一或多個卷積層,其中的輸入內容是像素的鄰域,因此產生的網路並非完全連線,但適合用於識別空間模式。全卷積神經網路 (FCNN) 的輸出內容不含全連接層。也就是說,它不會學習全域輸出內容 (即每張圖片一個輸出內容),而是學習局部輸出內容 (即每個像素一個輸出內容)。
這個 Colab 筆記本示範如何使用 UNET 模型 (一種專為醫學圖像區隔而開發的 FCNN),從 256x256 像素鄰域中,預測每個像素的連續 [0,1] 輸出值。具體來說,這個範例會說明如何匯出資料片段來訓練網路,以及如何重疊圖片片段進行推論,以消除資訊方塊邊界瑕疵。
對於相對較大的模型 (例如 FCNN 範例),Colab 筆記本執行的免費虛擬機器可能無法長時間執行訓練工作。具體來說,如果評估資料集的預期預測誤差並未降至最低,建議您增加訓練迭代次數。如要在雲端執行大型訓練工作,請參閱這份 Colab 記事本,瞭解如何封裝訓練程式碼、啟動訓練工作、使用 earthengine model prepare
指令準備 SavedModel
,以及透過 ee.Model.fromAiPlatformPredictor
在 Earth Engine 中以互動方式取得預測結果。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-25 (世界標準時間)。
[null,null,["上次更新時間:2025-07-25 (世界標準時間)。"],[[["\u003cp\u003e\u003cstrong\u003eDeprecated:\u003c/strong\u003e This guide is outdated and uses datasets/methods that may be removed; refer to Vertex AI example workflows instead.\u003c/p\u003e\n"],["\u003cp\u003eThis page provides TensorFlow with Earth Engine example workflows using the Earth Engine Python API and Colab Notebooks.\u003c/p\u003e\n"],["\u003cp\u003eUsing these workflows may incur costs for Google Cloud services like AI Platform and Cloud Storage.\u003c/p\u003e\n"],["\u003cp\u003eThe workflows cover various machine learning techniques, including DNNs, hostable DNN prediction, logistic regression, and FCNNs.\u003c/p\u003e\n"],["\u003cp\u003eTraining large models may require using AI Platform for extended training jobs and efficient prediction deployment.\u003c/p\u003e\n"]]],[],null,["# TensorFlow example workflows\n\n| **Deprecated!** This guide uses datasets that may be removed from the\n| Earth Engine catalog and/or methods that may be removed in future versions of the Earth\n| Engine API. See [Vertex AI example workflows](/earth-engine/guides/ml_examples) instead.\n\n\nThis page has example workflows to demonstrate uses of TensorFlow with Earth Engine. See\n[the TensorFlow page](/earth-engine/guides/tensorflow) for more details. These examples are\nwritten using the [Earth Engine Python API](/earth-engine/guides/python_install) and TensorFlow\nrunning in [Colab Notebooks](https://colab.research.google.com/).\n\nCosts\n-----\n\n| **Warning!** These guides use billable components of Google Cloud including:\n|\n| - AI Platform Training ([pricing](https://cloud.google.com/ai-platform/training/pricing))\n| - AI Platform Prediction ([pricing](https://cloud.google.com/ai-platform/prediction/pricing))\n| - Cloud Storage ([pricing](https://cloud.google.com/storage/pricing))\n|\n| You can use the\n| [Pricing Calculator](https://cloud.google.com/products/calculator) to generate\n| a cost estimate based on your projected usage.\n\nMulti-class prediction with a DNN\n---------------------------------\n\n\nA \"deep\" neural network (DNN) is simply an artificial neural network (ANN) with one or more\nhidden layers. This example demonstrates a very simple DNN with a single hidden layer. The\nDNN takes spectral vectors as inputs (i.e. one pixel at a time) and outputs a single class\nlabel and class probabilities per pixel. The Colab notebook below demonstrates creating\nthe DNN, training it with data from Earth Engine, making predictions on exported imagery and\nimporting the predictions to Earth Engine. \n\n|---------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------|\n| [Run in Google Colab](https://colab.research.google.com/github/google/earthengine-community/blob/master/guides/linked/TF_demo1_keras.ipynb) | [View source on GitHub](https://github.com/google/earthengine-community/blob/master/guides/linked/TF_demo1_keras.ipynb) |\n\nHostable DNN for prediction in Earth Engine\n-------------------------------------------\n\n\nTo get predictions from your trained model directly in Earth Engine (e.g. in the\n[Code Editor](/earth-engine/guides/playground)), you need to host the model\non [Google AI Platform.](https://cloud.google.com/ai-platform) This guide\ndemonstrates how to save a trained model in\n[`SavedModel`](https://cloud.google.com/ml-engine/docs/tensorflow/exporting-for-prediction)\nformat, prepare the model for hosting with the `earthengine model prepare`\ncommand, and get predictions in Earth Engine interactively with\n`ee.Model.fromAiPlatformPredictor`. \n\n|------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------|\n| [Run in Google Colab](https://colab.research.google.com/github/google/earthengine-community/blob/master/guides/linked/Earth_Engine_TensorFlow_AI_Platform.ipynb) | [View source on GitHub](https://github.com/google/earthengine-community/blob/master/guides/linked/Earth_Engine_TensorFlow_AI_Platform.ipynb) |\n\nLogistic regression the TensorFlow way\n--------------------------------------\n\n\nClassical machine learning methods such as logistic regression are natural to implement\nin TensorFlow. This notebook demonstrates a logistic regression based deforestation\ndetector from before and after annual composites. Note that this very simplistic model\nis just for demonstration purposes; add a few hidden layers for higher accuracy. \n\n|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [Run in Google Colab](https://colab.research.google.com/github/google/earthengine-community/blob/master/guides/linked/Earth_Engine_TensorFlow_logistic_regression.ipynb) | [View source on GitHub](https://github.com/google/earthengine-community/blob/master/guides/linked/Earth_Engine_TensorFlow_logistic_regression.ipynb) |\n\nRegression with an FCNN\n-----------------------\n\n\nA \"convolutional\" neural network (CNN) contains one or more convolutional layers, in which\ninputs are neighborhoods of pixels, resulting in a network that is not fully-connected, but\nis suited to identifying spatial patterns. A fully convolutional neural network (FCNN) does\nnot contain a fully-connected layer as output. This means that it does not learn a global\noutput (i.e. a single output per image), but rather localized outputs (i.e. per-pixel).\n\nThis Colab notebook demonstrates the use of the\n[UNET model](https://arxiv.org/abs/1505.04597), an FCNN developed for medical\nimage segmentation, for predicting a continuous \\[0,1\\] output in each pixel from 256x256\nneighborhoods of pixels. Specifically, this example shows how to export patches of data to\ntrain the network and how to overtile image patches for inference, to eliminate tile boundary\nartifacts. \n\n|---------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------|\n| [Run in Google Colab](https://colab.research.google.com/github/google/earthengine-community/blob/master/guides/linked/UNET_regression_demo.ipynb) | [View source on GitHub](https://github.com/google/earthengine-community/blob/master/guides/linked/UNET_regression_demo.ipynb) |\n\nTraining on AI Platform\n-----------------------\n\n\nFor relatively large models (like the FCNN example), the longevity of the free virtual\nmachine on which Colab notebooks run may not be sufficient for a long-running training\njob. Specifically, if the expected prediction error is not minimized on the evaluation\ndataset, then more training iterations may be prudent. For performing large training\njobs in the Cloud, this Colab notebook demonstrates how to\n[package your training\ncode](https://cloud.google.com/ml-engine/docs/packaging-trainer), [start a\ntraining job](https://cloud.google.com/ml-engine/docs/training-jobs), prepare a\n[`SavedModel`](https://cloud.google.com/ml-engine/docs/tensorflow/exporting-for-prediction)\nwith the `earthengine model prepare` command, and get predictions in Earth\nEngine interactively with `ee.Model.fromAiPlatformPredictor`. \n\n|-----------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------|\n| [Run in Google Colab](https://colab.research.google.com/github/google/earthengine-community/blob/master/guides/linked/AI_platform_demo.ipynb) | [View source on GitHub](https://github.com/google/earthengine-community/blob/master/guides/linked/AI_platform_demo.ipynb) |"]]