mirror of
https://github.com/GoogleCloudPlatform/vertex-ai-samples.git
synced 2026-09-26 14:42:04 +00:00
feat: finish hpt components notebook (#239)
* feat: friday update * feat: friday update * feat: hpt notebook * feat: hpt notebook
This commit is contained in:
@@ -70,6 +70,35 @@
|
||||
"The dataset used for this tutorial is the [Horses or Humans](https://www.tensorflow.org/datasets/catalog/horses_or_humans) from [TensorFlow Datasets](https://www.tensorflow.org/datasets/catalog/overview). The trained model predicts whether an image is a horse or human being."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "objective:mlops,stage3,get_started_hpt_pipeline_components"
|
||||
},
|
||||
"source": [
|
||||
"### Objective\n",
|
||||
"\n",
|
||||
"In this tutorial, you learn how to use prebuilt `Google Cloud Pipeline Components` for `BigQuery ML`.\n",
|
||||
"\n",
|
||||
"This tutorial uses the following Google Cloud ML services:\n",
|
||||
"\n",
|
||||
"- `BigQuery ML`\n",
|
||||
"- `Google Cloud Pipeline Components`\n",
|
||||
"- `Vertex AI Dataset, Model and Endpoint` resources\n",
|
||||
"- `Vertex AI Prediction`\n",
|
||||
"\n",
|
||||
"The steps performed include:\n",
|
||||
"\n",
|
||||
"- Construct a pipeline for:\n",
|
||||
" - Training BigQuery ML model.\n",
|
||||
" - Evaluating the BigQuery ML model.\n",
|
||||
" - Exporting the BigQuery ML model.\n",
|
||||
" - Importing the BigQuery ML model to a Vertex AI model.\n",
|
||||
" - Deploy the Vertex AI model.\n",
|
||||
"- Execute a Vertex AI pipeline.\n",
|
||||
"- Make a prediction with the deployed Vertex AI model."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
@@ -418,6 +447,28 @@
|
||||
"from kfp.v2.dsl import component"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "import_tf"
|
||||
},
|
||||
"source": [
|
||||
"#### Import TensorFlow\n",
|
||||
"\n",
|
||||
"Import the TensorFlow package into your Python environment."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "import_tf"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import tensorflow as tf"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
@@ -487,6 +538,53 @@
|
||||
" DEPLOY_GPU, DEPLOY_NGPU = (None, None)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "container:prediction"
|
||||
},
|
||||
"source": [
|
||||
"#### Set pre-built containers\n",
|
||||
"\n",
|
||||
"Set the pre-built Docker container image for prediction.\n",
|
||||
"\n",
|
||||
"- Set the variable `TF` to the TensorFlow version of the container image. For example, `2-1` would be version 2.1, and `1-15` would be version 1.15. The following list shows some of the pre-built images available:\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"For the latest list, see [Pre-built containers for prediction](https://cloud.google.com/ai-platform-unified/docs/predictions/pre-built-containers)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "container:prediction"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"if os.getenv(\"IS_TESTING_TF\"):\n",
|
||||
" TF = os.getenv(\"IS_TESTING_TF\")\n",
|
||||
"else:\n",
|
||||
" TF = \"2.5\".replace(\".\", \"-\")\n",
|
||||
"\n",
|
||||
"if TF[0] == \"2\":\n",
|
||||
" if DEPLOY_GPU:\n",
|
||||
" DEPLOY_VERSION = \"tf2-gpu.{}\".format(TF)\n",
|
||||
" else:\n",
|
||||
" DEPLOY_VERSION = \"tf2-cpu.{}\".format(TF)\n",
|
||||
"else:\n",
|
||||
" if DEPLOY_GPU:\n",
|
||||
" DEPLOY_VERSION = \"tf-gpu.{}\".format(TF)\n",
|
||||
" else:\n",
|
||||
" DEPLOY_VERSION = \"tf-cpu.{}\".format(TF)\n",
|
||||
"\n",
|
||||
"DEPLOY_IMAGE = \"{}-docker.pkg.dev/vertex-ai/prediction/{}:latest\".format(\n",
|
||||
" REGION.split(\"-\")[0], DEPLOY_VERSION\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"print(\"Deployment:\", DEPLOY_IMAGE, DEPLOY_GPU)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
@@ -614,12 +712,19 @@
|
||||
" - `learning_rate`: Hyperparameter for learning rate.\n",
|
||||
" - `batch_size`: Hyperparameter for batch size.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"- Data preprocessing (`get_data()`)\n",
|
||||
" - Loads and preprocesses the dataset as a `tf.data.Dataset` generator.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"- Model architecture (`get_model()`):\n",
|
||||
" - Builds the corresponding model architecture.\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"- Training (`train_model()`):\n",
|
||||
" - Trains the model\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"- Model artifact saving\n",
|
||||
" - Saves the model artifacts where the Cloud Storage location is specified."
|
||||
]
|
||||
@@ -642,7 +747,6 @@
|
||||
"import hypertune\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def get_args():\n",
|
||||
" '''Parses args. Must include all hyperparameters you want to tune.'''\n",
|
||||
"\n",
|
||||
@@ -901,7 +1005,10 @@
|
||||
"- Create/Execute a hyperparameter tuning job\n",
|
||||
"- Get all trial results.\n",
|
||||
"- Get the best trial results.\n",
|
||||
"- BLAH"
|
||||
"- Determine if the best trial results exceed a threshold\n",
|
||||
" - Retrieve the hyperparameter values\n",
|
||||
" - Determine Cloud Storage location of the best model\n",
|
||||
" - Upload the best model as a Vertex AI Model resource."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -915,6 +1022,15 @@
|
||||
"PIPELINE_ROOT = \"{}/pipeline_root/custom_icn_tuning\".format(BUCKET_NAME)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"@component(packages_to_install=[\"google-cloud-aiplatform\"])\n",
|
||||
"def model_dir(best_trial: str) -> str:\n",
|
||||
" from google.cloud.aiplatform_v1.types import study\n",
|
||||
"\n",
|
||||
" trial_proto = study.Trial.from_json(best_trial)\n",
|
||||
" model_id = trial_proto.id\n",
|
||||
" return f\"{PIPELINE_ROOT}/{model_id}/model\"\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"@dsl.pipeline(\n",
|
||||
" name=\"hp-tuning\", description=\"Custom image classification hyperparameter tuning\"\n",
|
||||
")\n",
|
||||
@@ -923,9 +1039,12 @@
|
||||
" worker_pool_specs: list,\n",
|
||||
" study_spec_metrics: list,\n",
|
||||
" study_spec_parameters: list,\n",
|
||||
" threshold: float,\n",
|
||||
" deploy_image: str,\n",
|
||||
" max_trial_count: int = 5,\n",
|
||||
" parallel_trial_count: int = 1,\n",
|
||||
" base_output_directory: str = PIPELINE_ROOT,\n",
|
||||
" labels: dict = {},\n",
|
||||
" project: str = PROJECT_ID,\n",
|
||||
" region: str = REGION,\n",
|
||||
"):\n",
|
||||
@@ -955,8 +1074,29 @@
|
||||
" )\n",
|
||||
"\n",
|
||||
" threshold_op = hyperparameter_tuning_job.IsMetricBeyondThresholdOp(\n",
|
||||
" trial=best_trial_op.output, study_spec_metrics=study_spec_metrics, threshold=0.7\n",
|
||||
" )"
|
||||
" trial=best_trial_op.output,\n",
|
||||
" study_spec_metrics=study_spec_metrics,\n",
|
||||
" threshold=threshold,\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" with dsl.Condition(\n",
|
||||
" threshold_op.output == \"true\",\n",
|
||||
" name=\"deploy_decision\",\n",
|
||||
" ):\n",
|
||||
" best_hyperparameters_op = hyperparameter_tuning_job.GetHyperparametersOp(\n",
|
||||
" trial=best_trial_op.output\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" model_dir_op = model_dir(best_trial_op.output)\n",
|
||||
"\n",
|
||||
" model_upload_op = gcc_aip.ModelUploadOp(\n",
|
||||
" display_name=display_name,\n",
|
||||
" artifact_uri=model_dir_op.output,\n",
|
||||
" serving_container_image_uri=deploy_image,\n",
|
||||
" labels=labels,\n",
|
||||
" project=project,\n",
|
||||
" location=region,\n",
|
||||
" )"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -979,7 +1119,7 @@
|
||||
"\n",
|
||||
"**Metric specification**\n",
|
||||
"\n",
|
||||
"BLAH\n"
|
||||
"This specification describes the metric(s) to be evaluated in the study and wether to minize or maximize the metric."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1043,9 +1183,9 @@
|
||||
"\n",
|
||||
"- `display_name`: A human readable name for the pipeline job.\n",
|
||||
"- `import_file`: The Cloud Storage location to the dataset.\n",
|
||||
"- `worker_pool_specs`: BLAH\n",
|
||||
"- `study_spec_metrics`:\n",
|
||||
"- `study_spec_parameters`:"
|
||||
"- `worker_pool_specs`: The the machine and container, and auto-scaling requirements, as well as command line arguments.\n",
|
||||
"- `study_spec_metrics`: The metrics to optimize in the study trials.\n",
|
||||
"- `study_spec_parameters`: The parameters to tune."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1069,13 +1209,15 @@
|
||||
" \"worker_pool_specs\": worker_pool_specs,\n",
|
||||
" \"study_spec_metrics\": metric_spec,\n",
|
||||
" \"study_spec_parameters\": parameter_spec,\n",
|
||||
" \"threshold\": 0.7,\n",
|
||||
" \"deploy_image\": DEPLOY_IMAGE,\n",
|
||||
" },\n",
|
||||
" enable_caching=False,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"pipeline.run()\n",
|
||||
"\n",
|
||||
"! rm -f hp_tune_pipeline_job.json custom custom.tar.gz"
|
||||
"! rm -rf hp_tune_pipeline_job.json custom custom.tar.gz"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1095,8 +1237,6 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import tensorflow as tf\n",
|
||||
"\n",
|
||||
"PROJECT_NUMBER = pipeline.gca_resource.name.split(\"/\")[1]\n",
|
||||
"print(PROJECT_NUMBER)\n",
|
||||
"\n",
|
||||
@@ -1160,24 +1300,21 @@
|
||||
"learning_rate = parameters[1][\"value\"]\n",
|
||||
"print(\"LR\", learning_rate)\n",
|
||||
"MODEL_DIR = f\"{PIPELINE_ROOT}/{model_id}/model\"\n",
|
||||
"\n",
|
||||
"print(\"ismetricbeyondthresholdop\")\n",
|
||||
"artifacts = print_pipeline_output(pipeline, \"ismetricbeyondthresholdop\")\n",
|
||||
"print(\"\\n\\n\")\n",
|
||||
"print(\"deploy-decision\")\n",
|
||||
"artifacts = print_pipeline_output(pipeline, \"deploy-decision\")\n",
|
||||
"print(\"\\n\\n\")\n",
|
||||
"print(\"model-dir\")\n",
|
||||
"artifacts = print_pipeline_output(pipeline, \"model-dir\")\n",
|
||||
"print(\"\\n\\n\")\n",
|
||||
"print(\"model-upload\")\n",
|
||||
"artifacts = print_pipeline_output(pipeline, \"model-upload\")\n",
|
||||
"print(\"\\n\\n\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "61d3e8fd07b1"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"MODEL_DIR = f\"{PIPELINE_ROOT}/{model_id}/model\"\n",
|
||||
"print(MODEL_DIR)\n",
|
||||
"!gsutil ls {MODEL_DIR}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
|
||||
Reference in New Issue
Block a user