projects/vision-ai-edge-platform/pipelines/segmentation/deeplabv3plus/notebooks/launch_pipeline.ipynb (345 lines of code) (raw):
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Launch a Visual Inspection AI Model Training Pipeline\n",
"\n",
"Contributors: michaelmenzel@google.com"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"\"\"\"\n",
"Copyright 2024 Google LLC\n",
"\n",
"Licensed under the Apache License, Version 2.0 (the \"License\");\n",
"you may not use this file except in compliance with the License.\n",
"You may obtain a copy of the License at\n",
"\n",
" https://www.apache.org/licenses/LICENSE-2.0\n",
"\n",
"Unless required by applicable law or agreed to in writing, software\n",
"distributed under the License is distributed on an \"AS IS\" BASIS,\n",
"WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
"See the License for the specific language governing permissions and\n",
"limitations under the License.\n",
"\"\"\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Set Parameters:"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"# Define project id and location for the pipeline\n",
"PROJECT_ID = 'visual-inspection-demo-2184'\n",
"LOCATION = 'us-central1'\n",
"\n",
"# A staging bucket for the pipeline and training program\n",
"BUCKET = 'gs://viai-demo-data-us-central1/ml-trainings'\n",
"# The service account that the pipeline and training program acts as\n",
"SERVICE_ACCOUNT = '1047381110578-compute@developer.gserviceaccount.com'\n",
"# VPC network to deploy the model as private endpoint\n",
"VPC_NETWORK = 'projects/1047381110578/global/networks/viai-demo'\n",
"\n",
"# The dataset id in Vertex to use for model training\n",
"DATASET_ID = '1850063553663336448'\n",
"\n",
"# Container image for the training program\n",
"CONTAINER_LOCATION = 'us'\n",
"CONTAINER_NAME = 'trainer'\n",
"CONTAINER_REPO = 'visual-inspection-ml-training'\n",
"CONTAINER_TAG = 'latest'\n",
"CONTAINER_URI = f'{CONTAINER_LOCATION}-docker.pkg.dev/{PROJECT_ID}/{CONTAINER_REPO}/segmentation-deeplabv3plus-{CONTAINER_NAME}:{CONTAINER_TAG}'\n",
"\n",
"# Experiment to log metrics and parameters from the model training\n",
"EXPERIMENT = f'{PROJECT_ID}-viai-segmentation-deeplabv3'\n",
"\n",
"# Name of the pipeline in Vertex AI\n",
"PIPELINE_DISPLAY_NAME = 'viai-segmentation-pipeline'\n",
"\n",
"\n",
"# Change the folder names below if you want the pipeline and training program \n",
"# to write in different folders\n",
"import os\n",
"STAGING_BUCKET=os.path.join(BUCKET, 'viai-oss-pipeline')\n",
"PIPELINE_BUCKET=os.path.join(STAGING_BUCKET, 'viai-segmentation-deeplabv3')\n",
"TRAINING_BUCKET=os.path.join(PIPELINE_BUCKET, 'trainer')\n",
"PIPELINE_ROOT = os.path.join(BUCKET, 'pipeline_root/viai-oss-pipeline')\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Install dependencies:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!pip install --upgrade -q matplotlib \\\n",
" google-cloud-aiplatform[autologging]==1.65.0 \\\n",
" google-cloud-pipeline-components==2.16.1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Import libraries and initialize the Vertex AI client:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from kfp import compiler, dsl\n",
"\n",
"from google.cloud import aiplatform\n",
"from google_cloud_pipeline_components.v1.custom_job.component import custom_training_job as CustomTrainingJobOp\n",
"from google_cloud_pipeline_components.v1.dataset import GetVertexDatasetOp\n",
"from google_cloud_pipeline_components.v1.endpoint import (EndpointCreateOp,\n",
" ModelDeployOp)\n",
"from google_cloud_pipeline_components.v1.model import ModelUploadOp\n",
"from google_cloud_pipeline_components.types import artifact_types\n",
"\n",
"aiplatform.init(project=PROJECT_ID, \n",
" location=LOCATION,\n",
" staging_bucket=STAGING_BUCKET, \n",
" experiment=EXPERIMENT)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Implement a custom training function:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"@dsl.component(base_image='python:3.11-slim', packages_to_install=['google-cloud-aiplatform', 'google-cloud-pipeline-components'])\n",
"def model_train(project_id: str, location: str, display_name: str, \n",
" trainer_bucket: str, container_uri: str,\n",
" experiment: str, service_account: str, dataset: dsl.Input[artifact_types.VertexDataset], \n",
" deeplab_preset: str, img_width: int, img_height: int,\n",
" batch_size: int, optimizer: str, num_epochs: int, patience_epochs: int,\n",
" loss_function: str, augmentation_factor: int,\n",
" model: dsl.Output[artifact_types.VertexModel]):\n",
"\n",
" from google.cloud import aiplatform as aip\n",
" from google_cloud_pipeline_components.types import artifact_types\n",
" \n",
" aip.init(location=location, project=project_id, staging_bucket=trainer_bucket)\n",
" \n",
" vertex_ai_custom_job = aip.CustomContainerTrainingJob(\n",
" display_name=display_name,\n",
" container_uri=container_uri,\n",
" model_serving_container_image_uri='us-docker.pkg.dev/vertex-ai/prediction/tf2-cpu.2-13:latest'\n",
" )\n",
" vertex_ai_model = vertex_ai_custom_job.run(\n",
" machine_type='n1-standard-8',\n",
" replica_count=1,\n",
" accelerator_type = 'NVIDIA_TESLA_V100',\n",
" accelerator_count = 1,\n",
" dataset=aip.ImageDataset(dataset.metadata['resourceName'], location=location),\n",
" annotation_schema_uri='gs://google-cloud-aiplatform/schema/dataset/annotation/image_segmentation_1.0.0.yaml',\n",
" args=[f'--experiment={experiment}',\n",
" f'--deeplab-preset={deeplab_preset}',\n",
" f'--img-width={img_width}',\n",
" f'--img-height={img_height}',\n",
" f'--num-epochs={num_epochs}', \n",
" f'--batch-size={batch_size}',\n",
" f'--optimizer={optimizer}',\n",
" f'--patience-epochs={patience_epochs}',\n",
" f'--loss-function={loss_function}',\n",
" f'--augmentation-factor={augmentation_factor}'],\n",
" restart_job_on_worker_restart=True,\n",
" service_account=service_account\n",
" )\n",
"\n",
" model.uri = f'https://{location}-aiplatform.googleapis.com/v1/{vertex_ai_model.versioned_resource_name}'\n",
" model.metadata = {\n",
" 'resourceName': vertex_ai_model.resource_name,\n",
" }"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Define the pipeline and steps:"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"@dsl.pipeline(\n",
" name=PIPELINE_DISPLAY_NAME,\n",
" description=\"A semantic segmentation with deeplabv3 training pipeline\",\n",
" pipeline_root=PIPELINE_ROOT,\n",
")\n",
"def pipeline(project_id: str, location: str, staging_bucket: str, trainer_bucket: str,\n",
" container_uri: str, deploy_model: bool, deploy_vpc: str,\n",
" experiment: str, dataset_id: str, service_account: str,\n",
" deeplab_preset: str, img_width: int, img_height: int,\n",
" batch_size: int, optimizer: str,\n",
" num_epochs: int, patience_epochs: int,\n",
" loss_function: str, augmentation_factor: int):\n",
"\n",
" dataset_op = GetVertexDatasetOp(\n",
" dataset_resource_name=f'projects/{project_id}/locations/{location}/datasets/{dataset_id}'\n",
" )\n",
" \n",
" train_model_op = model_train(\n",
" project_id=project_id,\n",
" location=location,\n",
" display_name=f'{PIPELINE_DISPLAY_NAME}-trainer',\n",
" trainer_bucket=trainer_bucket,\n",
" container_uri=container_uri,\n",
" experiment=experiment,\n",
" service_account=service_account,\n",
" dataset=dataset_op.outputs['dataset'],\n",
" deeplab_preset=deeplab_preset,\n",
" img_width=img_width,\n",
" img_height=img_height,\n",
" batch_size=batch_size,\n",
" optimizer=optimizer,\n",
" num_epochs=num_epochs,\n",
" patience_epochs=patience_epochs,\n",
" loss_function=loss_function,\n",
" augmentation_factor=augmentation_factor\n",
" )\n",
"\n",
" \n",
" with dsl.If(deploy_model == True, 'condition-should-model-deploy'):\n",
" create_endpoint_op = EndpointCreateOp(\n",
" project=project_id,\n",
" location=location,\n",
" network=deploy_vpc,\n",
" display_name = f'{PIPELINE_DISPLAY_NAME}-ep',\n",
" )\n",
" \n",
" model_deploy_yaml_op = ModelDeployOp(\n",
" model=train_model_op.outputs['model'],\n",
" endpoint=create_endpoint_op.outputs['endpoint'],\n",
" dedicated_resources_machine_type='n1-standard-8',\n",
" dedicated_resources_min_replica_count=1,\n",
" dedicated_resources_max_replica_count=1,\n",
" )\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Compile the pipeline to JSON:"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"compiler.Compiler().compile(\n",
" pipeline_func=pipeline, \n",
" package_path=\"build/training_pipeline.json\",\n",
" type_check=False\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Launch a Vertex AI Pipelines job:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"job = aiplatform.PipelineJob(\n",
" display_name=PIPELINE_DISPLAY_NAME,\n",
" template_path=\"build/training_pipeline.json\",\n",
" pipeline_root=PIPELINE_ROOT,\n",
" parameter_values={\n",
" 'project_id': PROJECT_ID, \n",
" 'location': LOCATION, \n",
" 'staging_bucket': STAGING_BUCKET,\n",
" 'trainer_bucket': TRAINING_BUCKET,\n",
" 'container_uri': CONTAINER_URI,\n",
" 'deploy_model': True,\n",
" 'deploy_vpc': VPC_NETWORK,\n",
" 'experiment': EXPERIMENT,\n",
" 'dataset_id': DATASET_ID,\n",
" 'service_account': SERVICE_ACCOUNT,\n",
" 'deeplab_preset': 'resnet50_v2_imagenet',\n",
" 'img_width': 512,\n",
" 'img_height': 512,\n",
" 'num_epochs': 234,\n",
" 'patience_epochs': 50,\n",
" 'optimizer': 'adam',\n",
" 'batch_size': 2,\n",
" 'loss_function': 'dice_focal',\n",
" 'augmentation_factor': 10\n",
" }\n",
")\n",
"\n",
"job.submit(experiment=EXPERIMENT)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}