GeezerVison is showing github.com — feat: add video action recognition samples (#77) · googleapis/python-aiplatform@4c60ad6
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +60Lines changed: 60 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# Copyright 2020 Google LLC | | 2 | +# | | 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); | | 4 | +# you may not use this file except in compliance with the License. | | 5 | +# You may obtain a copy of the License at | | 6 | +# | | 7 | +# https://www.apache.org/licenses/LICENSE-2.0 | | 8 | +# | | 9 | +# Unless required by applicable law or agreed to in writing, software | | 10 | +# distributed under the License is distributed on an "AS IS" BASIS, | | 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | | 12 | +# See the License for the specific language governing permissions and | | 13 | +# limitations under the License. | | 14 | + | | 15 | +# [START aiplatform_create_batch_prediction_job_video_action_recognition_sample] | | 16 | +from google.cloud import aiplatform | | 17 | +from google.protobuf import json_format | | 18 | +from google.protobuf.struct_pb2 import Value | | 19 | + | | 20 | + | | 21 | +def create_batch_prediction_job_video_action_recognition_sample( | | 22 | + project: str, | | 23 | + display_name: str, | | 24 | + model: str, | | 25 | + gcs_source_uri: str, | | 26 | + gcs_destination_output_uri_prefix: str, | | 27 | + location: str = "us-central1", | | 28 | + api_endpoint: str = "us-central1-aiplatform.googleapis.com", | | 29 | +): | | 30 | + client_options = {"api_endpoint": api_endpoint} | | 31 | + # Initialize client that will be used to create and send requests. | | 32 | + # This client only needs to be created once, and can be reused for multiple requests. | | 33 | + client = aiplatform.gapic.JobServiceClient(client_options=client_options) | | 34 | + model_parameters_dict = { | | 35 | + "confidenceThreshold": 0.5, | | 36 | + } | | 37 | + model_parameters = json_format.ParseDict(model_parameters_dict, Value()) | | 38 | + | | 39 | + batch_prediction_job = { | | 40 | + "display_name": display_name, | | 41 | + # Format: 'projects/{project}/locations/{location}/models/{model_id}' | | 42 | + "model": model, | | 43 | + "model_parameters": model_parameters, | | 44 | + "input_config": { | | 45 | + "instances_format": "jsonl", | | 46 | + "gcs_source": {"uris": [gcs_source_uri]}, | | 47 | + }, | | 48 | + "output_config": { | | 49 | + "predictions_format": "jsonl", | | 50 | + "gcs_destination": {"output_uri_prefix": gcs_destination_output_uri_prefix}, | | 51 | + }, | | 52 | + } | | 53 | + parent = f"projects/{project}/locations/{location}" | | 54 | + response = client.create_batch_prediction_job( | | 55 | + parent=parent, batch_prediction_job=batch_prediction_job | | 56 | + ) | | 57 | + print("response:", response) | | 58 | + | | 59 | + | | 60 | +# [END aiplatform_create_batch_prediction_job_video_action_recognition_sample] |
Collapse file Copy file name to clipboard +82Lines changed: 82 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# Copyright 2020 Google LLC | | 2 | +# | | 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); | | 4 | +# you may not use this file except in compliance with the License. | | 5 | +# You may obtain a copy of the License at | | 6 | +# | | 7 | +# https://www.apache.org/licenses/LICENSE-2.0 | | 8 | +# | | 9 | +# Unless required by applicable law or agreed to in writing, software | | 10 | +# distributed under the License is distributed on an "AS IS" BASIS, | | 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | | 12 | +# See the License for the specific language governing permissions and | | 13 | +# limitations under the License. | | 14 | + | | 15 | +import uuid | | 16 | +import pytest | | 17 | +import os | | 18 | + | | 19 | +import helpers | | 20 | + | | 21 | +import create_batch_prediction_job_video_action_recognition_sample | | 22 | + | | 23 | +from google.cloud import aiplatform | | 24 | + | | 25 | +PROJECT_ID = os.getenv("BUILD_SPECIFIC_GCLOUD_PROJECT") | | 26 | +LOCATION = "us-central1" | | 27 | +MODEL_ID = "3530998029718913024" # permanent_swim_run_videos_action_recognition_model | | 28 | +DISPLAY_NAME = f"temp_create_batch_prediction_job_video_action_recognition_test_{uuid.uuid4()}" | | 29 | +GCS_SOURCE_URI = "gs://automl-video-demo-data/ucaip-var/swimrun_bp.jsonl" | | 30 | +GCS_OUTPUT_URI = "gs://ucaip-samples-test-output/" | | 31 | +API_ENDPOINT = "us-central1-aiplatform.googleapis.com" | | 32 | + | | 33 | +@pytest.fixture | | 34 | +def shared_state(): | | 35 | + state = {} | | 36 | + yield state | | 37 | + | | 38 | + | | 39 | +@pytest.fixture | | 40 | +def job_client(): | | 41 | + client_options = {"api_endpoint": API_ENDPOINT} | | 42 | + job_client = aiplatform.gapic.JobServiceClient( | | 43 | + client_options=client_options) | | 44 | + yield job_client | | 45 | + | | 46 | + | | 47 | +@pytest.fixture(scope="function", autouse=True) | | 48 | +def teardown(shared_state, job_client): | | 49 | + yield | | 50 | + job_client.delete_batch_prediction_job( | | 51 | + name=shared_state["batch_prediction_job_name"] | | 52 | + ) | | 53 | + | | 54 | + | | 55 | +# Creating AutoML Video Object Tracking batch prediction job | | 56 | +def test_create_batch_prediction_job_video_action_recognition_sample( | | 57 | + capsys, shared_state, job_client | | 58 | +): | | 59 | + | | 60 | + model = f"projects/{PROJECT_ID}/locations/{LOCATION}/models/{MODEL_ID}" | | 61 | + | | 62 | + create_batch_prediction_job_video_action_recognition_sample.create_batch_prediction_job_video_action_recognition_sample( | | 63 | + project=PROJECT_ID, | | 64 | + display_name=DISPLAY_NAME, | | 65 | + model=model, | | 66 | + gcs_source_uri=GCS_SOURCE_URI, | | 67 | + gcs_destination_output_uri_prefix=GCS_OUTPUT_URI, | | 68 | + ) | | 69 | + | | 70 | + out, _ = capsys.readouterr() | | 71 | + | | 72 | + # Save resource name of the newly created batch prediction job | | 73 | + shared_state["batch_prediction_job_name"] = helpers.get_name(out) | | 74 | + | | 75 | + # Waiting for batch prediction job to be in CANCELLED state | | 76 | + helpers.wait_for_job_state( | | 77 | + get_job_method=job_client.get_batch_prediction_job, | | 78 | + name=shared_state["batch_prediction_job_name"], | | 79 | + expected_state="SUCCEEDED", | | 80 | + timeout=600, | | 81 | + freq=20, | | 82 | + ) |
Collapse file Copy file name to clipboard +54Lines changed: 54 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# Copyright 2020 Google LLC | | 2 | +# | | 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); | | 4 | +# you may not use this file except in compliance with the License. | | 5 | +# You may obtain a copy of the License at | | 6 | +# | | 7 | +# https://www.apache.org/licenses/LICENSE-2.0 | | 8 | +# | | 9 | +# Unless required by applicable law or agreed to in writing, software | | 10 | +# distributed under the License is distributed on an "AS IS" BASIS, | | 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | | 12 | +# See the License for the specific language governing permissions and | | 13 | +# limitations under the License. | | 14 | + | | 15 | +# [START aiplatform_create_training_pipeline_video_action_recognition_sample] | | 16 | +from google.cloud import aiplatform | | 17 | +from google.protobuf import json_format | | 18 | +from google.protobuf.struct_pb2 import Value | | 19 | + | | 20 | + | | 21 | +def create_training_pipeline_video_action_recognition_sample( | | 22 | + project: str, | | 23 | + display_name: str, | | 24 | + dataset_id: str, | | 25 | + model_display_name: str, | | 26 | + model_type: str, | | 27 | + location: str = "us-central1", | | 28 | + api_endpoint: str = "us-central1-aiplatform.googleapis.com", | | 29 | +): | | 30 | + client_options = {"api_endpoint": api_endpoint} | | 31 | + # Initialize client that will be used to create and send requests. | | 32 | + # This client only needs to be created once, and can be reused for multiple requests. | | 33 | + client = aiplatform.gapic.PipelineServiceClient(client_options=client_options) | | 34 | + training_task_inputs_dict = { | | 35 | + # modelType can be either 'CLOUD' or 'MOBILE_VERSATILE_1' | | 36 | + "modelType": model_type | | 37 | + } | | 38 | + training_task_inputs = json_format.ParseDict(training_task_inputs_dict, Value()) | | 39 | + | | 40 | + training_pipeline = { | | 41 | + "display_name": display_name, | | 42 | + "training_task_definition": "gs://google-cloud-aiplatform/schema/trainingjob/definition/automl_video_action_recognition_1.0.0.yaml", | | 43 | + "training_task_inputs": training_task_inputs, | | 44 | + "input_data_config": {"dataset_id": dataset_id}, | | 45 | + "model_to_upload": {"display_name": model_display_name}, | | 46 | + } | | 47 | + parent = f"projects/{project}/locations/{location}" | | 48 | + response = client.create_training_pipeline( | | 49 | + parent=parent, training_pipeline=training_pipeline | | 50 | + ) | | 51 | + print("response:", response) | | 52 | + | | 53 | + | | 54 | +# [END aiplatform_create_training_pipeline_video_action_recognition_sample] |
Collapse file Copy file name to clipboard +100Lines changed: 100 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# Copyright 2020 Google LLC | | 2 | +# | | 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); | | 4 | +# you may not use this file except in compliance with the License. | | 5 | +# You may obtain a copy of the License at | | 6 | +# | | 7 | +# https://www.apache.org/licenses/LICENSE-2.0 | | 8 | +# | | 9 | +# Unless required by applicable law or agreed to in writing, software | | 10 | +# distributed under the License is distributed on an "AS IS" BASIS, | | 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | | 12 | +# See the License for the specific language governing permissions and | | 13 | +# limitations under the License. | | 14 | + | | 15 | +import uuid | | 16 | +import pytest | | 17 | +import os | | 18 | + | | 19 | +import helpers | | 20 | + | | 21 | +import create_training_pipeline_video_action_recognition_sample | | 22 | + | | 23 | +from google.cloud import aiplatform | | 24 | + | | 25 | +LOCATION = "us-central1" | | 26 | +PROJECT_ID = os.getenv("BUILD_SPECIFIC_GCLOUD_PROJECT") | | 27 | +DATASET_ID = "6881957627459272704" # permanent_swim_run_videos_action_recognition_dataset | | 28 | +DISPLAY_NAME = f"temp_create_training_pipeline_video_action_recognition_test_{uuid.uuid4()}" | | 29 | +MODEL_DISPLAY_NAME = f"Temp Model for {DISPLAY_NAME}" | | 30 | +MODEL_TYPE = "CLOUD" | | 31 | +API_ENDPOINT = "us-central1-aiplatform.googleapis.com" | | 32 | + | | 33 | +@pytest.fixture | | 34 | +def shared_state(): | | 35 | + state = {} | | 36 | + yield state | | 37 | + | | 38 | + | | 39 | +@pytest.fixture | | 40 | +def pipeline_client(): | | 41 | + client_options = {"api_endpoint": API_ENDPOINT} | | 42 | + pipeline_client = aiplatform.gapic.PipelineServiceClient( | | 43 | + client_options=client_options | | 44 | + ) | | 45 | + yield pipeline_client | | 46 | + | | 47 | + | | 48 | +@pytest.fixture | | 49 | +def model_client(): | | 50 | + client_options = {"api_endpoint": API_ENDPOINT} | | 51 | + model_client = aiplatform.gapic.ModelServiceClient( | | 52 | + client_options=client_options) | | 53 | + yield model_client | | 54 | + | | 55 | + | | 56 | +@pytest.fixture(scope="function", autouse=True) | | 57 | +def teardown(shared_state, model_client, pipeline_client): | | 58 | + yield | | 59 | + model_client.delete_model(name=shared_state["model_name"]) | | 60 | + pipeline_client.delete_training_pipeline( | | 61 | + name=shared_state["training_pipeline_name"] | | 62 | + ) | | 63 | + | | 64 | + | | 65 | +# Training AutoML Vision Model | | 66 | +def test_create_training_pipeline_video_action_recognition_sample( | | 67 | + capsys, shared_state, pipeline_client | | 68 | +): | | 69 | + create_training_pipeline_video_action_recognition_sample.create_training_pipeline_video_action_recognition_sample( | | 70 | + project=PROJECT_ID, | | 71 | + display_name=DISPLAY_NAME, | | 72 | + dataset_id=DATASET_ID, | | 73 | + model_display_name=MODEL_DISPLAY_NAME, | | 74 | + model_type=MODEL_TYPE, | | 75 | + ) | | 76 | + | | 77 | + out, _ = capsys.readouterr() | | 78 | + | | 79 | + assert "response:" in out | | 80 | + | | 81 | + # Save resource name of the newly created training pipeline | | 82 | + shared_state["training_pipeline_name"] = helpers.get_name(out) | | 83 | + | | 84 | + # Poll until the pipeline succeeds because we want to test the model_upload step as well. | | 85 | + helpers.wait_for_job_state( | | 86 | + get_job_method=pipeline_client.get_training_pipeline, | | 87 | + name=shared_state["training_pipeline_name"], | | 88 | + expected_state="SUCCEEDED", | | 89 | + timeout=5000, | | 90 | + freq=20, | | 91 | + ) | | 92 | + | | 93 | + training_pipeline = pipeline_client.get_training_pipeline( | | 94 | + name=shared_state["training_pipeline_name"] | | 95 | + ) | | 96 | + | | 97 | + # Check that the model indeed has been uploaded. | | 98 | + assert training_pipeline.model_to_upload.name != "" | | 99 | + | | 100 | + shared_state["model_name"] = training_pipeline.model_to_upload.name |
Collapse file Copy file name to clipboard +44Lines changed: 44 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# Copyright 2020 Google LLC | | 2 | +# | | 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); | | 4 | +# you may not use this file except in compliance with the License. | | 5 | +# You may obtain a copy of the License at | | 6 | +# | | 7 | +# https://www.apache.org/licenses/LICENSE-2.0 | | 8 | +# | | 9 | +# Unless required by applicable law or agreed to in writing, software | | 10 | +# distributed under the License is distributed on an "AS IS" BASIS, | | 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | | 12 | +# See the License for the specific language governing permissions and | | 13 | +# limitations under the License. | | 14 | + | | 15 | +# [START aiplatform_export_model_video_action_recognition_sample] | | 16 | +from google.cloud import aiplatform | | 17 | + | | 18 | + | | 19 | +def export_model_video_action_recognition_sample( | | 20 | + project: str, | | 21 | + model_id: str, | | 22 | + gcs_destination_output_uri_prefix: str, | | 23 | + export_format: str, | | 24 | + location: str = "us-central1", | | 25 | + api_endpoint: str = "us-central1-aiplatform.googleapis.com", | | 26 | + timeout: int = 300, | | 27 | +): | | 28 | + client_options = {"api_endpoint": api_endpoint} | | 29 | + # Initialize client that will be used to create and send requests. | | 30 | + # This client only needs to be created once, and can be reused for multiple requests. | | 31 | + client = aiplatform.gapic.ModelServiceClient(client_options=client_options) | | 32 | + gcs_destination = {"output_uri_prefix": gcs_destination_output_uri_prefix} | | 33 | + output_config = { | | 34 | + "artifact_destination": gcs_destination, | | 35 | + "export_format_id": export_format, | | 36 | + } | | 37 | + name = client.model_path(project=project, location=location, model=model_id) | | 38 | + response = client.export_model(name=name, output_config=output_config) | | 39 | + print("Long running operation:", response.operation.name) | | 40 | + export_model_response = response.result(timeout=timeout) | | 41 | + print("export_model_response:", export_model_response) | | 42 | + | | 43 | + | | 44 | +# [END aiplatform_export_model_video_action_recognition_sample] |
Collapse file Copy file name to clipboard +45Lines changed: 45 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# Copyright 2020 Google LLC | | 2 | +# | | 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); | | 4 | +# you may not use this file except in compliance with the License. | | 5 | +# You may obtain a copy of the License at | | 6 | +# | | 7 | +# https://www.apache.org/licenses/LICENSE-2.0 | | 8 | +# | | 9 | +# Unless required by applicable law or agreed to in writing, software | | 10 | +# distributed under the License is distributed on an "AS IS" BASIS, | | 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | | 12 | +# See the License for the specific language governing permissions and | | 13 | +# limitations under the License. | | 14 | + | | 15 | +import pytest | | 16 | +import os | | 17 | + | | 18 | +import export_model_video_action_recognition_sample | | 19 | +from google.cloud import storage | | 20 | + | | 21 | +PROJECT_ID = os.getenv("BUILD_SPECIFIC_GCLOUD_PROJECT") | | 22 | +MODEL_ID = "3422489426196955136" # permanent_swim_run_videos_action_recognition_edge_model | | 23 | +GCS_URI = "gs://ucaip-samples-test-output/tmp/export_model_video_action_recognition_sample" | | 24 | +EXPORT_FORMAT = "tf-saved-model" | | 25 | + | | 26 | +@pytest.fixture(scope="function", autouse=True) | | 27 | +def teardown(): | | 28 | + yield | | 29 | + | | 30 | + storage_client = storage.Client() | | 31 | + bucket = storage_client.get_bucket("ucaip-samples-test-output") | | 32 | + blobs = bucket.list_blobs(prefix="tmp/export_model_video_action_recognition_sample") | | 33 | + for blob in blobs: | | 34 | + blob.delete() | | 35 | + | | 36 | + | | 37 | +def test_export_model_video_action_recognition_sample(capsys): | | 38 | + export_model_video_action_recognition_sample.export_model_video_action_recognition_sample( | | 39 | + project=PROJECT_ID, | | 40 | + model_id=MODEL_ID, | | 41 | + gcs_destination_output_uri_prefix=GCS_URI, | | 42 | + export_format=EXPORT_FORMAT, | | 43 | + ) | | 44 | + out, _ = capsys.readouterr() | | 45 | + assert "export_model_response" in out |
|