Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added sample for geospatial classification #7291

Merged
merged 34 commits into from
Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
8f6f07e
Added sample for geospatial classification
nikitamaia Dec 21, 2021
8ab2d19
Added noxfile
nikitamaia Dec 21, 2021
2279435
added emojis to notebook
nikitamaia Dec 24, 2021
37a7898
Small updates to geospatial sample
nikitamaia Jan 4, 2022
387b7b8
updates to requirements txt
nikitamaia Jan 5, 2022
78638a1
fix minor bugs in notebook
nikitamaia Jan 5, 2022
0d344cf
added requirmenets file
nikitamaia Jan 5, 2022
ffbd00d
simplify prediction and data extraction logic
nikitamaia Jan 6, 2022
3e94ff6
update tests
nikitamaia Jan 6, 2022
b2b939c
fix linter fail
nikitamaia Jan 6, 2022
964453c
fix linter and header fails
nikitamaia Jan 6, 2022
bfbcb6d
auth fix for tests
nikitamaia Jan 6, 2022
0d2696c
add noxfile to serving app
nikitamaia Jan 6, 2022
c06e0e4
minor updates based on comments
nikitamaia Jan 7, 2022
105d587
small updates
nikitamaia Jan 11, 2022
e1b6061
Merge branch 'main' into geospatial-sandbox
davidcavazos Jan 12, 2022
e8a2618
update credentials
nikitamaia Jan 12, 2022
3a9082b
added staging bucket to deploy
nikitamaia Jan 12, 2022
117bbe9
update staging bucket
nikitamaia Jan 12, 2022
e0cb2ed
removed serving app noxfile
nikitamaia Jan 13, 2022
afcbf5a
clarify use of timestamp in sample
nikitamaia Jan 13, 2022
74b9788
fix permissions error
nikitamaia Jan 13, 2022
9f4dabd
Added project to gcloud build
nikitamaia Jan 13, 2022
b0136c1
add GPU support
nikitamaia Jan 13, 2022
c4d6f6e
deploy from source code
nikitamaia Jan 13, 2022
bd937e5
update READMEs
nikitamaia Jan 18, 2022
d487f9a
Merge branch 'main' into geospatial-sandbox
davidcavazos Jan 25, 2022
fd947dc
Merge branch 'main' into geospatial-sandbox
davidcavazos Jan 25, 2022
3a166e4
added constrants file
nikitamaia Jan 27, 2022
5857fe7
added type hints
nikitamaia Jan 27, 2022
b032a5b
fix type hint errors
nikitamaia Jan 27, 2022
b0e795d
final updates to notebook
nikitamaia Jan 27, 2022
b0dacee
update notebook
nikitamaia Jan 27, 2022
e9a8eb9
Merge branch 'main' into geospatial-sandbox
davidcavazos Jan 27, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,469 changes: 1,469 additions & 0 deletions people-and-planet-ai/geospatial-classification/README.ipynb

Large diffs are not rendered by default.

342 changes: 342 additions & 0 deletions people-and-planet-ai/geospatial-classification/e2e_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,342 @@
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from datetime import datetime, timedelta
import logging
import os
import platform
import subprocess
import time
import uuid

import ee
from google.cloud import aiplatform
from google.cloud import storage
import google.auth
import pandas as pd
import pytest
import requests


PYTHON_VERSION = "".join(platform.python_version_tuple()[0:2])

NAME = f"ppai/geospatial-classification-py{PYTHON_VERSION}"

UUID = uuid.uuid4().hex[0:6]
PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"]
REGION = "us-central1"

TIMEOUT_SEC = 30 * 60 # 30 minutes in seconds
POLL_INTERVAL_SEC = 60 # 1 minute in seconds

VERTEX_AI_SUCCESS_STATE = "PIPELINE_STATE_SUCCEEDED"
VERTEX_AI_FINISHED_STATE = {
"PIPELINE_STATE_SUCCEEDED",
"PIPELINE_STATE_FAILED",
"PIPELINE_STATE_CANCELLED",
}

EARTH_ENGINE_SUCCESS_STATE = "SUCCEEDED"
EARTH_ENGINE_FINISHED_STATE = {"SUCCEEDED"}

BANDS = [
nikitamaia marked this conversation as resolved.
Show resolved Hide resolved
"B1",
"B2",
"B3",
"B4",
"B5",
"B6",
"B7",
"B8",
"B8A",
"B9",
"B10",
"B11",
"B12",
]
LABEL = "is_powered_on"

IMAGE_COLLECTION = "COPERNICUS/S2"
SCALE = 10

TRAIN_VALIDATION_SPLIT = 0.7

PATCH_SIZE = 16

credentials, project = google.auth.default()
nikitamaia marked this conversation as resolved.
Show resolved Hide resolved
ee.Initialize(credentials, project=PROJECT)

logging.getLogger().setLevel(logging.INFO)


@pytest.fixture(scope="session")
def bucket_name():
storage_client = storage.Client()

bucket_name = f"{NAME.replace('/', '-')}-{UUID}"
bucket = storage_client.create_bucket(bucket_name, location=REGION)

logging.info(f"bucket_name: {bucket_name}")
yield bucket_name

bucket.delete(force=True)


@pytest.fixture(scope="session")
def test_data(bucket_name):
labels_dataframe = pd.read_csv("labeled_geospatial_data.csv")
train_dataframe = labels_dataframe.sample(
frac=TRAIN_VALIDATION_SPLIT, random_state=200
) # random state is a seed value
validation_dataframe = labels_dataframe.drop(train_dataframe.index).sample(frac=1.0)

train_features = [labeled_feature(row) for row in train_dataframe.itertuples()]

validation_features = [
labeled_feature(row) for row in validation_dataframe.itertuples()
]

training_task = ee.batch.Export.table.toCloudStorage(
collection=ee.FeatureCollection(train_features),
description="Training image export: test",
bucket=bucket_name,
fileNamePrefix="geospatial_training",
selectors=BANDS + [LABEL],
fileFormat="TFRecord",
)

training_task.start()

validation_task = ee.batch.Export.table.toCloudStorage(
collection=ee.FeatureCollection(validation_features),
description="Validation image export",
bucket=bucket_name,
fileNamePrefix="geospatial_validation",
selectors=BANDS + [LABEL],
fileFormat="TFRecord",
)

validation_task.start()

train_status = None
val_status = None

logging.info("Waiting for data export to complete.")
for _ in range(0, TIMEOUT_SEC, POLL_INTERVAL_SEC):
train_status = ee.data.getOperation(training_task.name)["metadata"]["state"]
nikitamaia marked this conversation as resolved.
Show resolved Hide resolved
val_status = ee.data.getOperation(validation_task.name)["metadata"]["state"]
if (
train_status in EARTH_ENGINE_FINISHED_STATE
and val_status in EARTH_ENGINE_FINISHED_STATE
):
break
time.sleep(POLL_INTERVAL_SEC)

assert train_status == EARTH_ENGINE_SUCCESS_STATE
assert val_status == EARTH_ENGINE_SUCCESS_STATE
logging.info(f"Export finished with status {train_status}")

yield training_task.name


def labeled_feature(row):
start = datetime.fromisoformat(row.timestamp)
end = start + timedelta(days=1)
image = (
ee.ImageCollection(IMAGE_COLLECTION)
.filterDate(start.strftime("%Y-%m-%d"), end.strftime("%Y-%m-%d"))
.select(BANDS)
.mosaic()
)
point = ee.Feature(
ee.Geometry.Point([row.lon, row.lat]),
{LABEL: row.is_powered_on},
)
return (
image.neighborhoodToArray(ee.Kernel.square(PATCH_SIZE))
.sampleRegions(ee.FeatureCollection([point]), scale=SCALE)
.first()
)


@pytest.fixture(scope="session")
def container_image():
nikitamaia marked this conversation as resolved.
Show resolved Hide resolved
# https://cloud.google.com/sdk/gcloud/reference/builds/submit
container_image = f"gcr.io/{PROJECT}/{NAME}:{UUID}"
subprocess.check_call(
[
"gcloud",
"builds",
"submit",
"serving_app",
f"--tag={container_image}",
"--machine-type=e2-highcpu-8",
"--timeout=15m",
"--quiet",
]
)

logging.info(f"container_image: {container_image}")
yield container_image

# https://cloud.google.com/sdk/gcloud/reference/container/images/delete
subprocess.check_call(
[
"gcloud",
"container",
"images",
"delete",
container_image,
f"--project={PROJECT}",
"--force-delete-tags",
"--quiet",
]
)


@pytest.fixture(scope="session")
def service_url(bucket_name, container_image):
# https://cloud.google.com/sdk/gcloud/reference/run/deploy
service_name = f"{NAME.replace('/', '-')}-{UUID}"
subprocess.check_call(
[
"gcloud",
"run",
"deploy",
service_name,
f"--image={container_image}",
"--command=gunicorn",
"--args=--threads=8,--timeout=0,main:app",
"--platform=managed",
f"--project={PROJECT}",
f"--region={REGION}",
"--memory=1G",
nikitamaia marked this conversation as resolved.
Show resolved Hide resolved
"--no-allow-unauthenticated",
]
)

# https://cloud.google.com/sdk/gcloud/reference/run/services/describe
service_url = (
subprocess.run(
[
"gcloud",
"run",
"services",
"describe",
service_name,
"--platform=managed",
f"--project={PROJECT}",
f"--region={REGION}",
"--format=get(status.url)",
],
capture_output=True,
)
.stdout.decode("utf-8")
.strip()
)

logging.info(f"service_url: {service_url}")
yield service_url

# https://cloud.google.com/sdk/gcloud/reference/run/services/delete
subprocess.check_call(
[
"gcloud",
"run",
"services",
"delete",
service_name,
"--platform=managed",
f"--project={PROJECT}",
f"--region={REGION}",
"--quiet",
]
)


@pytest.fixture(scope="session")
def identity_token():
yield (
subprocess.run(
["gcloud", "auth", "print-identity-token", f"--project={PROJECT}"],
capture_output=True,
)
.stdout.decode("utf-8")
.strip()
)


@pytest.fixture(scope="session")
def train_model(bucket_name):
aiplatform.init(project=PROJECT, staging_bucket=bucket_name)
job = aiplatform.CustomTrainingJob(
display_name="climate_script_colab",
script_path="task.py",
container_uri="us-docker.pkg.dev/vertex-ai/training/tf-cpu.2-7:latest",
)

job.run(args=[f"--bucket={bucket_name}"])

logging.info(f"train_model resource_name: {job.resource_name}")

# Wait until the model training job finishes.
status = None
logging.info("Waiting for model to train.")
for _ in range(0, TIMEOUT_SEC, POLL_INTERVAL_SEC):
# https://googleapis.dev/python/aiplatform/latest/aiplatform_v1/job_service.html
status = job.state.name
if status in VERTEX_AI_FINISHED_STATE:
break
time.sleep(POLL_INTERVAL_SEC)

logging.info(f"Model job finished with status {status}")
assert status == VERTEX_AI_SUCCESS_STATE
yield job.resource_name


def get_prediction_data(lon, lat, start, end):
"""Extracts Sentinel image as json at specific lat/lon and timestamp."""

location = ee.Feature(ee.Geometry.Point([lon, lat]))
image = (
ee.ImageCollection(IMAGE_COLLECTION)
.filterDate(start, end)
.select(BANDS)
.mosaic()
)

feature = image.neighborhoodToArray(ee.Kernel.square(PATCH_SIZE)).sampleRegions(
collection=ee.FeatureCollection([location]), scale=SCALE
)

return feature.getInfo()["features"][0]["properties"]


def test_predict(bucket_name, test_data, train_model, service_url, identity_token):

# Test point
prediction_data = get_prediction_data(
-84.80529, 39.11613, "2021-10-01", "2021-10-31"
)

# Make prediction
response = requests.post(
url=f"{service_url}/predict",
headers={"Authorization": f"Bearer {identity_token}"},
json={"data": prediction_data, "bucket": bucket_name},
).json()

# Check that we get non-empty predictions.
assert "predictions" in response["predictions"]
assert len(response["predictions"]) > 0
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading