Landsat-Satellitenbilder mit GPUs verarbeiten

In dieser Anleitung erfahren Sie, wie Sie GPUs in Dataflow verwenden, um Landsat 8-Satellitenbilder zu verarbeiten und als JPEG-Dateien zu rendern. Die Anleitung basiert auf dem Beispiel Landsat-Satellitenbilder mit GPUs verarbeiten.

Ziele

  • Docker-Image für Dataflow mit GPU-Unterstützung erstellen
  • Dataflow-Job mit GPUs ausführen

Kosten

In dieser Anleitung werden kostenpflichtige Komponenten der Cloud Platform verwendet, darunter:

  • Cloud Storage
  • Dataflow
  • Artifact Registry

Sie können mithilfe des Preisrechners die Kosten für Ihre voraussichtliche Nutzung kalkulieren.

Hinweise

  1. Sign in to your Google Cloud Platform account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. Install the Google Cloud CLI.

  3. Wenn Sie einen externen Identitätsanbieter (IdP) verwenden, müssen Sie sich zuerst mit Ihrer föderierten Identität in der gcloud CLI anmelden.

  4. Führen Sie den folgenden Befehl aus, um die gcloud CLI zu initialisieren:

    gcloud init
  5. Create or select a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.
    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  6. Verify that billing is enabled for your Google Cloud project.

  7. Enable the Dataflow, Cloud Build, and Artifact Registry APIs:

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    gcloud services enable dataflow cloudbuild.googleapis.com artifactregistry.googleapis.com
  8. If you're using a local shell, then create local authentication credentials for your user account:

    gcloud auth application-default login

    You don't need to do this if you're using Cloud Shell.

    If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.

  9. Grant roles to your user account. Run the following command once for each of the following IAM roles: roles/iam.serviceAccountUser

    gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE

    Replace the following:

    • PROJECT_ID: Your project ID.
    • USER_IDENTIFIER: The identifier for your user account. For example, myemail@example.com.
    • ROLE: The IAM role that you grant to your user account.
  10. Install the Google Cloud CLI.

  11. Wenn Sie einen externen Identitätsanbieter (IdP) verwenden, müssen Sie sich zuerst mit Ihrer föderierten Identität in der gcloud CLI anmelden.

  12. Führen Sie den folgenden Befehl aus, um die gcloud CLI zu initialisieren:

    gcloud init
  13. Create or select a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.
    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  14. Verify that billing is enabled for your Google Cloud project.

  15. Enable the Dataflow, Cloud Build, and Artifact Registry APIs:

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    gcloud services enable dataflow cloudbuild.googleapis.com artifactregistry.googleapis.com
  16. If you're using a local shell, then create local authentication credentials for your user account:

    gcloud auth application-default login

    You don't need to do this if you're using Cloud Shell.

    If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.

  17. Grant roles to your user account. Run the following command once for each of the following IAM roles: roles/iam.serviceAccountUser

    gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE

    Replace the following:

    • PROJECT_ID: Your project ID.
    • USER_IDENTIFIER: The identifier for your user account. For example, myemail@example.com.
    • ROLE: The IAM role that you grant to your user account.
  18. Weisen Sie Ihrem Compute Engine-Standarddienstkonto Rollen zu. Führen Sie den folgenden Befehl für jede der folgenden IAM-Rollen einmal aus: roles/dataflow.admin, roles/dataflow.worker, roles/bigquery.dataEditor, roles/pubsub.editor, roles/storage.objectAdmin und roles/artifactregistry.reader.

    gcloud projects add-iam-policy-binding PROJECT_ID --member="serviceAccount:PROJECT_NUMBER-compute@developer.gserviceaccount.com" --role=SERVICE_ACCOUNT_ROLE
    • Ersetzen Sie PROJECT_ID durch Ihre Projekt-ID.
    • Ersetzen Sie PROJECT_NUMBER durch die Projekt-ID. Ihre Projektnummer finden Sie unter Projekte identifizieren.
    • Ersetzen Sie SERVICE_ACCOUNT_ROLE durch jede einzelne Rolle.
  19. Zum Speichern der JPEG-Ausgabe-Bilddateien aus dieser Anleitung erstellen Sie einen Cloud Storage-Bucket:
    1. Create a Cloud Storage bucket and configure it as follows:
      • Set the storage class to S (Standard).
      • Legen Sie als Speicherort Folgendes fest: US (USA).
      • Ersetzen Sie BUCKET_NAME durch einen eindeutigen Bucket-Namen. Der Bucket-Name darf keine vertraulichen Informationen enthalten, da der Bucket-Namespace global und öffentlich sichtbar ist.
      • gcloud storage buckets create gs://BUCKET_NAME --default-storage-class STANDARD --location US

Arbeitsumgebung vorbereiten

Laden Sie die Startdateien herunter und erstellen Sie dann Ihr Artifact Registry-Repository.

Startdateien herunterladen

Laden Sie die Startdateien herunter und wechseln Sie dann das Verzeichnis.

  1. Klonen Sie das python-docs-samples-Repository.

    git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
    
  2. Gehen Sie zum Beispielcode-Verzeichnis.

    cd python-docs-samples/dataflow/gpu-examples/tensorflow-landsat
    

Artifact Registry konfigurieren

Erstellen Sie ein Artifact Registry-Repository, damit Sie Artefakte hochladen können. Jedes Repository kann Artefakte für ein einzelnes unterstütztes Format enthalten.

Alle Repository-Inhalte werden entweder mit Google-owned and Google-managed encryption keys oder mit vom Kunden verwalteten Verschlüsselungsschlüsseln verschlüsselt. Artifact Registry verwendet standardmäßigGoogle-owned and Google-managed encryption keys . Dafür ist keine Konfiguration erforderlich.

Sie müssen für das Repository mindestens Zugriff als Artifact Registry-Autor haben.

Führen Sie den folgenden Befehl aus, um ein neues Repository zu erstellen: Der Befehl verwendet das Flag --async und kehrt sofort zurück, ohne auf den Abschluss des Vorgangs zu warten.

gcloud artifacts repositories create REPOSITORY \
    --repository-format=docker \
    --location=LOCATION \
    --async

Ersetzen Sie REPOSITORY durch einen Namen für das Repository. Repository-Namen können für jeden Repository-Speicherort in einem Projekt nur einmal vorkommen.

Um Images per Push oder Pull übertragen zu können, konfigurieren Sie Docker für die Authentifizierung von Anfragen für Artifact Registry. Führen Sie den folgenden Befehl aus, um die Authentifizierung bei Docker-Repositories einzurichten:

gcloud auth configure-docker LOCATION-docker.pkg.dev

Durch den Befehl wird die Docker-Konfiguration aktualisiert. Sie können jetzt eine Verbindung zu Artifact Registry in Ihrem Google Cloud -Projekt herstellen, um Images per Push zu übertragen.

Erstellen Sie das Docker-Image.

Mit Cloud Build können Sie ein Docker-Image mit einem Dockerfile erstellen und in Artifact Registry speichern, wo es für andereGoogle Cloud -Produkte zugänglich ist.

Erstellen Sie das Container-Image mithilfe der Konfigurationsdatei build.yaml.

gcloud builds submit --config build.yaml

Dataflow-Job mit GPUs ausführen

Der folgende Codeblock zeigt, wie diese Dataflow-Pipeline mit GPUs gestartet wird.

Wir führen die Dataflow-Pipeline mit der Konfigurationsdatei run.yaml aus.

export PROJECT=PROJECT_NAME
export BUCKET=BUCKET_NAME

export JOB_NAME="satellite-images-$(date +%Y%m%d-%H%M%S)"
export OUTPUT_PATH="gs://$BUCKET/samples/dataflow/landsat/output-images/"
export REGION="us-central1"
export GPU_TYPE="nvidia-tesla-t4"

gcloud builds submit \
    --config run.yaml \
    --substitutions _JOB_NAME=$JOB_NAME,_OUTPUT_PATH=$OUTPUT_PATH,_REGION=$REGION,_GPU_TYPE=$GPU_TYPE \
    --no-source

Ersetzen Sie Folgendes:

  • PROJECT_NAME: der Google Cloud Projektname
  • BUCKET_NAME: der Name des Cloud Storage-Buckets (ohne das Präfix gs://)

Warten Sie nach dem Ausführen dieser Pipeline, bis der Befehl abgeschlossen ist. Wenn Sie die Shell beenden, gehen möglicherweise die von Ihnen festgelegten Umgebungsvariablen verloren.

In diesem Beispiel wird ein Maschinentyp mit 1 vCPU verwendet, damit die GPU nicht zwischen mehreren Worker-Prozessen geteilt wird. Die Speicheranforderungen der Pipeline werden mithilfe von 13 GB erweitertem Speicher erfüllt. Weitere Informationen finden Sie unter GPUs und Worker-Parallelität.

Ergebnisse ansehen

Die Pipeline in tensorflow-landsat/main.py verarbeitet Landsat 8-Satellitenbilder und rendert sie als JPEG-Dateien. Führen Sie die folgenden Schritte aus, um diese Dateien aufzurufen.

  1. Listen Sie die JPEG-Ausgabedateien inklusive Details mithilfe der Google Cloud CLI auf.

    gcloud storage ls "gs://$BUCKET/samples/dataflow/landsat/" --long --readable-sizes
    
  2. Kopieren Sie die Dateien in Ihr lokales Verzeichnis.

    mkdir outputs
    gcloud storage cp "gs://$BUCKET/samples/dataflow/landsat/*" outputs/
    
  3. Öffnen Sie die Bilddateien in einem beliebigen Image-Viewer Ihrer Wahl.

Bereinigen

Damit Ihrem Google Cloud-Konto die in dieser Anleitung verwendeten Ressourcen nicht in Rechnung gestellt werden, löschen Sie entweder das Projekt, das die Ressourcen enthält, oder Sie behalten das Projekt und löschen die einzelnen Ressourcen.

Projekt löschen

Am einfachsten vermeiden Sie weitere Kosten durch Löschen des für die Anleitung erstellten Projekts.

So löschen Sie das Projekt:

  1. In the Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

Nächste Schritte