Skip to content

Commit

Permalink
samples: migrate vision automl samples (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
munkhuushmgl authored and dandhlee committed Nov 17, 2022
1 parent 6aa69df commit 5e12437
Show file tree
Hide file tree
Showing 9 changed files with 457 additions and 0 deletions.
43 changes: 43 additions & 0 deletions automl/beta/automl_vision_create_model_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env python

# Copyright 2018 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.

import datetime
import os

from google.cloud import automl_v1beta1 as automl
import pytest

project_id = os.environ["GOOGLE_CLOUD_PROJECT"]
compute_region = "us-central1"


@pytest.mark.skip(reason="creates too many models")
def test_model_create_status_delete(capsys):
# create model
client = automl.AutoMlClient()
model_name = "test_" + datetime.datetime.now().strftime("%Y%m%d%H%M%S")
project_location = client.location_path(project_id, compute_region)
my_model = {
"display_name": model_name,
"dataset_id": "3946265060617537378",
"image_classification_model_metadata": {"train_budget": 24},
}
response = client.create_model(project_location, my_model)
operation_name = response.operation.name
assert operation_name

# cancel operation
response.cancel()
93 changes: 93 additions & 0 deletions automl/beta/automl_vision_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#!/usr/bin/env python

# Copyright 2018 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.

"""This application demonstrates how to perform basic operations on model
with the Google AutoML Vision API.
For more information, the documentation at
https://cloud.google.com/vision/automl/docs.
"""

import argparse
import os


def create_model(
project_id, compute_region, dataset_id, model_name, train_budget=24
):
"""Create a model."""
# [START automl_vision_create_model]
# TODO(developer): Uncomment and set the following variables
# project_id = 'PROJECT_ID_HERE'
# compute_region = 'COMPUTE_REGION_HERE'
# dataset_id = 'DATASET_ID_HERE'
# model_name = 'MODEL_NAME_HERE'
# train_budget = integer amount for maximum cost of model

from google.cloud import automl_v1beta1 as automl

client = automl.AutoMlClient()

# A resource that represents Google Cloud Platform location.
project_location = client.location_path(project_id, compute_region)

# Set model name and model metadata for the image dataset.
my_model = {
"display_name": model_name,
"dataset_id": dataset_id,
"image_classification_model_metadata": {"train_budget": train_budget}
if train_budget
else {},
}

# Create a model with the model metadata in the region.
response = client.create_model(project_location, my_model)

print("Training operation name: {}".format(response.operation.name))
print("Training started...")

# [END automl_vision_create_model]


if __name__ == "__main__":
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter,
)
subparsers = parser.add_subparsers(dest="command")

create_model_parser = subparsers.add_parser(
"create_model", help=create_model.__doc__
)
create_model_parser.add_argument("dataset_id")
create_model_parser.add_argument("model_name")
create_model_parser.add_argument(
"train_budget", type=int, nargs="?", default=0
)

project_id = os.environ["PROJECT_ID"]
compute_region = os.environ["REGION_NAME"]

args = parser.parse_args()

if args.command == "create_model":
create_model(
project_id,
compute_region,
args.dataset_id,
args.model_name,
args.train_budget,
)
29 changes: 29 additions & 0 deletions automl/vision_edge/edge_container_predict/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright 2019 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.

ARG TF_SERVING_IMAGE_TAG
FROM tensorflow/serving:${TF_SERVING_IMAGE_TAG}

ENV GCS_READ_CACHE_MAX_STALENESS 300
ENV GCS_STAT_CACHE_MAX_AGE 300
ENV GCS_MATCHING_PATHS_CACHE_MAX_AGE 300

EXPOSE 8500
EXPOSE 8501
ENTRYPOINT /usr/bin/tensorflow_model_server \
--port=8500 \
--rest_api_port=8501 \
--model_base_path=/tmp/mounted_model/ \
--tensorflow_session_parallelism=0 \
--file_system_poll_wait_seconds=31540000
78 changes: 78 additions & 0 deletions automl/vision_edge/edge_container_predict/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# AutoML Vision Edge Container Prediction

This is an example to show how to predict with AutoML Vision Edge Containers.
The test (automl_vision_edge_container_predict_test.py) shows an automatical way
to run the prediction.

If you want to try the test manually with a sample model, please install
[gsutil tools](https://cloud.google.com/storage/docs/gsutil_install) and
[Docker CE](https://docs.docker.com/install/) first, and then follow the steps
below. All the following instructions with commands assume you are in this
folder with system variables as

```bash
$ CONTAINER_NAME=AutomlVisionEdgeContainerPredict
$ PORT=8505
```

+ Step 1. Pull the Docker image.

```bash
# This is a CPU TFServing 1.14.0 with some default settings compiled from
# https://hub.docker.com/r/tensorflow/serving.
$ DOCKER_GCS_DIR=gcr.io/cloud-devrel-public-resources
$ CPU_DOCKER_GCS_PATH=${DOCKER_GCS_DIR}/gcloud-container-1.14.0:latest
$ sudo docker pull ${CPU_DOCKER_GCS_PATH}
```

+ Step 2. Get a sample saved model.

```bash
$ MODEL_GCS_DIR=gs://cloud-samples-data/vision/edge_container_predict
$ SAMPLE_SAVED_MODEL=${MODEL_GCS_DIR}/saved_model.pb
$ mkdir model_path
$ YOUR_MODEL_PATH=$(realpath model_path)
$ gsutil -m cp ${SAMPLE_SAVED_MODEL} ${YOUR_MODEL_PATH}
```

+ Step 3. Run the Docker container.

```bash
$ sudo docker run --rm --name ${CONTAINER_NAME} -p ${PORT}:8501 -v \
${YOUR_MODEL_PATH}:/tmp/mounted_model/0001 -t ${CPU_DOCKER_GCS_PATH}
```

+ Step 4. Send a prediction request.

```bash
$ python automl_vision_edge_container_predict.py --image_file_path=./test.jpg \
--image_key=1 --port_number=${PORT}
```

The outputs are

```
{
'predictions':
[
{
'scores': [0.0914393, 0.458942, 0.027604, 0.386767, 0.0352474],
labels': ['daisy', 'dandelion', 'roses', 'sunflowers', 'tulips'],
'key': '1'
}
]
}
```

+ Step 5. Stop the container.

```bash
sudo docker stop ${CONTAINER_NAME}
```

Note: The docker image is uploaded with the following command.

```bash
gcloud builds --project=cloud-devrel-public-resources \
submit --config cloudbuild.yaml
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/usr/bin/env python

# Copyright 2019 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.

r"""This is an example to call REST API from TFServing docker containers.
Examples:
python automl_vision_edge_container_predict.py \
--image_file_path=./test.jpg --image_key=1 --port_number=8051
"""

import argparse
# [START automl_vision_edge_container_predict]
import base64
import io
import json

import requests


def container_predict(image_file_path, image_key, port_number=8501):
"""Sends a prediction request to TFServing docker container REST API.
Args:
image_file_path: Path to a local image for the prediction request.
image_key: Your chosen string key to identify the given image.
port_number: The port number on your device to accept REST API calls.
Returns:
The response of the prediction request.
"""

with io.open(image_file_path, 'rb') as image_file:
encoded_image = base64.b64encode(image_file.read()).decode('utf-8')

# The example here only shows prediction with one image. You can extend it
# to predict with a batch of images indicated by different keys, which can
# make sure that the responses corresponding to the given image.
instances = {
'instances': [
{'image_bytes': {'b64': str(encoded_image)},
'key': image_key}
]
}

# This example shows sending requests in the same server that you start
# docker containers. If you would like to send requests to other servers,
# please change localhost to IP of other servers.
url = 'http://localhost:{}/v1/models/default:predict'.format(port_number)

response = requests.post(url, data=json.dumps(instances))
print(response.json())
# [END automl_vision_edge_container_predict]
return response.json()


def main():
parser = argparse.ArgumentParser()
parser.add_argument('--image_file_path', type=str)
parser.add_argument('--image_key', type=str, default='1')
parser.add_argument('--port_number', type=int, default=8501)
args = parser.parse_args()

container_predict(args.image_file_path, args.image_key, args.port_number)


if __name__ == '__main__':
main()
Loading

0 comments on commit 5e12437

Please sign in to comment.