Compare commits

..
Author SHA1 Message Date
ivanmkc d7591ed256 Improved git diff logic 2022-06-22 20:53:03 -04:00
Andrew FerlitschandGitHub 41fc83ad03 upgrade: current notebook standards (#655)
* update: current standards

* update: current standards

* Update sdk_custom_image_classification_batch_explain.ipynb

* fix: bucket nit
2022-06-22 16:59:39 -07:00
Andrew FerlitschandGitHub 9928276dc3 upgrade: current notebook standard (#653)
* upgrade: current notebook standard

* upgrade: current notebook standard

* Update sdk_automl_tabular_binary_classification_batch_explain.ipynb

* fix: bucket nit
2022-06-22 16:58:42 -07:00
Andrew FerlitschandGitHub 6607f93f5e upgrade: updated curated notebook to current standards (#648)
* upgrade: current standards

* upgrade: current standards

* fix: missed nits

* fix: missed nits

* Update automl-text-classification.ipynb

* Update automl-text-classification.ipynb

* rm extra comma
2022-06-22 16:56:38 -07:00
Andrew FerlitschandGitHub dcc0cfe06e upgrade: current notebook standards (#686)
* upgrade: notebook standard

* upgrade: notebook standard
2022-06-22 16:50:13 -07:00
Andrew FerlitschandGitHub 00d5c5c4bf Update README.md 2022-06-22 15:34:01 -07:00
Andrew FerlitschandGitHub f8152dde19 Update README.md 2022-06-22 15:24:14 -07:00
Andrew FerlitschandGitHub 0586e04c21 upgrade: current notebook standards (#656)
* update: current standards

* update: current standards

* fix: bucket nit
2022-06-22 15:06:01 -07:00
Andrew FerlitschandGitHub 3de18a7fab upgrade: current notebook standards (#674)
* upgrade: current notebook standard

* upgrade: current notebook standard

* fix: bucket

* fix: bucket
2022-06-22 15:03:54 -07:00
Andrew FerlitschandGitHub dc0bf24cc5 Update README.md 2022-06-22 14:58:28 -07:00
Andrew FerlitschandGitHub 8ce4c3070c Update README.md 2022-06-22 14:53:54 -07:00
Andrew FerlitschandGitHub 13c3acb976 Update README.md 2022-06-22 14:50:45 -07:00
Andrew FerlitschandGitHub c1150ff584 Update README.md 2022-06-22 14:39:06 -07:00
Andrew FerlitschandGitHub 4f11d70f7e Update README.md 2022-06-22 14:34:02 -07:00
Andrew FerlitschandGitHub 2bf9a2b317 Update README.md 2022-06-22 14:24:53 -07:00
Andrew FerlitschandGitHub b1e0ad0c4f Update README.md 2022-06-22 14:15:22 -07:00
Andrew FerlitschandGitHub 5624f92f02 Update README.md 2022-06-22 14:12:18 -07:00
Andrew FerlitschandGitHub 1335032954 Update README.md 2022-06-22 14:05:24 -07:00
Andrew FerlitschandGitHub 0b13c66e07 Update README.md 2022-06-22 14:01:55 -07:00
Andrew FerlitschandGitHub ef25b54926 update: add index (#684) 2022-06-22 13:58:06 -07:00
Andrew FerlitschandGitHub 064dbfeefa fix: bucket nit 2022-06-22 12:34:50 -07:00
Andrew FerlitschandGitHub 044c69e7a5 upgrade: current notebook standards (#654)
* update: current standards

* update: current standards
2022-06-22 12:33:26 -07:00
Andrew FerlitschandGitHub 32a46e7471 upgrade: current standards (#649)
* upgrade: current standards

* upgrade: current standards

* fix: missed nits

* fix: missed nits
2022-06-22 12:30:16 -07:00
Andrew FerlitschandGitHub b52d59822d upgrade: current notebook standards (#657)
* update: current standards

* update: current standards

* update: current standards

* update: current standards

* Update sdk_custom_tabular_regression_batch_explain.ipynb

* fix: indent issue

* fix: indent issue

* fix: bucket

* fix: bucket

* fix: image

* fix: image

* fix: cleanup

* fix: cleanup

* fix: cleanup
2022-06-22 11:47:17 -07:00
Andrew FerlitschandGitHub 529995ecde upgrade: current notebook standard (#671)
* upgrade: current notebook standard

* upgrade: current notebook standard

* Update google_cloud_pipeline_components_automl_tabular.ipynb

* fix: bucket

* fix: bucket
2022-06-22 11:35:38 -07:00
Andrew FerlitschandGitHub 0726328c92 upgrade: current notebook standard (#667)
* upgrade: current notebook standard

* upgrade: current notebook standard

* Update model_monitoring.ipynb

* Update model_monitoring.ipynb
2022-06-22 11:21:32 -07:00
Andrew FerlitschandGitHub 090e83c286 fix: more broken links 2022-06-22 11:19:17 -07:00
Andrew FerlitschandGitHub c3802626b9 fix: links issue 672 2022-06-22 11:18:08 -07:00
Andrew FerlitschandGitHub 9570c2477f upgrade: current notebook standards (#682)
* upgrade: current notebook standards

* upgrade: current notebook standards
2022-06-22 11:09:27 -07:00
Andrew FerlitschandGitHub 2b1a898b2a upgrade: current notebook standards (#680)
* upgrade: current notebook standards

* upgrade: current notebook standards
2022-06-22 11:09:04 -07:00
Andrew FerlitschandGitHub f2a42aa66e upgrade: current notebook standard (#679)
* upgrade: notebook standard

* upgrade: notebook standard

* fix: bucket

* fix: bucket
2022-06-22 11:08:37 -07:00
Andrew FerlitschandGitHub 30a03f1fd1 upgrade: current notebook standards (#678)
* upgrade: notebook standard

* upgrade: notebook standard

* Update google_cloud_pipeline_components_model_train_upload_deploy.ipynb

* fix: bucket nits

* fix: bucket
2022-06-22 11:08:08 -07:00
Andrew FerlitschandGitHub ca25448f59 upgrade: current notebook standard (#673)
* upgrade: current notebook standard

* upgrade: current notebook standard

* fix: bucket nit

* fix: bucket nit
2022-06-22 11:07:37 -07:00
Andrew FerlitschandGitHub 8f922710b0 upgrade: current notebook standard (#670)
* upgrade: current notebook standard

* upgrade: current notebook standard

* Update google_cloud_pipeline_components_automl_images.ipynb

* fix: bucket

* fix: bucket
2022-06-22 11:06:31 -07:00
Andrew FerlitschandGitHub 9509c6ab9d upgrade: current notebook standard (#669)
* upgrade: current notebook standard

* upgrade: current notebook standard

* Update lightweight_functions_component_io_kfp.ipynb

* fix: nits

* fix: nits

* fix: nits

* fix: nits
2022-06-22 11:05:47 -07:00
Andrew FerlitschandGitHub ccba176979 upgrade: current notebook standard (#665)
* upgrade: notebook standard

* upgrade: notebook standard

* Update sdk-feature-store.ipynb

* Update sdk-feature-store.ipynb

* Update sdk-feature-store.ipynb

* fix: aip reference

* fix: nits

* fix: nits
2022-06-22 11:05:07 -07:00
Andrew FerlitschandGitHub 1b8f383897 upgrade: current notebook standard (#663)
* update: current standards

* update: current standards

* fix: bucket

* fix: bucket
2022-06-22 09:36:49 -07:00
Andrew FerlitschandGitHub e5cd9e86d2 upgrade: notebook to latest standard (#651)
* fix: missed nits

* fix: missed nits
2022-06-22 08:56:10 -07:00
Andrew FerlitschandGitHub a7e86a4f26 upgrade: current notebook standard (#668)
* upgrade: current notebook standard

* upgrade: current notebook standard

* Update sdk-metric-parameter-tracking-for-locally-trained-models.ipynb
2022-06-21 20:23:18 -07:00
Ivan CheungandGitHub 8f0c73b32c Fixed cleanup scripts (#676) 2022-06-21 20:13:37 -07:00
Andrew FerlitschandGitHub c7d7b48a91 upgrade: notebook to current standards (#650)
* upgrade: current standards

* upgrade: current standards
2022-06-21 18:25:25 -07:00
Karl WeinmeisterandGitHub 583eb90f07 fix: CONTRIBUTING.md did not have nbfmt as final step 2022-06-20 13:56:49 -05:00
c3a9249c0c Workaround tensorboard/GCS issue for Cloud Shell (#386)
* Workaround tensorboard/GCS issue for Cloud Shell

Without `--load_fast=false` there will be `401 Unauthorized` for GCS log loads. 
See https://github.com/tensorflow/tensorboard/issues/4784#issuecomment-868945650

* PR #386: Fix missing import

`import json` was missing.

Co-authored-by: Karl Weinmeister <11586922+kweinmeister@users.noreply.github.com>
Co-authored-by: Andrew Ferlitsch <aferlitsch@google.com>
2022-06-19 10:07:09 -05:00
Karl WeinmeisterandGitHub 81b70e779c ci: Remove protobuf from requirements.txt 2022-06-19 09:38:45 -05:00
Karl WeinmeisterandGitHub 085713a818 ci: Add protobuf to requirements.txt 2022-06-18 17:20:45 -05:00
Karl WeinmeisterandGitHub 5af7c851dc Fix: update typo in GAPIC Feature Store notebook 2022-06-18 17:10:49 -05:00
691312d467 Add import feature analysis config sample code into gapic-feature-sto… (#548)
* Add import feature analysis config sample code into gapic-feature-store.ipynb

* Fixing linter for gapic-feature-store.ipynb

Co-authored-by: Ivan Cheung <ivans.mailbox@gmail.com>
Co-authored-by: Andrew Ferlitsch <aferlitsch@google.com>
Co-authored-by: Karl Weinmeister <11586922+kweinmeister@users.noreply.github.com>
2022-06-18 17:09:37 -05:00
650c256c13 Fixes link to colab (#627)
Co-authored-by: Karl Weinmeister <11586922+kweinmeister@users.noreply.github.com>
2022-06-18 16:26:36 -05:00
9b00c4380b chore(deps): update actions/setup-python action to v4 (#622)
Co-authored-by: Karl Weinmeister <11586922+kweinmeister@users.noreply.github.com>
2022-06-18 16:21:49 -05:00
Karl WeinmeisterandGitHub 4851457e93 ci: Add Python version to support setup-python v4 2022-06-18 16:19:09 -05:00
dependabot[bot]GitHubdependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>Karl Weinmeister
1c54878fab build(deps): bump tensorflow (#595)
Bumps [tensorflow](https://github.com/tensorflow/tensorflow) from 2.5.3 to 2.7.2.
- [Release notes](https://github.com/tensorflow/tensorflow/releases)
- [Changelog](https://github.com/tensorflow/tensorflow/blob/master/RELEASE.md)
- [Commits](https://github.com/tensorflow/tensorflow/compare/v2.5.3...v2.7.2)

---
updated-dependencies:
- dependency-name: tensorflow
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Karl Weinmeister <11586922+kweinmeister@users.noreply.github.com>
2022-06-18 16:08:07 -05:00
dependabot[bot]GitHubdependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
c139b84454 build(deps): bump tensorflow (#593)
Bumps [tensorflow](https://github.com/tensorflow/tensorflow) from 2.5.3 to 2.7.2.
- [Release notes](https://github.com/tensorflow/tensorflow/releases)
- [Changelog](https://github.com/tensorflow/tensorflow/blob/master/RELEASE.md)
- [Commits](https://github.com/tensorflow/tensorflow/compare/v2.5.3...v2.7.2)

---
updated-dependencies:
- dependency-name: tensorflow
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-06-18 16:02:53 -05:00
15c38b4ca6 chore(deps): update dependency pyupgrade to v2.34.0 (#457)
Co-authored-by: Karl Weinmeister <11586922+kweinmeister@users.noreply.github.com>
2022-06-18 15:59:54 -05:00
Ivan CheungandGitHub 271c949a71 Update README.md (#642) 2022-06-17 14:59:05 -07:00
Andrew FerlitschandGitHub 09b5401434 update: fine-tuning notebook (#641)
* feat: add example of import from dataframe

* feat: add example of import from dataframe

* update: change in required perms

* update: change in required perms

* review: updates from review

* review: updates from review

* updates: fine tuning
2022-06-17 13:41:10 -07:00
dad76547f0 inardini - mobile gaming feature store blog review (#635)
* review content and image

* linter test passed

* andy review fixes

* linter test passed

Co-authored-by: Andrew Ferlitsch <aferlitsch@google.com>
2022-06-17 10:39:18 -07:00
Andrew FerlitschandGitHub c14a110c81 Update get_started_with_custom_training_pipeline_components.ipynb 2022-06-16 14:24:08 -07:00
Andrew FerlitschandGitHub d1e16546cc Update get_started_with_bqml_pipeline_components.ipynb 2022-06-16 14:23:29 -07:00
Andrew FerlitschandGitHub f91bc3d0c9 Update get_started_with_bq_tfdv_pipeline_components.ipynb 2022-06-16 14:22:56 -07:00
Andrew FerlitschandGitHub 5168808b6c fix: colab link 2022-06-16 14:22:23 -07:00
Andrew FerlitschandGitHub 501cca7b5e fix: colab link 2022-06-16 14:21:31 -07:00
Andrew FerlitschandGitHub f19d40d858 Update mlops_experimentation.ipynb 2022-06-16 13:49:17 -07:00
Andrew FerlitschandGitHub 5a721ce01d Update get_started_with_visionapi_and_automl.ipynb 2022-06-16 13:48:31 -07:00
Andrew FerlitschandGitHub 66421fb4d8 fix: colab link 2022-06-16 13:47:47 -07:00
Andrew FerlitschandGitHub 3658ee8c88 Update get_started_with_tabnet.ipynb 2022-06-16 13:46:10 -07:00
Andrew FerlitschandGitHub afaab4bb02 Update get_started_with_cmek_training.ipynb 2022-06-16 13:45:08 -07:00
Andrew FerlitschandGitHub c1e004bdff fix: colab link 2022-06-16 13:44:22 -07:00
Andrew FerlitschandGitHub 874e5a3ef5 Update get_started_vertex_training_xgboost.ipynb 2022-06-16 13:43:11 -07:00
Andrew FerlitschandGitHub 51529f370c fix: broken table 2022-06-16 13:41:01 -07:00
Andrew FerlitschandGitHub 5ddf98866c Update get_started_vertex_training_sklearn.ipynb 2022-06-16 13:40:22 -07:00
Andrew FerlitschandGitHub fbb6830876 fix: colab link 2022-06-16 13:30:01 -07:00
Andrew FerlitschandGitHub ee1bb281da fix: colab link 2022-06-16 13:28:16 -07:00
Andrew FerlitschandGitHub da2f7e88fe fix: update location of public dataset bucket 2022-06-16 12:43:15 -07:00
Andrew FerlitschandGitHub 3fb28e353f fix: remove internal link 2022-06-16 12:40:02 -07:00
Andrew FerlitschandGitHub c8e7f44f0a fix: updates from review (#640)
* feat: add example of import from dataframe

* feat: add example of import from dataframe

* update: change in required perms

* update: change in required perms

* review: updates from review

* review: updates from review
2022-06-16 12:36:28 -07:00
3d9049aeeb remove old hard-coded instances for prediction (#633)
Co-authored-by: Andrew Ferlitsch <aferlitsch@google.com>
2022-06-16 11:50:01 -07:00
Andrew FerlitschandGitHub 7b8726af24 fix: perms for using MR in BQML (#634)
* feat: add example of import from dataframe

* feat: add example of import from dataframe

* update: change in required perms

* update: change in required perms
2022-06-15 14:55:17 -07:00
Andrew FerlitschandGitHub 0443b05360 feat: add create tabular dataset from dataframe example (#632)
* feat: add example of import from dataframe

* feat: add example of import from dataframe
2022-06-14 11:44:30 -07:00
Ivan CheungandGitHub e422dbdef2 Added official version of tabular regression batch bq (#331)
* Added official version of tabular regression batch bq

* Ran linter

* Fixed cleanup

* Additional cleanup

* Added working version

* Refactored and made work

* Ran linter and cleaned up

* Renamed aip to aiplatform

* Replaced online with batch

* Renamed notebook

* Ran linter and cleaned up

* Fixed bug

* Fixed SQL by adding backticks

* Install google-cloud-bigquery[all]

* Refactored datasets

* Ran linter

* Removed GCS cells

* Fixed import file

* Fixed SQL issues and added cleanup of training dataset

* Fixed hardcorded table

* Fixed brand names

* Fixed header

* Fixed results table

* Addressed tech writing review comments

* Ran linter
2022-06-14 14:14:06 -04:00
Andrew FerlitschandGitHub ec5fc0b1c8 Update get_started_vertex_training_r.ipynb 2022-06-14 10:05:36 -07:00
Andrew FerlitschandGitHub c52e3f20ba Update get_started_vertex_training_pytorch.ipynb 2022-06-14 10:04:45 -07:00
Andrew FerlitschandGitHub e367dceceb Update get_started_vertex_training_lightgbm.ipynb 2022-06-14 10:04:11 -07:00
Andrew FerlitschandGitHub 19b8666808 Update get_started_vertex_training.ipynb 2022-06-14 10:03:24 -07:00
Andrew FerlitschandGitHub a2df0e9fca Update get_started_vertex_tensorboard.ipynb 2022-06-14 10:01:52 -07:00
Andrew FerlitschandGitHub 73b094550e Update get_started_vertex_feature_store.ipynb 2022-06-14 09:59:24 -07:00
Andrew FerlitschandGitHub d05ae109e1 Update get_started_vertex_experiments.ipynb 2022-06-14 09:57:55 -07:00
Andrew FerlitschandGitHub fdb25791d5 Update get_started_vertex_distributed_training.ipynb 2022-06-14 09:57:19 -07:00
Andrew FerlitschandGitHub 5a88492498 Update get_started_bqml_training.ipynb 2022-06-14 09:56:34 -07:00
Andrew FerlitschandGitHub f19a12b829 Update get_started_automl_training.ipynb 2022-06-14 09:55:53 -07:00
Andrew FerlitschandGitHub 3ed0ea73f4 Update get_started_bq_datasets.ipynb 2022-06-14 09:08:47 -07:00
Andrew FerlitschandGitHub b98fd24a72 Update get_started_bq_datasets.ipynb 2022-06-13 21:29:41 -07:00
Andrew FerlitschandGitHub eb4e9a0f91 Update get_started_vertex_datasets.ipynb 2022-06-13 21:27:16 -07:00
Andrew FerlitschandGitHub ec7c136b0a Update get_started_vertex_datasets.ipynb 2022-06-13 21:26:24 -07:00
Andrew FerlitschandGitHub ee7e43cc1a Update mlops_data_management.ipynb 2022-06-13 20:24:57 -07:00
Andrew FerlitschandGitHub ea9f5f3c5c Update get_started_with_data_labeling.ipynb 2022-06-13 20:24:22 -07:00
Andrew FerlitschandGitHub bd44798412 Update get_started_vertex_datasets.ipynb 2022-06-13 20:23:37 -07:00
Andrew FerlitschandGitHub 2af0cc0f80 fix: test for local execution 2022-06-13 20:22:57 -07:00
Andrew FerlitschandGitHub 5c0fa14d2d Update get_started_bq_datasets.ipynb 2022-06-13 20:21:44 -07:00
Andrew FerlitschandGitHub 7cc50d3203 Update README.md 2022-06-13 13:58:20 -07:00
Andrew FerlitschandGitHub 9c7da13177 Update gapic-vizier-multi-objective-optimization.ipynb 2022-06-13 08:39:33 -07:00
Andrew FerlitschandGitHub 45e0645f0b Update gapic-vizier-multi-objective-optimization.ipynb 2022-06-13 08:38:55 -07:00
Andrew FerlitschandGitHub cb884cc74a Update gapic-vizier-multi-objective-optimization.ipynb 2022-06-13 08:38:22 -07:00
Ivan CheungandGitHub d3a6475580 Fixed mistake in batch prediction request section (#617)
* Fixed mistake in batch prediction request section

* Fixed linter requirements
2022-06-10 17:13:45 -04:00
4e7061b2db added MLPerf benchmark reference and updated Criteo sample to use GRPC for stock containers (#630)
* added MLPerf benchmark reference and updated Criteo sample to use GRPC for stock containers

* addressed feedback for BERT sample and did similar changes to Criteo sample

Co-authored-by: Andrew Ferlitsch <aferlitsch@google.com>
2022-06-10 12:06:25 -07:00
Ivan CheungandGitHub 0250879f65 Added notebook substitution for common case of 1 notebook (#625) 2022-06-10 14:44:43 -04:00
Andrew FerlitschandGitHub 5cee23ae68 Update get_started_with_autoscaling.ipynb 2022-06-09 15:02:28 -07:00
Andrew FerlitschandGitHub d518558b3d Update README.md 2022-06-09 15:01:55 -07:00
Andrew FerlitschandGitHub 5c085c843f feat: notebook on autoscaling (#629)
* feat: XAI + custom server

* feat: add DIY MLMD for AutoML

* feat: add DIY MLMD for AutoML

* fix: replace BLAH

* fix: replace BLAH

* update: AutoML + MLMD

* update: AutoML + MLMD

* co-hosting

* co-hosting

* feat: new R notebook

* feat: new R notebook

* feat: airflow+vertex

* feat: airflow+vertex

* feat: notebook on autoscaling

* feat: notebook on autoscaling
2022-06-09 14:57:41 -07:00
Michael HuandGitHub 936b434ac4 fix: typo in links in bqml arima notebook (#616)
Notebook used as template had incorrect link format. Apply the same fixes as #514 to the arima notebook.
2022-06-09 17:52:12 -04:00
Michael HuandGitHub 43059c9fd9 fix: pin protobuf version to 3.19.0 (#621) 2022-06-09 12:49:45 -07:00
Andrew FerlitschandGitHub 19f72d426d fix: typos 2022-06-08 11:59:37 -07:00
Andrew FerlitschandGitHub 14ecaf3023 Update README.md 2022-06-08 11:58:18 -07:00
Andrew FerlitschandGitHub 80c93a9b6d feat: airflow with vertex pipelines (#624)
* feat: XAI + custom server

* feat: add DIY MLMD for AutoML

* feat: add DIY MLMD for AutoML

* fix: replace BLAH

* fix: replace BLAH

* update: AutoML + MLMD

* update: AutoML + MLMD

* co-hosting

* co-hosting

* feat: new R notebook

* feat: new R notebook

* feat: airflow+vertex

* feat: airflow+vertex
2022-06-08 11:55:11 -07:00
Andrew FerlitschandGitHub 06a1b4dc57 Update README.md 2022-06-07 09:48:16 -07:00
Andrew FerlitschandGitHub 4e779eedf1 Update README.md 2022-06-07 09:45:10 -07:00
Andrew FerlitschandGitHub afb341f5fd feat: new R notebook (#618)
* feat: XAI + custom server

* feat: add DIY MLMD for AutoML

* feat: add DIY MLMD for AutoML

* fix: replace BLAH

* fix: replace BLAH

* update: AutoML + MLMD

* update: AutoML + MLMD

* co-hosting

* co-hosting

* feat: new R notebook

* feat: new R notebook
2022-06-07 09:39:44 -07:00
e34b0fa115 chore(deps): pin dependency protobuf to v (#606)
Co-authored-by: Andrew Ferlitsch <aferlitsch@google.com>
2022-06-06 18:13:56 -07:00
dependabot[bot]GitHubdependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
6fa0345f25 build(deps): bump tensorflow (#596)
Bumps [tensorflow](https://github.com/tensorflow/tensorflow) from 2.5.3 to 2.7.2.
- [Release notes](https://github.com/tensorflow/tensorflow/releases)
- [Changelog](https://github.com/tensorflow/tensorflow/blob/master/RELEASE.md)
- [Commits](https://github.com/tensorflow/tensorflow/compare/v2.5.3...v2.7.2)

---
updated-dependencies:
- dependency-name: tensorflow
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-06-06 18:09:52 -07:00
dependabot[bot]GitHubdependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>Andrew Ferlitsch
dd43ae6639 build(deps): bump tensorflow (#594)
Bumps [tensorflow](https://github.com/tensorflow/tensorflow) from 2.5.3 to 2.7.2.
- [Release notes](https://github.com/tensorflow/tensorflow/releases)
- [Changelog](https://github.com/tensorflow/tensorflow/blob/master/RELEASE.md)
- [Commits](https://github.com/tensorflow/tensorflow/compare/v2.5.3...v2.7.2)

---
updated-dependencies:
- dependency-name: tensorflow
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Andrew Ferlitsch <aferlitsch@google.com>
2022-06-06 18:08:37 -07:00
dependabot[bot]GitHubdependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>Ivan CheungAndrew Ferlitsch
840385e0a7 build(deps): bump tensorflow (#592)
Bumps [tensorflow](https://github.com/tensorflow/tensorflow) from 2.5.3 to 2.7.2.
- [Release notes](https://github.com/tensorflow/tensorflow/releases)
- [Changelog](https://github.com/tensorflow/tensorflow/blob/master/RELEASE.md)
- [Commits](https://github.com/tensorflow/tensorflow/compare/v2.5.3...v2.7.2)

---
updated-dependencies:
- dependency-name: tensorflow
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Ivan Cheung <ivans.mailbox@gmail.com>
Co-authored-by: Andrew Ferlitsch <aferlitsch@google.com>
2022-06-06 18:07:48 -07:00
Michael HuandGitHub 9daaf51a1f add bqml arima and vertex forecasting comparison notebook (#581)
Adds a notebook that demonstrates how to compare a Vertex Forecasting model against a BQML ARIMA+ model trained using a first-party GCPC pipeline.
2022-06-06 20:48:30 -04:00
Andrew FerlitschandGitHub 9b2511e54e Update README.md 2022-06-06 13:38:55 -07:00
Andrew FerlitschandGitHub b6674e6540 feat: notebook for co-hosting models (#615)
* feat: XAI + custom server

* feat: add DIY MLMD for AutoML

* feat: add DIY MLMD for AutoML

* fix: replace BLAH

* fix: replace BLAH

* update: AutoML + MLMD

* update: AutoML + MLMD

* co-hosting

* co-hosting
2022-06-06 13:35:47 -07:00
Ivan CheungandGitHub a109cb6d44 fix: Added explanations output to forecasting notebook (#613)
* Added explanations output to forecasting notebook

* Simplified and added XAI

* Fix conflicts

* Ran linter

* Fixed batch prediction request explanation
2022-06-06 10:00:07 -07:00
Ivan CheungandGitHub f730d6b9de Added ability to test a single notebook (#608)
* Added ability to test a single notebook

* Added output_url to table

* Removed ML Ops notebooks
2022-06-06 10:30:31 -04:00
0947f2792d Made some minor changes to Sdk big query custom container training (#522)
* minor changes done

* ran linter test

Co-authored-by: Andrew Ferlitsch <aferlitsch@google.com>
2022-06-05 12:58:45 -07:00
dbf19acde4 Adds the updated telecom-subscriber-churn-prediction notebook to official and removes from community (#506)
* updates and adds the telecom-subscriber-churn-prediction notebook to official and removes from the community

* ran linter test

Co-authored-by: Andrew Ferlitsch <aferlitsch@google.com>
2022-06-05 12:53:21 -07:00
1aaa833153 Adds manual-scaling config and explanation to the Automl-forecasting-batch notebook in official folder (#514)
* adds manual-scaling config and explanation to the notebook

* ran linter test after installing linter requirement updates

Co-authored-by: Andrew Ferlitsch <aferlitsch@google.com>
2022-06-05 12:50:23 -07:00
c73d995680 Made minor changes to sdk_automl_image_object_detection_batch.ipynb file (#513)
* modified notebook

* ran linter test

Co-authored-by: Andrew Ferlitsch <aferlitsch@google.com>
2022-06-05 12:45:55 -07:00
3fe5028724 Malansari automl vision api notebook update (#612)
* Delete revised version

* Copy notebook from /notebooks/official

* Renamed base notebook

* Added first version by mansari@

* Updated to revised version by andrewferlitsch@

* Added author / reviewer information

Added sample files

* Updated CODEOWNERS

* Fixed links for opening the notebook in Colab/Github/Vertex

Removed installation of and references to pandas

Fixed gcs_annotation_file_name string reference

* Fixed the links for opening notebook (again!)

* Added attribution and references

* Removed references as covered at top

* Updated installation commands to match

* Updated Vertex AI region name to be more clear

* Added db-types dependency for pandas operations
that are now failing

* Minor edits

* Combined package installation and
added a note to ignore the errors

* Minor edit to message

* Added special thanks to andrewferlitsch@

* Updated andrewferlitsch@ GithHub profile link

* Updated sample files URLs to absolute URLs

* Removed empty code block

* Added additional attribution (and the one that did not make it into previous commit!)

* Fixed multi-package import formatting
Switched to pandas instead of db-dtypes

* Fixed isort issue

* Removed unnecessary pandas import

* Formatted the notebook with nbfmt

* Additional notebook formatting

* Updated link to open in Vertex AI Workbench
to point to raw .ipynb file

* Fixed lint issues

* Formatting changes

Added additional APIs to be enabled

* Fixed sample dataset link to point to public version

* Fixed linting issues

Co-authored-by: Andrew Ferlitsch <aferlitsch@google.com>
2022-06-04 09:14:23 -07:00
Andrew FerlitschandGitHub 44dd24f910 Update README.md 2022-06-02 15:57:04 -07:00
Andrew FerlitschandGitHub c20a9c4e63 update: AutoML + MLMD (#605)
* feat: XAI + custom server

* feat: add DIY MLMD for AutoML

* feat: add DIY MLMD for AutoML

* fix: replace BLAH

* fix: replace BLAH

* update: AutoML + MLMD

* update: AutoML + MLMD
2022-06-02 15:50:05 -07:00
Ivan CheungandGitHub edffdf34b7 Pin protobuf version to avoid broken dependencies. 2022-06-02 17:25:28 -04:00
340c24c5b4 Added custom container explainability notebook (#564)
* Commit for lint

* Commit after name change

* Commit of notebook and CODEOWNERS

Added custom container with xai notebook, and explainable_ai folder in the community folder

* Removed extra copy of file

* Remove extra file

* Updated per review from DPE

* Lint test updates

* linter ran

Co-authored-by: Andrew Ferlitsch <aferlitsch@google.com>
2022-06-02 12:28:15 -07:00
Andrew FerlitschandGitHub 11f20f3f08 fix: replace BLAH (#600)
* feat: XAI + custom server

* feat: add DIY MLMD for AutoML

* feat: add DIY MLMD for AutoML

* fix: replace BLAH

* fix: replace BLAH
2022-06-01 11:19:02 -07:00
Andrew FerlitschandGitHub 199b330aa5 Update get_started_automl_mlmd.ipynb 2022-05-31 18:37:05 -07:00
Andrew FerlitschandGitHub 5143db1024 feat: Add DIY MLMD with AutoML (#598)
* feat: XAI + custom server

* feat: add DIY MLMD for AutoML

* feat: add DIY MLMD for AutoML
2022-05-31 18:35:23 -07:00
Andrew FerlitschandGitHub 2f0bd3de15 fix: correct reference to service 2022-05-31 13:50:51 -07:00
Andrew FerlitschandGitHub 4d6c541967 feat: XAI + custom server (#597) 2022-05-31 13:31:20 -07:00
Andrew FerlitschandGitHub 5228a5c978 Update README.md 2022-05-31 12:47:16 -07:00
Andrew FerlitschandGitHub c6118bd17d feat: start stage 7 (#591)
* fix: enable apis

* fix: enable apis

* fix: enable apis

* fix: enable apis

* fix: enable apis

* fix: enable apis

* fix: enable apis

* fix: enable apis

* fix: enable apis

* fix: enable apis

* fix: enable apis

* fix: enable apis

* feat: swivel and matching engine

* feat: swivel and matching engine

* feat: LightGBM

* feat: LightGBM

* feat: start stage7

* feat: start stage8
2022-05-31 09:48:49 -07:00
Andrew FerlitschandGitHub d2c9e84af4 Add files via upload 2022-05-26 09:30:17 -07:00
Andrew FerlitschandGitHub fe0a0ff055 Update README.md 2022-05-26 09:30:04 -07:00
Andrew FerlitschandGitHub 40e287245d Delete stage5v2.png 2022-05-26 09:29:50 -07:00
Andrew FerlitschandGitHub c79646a537 Add files via upload 2022-05-26 09:26:24 -07:00
Andrew FerlitschandGitHub e293e1c2fb Update README.md 2022-05-26 09:26:09 -07:00
Andrew FerlitschandGitHub f2ab65bd03 Delete stage4v2.png 2022-05-26 09:25:51 -07:00
Andrew FerlitschandGitHub aa686516d3 Add files via upload 2022-05-25 14:42:01 -07:00
Andrew FerlitschandGitHub 11c80a0251 Update README.md 2022-05-25 14:41:45 -07:00
Andrew FerlitschandGitHub c5e9e5812b Delete stage3v2.png 2022-05-25 14:41:29 -07:00
Andrew FerlitschandGitHub 98b126fa0b Update README.md 2022-05-25 14:34:29 -07:00
Andrew FerlitschandGitHub 55553f9e69 Update README.md 2022-05-25 14:33:56 -07:00
Andrew FerlitschandGitHub 563013c745 Add files via upload 2022-05-25 14:33:10 -07:00
Andrew FerlitschandGitHub fefb9778a7 Update README.md 2022-05-25 14:32:53 -07:00
Andrew FerlitschandGitHub be1831082c Update README.md 2022-05-25 14:11:47 -07:00
Andrew FerlitschandGitHub 9e15ee2e8a Add files via upload 2022-05-25 14:11:21 -07:00
Andrew FerlitschandGitHub ceff7d7271 Delete stage2v2.png 2022-05-25 14:10:51 -07:00
Andrew FerlitschandGitHub df9b5cd6f0 Add files via upload 2022-05-24 16:17:27 -07:00
Andrew FerlitschandGitHub bc57801525 Update README.md 2022-05-24 16:17:06 -07:00
Andrew FerlitschandGitHub 1b403373d3 Delete stage1.png 2022-05-24 16:16:49 -07:00
Andrew FerlitschandGitHub 987fb74ca6 Add files via upload 2022-05-24 15:13:15 -07:00
Andrew FerlitschandGitHub 65a209bc7b Update README.md 2022-05-24 15:12:55 -07:00
Andrew FerlitschandGitHub 16e6d9e90f Delete stage6c.png 2022-05-24 15:12:32 -07:00
Andrew FerlitschandGitHub 7ce6bee763 Delete stage6b.png 2022-05-24 15:12:18 -07:00
Andrew FerlitschandGitHub 6d7ca3eb55 Delete stage6a.png 2022-05-24 15:12:05 -07:00
Andrew FerlitschandGitHub 8e9f205e9a Add files via upload 2022-05-24 14:58:15 -07:00
Andrew FerlitschandGitHub 5fdb6c4368 Update README.md 2022-05-24 14:57:50 -07:00
Andrew FerlitschandGitHub 71ebfd402c Delete stage5.png 2022-05-24 14:57:33 -07:00
Andrew FerlitschandGitHub 6e4ca83531 Update README.md 2022-05-24 14:43:12 -07:00
Andrew FerlitschandGitHub 05793d6a3c Add files via upload 2022-05-24 14:42:39 -07:00
Andrew FerlitschandGitHub 3dc374b7db Delete stage4.png 2022-05-24 14:42:13 -07:00
Andrew FerlitschandGitHub 3ac8a4f617 Add files via upload 2022-05-24 14:22:20 -07:00
Andrew FerlitschandGitHub 21b4b5b063 Delete stage3v3.png 2022-05-24 14:22:11 -07:00
Andrew FerlitschandGitHub 9ffd921ca4 Update README.md 2022-05-24 14:21:38 -07:00
Andrew FerlitschandGitHub 14e6ebbb95 Add files via upload 2022-05-24 14:21:08 -07:00
Andrew FerlitschandGitHub 277b685a39 Delete stage3.png 2022-05-24 14:20:58 -07:00
Andrew FerlitschandGitHub 67e7715ed9 Update README.md 2022-05-24 13:59:19 -07:00
Andrew FerlitschandGitHub bb87900209 Add files via upload 2022-05-24 13:58:51 -07:00
Andrew FerlitschandGitHub 96ebe7286b Delete stage2.png 2022-05-24 13:58:24 -07:00
Andrew FerlitschandGitHub 0b3a5dde09 Add files via upload 2022-05-24 13:57:41 -07:00
Andrew FerlitschandGitHub 17a30360b4 Delete stage2.png 2022-05-24 13:57:24 -07:00
Andrew FerlitschandGitHub b386f51916 Update README.md 2022-05-24 13:40:25 -07:00
Andrew FerlitschandGitHub dd4f40c7f4 Add files via upload 2022-05-24 13:39:51 -07:00
Andrew FerlitschandGitHub d402fc085a Delete stage1.jpg 2022-05-24 13:39:38 -07:00
Andrew FerlitschandGitHub a12b9cd60f Add files via upload 2022-05-24 13:38:11 -07:00
Andrew FerlitschandGitHub bf1032d550 Delete stage1.jpg 2022-05-24 13:38:01 -07:00
Andrew FerlitschandGitHub dea05e1e36 Add files via upload 2022-05-24 13:36:59 -07:00
Andrew FerlitschandGitHub a9f0117c82 Update README.md 2022-05-23 17:09:39 -07:00
Andrew FerlitschandGitHub 83f1fe9ecd Add files via upload 2022-05-23 17:08:06 -07:00
Andrew FerlitschandGitHub 7344274037 Add files via upload 2022-05-23 17:06:53 -07:00
Andrew FerlitschandGitHub e36bfa9a3b Add files via upload 2022-05-23 17:03:27 -07:00
Andrew FerlitschandGitHub 950aa245b8 Update README.md 2022-05-23 16:55:52 -07:00
Andrew FerlitschandGitHub ac47b2e370 Update README.md 2022-05-20 13:01:42 -07:00
Andrew FerlitschandGitHub 6662fc809b Update README.md 2022-05-20 13:00:49 -07:00
Andrew FerlitschandGitHub 12b3171d5e feat: LightGBM (#583)
* fix: enable apis

* fix: enable apis

* fix: enable apis

* fix: enable apis

* fix: enable apis

* fix: enable apis

* fix: enable apis

* fix: enable apis

* fix: enable apis

* fix: enable apis

* fix: enable apis

* fix: enable apis

* feat: swivel and matching engine

* feat: swivel and matching engine

* feat: LightGBM

* feat: LightGBM
2022-05-20 12:58:37 -07:00
Andrew FerlitschandGitHub 579d4751bd Update README.md 2022-05-20 12:41:10 -07:00
Andrew FerlitschandGitHub d4c607323d feat: swivel + ME (#582)
* fix: enable apis

* fix: enable apis

* fix: enable apis

* fix: enable apis

* fix: enable apis

* fix: enable apis

* fix: enable apis

* fix: enable apis

* fix: enable apis

* fix: enable apis

* fix: enable apis

* fix: enable apis

* feat: swivel and matching engine

* feat: swivel and matching engine
2022-05-20 12:39:42 -07:00
nayaknishantandGitHub 3ad30738a7 docs: fixing CODEOWNERS and instructions hyperlinks (#580)
When opening a PR, the CODEOWNERS and instructions hyperlinks throw a 404 error because they point to a URL that has been changed. Fixing these hyperlinks.
2022-05-19 13:20:23 -07:00
118 changed files with 21710 additions and 3472 deletions
+19 -14
View File
@@ -1,9 +1,13 @@
from typing import List
from ratemate import RateLimit
from resource_cleanup_manager import (
DatasetResourceCleanupManager,
ModelResourceCleanupManager,
EndpointResourceCleanupManager,
ResourceCleanupManager,
)
from resource_cleanup_manager import (DatasetResourceCleanupManager,
EndpointResourceCleanupManager,
ModelResourceCleanupManager,
ResourceCleanupManager)
rate_limit = RateLimit(max_count=25, per=60, greedy=False)
def run_cleanup_managers(managers: List[ResourceCleanupManager], is_dry_run: bool):
@@ -14,17 +18,18 @@ def run_cleanup_managers(managers: List[ResourceCleanupManager], is_dry_run: boo
resources = manager.list()
print(f"Found {len(resources)} {type_name}'s")
for resource in resources:
if not manager.is_deletable(resource):
continue
try:
if not manager.is_deletable(resource):
continue
if is_dry_run:
resource_name = manager.resource_name(resource)
print(f"Will delete '{type_name}': {resource_name}")
else:
try:
if is_dry_run:
resource_name = manager.resource_name(resource)
print(f"Will delete '{type_name}': {resource_name}")
else:
rate_limit.wait() # wait before deleting
manager.delete(resource)
except Exception as exception:
print(exception)
except Exception as exception:
print(exception)
print("")
@@ -38,7 +43,7 @@ if is_dry_run:
managers = [
DatasetResourceCleanupManager(),
EndpointResourceCleanupManager(),
ModelResourceCleanupManager(),
ModelResourceCleanupManager(), # ModelResourceCleanupManager must follow EndpointResourceCleanupManager due to deployed models blocking model deletion.
]
run_cleanup_managers(managers=managers, is_dry_run=is_dry_run)
@@ -1,5 +1,5 @@
import abc
from typing import Any
from typing import Any, Type
from google.cloud import aiplatform
from google.cloud.aiplatform import base
@@ -41,7 +41,7 @@ class ResourceCleanupManager(abc.ABC):
# Check that it wasn't created too recently, to prevent race conditions
if time_difference <= RESOURCE_UPDATE_BUFFER_IN_SECONDS:
print(
f"Skipping '{resource}' due update_time being '{time_difference}', which is less than '{RESOURCE_UPDATE_BUFFER_IN_SECONDS}'."
f"Skipping '{resource}' due to update_time being '{time_difference}', which is less than '{RESOURCE_UPDATE_BUFFER_IN_SECONDS}'."
)
return False
@@ -51,7 +51,7 @@ class ResourceCleanupManager(abc.ABC):
class VertexAIResourceCleanupManager(ResourceCleanupManager):
@property
@abc.abstractmethod
def vertex_ai_resource(self) -> base.VertexAiResourceNounWithFutureManager:
def vertex_ai_resource(self) -> Type[base.VertexAiResourceNounWithFutureManager]:
pass
@property
@@ -61,7 +61,9 @@ class VertexAIResourceCleanupManager(ResourceCleanupManager):
def list(self) -> Any:
return self.vertex_ai_resource.list()
def resource_name(self, resource: Any) -> str:
def resource_name(
self, resource: Type[base.VertexAiResourceNounWithFutureManager]
) -> str:
return resource.display_name
def delete(self, resource):
@@ -75,12 +77,33 @@ class VertexAIResourceCleanupManager(ResourceCleanupManager):
class DatasetResourceCleanupManager(VertexAIResourceCleanupManager):
vertex_ai_resource = aiplatform.datasets._Dataset
dataset_types = [
aiplatform.ImageDataset,
aiplatform.TabularDataset,
aiplatform.TextDataset,
aiplatform.TimeSeriesDataset,
aiplatform.VideoDataset,
]
def list(self) -> Any:
return [
dataset
for dataset_type in self.dataset_types
for dataset in dataset_type.list()
]
class EndpointResourceCleanupManager(VertexAIResourceCleanupManager):
vertex_ai_resource = aiplatform.Endpoint
def delete(self, resource):
# TODO: Remove this once https://github.com/googleapis/python-aiplatform/issues/1441 is fixed
resource._sync_gca_resource()
for deployed_model_id in [
models.id for models in resource._gca_resource.deployed_models
]:
resource._undeploy(deployed_model_id=deployed_model_id)
resource.delete(force=True)
@@ -17,6 +17,7 @@ import concurrent
import dataclasses
import datetime
import functools
import git
import operator
import os
import pathlib
@@ -24,6 +25,7 @@ import re
import subprocess
from typing import List, Optional
import execute_notebook_helper
import execute_notebook_remote
import nbformat
from google.cloud.devtools.cloudbuild_v1.types import BuildOperationMetadata
@@ -231,20 +233,39 @@ def get_changed_notebooks(
# Find notebooks
notebooks = []
# Instantiate GitPython objects
repo = git.Repo(os.getcwd())
index = repo.index
if base_branch:
print(f"Looking for notebooks that changed from branch: {base_branch}")
notebooks = subprocess.check_output(
["git", "diff", "--name-only", f"origin/{base_branch}..."] + test_paths
)
# Get the point at which this branch branches off from main
branching_commits = repo.merge_base("HEAD", f"origin/{base_branch}")
if len(branching_commits) > 0:
branching_commit = branching_commits[0]
print(f"Looking for notebooks that changed from branch: {branching_commit}")
notebooks = [
diff.b_path
for diff in index.diff(branching_commit, paths=test_paths)
if diff.b_path is not None
]
else:
notebooks = []
else:
print(f"Looking for all notebooks.")
notebooks = subprocess.check_output(["git", "ls-files"] + test_paths)
notebooks = notebooks.decode("utf-8").split("\n")
notebooks = [notebook for notebook in notebooks if notebook.endswith(".ipynb")]
notebooks = [notebook for notebook in notebooks if len(notebook) > 0]
notebooks = [notebook for notebook in notebooks if pathlib.Path(notebook).exists()]
if len(notebooks) > 0:
print(f"Found {len(notebooks)} notebooks:")
for notebook in notebooks:
print(f"\t{notebook}")
return notebooks
@@ -287,14 +308,15 @@ def process_and_execute_notebooks(
timeout (str):
Required. Timeout string according to https://cloud.google.com/build/docs/build-config-file-schema#timeout.
"""
notebook_execution_results: List[NotebookExecutionResult] = []
# Calculate deadline
deadline = datetime.datetime.now() + datetime.timedelta(
seconds=max(timeout - WORKER_TIMEOUT_BUFFER_IN_SECONDS, 0)
)
if len(notebooks) > 0:
if len(notebooks) > 1:
notebook_execution_results: List[NotebookExecutionResult] = []
print(f"Found {len(notebooks)} modified notebooks: {notebooks}")
if should_parallelize and len(notebooks) > 1:
@@ -333,43 +355,64 @@ def process_and_execute_notebooks(
)
for notebook in notebooks
]
print("\n=== RESULTS ===\n")
results_sorted = sorted(
notebook_execution_results,
key=lambda result: result.is_pass,
reverse=True,
)
# Print results
print(
tabulate(
[
[
result.name,
"PASSED" if result.is_pass else "FAILED",
format_timedelta(result.duration),
result.log_url,
result.output_uri,
]
for result in results_sorted
],
headers=["build_tag", "status", "duration", "log_url", "output_url"],
)
)
print("\n=== END RESULTS===\n")
total_notebook_duration = functools.reduce(
operator.add,
[datetime.timedelta(seconds=0)]
+ [result.duration for result in results_sorted],
)
print(
f"Cumulative notebook duration: {format_timedelta(total_notebook_duration)}"
)
# Raise error if any notebooks failed
if not all([result.is_pass for result in results_sorted]):
raise RuntimeError("Notebook failures detected. See logs for details")
elif len(notebooks) == 1:
notebook = notebooks[0]
# Pre-process notebook by substituting variable names
_process_notebook(
notebook_path=notebook,
variable_project_id=variable_project_id,
variable_region=variable_region,
)
execute_notebook_helper.execute_notebook(
notebook_source=notebook,
output_file_or_uri="/".join(
[artifacts_bucket, pathlib.Path(notebook).name]
),
should_log_output=True,
)
else:
print("No notebooks modified in this pull request.")
print("\n=== RESULTS ===\n")
results_sorted = sorted(
notebook_execution_results,
key=lambda result: result.is_pass,
reverse=True,
)
# Print results
print(
tabulate(
[
[
result.name,
"PASSED" if result.is_pass else "FAILED",
format_timedelta(result.duration),
result.log_url,
]
for result in results_sorted
],
headers=["build_tag", "status", "duration", "log_url"],
)
)
print("\n=== END RESULTS===\n")
total_notebook_duration = functools.reduce(
operator.add,
[datetime.timedelta(seconds=0)]
+ [result.duration for result in results_sorted],
)
print(f"Cumulative notebook duration: {format_timedelta(total_notebook_duration)}")
# Raise error if any notebooks failed
if not all([result.is_pass for result in results_sorted]):
raise RuntimeError("Notebook failures detected. See logs for details")
@@ -11,12 +11,9 @@ steps:
args:
- -c
- 'python3 .cloud-build/CheckPythonVersion.py'
# Fetch base branch if required
- name: ${_PYTHON_IMAGE}
entrypoint: /bin/sh
args:
- -c
- 'if [ -n "${_BASE_BRANCH}" ]; then git fetch origin "${_BASE_BRANCH}":refs/remotes/origin/"${_BASE_BRANCH}"; else echo "Skipping fetch."; fi'
# Fetch full repo for diff purposes
- name: gcr.io/cloud-builders/git
args: [fetch, --unshallow]
# Install Python dependencies
- name: ${_PYTHON_IMAGE}
entrypoint: /bin/sh
+1
View File
@@ -10,3 +10,4 @@ google-cloud-aiplatform
google-cloud-storage
google-cloud-build
ratemate
GitPython
-1
View File
@@ -1,3 +1,2 @@
notebooks/official
notebooks/notebook_template.ipynb
notebooks/community/ml_ops
+3 -1
View File
@@ -7,7 +7,9 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Set up Python
uses: actions/setup-python@v3
uses: actions/setup-python@v4
with:
python-version: '3.x'
- name: Fetch pull request branch
uses: actions/checkout@v3
with:
+2 -1
View File
@@ -3,7 +3,8 @@ ipython
jupyter
nbconvert
black==22.3.0
pyupgrade==2.31.1
pyupgrade==2.34.0
isort==5.10.1
flake8==4.0.1
nbqa==1.3.1
+1 -1
View File
@@ -48,8 +48,8 @@ then you will need to manually address them before submitting your PR.
nbqa black "$notebook"
nbqa pyupgrade "$notebook"
nbqa isort "$notebook"
python3 -m tensorflow_docs.tools.nbfmt --remove_outputs "$notebook"
nbqa flake8 "$notebook" --extend-ignore=W391,E501,F821,E402,F404,W503,E203,E722,W293,W291
python3 -m tensorflow_docs.tools.nbfmt --remove_outputs "$notebook"
```
## Code Reviews
@@ -1,4 +1,4 @@
google-cloud-bigquery==2.20.0
tensorflow==2.5.3
tensorflow==2.7.2
pillow==9.0.1
tf-agents==0.8.0
@@ -1,4 +1,4 @@
google-cloud-pubsub==2.5.0
pillow==9.0.1
tf-agents==0.8.0
tensorflow==2.5.3
tensorflow==2.7.2
@@ -1,5 +1,5 @@
dataclasses==0.6
google-cloud-aiplatform==1.8.1
tensorflow==2.5.3
tensorflow==2.7.2
pillow==9.0.1
tf-agents==0.8.0
@@ -1 +1 @@
tensorflow==2.5.3
tensorflow==2.7.2
+2 -2
View File
@@ -1,5 +1,5 @@
The [official](https://github.com/GoogleCloudPlatform/vertex-ai-samples/tree/main/notebooks/official) folder contains notebooks organized by Google Cloud product.
The [official](https://github.com/GoogleCloudPlatform/vertex-ai-samples/tree/main/notebooks/official) folder contains notebooks organized by Google Cloud product. These are tested weekly and maintained by Google.
The [community](https://github.com/GoogleCloudPlatform/vertex-ai-samples/tree/main/notebooks/community) folder contains notebooks that aren't officially supported by Google.
The [community](https://github.com/GoogleCloudPlatform/vertex-ai-samples/tree/main/notebooks/community) folder contains notebooks that may be created by Google or external contributors. They are not necessary maintained.
Contributions to the repo should use the [notebook template](https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/notebook_template.ipynb) as a starting point.
+1
View File
@@ -12,6 +12,7 @@
/managed_notebooks/
/sdk/SDK_FBProphet_Forecasting_Online.ipynb @brianchunkang
/pipelines/google_cloud_pipeline_components_TPU_model_train_upload_deploy.ipynb @brianchunkang
/explainable_ai/SDK_Custom_Container_XAI.ipynb @brianchunkang
/matching_engine/sdk_matching_engine_for_indexing.ipynb @ivanmkc
/matching_engine/matching_engine_for_indexing.ipynb @yinghsienwu
/sdk/pytorch_lightning_custom_container_training.ipynb @brianchunkang
File diff suppressed because it is too large Load Diff
Binary file not shown.

Before

Width:  |  Height:  |  Size: 122 KiB

After

Width:  |  Height:  |  Size: 140 KiB

@@ -32,18 +32,18 @@
"<table align=\"left\">\n",
"\n",
" <td>\n",
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/notebook_template.ipynb\">\n",
" <a href=\"https://colab.research.google.com/github/vertex-ai-samples/blob/main/notebooks/community/feature_store/mobile_gaming/mobile_gaming_feature_store.ipynb\">\n",
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"> Run in Colab\n",
" </a>\n",
" </td>\n",
" <td>\n",
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/notebook_template.ipynb\">\n",
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/community/feature_store/mobile_gaming/mobile_gaming_feature_store.ipynb\">\n",
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\">\n",
" View on GitHub\n",
" </a>\n",
" </td>\n",
" <td>\n",
" <a href=\"https://console.cloud.google.com/vertex-ai/workbench/deploy-notebook?download_url=https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/notebook_template.ipynb\">\n",
" <a href=\"https://console.cloud.google.com/vertex-ai/workbench/deploy-notebook?download_url=https://raw.githubusercontent.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/community/feature_store/mobile_gaming/mobile_gaming_feature_store.ipynb\">\n",
" <img src=\"https://lh3.googleusercontent.com/UiNooY4LUgW_oTvpsNhPpQzsstV5W8F7rYgxgGBD85cWJoLmrOzhVs_ksK_vgx40SHs7jCqkTkCk=e14-rj-sc0xffffff-h130-w32\" alt=\"Vertex AI logo\">\n",
" Open in Vertex AI Workbench\n",
" </a>\n",
@@ -54,52 +54,52 @@
{
"cell_type": "markdown",
"metadata": {
"id": "7FZeBEwdXS4d"
"id": "tvgnzT1CKxrO"
},
"source": [
"## Overview\n",
"\n",
"Imagine you are a member of the Data Science team working on the same Mobile Gaming application reported in the [Churn prediction for game developers using Google Analytics 4 (GA4) and BigQuery ML](https://cloud.google.com/blog/topics/developers-practitioners/churn-prediction-game-developers-using-google-analytics-4-ga4-and-bigquery-ml) blog post. \n",
"\n",
"Business wants to use that information in real-time to take immediate intervention actions in-game to prevent churn. In particular, for each player, they want to provide gaming incentives like new items or bonus packs depending on the customer demographic, behavioral information and the resulting propensity of return. \n",
"\n",
"Last year, Google Cloud announced Vertex AI, a managed machine learning (ML) platform that allows data science teams to accelerate the deployment and maintenance of ML models. One of the platform building blocks is Vertex AI Feature store which provides a managed service for low latency scalable feature serving. Also it is a centralized feature repository with easy APIs to search & discover features and feature monitoring capabilities to track drift and other quality issues. \n",
"\n",
"In this notebook, we will show how the role of Vertex AI Feature Store in a ready to production scenario when the user's activities within the first 24 hours of last engagment and the gaming platform would consume in order to improver UX. Below you can find the high level picture of the system\n",
"\n",
" \n",
"Imagine you are a member of the Data Science team working on the same Mobile Gaming application reported in the [Churn prediction for game developers using Google Analytics 4 (GA4) and BigQuery ML](https://cloud.google.com/blog/topics/developers-practitioners/churn-prediction-game-developers-using-google-analytics-4-ga4-and-bigquery-ml) blog post.\n",
" \n",
"Business wants to use that information in real-time to take immediate intervention actions in-game to prevent churn. In particular, for each player, they want to provide gaming incentives like new items or bonus packs depending on the customer demographic, behavioral information and the resulting propensity of return.\n",
" \n",
"Last year, Google Cloud announced Vertex AI, a managed machine learning (ML) platform that allows data science teams to accelerate the deployment and maintenance of ML models. One of the platform building blocks is Vertex AI Feature store which provides a managed service for low latency scalable feature serving. Also it is a centralized feature repository with easy APIs to search & discover features and feature monitoring capabilities to track drift and other quality issues.\n",
" \n",
"In this notebook, we will show how the role of Vertex AI Feature Store in a ready to production scenario when the user's activities within the first 24 hours of last engagement and the gaming platform would consume in order to improve UX. Below you can find the high level picture of the system\n",
" \n",
"<img src=\"./assets/mobile_gaming_architecture_1.png\">\n",
"\n",
"\n",
" \n",
" \n",
"### Dataset\n",
"\n",
" \n",
"The dataset is the public sample export data from an actual mobile game app called \"Flood It!\" (Android, iOS)\n",
"\n",
" \n",
"### Objective\n",
"\n",
" \n",
"In the following notebook, you will learn how Vertex AI Feature store\n",
"\n",
"1. Provide a centralized feature repository with easy APIs to search & discover features and fetch them for training/serving. \n",
"\n",
"2. Simplify deployments of models for Online Prediction, via low latency scalable feature serving.\n",
"\n",
"3. Mitigate training serving skew and data leakage by performing point in time lookups to fetch historical data for training.\n",
"\n",
" \n",
"1. Provide a centralized feature repository with easy APIs to search & discover features and fetch them for training/serving.\n",
" \n",
"2. Simplify deployments of models for Online Prediction, via low latency scalable feature serving.\n",
" \n",
"3. Mitigate training serving skew and data leakage by performing point in time lookups to fetch historical data for training.\n",
" \n",
"**Notice that we assume that already know how to set up a Vertex AI Feature store. In case you are not, please check out [this detailed notebook](https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/feature_store/gapic-feature-store.ipynb).**\n",
"\n",
"\n",
"### Costs \n",
"\n",
" \n",
" \n",
"### Costs\n",
" \n",
"This tutorial uses billable components of Google Cloud:\n",
"\n",
" \n",
"* Vertex AI\n",
"* BigQuery\n",
"* Cloud Storage\n",
"\n",
" \n",
"Learn about [Vertex AI\n",
"pricing](https://cloud.google.com/vertex-ai/pricing) and [Cloud Storage\n",
"pricing](https://cloud.google.com/storage/pricing), and use the [Pricing\n",
"Calculator](https://cloud.google.com/products/calculator/)\n",
"to generate a cost estimate based on your projected usage."
"to generate a cost estimate based on your projected usage.\n"
]
},
{
@@ -110,7 +110,7 @@
"source": [
"### Set up your local development environment\n",
"\n",
"**If you are using Colab or Google Cloud Notebooks**, your environment already meets\n",
"**If you are using Colab or Vertex AI Workbench Notebooks**, your environment already meets\n",
"all the requirements to run this notebook. You can skip this step."
]
},
@@ -159,7 +159,7 @@
"source": [
"### Install additional packages\n",
"\n",
"Install additional package dependencies not installed in your notebook environment, such as {XGBoost, AdaNet, or TensorFlow Hub TODO: Replace with relevant packages for the tutorial}. Use the latest major GA version of each package."
"Install additional package dependencies not installed in your notebook environment, such as XGBoost. Use the latest major GA version of each package."
]
},
{
@@ -172,12 +172,15 @@
"source": [
"import os\n",
"\n",
"# The Google Cloud Notebook product has specific requirements\n",
"IS_GOOGLE_CLOUD_NOTEBOOK = os.path.exists(\"/opt/deeplearning/metadata/env_version\")\n",
"# The Vertex AI Workbench Notebook product has specific requirements\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\")\n",
"IS_USER_MANAGED_WORKBENCH_NOTEBOOK = os.path.exists(\n",
" \"/opt/deeplearning/metadata/env_version\"\n",
")\n",
"\n",
"# Google Cloud Notebook requires dependencies to be installed with '--user'\n",
"# Vertex AI Notebook requires dependencies to be installed with '--user'\n",
"USER_FLAG = \"\"\n",
"if IS_GOOGLE_CLOUD_NOTEBOOK:\n",
"if IS_WORKBENCH_NOTEBOOK:\n",
" USER_FLAG = \"--user\""
]
},
@@ -185,11 +188,11 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "SzEo6DeE2GOP"
"id": "_vr6BYED_5my"
},
"outputs": [],
"source": [
"! pip3 install {USER_FLAG} --upgrade pip\n",
"! pip3 install {USER_FLAG} --upgrade pip -q\n",
"! pip3 install {USER_FLAG} --upgrade google-cloud-aiplatform==1.11.0 -q --no-warn-conflicts\n",
"! pip3 install {USER_FLAG} git+https://github.com/googleapis/python-aiplatform.git@main # For features monitoring\n",
"! pip3 install {USER_FLAG} --upgrade google-cloud-bigquery==2.24.0 -q --no-warn-conflicts\n",
@@ -249,7 +252,7 @@
"\n",
"1. [Make sure that billing is enabled for your project](https://cloud.google.com/billing/docs/how-to/modify-project).\n",
"\n",
"1. [Enable the Vertex AI API and Compute Engine API](https://console.cloud.google.com/flows/enableapi?apiid=aiplatform.googleapis.com,compute_component). \n",
"1. [Enable the APIs](https://console.cloud.google.com/flows/enableapi?apiid=aiplatform.googleapis.com,notebooks.googleapis.com, ). \n",
"\n",
"1. If you are running this notebook locally, you will need to install the [Cloud SDK](https://cloud.google.com/sdk).\n",
"\n",
@@ -307,18 +310,76 @@
"outputs": [],
"source": [
"if PROJECT_ID == \"\" or PROJECT_ID is None:\n",
" PROJECT_ID = \"\" # @param {type:\"string\"}"
" PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "dEjRdjxBuDsi"
"id": "set_gcloud_project_id"
},
"outputs": [],
"source": [
"!gcloud config set project $PROJECT_ID #change it"
"! gcloud config set project $PROJECT_ID"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "23988890fef6"
},
"source": [
"#### Get your project number (Optional)\n",
"\n",
"Now that the project ID is set, you get your corresponding project number."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "2d6950574e1d"
},
"outputs": [],
"source": [
"shell_output = ! gcloud projects list --filter=\"PROJECT_ID:'{PROJECT_ID}'\" --format='value(PROJECT_NUMBER)'\n",
"PROJECT_NUMBER = shell_output[0]\n",
"print(\"Project Number:\", PROJECT_NUMBER)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "region"
},
"source": [
"#### Region\n",
"\n",
"You can also change the `REGION` variable, which is used for operations\n",
"throughout the rest of this notebook. Below are regions supported for Vertex AI. We recommend that you choose the region closest to you.\n",
"\n",
"- Americas: `us-central1`\n",
"- Europe: `europe-west4`\n",
"- Asia Pacific: `asia-east1`\n",
"\n",
"You may not use a multi-regional bucket for training with Vertex AI. Not all regions provide support for all Vertex AI services.\n",
"\n",
"Learn more about [Vertex AI regions](https://cloud.google.com/vertex-ai/docs/general/locations)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "jIcZV7-C2RrX"
},
"outputs": [],
"source": [
"REGION = \"[your-region]\" # @param {type: \"string\"}\n",
"\n",
"if REGION == \"[your-region]\":\n",
" REGION = \"us-central1\""
]
},
{
@@ -353,7 +414,7 @@
"source": [
"### Authenticate your Google Cloud account\n",
"\n",
"**If you are using Google Cloud Notebooks**, your environment is already\n",
"**If you are using Vertex AI Workbench Notebooks**, your environment is already\n",
"authenticated. Skip this step."
]
},
@@ -376,9 +437,13 @@
"3. In the **Service account name** field, enter a name, and\n",
" click **Create**.\n",
"\n",
"4. In the **Grant this service account access to project** section, click the **Role** drop-down list. Type \"Vertex AI\"\n",
"into the filter box, and select\n",
" **Vertex AI Administrator**. Type \"Storage Object Admin\" into the filter box, and select **Storage Object Admin**.\n",
"4. In the **Grant this service account access to project** section, click the **Role** drop-down list and add the following roles:\n",
" - BigQuery Admin\n",
" - Storage Admin\n",
" - Storage Object Admin\n",
" - Vertex AI Administrator\n",
" - Vertex AI Feature Store Admin\n",
"\n",
"\n",
"5. Click *Create*. A JSON file that contains your key downloads to your\n",
"local environment.\n",
@@ -395,19 +460,19 @@
},
"outputs": [],
"source": [
"import os\n",
"import sys\n",
"\n",
"# If you are running this notebook in Colab, run this cell and follow the\n",
"# instructions to authenticate your GCP account. This provides access to your\n",
"# Cloud Storage bucket and lets you submit training jobs and prediction\n",
"# requests.\n",
"\n",
"# The Google Cloud Notebook product has specific requirements\n",
"IS_GOOGLE_CLOUD_NOTEBOOK = os.path.exists(\"/opt/deeplearning/metadata/env_version\")\n",
"import os\n",
"import sys\n",
"\n",
"# If on Google Cloud Notebooks, then don't execute this code\n",
"if not IS_GOOGLE_CLOUD_NOTEBOOK:\n",
"# If on Vertex AI Workbench, then don't execute this code\n",
"IS_COLAB = \"google.colab\" in sys.modules\n",
"if not os.path.exists(\"/opt/deeplearning/metadata/env_version\") and not os.getenv(\n",
" \"DL_ANACONDA_HOME\"\n",
"):\n",
" if \"google.colab\" in sys.modules:\n",
" from google.colab import auth as google_auth\n",
"\n",
@@ -447,8 +512,8 @@
},
"outputs": [],
"source": [
"BUCKET_URI = \"gs://[your-bucket-name]\" # @param {type:\"string\"}\n",
"REGION = \"[your-region]\" # @param {type:\"string\"}"
"BUCKET_NAME = \"[your-bucket-name]\" # @param {type:\"string\"}\n",
"BUCKET_URI = f\"gs://{BUCKET_NAME}\""
]
},
{
@@ -459,11 +524,9 @@
},
"outputs": [],
"source": [
"if BUCKET_URI == \"\" or BUCKET_URI is None or BUCKET_URI == \"gs://[your-bucket-name]\":\n",
" BUCKET_URI = \"gs://\" + PROJECT_ID + \"-aip-\" + TIMESTAMP\n",
"\n",
"if REGION == \"[your-region]\":\n",
" REGION = \"us-central1\""
"if BUCKET_NAME == \"\" or BUCKET_NAME is None or BUCKET_NAME == \"[your-bucket-name]\":\n",
" BUCKET_NAME = PROJECT_ID + \"-aip-\" + TIMESTAMP\n",
" BUCKET_URI = f\"gs://{BUCKET_NAME}\""
]
},
{
@@ -486,26 +549,6 @@
"! gsutil mb -l $REGION -p $PROJECT_ID $BUCKET_URI"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "994afa65eaa2"
},
"source": [
"Run the following cell to grant access to your Cloud Storage resources from Vertex AI Feature store"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "psP1rPU9TRnX"
},
"outputs": [],
"source": [
"! gsutil uniformbucketlevelaccess set on $BUCKET_URI"
]
},
{
"cell_type": "markdown",
"metadata": {
@@ -526,6 +569,78 @@
"! gsutil ls -al $BUCKET_URI"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "set_service_account"
},
"source": [
"#### Service Account (Optional)\n",
"\n",
"If you do not want to use your project's Compute Engine service account, set `SERVICE_ACCOUNT` to another service account ID."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "MQVV9haf2Rra"
},
"outputs": [],
"source": [
"SERVICE_ACCOUNT = \"[your-service-account]\" # @param {type:\"string\"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "autoset_service_account"
},
"outputs": [],
"source": [
"if (\n",
" SERVICE_ACCOUNT == \"\"\n",
" or SERVICE_ACCOUNT is None\n",
" or SERVICE_ACCOUNT == \"[your-service-account]\"\n",
"):\n",
" # Get your service account from gcloud\n",
" if not IS_COLAB:\n",
" shell_output = !gcloud auth list 2>/dev/null\n",
" SERVICE_ACCOUNT = shell_output[2].replace(\"*\", \"\").strip()\n",
"\n",
" else: # IS_COLAB:\n",
" shell_output = ! gcloud projects describe $PROJECT_ID\n",
" project_number = shell_output[-1].split(\":\")[1].strip().replace(\"'\", \"\")\n",
" SERVICE_ACCOUNT = f\"{project_number}-compute@developer.gserviceaccount.com\"\n",
"\n",
" print(\"Service Account:\", SERVICE_ACCOUNT)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "set_service_account:pipelines"
},
"source": [
"#### Set service account access\n",
"\n",
"Run the following commands to grant your service account access. You only need to run this step once per service account."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "U4UpQThc2Rrb"
},
"outputs": [],
"source": [
"! gsutil iam ch serviceAccount:{SERVICE_ACCOUNT}:roles/storage.objectCreator $BUCKET_URI\n",
"\n",
"! gsutil iam ch serviceAccount:{SERVICE_ACCOUNT}:roles/storage.objectViewer $BUCKET_URI"
]
},
{
"cell_type": "markdown",
"metadata": {
@@ -541,13 +656,22 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "G3G2BXqswb_J"
"id": "8615339fa4ca"
},
"outputs": [],
"source": [
"BQ_DATASET = \"Mobile_Gaming\" # @param {type:\"string\"}\n",
"LOCATION = \"US\"\n",
"\n",
"LOCATION = \"US\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "G3G2BXqswb_J"
},
"outputs": [],
"source": [
"!bq mk --location=$LOCATION --dataset $PROJECT_ID:$BQ_DATASET"
]
},
@@ -600,12 +724,9 @@
"outputs": [],
"source": [
"# Data Engineering and Feature Engineering\n",
"TODAY = \"2018-10-03\"\n",
"TOMORROW = \"2018-10-04\"\n",
"TODAY = \"2022-06-16\"\n",
"LABEL_TABLE = f\"label_table_{TODAY}\".replace(\"-\", \"\")\n",
"FEATURES_TABLE = \"wide_features_table\" # @param {type:\"string\"}\n",
"FEATURES_TABLE_TODAY = f\"wide_features_table_{TODAY}\".replace(\"-\", \"\")\n",
"FEATURES_TABLE_TOMORROW = f\"wide_features_table_{TOMORROW}\".replace(\"-\", \"\")\n",
"FEATURES_TABLE = f\"wide_features_table_{TODAY}\" # @param {type:\"string\"}\n",
"FEATURESTORE_ID = \"mobile_gaming\" # @param {type:\"string\"}\n",
"ENTITY_TYPE_ID = \"user\"\n",
"\n",
@@ -947,13 +1068,37 @@
"You will cover those steps in details below."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "init_aip:mbsdk,all"
},
"source": [
"### Initialize Vertex AI SDK for Python\n",
"\n",
"Initialize the Vertex AI SDK for Python for your project and corresponding bucket."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "poLJ0fV52Rrc"
},
"outputs": [],
"source": [
"vertex_ai.init(project=PROJECT_ID, location=REGION, staging_bucket=BUCKET_URI)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "4ffd54e97270"
},
"source": [
"## Initiate clients"
"### Initialize BigQuery SDK for Python\n",
"\n",
"Initialize the BigQuery AI SDK for Python for your project and corresponding bucket."
]
},
{
@@ -964,55 +1109,53 @@
},
"outputs": [],
"source": [
"bq_client = bigquery.Client(project=PROJECT_ID, location=LOCATION)\n",
"vertex_ai.init(project=PROJECT_ID, location=REGION, staging_bucket=BUCKET_URI)"
"bq_client = bigquery.Client(project=PROJECT_ID, location=LOCATION)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "zmMWIpCwsET9"
"id": "WnUQO2IHC9pZ"
},
"source": [
"## Identify users and build your features\n",
"\n",
"This section we will static features we want to fetch from Vertex AI Feature Store. In particular, we will cover the following steps:\n",
"\n",
" \n",
"This section we will have static features we want to fetch from Vertex AI Feature Store. In particular, we will cover the following steps:\n",
" \n",
"1. Identify users, process demographic features and process behavioral features within the last 24 hours using **BigQuery**\n",
"\n",
" \n",
"2. Set up the feature store\n",
"\n",
" \n",
"3. Register features using **Vertex AI Feature Store** and the SDK.\n",
"\n",
"Below you have a picture that shows the process. \n",
"\n",
" \n",
"Below you have a picture that shows the process.\n",
" \n",
"<img src=\"./assets/feature_store_ingestion_2.png\">\n",
"\n",
"\n",
"\n",
"The original dataset contains raw event data we cannot ingest in the feature store as they are. We need to pre-process the raw data in order to get user features. \n",
"\n",
"**Notice we simulate those transformations in different point of time (today and tomorrow).**\n"
" \n",
" \n",
"The original dataset contains raw event data we cannot ingest in the feature store as they are. We need to pre-process the raw data in order to get user features.\n",
" \n",
"**Notice we simulate those transformations in different points of time (today and tomorrow).**\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "8avYy5QOv02s"
"id": "e9zIrwhpDF2q"
},
"source": [
"### Label, Demographic and Behavioral Transformations\n",
"\n",
"This section is based on the [Churn prediction for game developers using Google Analytics 4 (GA4) and BigQuery ML](https://cloud.google.com/blog/topics/developers-practitioners/churn-prediction-game-developers-using-google-analytics-4-ga4-and-bigquery-ml?utm_source=linkedin&utm_medium=unpaidsoc&utm_campaign=FY21-Q2-Google-Cloud-Tech-Blog&utm_content=google-analytics-4&utm_term=-) blog article by Minhaz Kazi and Polong Lin. \n",
"\n",
"You will adapt it in order to turn a batch churn prediction (using features within the first 24h user of first engagment) in a real-time churn prediction (using features within the first 24h user of last engagment)."
" \n",
"This section is based on the [Churn prediction for game developers using Google Analytics 4 (GA4) and BigQuery ML](https://cloud.google.com/blog/topics/developers-practitioners/churn-prediction-game-developers-using-google-analytics-4-ga4-and-bigquery-ml?utm_source=linkedin&utm_medium=unpaidsoc&utm_campaign=FY21-Q2-Google-Cloud-Tech-Blog&utm_content=google-analytics-4&utm_term=-) blog article by Minhaz Kazi and Polong Lin.\n",
" \n",
"You will adapt it to turn a batch churn prediction (using features within the first 24h user of first engagement) into a real-time churn prediction (using features within the first 6h user of last engagement).\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "YO28RAITh6L-"
"id": "RQX5m8UiC_px"
},
"outputs": [],
"source": [
@@ -1039,27 +1182,27 @@
" SELECT\n",
" event_timestamp,\n",
" user_pseudo_id,\n",
" SUM(IF(event_name = 'user_engagement', 1, 0)) OVER (PARTITION BY user_pseudo_id ORDER BY event_timestamp ASC RANGE BETWEEN 86400000000 PRECEDING\n",
" SUM(IF(event_name = 'user_engagement', 1, 0)) OVER (PARTITION BY user_pseudo_id ORDER BY event_timestamp ASC RANGE BETWEEN 21600000000 PRECEDING\n",
" AND CURRENT ROW ) AS cnt_user_engagement,\n",
" SUM(IF(event_name = 'level_start_quickplay', 1, 0)) OVER (PARTITION BY user_pseudo_id ORDER BY event_timestamp ASC RANGE BETWEEN 86400000000 PRECEDING\n",
" SUM(IF(event_name = 'level_start_quickplay', 1, 0)) OVER (PARTITION BY user_pseudo_id ORDER BY event_timestamp ASC RANGE BETWEEN 21600000000 PRECEDING\n",
" AND CURRENT ROW ) AS cnt_level_start_quickplay,\n",
" SUM(IF(event_name = 'level_end_quickplay', 1, 0)) OVER (PARTITION BY user_pseudo_id ORDER BY event_timestamp ASC RANGE BETWEEN 86400000000 PRECEDING\n",
" SUM(IF(event_name = 'level_end_quickplay', 1, 0)) OVER (PARTITION BY user_pseudo_id ORDER BY event_timestamp ASC RANGE BETWEEN 21600000000 PRECEDING\n",
" AND CURRENT ROW ) AS cnt_level_end_quickplay,\n",
" SUM(IF(event_name = 'level_complete_quickplay', 1, 0)) OVER (PARTITION BY user_pseudo_id ORDER BY event_timestamp ASC RANGE BETWEEN 86400000000 PRECEDING\n",
" SUM(IF(event_name = 'level_complete_quickplay', 1, 0)) OVER (PARTITION BY user_pseudo_id ORDER BY event_timestamp ASC RANGE BETWEEN 21600000000 PRECEDING\n",
" AND CURRENT ROW ) AS cnt_level_complete_quickplay,\n",
" SUM(IF(event_name = 'level_reset_quickplay', 1, 0)) OVER (PARTITION BY user_pseudo_id ORDER BY event_timestamp ASC RANGE BETWEEN 86400000000 PRECEDING\n",
" SUM(IF(event_name = 'level_reset_quickplay', 1, 0)) OVER (PARTITION BY user_pseudo_id ORDER BY event_timestamp ASC RANGE BETWEEN 21600000000 PRECEDING\n",
" AND CURRENT ROW ) AS cnt_level_reset_quickplay,\n",
" SUM(IF(event_name = 'post_score', 1, 0)) OVER (PARTITION BY user_pseudo_id ORDER BY event_timestamp ASC RANGE BETWEEN 86400000000 PRECEDING\n",
" SUM(IF(event_name = 'post_score', 1, 0)) OVER (PARTITION BY user_pseudo_id ORDER BY event_timestamp ASC RANGE BETWEEN 21600000000 PRECEDING\n",
" AND CURRENT ROW ) AS cnt_post_score,\n",
" SUM(IF(event_name = 'spend_virtual_currency', 1, 0)) OVER (PARTITION BY user_pseudo_id ORDER BY event_timestamp ASC RANGE BETWEEN 86400000000 PRECEDING\n",
" SUM(IF(event_name = 'spend_virtual_currency', 1, 0)) OVER (PARTITION BY user_pseudo_id ORDER BY event_timestamp ASC RANGE BETWEEN 21600000000 PRECEDING\n",
" AND CURRENT ROW ) AS cnt_spend_virtual_currency,\n",
" SUM(IF(event_name = 'ad_reward', 1, 0)) OVER (PARTITION BY user_pseudo_id ORDER BY event_timestamp ASC RANGE BETWEEN 86400000000 PRECEDING\n",
" SUM(IF(event_name = 'ad_reward', 1, 0)) OVER (PARTITION BY user_pseudo_id ORDER BY event_timestamp ASC RANGE BETWEEN 21600000000 PRECEDING\n",
" AND CURRENT ROW ) AS cnt_ad_reward,\n",
" SUM(IF(event_name = 'challenge_a_friend', 1, 0)) OVER (PARTITION BY user_pseudo_id ORDER BY event_timestamp ASC RANGE BETWEEN 86400000000 PRECEDING\n",
" SUM(IF(event_name = 'challenge_a_friend', 1, 0)) OVER (PARTITION BY user_pseudo_id ORDER BY event_timestamp ASC RANGE BETWEEN 21600000000 PRECEDING\n",
" AND CURRENT ROW ) AS cnt_challenge_a_friend,\n",
" SUM(IF(event_name = 'completed_5_levels', 1, 0)) OVER (PARTITION BY user_pseudo_id ORDER BY event_timestamp ASC RANGE BETWEEN 86400000000 PRECEDING\n",
" SUM(IF(event_name = 'completed_5_levels', 1, 0)) OVER (PARTITION BY user_pseudo_id ORDER BY event_timestamp ASC RANGE BETWEEN 21600000000 PRECEDING\n",
" AND CURRENT ROW ) AS cnt_completed_5_levels,\n",
" SUM(IF(event_name = 'use_extra_steps', 1, 0)) OVER (PARTITION BY user_pseudo_id ORDER BY event_timestamp ASC RANGE BETWEEN 86400000000 PRECEDING\n",
" SUM(IF(event_name = 'use_extra_steps', 1, 0)) OVER (PARTITION BY user_pseudo_id ORDER BY event_timestamp ASC RANGE BETWEEN 21600000000 PRECEDING\n",
" AND CURRENT ROW ) AS cnt_use_extra_steps,\n",
" FROM (\n",
" SELECT\n",
@@ -1071,7 +1214,7 @@
"\n",
"SELECT\n",
" -- PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', CONCAT('{TODAY}', ' ', STRING(TIME_TRUNC(CURRENT_TIME(), SECOND))), 'UTC') as timestamp,\n",
" PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', FORMAT_TIMESTAMP('%Y-%m-%d %H:%M:%S', TIMESTAMP_MICROS(beh.event_timestamp))) AS timestamp,\n",
" TIMESTAMP_ADD(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', FORMAT_TIMESTAMP('%Y-%m-%d %H:%M:%S', TIMESTAMP_MICROS(beh.event_timestamp))), INTERVAL 1351 DAY) AS timestamp,\n",
" dem.*,\n",
" CAST(IFNULL(beh.cnt_user_engagement, 0) AS FLOAT64) AS cnt_user_engagement,\n",
" CAST(IFNULL(beh.cnt_level_start_quickplay, 0) AS FLOAT64) AS cnt_level_start_quickplay,\n",
@@ -1097,7 +1240,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "Z6CjIOmDsET-"
"id": "oGYxLCSnD068"
},
"outputs": [],
"source": [
@@ -1107,31 +1250,31 @@
{
"cell_type": "markdown",
"metadata": {
"id": "Lx__2-assET-"
"id": "xQLIlsTCD_nk"
},
"source": [
"## Create a Vertex AI Feature store and ingest your features\n",
"\n",
"Now you have the wide table of features. It is time to ingest them into the feature store. \n",
"\n",
" \n",
"Now you have a wide table of features. It is time to ingest them into the feature store.\n",
" \n",
"Before to moving on, you may have a question: **Why do I need a feature store**\n",
"in this scenario at that point?\n",
"\n",
"One of the reason would be to make those features accessable across team by calculating once and reuse them many times. And in order to make it possible you need also be able to monitor those features over time to guarantee freshness and in case have a new feature engineerign run to refresh them. \n",
"\n",
"If it is not your case, I will give even more reasons about why you should consider feature store in the following sections. Just keep following me for now.\n",
"\n",
"One of the most important thing is related to its data model. As you can see in the picture below, Vertex AI Feature Store organizes resources hierarchically in the following order: `Featurestore -> EntityType -> Feature`. You must create these resources before you can ingest data into Vertex AI Feature Store.\n",
"\n",
" \n",
"One of the reasons would be to make those features accessible across teams by calculating once and reuse them many times. And in order to make it possible you need also be able to monitor those features over time to guarantee freshness and in case have a new feature engineering run to refresh them.\n",
" \n",
"If it is not your case, I will give even more reasons about why you should consider a feature store in the following sections. Just keep following me for now.\n",
" \n",
"One of the most important things is related to its data model. As you can see in the picture below, Vertex AI Feature Store organizes resources hierarchically in the following order: `Featurestore -> EntityType -> Feature`. You must create these resources before you can ingest data into Vertex AI Feature Store.\n",
" \n",
"<img src=\"./assets/feature_store_data_model_3.png\">\n",
"\n",
"In our case we are going to create **mobile_gaming** featurestore resource containing **user** entity type and all its associated **features** such as country or the number of times a user challenged a friend (cnt_challenge_a_friend)."
" \n",
"In our case we are going to create **mobile_gaming** featurestore resource containing **user** entity type and all its associated **features** such as country or the number of times a user challenged a friend (cnt_challenge_a_friend).\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "8dNlxda2sET_"
"id": "VR7BJEozED_Q"
},
"source": [
"### Create featurestore, ```mobile_gaming```\n",
@@ -1143,7 +1286,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "t2en8I7TSe4b"
"id": "vUFqtYU-EDTR"
},
"outputs": [],
"source": [
@@ -1164,7 +1307,7 @@
{
"cell_type": "markdown",
"metadata": {
"id": "rN-vlvPUsET_"
"id": "mUlCwfdpEHJG"
},
"source": [
"### Create the ```User``` entity type and its features\n",
@@ -1176,7 +1319,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "CbZ2RQ5XbuRq"
"id": "PnCU1wBND3W7"
},
"outputs": [],
"source": [
@@ -1194,7 +1337,7 @@
{
"cell_type": "markdown",
"metadata": {
"id": "B2PIAprPmnhB"
"id": "bT9LXzu1EOvW"
},
"source": [
"### Set Feature Monitoring\n",
@@ -1208,7 +1351,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "N6im2c3ymiwC"
"id": "8WBlYUkOERaI"
},
"outputs": [],
"source": [
@@ -1231,7 +1374,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "gp9xaLQXn0CS"
"id": "92X4-7PFETj5"
},
"outputs": [],
"source": [
@@ -1254,18 +1397,18 @@
{
"cell_type": "markdown",
"metadata": {
"id": "ustwKOMle8Qp"
"id": "hxAuZjt3EWFo"
},
"source": [
"### Create features\n",
"\n",
"In order to ingest features, you need to provide feature configuration and create them as featurestore resources.\n"
"In order to ingest features, you need to provide feature configuration and create them as featurestore resources."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ijeZCTKIfCRL"
"id": "hRXO2I5VEYwt"
},
"source": [
"#### Create Feature configuration\n",
@@ -1278,7 +1421,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "vX_uYmjUgd9x"
"id": "K26NEYZIEbvE"
},
"outputs": [],
"source": [
@@ -1359,7 +1502,7 @@
{
"cell_type": "markdown",
"metadata": {
"id": "ErkruXPJkPuy"
"id": "FjzMd1XbEfdo"
},
"source": [
"#### Create features using `batch_create_features` method\n",
@@ -1371,7 +1514,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "ZsCAO_IfsEUC"
"id": "nqlgCDI9pbCD"
},
"outputs": [],
"source": [
@@ -1388,19 +1531,19 @@
{
"cell_type": "markdown",
"metadata": {
"id": "9WisJk18qqgs"
"id": "7zpFV7wAppkC"
},
"source": [
"### Search features\n",
"\n",
"Vertex AI Feature store supports serching capabilities. Below you have a simple example that show how to filter a feature based on its name. "
"Vertex AI Feature store supports searching capabilities. Below you have a simple example that shows how to filter a feature based on its name. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "JzqyarMZqvZS"
"id": "BJXYLLOfppCL"
},
"outputs": [],
"source": [
@@ -1412,7 +1555,7 @@
{
"cell_type": "markdown",
"metadata": {
"id": "ugtBfW5gsEUD"
"id": "is9C_6-QpxG3"
},
"source": [
"## Ingest features \n",
@@ -1447,7 +1590,7 @@
" entity_id_field=ENTITY_ID_FIELD,\n",
" disable_online_serving=False,\n",
" worker_count=10,\n",
" sync=True,\n",
" sync=False,\n",
" )\n",
"except RuntimeError as error:\n",
" print(error)"
@@ -1456,7 +1599,7 @@
{
"cell_type": "markdown",
"metadata": {
"id": "3Yv8MenWXrRX"
"id": "8lCMpDPGp-oQ"
},
"source": [
"# Train and deploy a real-time churn ML model using Vertex AI Training and Endpoints\n",
@@ -1467,34 +1610,34 @@
"\n",
"<img src=\"./assets/train_model_4.png\">\n",
"\n",
"Let's dive into each step of this process.\n"
"Let's dive into each step of this process."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "saZZ3zWKX1YK"
"id": "VMrvnuyjqGfY"
},
"source": [
"## Fetch training data with point-in-time query using BigQuery and Vertex AI Feature store \n",
"\n",
"As we mentioned above, in real time churn prediction, it is so important defining the label you want to predict with your model. \n",
"\n",
"Let's assume that you decide to predict the churn probability over the last 24 hr. So now you have your label. Next step is to define your training sample. But let's think about that for a second. \n",
"\n",
"In that churn real time system, you have a high volume of transactions you could use to calculate those features which keep floating and are collected constantly over time. It implies that you always get fresh data to reconstruct features. And depending on when you decide to calculate one feature or another you can end up with a set of features that are not aligned in time. \n",
"\n",
"## Fetch training data with point-in-time query using BigQuery and Vertex AI Feature store \n",
" \n",
"As we mentioned above, in real time churn prediction, it is so important defining the label you want to predict with your model.\n",
" \n",
"Let's assume that you decide to predict the churn probability over the next hour. So now you have your label. Next step is to define your training sample. But let's think about that for a second.\n",
" \n",
"In that churn real time system, you have a high volume of transactions you could use to calculate those features which keep floating and are collected constantly over time. It implies that you always get fresh data to reconstruct features. And depending on when you decide to calculate one feature or another you can end up with a set of features that are not aligned in time.\n",
" \n",
"When you have labels available, it would be incredibly difficult to say which set of features contains the most up to date historical information associated with the label you want to predict. And, when you are not able to guarantee that, the performance of your model would be badly affected because you serve no representative features of the data and the label from the field when it goes live. So you need a way to get the most updated features you calculated over time before the label becomes available in order to avoid this informational skew.\n",
"\n",
"**With the Vertex AI Feature store, you can fetch feature values corresponding to a particular timestamp thanks to point-in-time lookup capability.** In our case, it would be the timestamp associated to the label you want to predict with your model. In this way, you will avoid data leakage and you will get the most updated features to train your model. \n",
"\n",
"Let's see how to do that. \n"
" \n",
"**With the Vertex AI Feature store, you can fetch feature values corresponding to a particular timestamp thanks to point-in-time lookup capability.** In our case, it would be the timestamp associated with the label you want to predict with your model. In this way, you will avoid data leakage and you will get the most updated features to train your model.\n",
" \n",
"Let's see how to do that.\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "YHNbIHqFcQiM"
"id": "RE_Pvmu-qdDt"
},
"source": [
"### Define query for reading instances at a specific point in time\n",
@@ -1506,7 +1649,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "DGUm0bYqhVV4"
"id": "bUDVw7l-qF2x"
},
"outputs": [],
"source": [
@@ -1518,13 +1661,13 @@
" # get training threshold ----------------------------------------------------------------------------------\n",
" get_training_threshold AS (\n",
" SELECT\n",
" (MAX(event_timestamp) - 86400000000) AS training_thrs\n",
" (MAX(event_timestamp) - 10800000000) AS training_thrs\n",
" FROM\n",
" `firebase-public-project.analytics_153293282.events_*`\n",
" WHERE\n",
" event_name=\"user_engagement\"\n",
" AND\n",
" PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', FORMAT_TIMESTAMP('%Y-%m-%d %H:%M:%S', TIMESTAMP_MICROS(event_timestamp))) < '{TODAY}'),\n",
" TIMESTAMP_ADD(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', FORMAT_TIMESTAMP('%Y-%m-%d %H:%M:%S', TIMESTAMP_MICROS(event_timestamp))), INTERVAL 1351 DAY) < '{TODAY}'),\n",
"\n",
" # query to create label -----------------------------------------------------------------------------------\n",
" get_label AS (\n",
@@ -1549,7 +1692,7 @@
" WHERE\n",
" event_name=\"user_engagement\"\n",
" AND\n",
" PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', FORMAT_TIMESTAMP('%Y-%m-%d %H:%M:%S', TIMESTAMP_MICROS(event_timestamp))) < '{TODAY}'\n",
" TIMESTAMP_ADD(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', FORMAT_TIMESTAMP('%Y-%m-%d %H:%M:%S', TIMESTAMP_MICROS(event_timestamp))), INTERVAL 1351 DAY) < '{TODAY}'\n",
" GROUP BY\n",
" user_pseudo_id )\n",
" GROUP BY\n",
@@ -1669,7 +1812,7 @@
"source": [
"!mkdir -m 777 -p trainer data/ingest data/raw model config\n",
"!gsutil -m cp -r $GCS_DESTINATION_OUTPUT_URI/*.csv data/ingest\n",
"!head -n 1000 data/ingest/*.csv > data/raw/sample.csv"
"!head -n 2000 data/ingest/*.csv > data/raw/sample.csv"
]
},
{
@@ -2071,7 +2214,7 @@
},
"outputs": [],
"source": [
"TRAIN_JOB_RESOURCE_NAME = \"\" # @param {type:\"string\"}"
"TRAIN_JOB_RESOURCE_NAME = \"[your-train-job-resource-name]\" # @param {type:\"string\"}"
]
},
{
@@ -2166,32 +2309,32 @@
{
"cell_type": "markdown",
"metadata": {
"id": "7c9330928aa1"
"id": "1TNzL_EGrVUm"
},
"source": [
"# Serve ML features at scale with low latency\n",
"\n",
"At that time, you are ready **to deploy our simple model which would requires fetching preprocessed attributes as input features in real time**. \n",
"\n",
" \n",
"At that time, you are ready **to deploy our simple model which would requires fetching preprocessed attributes as input features in real time**.\n",
" \n",
"Below you can see how it works\n",
"\n",
"<img src=\"./assets/online_serving_5.png\" width=\"600\">\n",
"\n",
"But think about those features for a second. \n",
"\n",
"Your behavioral features used to trained your model, they cannot be computed when you are going to serve the model online. \n",
"\n",
"How could you compute the number of time a user challenged a friend withing the last 24 hours on the fly?\n",
"\n",
"You simply can't do that. You need to be computed this feature on the server side and serve it with low latency. And becuase Bigquery is not optimized for those read operations, we need a different service that allows singleton lookup where the result is a single row with many columns.\n",
"\n",
"Also, even if it was not the case, when you deploy a model that requires preprocessing your data, you need to be sure to reproduce the same preprocessing steps you had when you trained it. If you are not able to do that a skew between training and serving data would happen and it will affect badly your model performance (and in the worst scenario break your serving system). \n",
"\n",
"You need a way to mitigate that in a way you don't need to implement those preprocessing steps online but just serve the same aggregated features you already have for training to generate online prediction. \n",
"\n",
"These are other valuable reasons to introduce Vertex AI Feature Store. With it, you have a service which helps you to serve feature at scale with low latency as they were available at training time mitigating in that way possible training-serving skew.\n",
"\n",
"Now that you know **why you need a feature store**, let's closing this journey by deploying your model and use feature store to retrieve features online, pass them to endpoint and generate predictions.\n"
" \n",
"<center><img src=\"./assets/online_serving_5.png\" width=\"800\"/></center>\n",
" \n",
"But think about those features for a second.\n",
" \n",
"Your behavioral features used to train your model, they cannot be computed when you are going to serve the model online.\n",
" \n",
"How could you compute the number of times a user challenged a friend within the last 24 hours on the fly?\n",
" \n",
"You need to be computed this feature on the server side and serve it with low latency. And because Bigquery is not optimized for those read operations, we need a different service that allows singleton lookup where the result is a single row with many columns.\n",
" \n",
"Also, even if it was not the case, when you deploy a model that requires preprocessing your data, you need to be sure to reproduce the same preprocessing steps you had when you trained it. If you are not able to do that a skew between training and serving data would happen and it will badly affect your model performance (and in the worst scenario break your serving system).\n",
" \n",
"You need a way to mitigate that in a way you don't need to implement those preprocessing steps online but just serve the same aggregated features you already have for training to generate online prediction.\n",
" \n",
"These are other valuable reasons to introduce Vertex AI Feature Store. With it, you have a service which helps you to serve features at scale with low latency as they were available at training time mitigating in that way possible training-serving skew.\n",
" \n",
"Now that you know **why you need a feature store**, let's conclude this journey by deploying your model using a feature store to retrieve features online, pass them to the endpoint and generate predictions.\n"
]
},
{
@@ -2221,13 +2364,13 @@
},
"outputs": [],
"source": [
"simulate_prediction(endpoint=endpoint, n_requests=1000, latency=1)"
"simulate_prediction(endpoint=endpoint, n_requests=10, latency=1)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "TpV-iwP9qw9c"
"id": "8d3S1d1urZOy"
},
"source": [
"## Cleaning up\n",
@@ -2267,11 +2410,12 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "sx_vKniMq9ZX"
"id": "FMXT2akXrZOy"
},
"outputs": [],
"source": [
"# Delete bucket\n",
"delete_bucket = False\n",
"if (delete_bucket or os.getenv(\"IS_TESTING\")) and \"BUCKET_URI\" in globals():\n",
" ! gsutil -m rm -r $BUCKET_URI"
]
+2 -2
View File
@@ -22,7 +22,7 @@ The first stage in MLOps is the collection and preparation for the purpose of de
- Data is preprocessed for training and evaluation using Dataflow.
- Data augmentation is performed on-the-fly and is coupled with model feeding.
<img src='stage1.jpg'>
<img src='stage1v2.png'>
## Notebooks
@@ -76,7 +76,7 @@ The steps performed include:
- image data
```
[Get Started with Data Labeling](get_started_data_labeling.ipynb)
[Get Started with Data Labeling](get_started_with_data_labeling.ipynb)
```
The steps performed include:
@@ -174,7 +174,7 @@
"import os\n",
"\n",
"# The Vertex AI Workbench Notebook product has specific requirements\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\")\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\") and not os.getenv(\"VIRTUAL_ENV\")\n",
"IS_USER_MANAGED_WORKBENCH_NOTEBOOK = os.path.exists(\n",
" \"/opt/deeplearning/metadata/env_version\"\n",
")\n",
@@ -389,7 +389,7 @@
},
"outputs": [],
"source": [
"i # If you are running this notebook in Colab, run this cell and follow the\n",
"# If you are running this notebook in Colab, run this cell and follow the\n",
"# instructions to authenticate your GCP account. This provides access to your\n",
"# Cloud Storage bucket and lets you submit training jobs and prediction\n",
"# requests.\n",
@@ -1132,7 +1132,7 @@
"source": [
"dataframe[\"station_number\"] = pd.to_numeric(dataframe[\"station_number\"])\n",
"labels = dataframe[\"mean_temp\"]\n",
"data = dataframe.drop(4)\n",
"data = dataframe.drop([\"mean_temp\"], axis=1)\n",
"\n",
"dtrain = xgb.DMatrix(data, label=labels)"
]
@@ -159,7 +159,7 @@
"import os\n",
"\n",
"# The Vertex AI Workbench Notebook product has specific requirements\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\")\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\") and not os.getenv(\"VIRTUAL_ENV\")\n",
"IS_USER_MANAGED_WORKBENCH_NOTEBOOK = os.path.exists(\n",
" \"/opt/deeplearning/metadata/env_version\"\n",
")\n",
@@ -171,7 +171,7 @@
"import os\n",
"\n",
"# The Vertex AI Workbench Notebook product has specific requirements\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\")\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\") and not os.getenv(\"VIRTUAL_ENV\")\n",
"IS_USER_MANAGED_WORKBENCH_NOTEBOOK = os.path.exists(\n",
" \"/opt/deeplearning/metadata/env_version\"\n",
")\n",
@@ -188,7 +188,7 @@
"! pip3 install --upgrade google-cloud-aiplatform $USER_FLAG -q\n",
"! pip3 install --upgrade google-cloud-bigquery $USER_FLAG -q\n",
"! pip3 install -U tensorflow-io==0.18 $USER_FLAG -q\n",
"! pip3 install --upgrade future $USER_FLAG -q"
"! pip3 install --upgrade db-dtypes $USER_FLAG -q! pip3 install --upgrade future $USER_FLAG -q"
]
},
{
@@ -408,12 +408,11 @@
"import sys\n",
"\n",
"# If on Vertex AI Workbench, then don't execute this code\n",
"IS_COLAB = False\n",
"IS_COLAB = \"google.colab\" in sys.modules\n",
"if not os.path.exists(\"/opt/deeplearning/metadata/env_version\") and not os.getenv(\n",
" \"DL_ANACONDA_HOME\"\n",
"):\n",
" if \"google.colab\" in sys.modules:\n",
" IS_COLAB = True\n",
" from google.colab import auth as google_auth\n",
"\n",
" google_auth.authenticate_user()\n",
@@ -558,7 +557,7 @@
},
"outputs": [],
"source": [
"aip.init(project=PROJECT_ID, location=REGION)"
"aip.init(project=PROJECT_ID, location=REGION, staging_bucket=BUCKET_URI)"
]
},
{
@@ -612,26 +611,13 @@
"Learn more about [All dataset documentation](https://cloud.google.com/vertex-ai/docs/datasets/datasets)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "import_file:flowers,csv,icn"
},
"outputs": [],
"source": [
"IMPORT_FILE = (\n",
" \"gs://cloud-samples-data/vision/automl_classification/flowers/all_data_v2.csv\"\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "create_dataset:image,icn"
},
"source": [
"### Create the Dataset\n",
"### Create an Image Dataset\n",
"\n",
"Next, create the `Dataset` resource using the `create` method for the `ImageDataset` class, which takes the following parameters:\n",
"\n",
@@ -646,6 +632,19 @@
"Learn more about [ImageDataset](https://cloud.google.com/vertex-ai/docs/datasets/prepare-image)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "import_file:flowers,csv,icn"
},
"outputs": [],
"source": [
"IMPORT_FILE = (\n",
" \"gs://cloud-samples-data/vision/automl_classification/flowers/all_data_v2.csv\"\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
@@ -663,24 +662,13 @@
"print(dataset.resource_name)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "import_file:hmdb,csv,vcn"
},
"outputs": [],
"source": [
"IMPORT_FILE = \"gs://automl-video-demo-data/hmdb_split1_5classes_train_inf.csv\""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "create_dataset:video,vcn"
},
"source": [
"### Create the Dataset\n",
"### Create a Video Dataset\n",
"\n",
"Next, create the `Dataset` resource using the `create` method for the `VideoDataset` class, which takes the following parameters:\n",
"\n",
@@ -694,6 +682,17 @@
"Learn more about [VideoDataset](https://cloud.google.com/vertex-ai/docs/datasets/prepare-video)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "import_file:hmdb,csv,vcn"
},
"outputs": [],
"source": [
"IMPORT_FILE = \"gs://automl-video-demo-data/hmdb_split1_5classes_train_inf.csv\""
]
},
{
"cell_type": "code",
"execution_count": null,
@@ -711,24 +710,13 @@
"print(dataset.resource_name)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "import_file:happydb,csv,tcn"
},
"outputs": [],
"source": [
"IMPORT_FILE = \"gs://cloud-ml-data/NL-classification/happiness.csv\""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "create_dataset:text,tcn"
},
"source": [
"### Create the Dataset\n",
"### Create a Text Dataset\n",
"\n",
"Next, create the `Dataset` resource using the `create` method for the `TextDataset` class, which takes the following parameters:\n",
"\n",
@@ -743,6 +731,17 @@
"Learn more about [TextDataset](https://cloud.google.com/vertex-ai/docs/datasets/prepare-text)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "import_file:happydb,csv,tcn"
},
"outputs": [],
"source": [
"IMPORT_FILE = \"gs://cloud-ml-data/NL-classification/happiness.csv\""
]
},
{
"cell_type": "code",
"execution_count": null,
@@ -760,6 +759,24 @@
"print(dataset.resource_name)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "create_dataset:tabular,bq,lrg,v2"
},
"source": [
"### Create a Tabular Dataset\n",
"\n",
"#### CSV input data\n",
"\n",
"Next, create the `Dataset` resource using the `create` method for the `TabularDataset` class for CSV input data, which takes the following parameters:\n",
"\n",
"- `display_name`: The human readable name for the `Dataset` resource.\n",
"- `gcs_source`: A list of one or more dataset index files to import the data items into the `Dataset` resource.\n",
"\n",
"Learn more about [TabularDataset from CSV files](https://cloud.google.com/vertex-ai/docs/datasets/create-dataset-api#aiplatform_create_dataset_tabular_gcs_sample-python)"
]
},
{
"cell_type": "code",
"execution_count": null,
@@ -768,27 +785,50 @@
},
"outputs": [],
"source": [
"IMPORT_FILE = \"bq://bigquery-public-data.samples.gsod\"\n",
"BQ_TABLE = \"bigquery-public-data.samples.gsod\""
"IMPORT_FILE = \"gs://cloud-samples-data/tables/iris_1000.csv\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "create_dataset:tabular,bq,lrg,v2"
},
"outputs": [],
"source": [
"dataset = aip.TabularDataset.create(\n",
" display_name=\"example\" + \"_\" + TIMESTAMP, gcs_source=[IMPORT_FILE]\n",
")\n",
"\n",
"print(dataset.resource_name)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "create_dataset:tabular,bq,lrg,v2"
"id": "854dd1e0195c"
},
"source": [
"### Create the Dataset\n",
"#### BigQuery input data\n",
"\n",
"#### CSV input data\n",
"\n",
"Next, create the `Dataset` resource using the `create` method for the `TabularDataset` class, which takes the following parameters:\n",
"Next, create the `Dataset` resource using the `create` method for the `TabularDataset` class for BigQuery table input, which takes the following parameters:\n",
"\n",
"- `display_name`: The human readable name for the `Dataset` resource.\n",
"- `gcs_source`: A list of one or more dataset index files to import the data items into the `Dataset` resource.\n",
"- `labels`: User defined metadata. In this example, you store the location of the Cloud Storage bucket containing the user defined data.\n",
"- `bq_source`: A list of one or more BigQuery tables to import the data items into the `Dataset` resource.\n",
"\n",
"Learn more about [TabularDataset from CSV files](https://cloud.google.com/vertex-ai/docs/datasets/create-dataset-api#aiplatform_create_dataset_tabular_gcs_sample-python)"
"Learn more about [TabularDataset from BigQuery table](https://cloud.google.com/vertex-ai/docs/datasets/create-dataset-api#aiplatform_create_dataset_tabular_bigquery_sample-pythonn)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "86343c146300"
},
"outputs": [],
"source": [
"IMPORT_FILE = \"bq://bigquery-public-data.samples.gsod\"\n",
"BQ_TABLE = \"bigquery-public-data.samples.gsod\""
]
},
{
@@ -806,15 +846,63 @@
"print(dataset.resource_name)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "82e9fe20ce71"
},
"source": [
"#### Dataframe input data\n",
"\n",
"Next, create the `Dataset` resource using the `create_from_dataframe` method for the `TabularDataset` class for pandas dataframe input, which takes the following parameters:\n",
"\n",
"- `display_name`: The human readable name for the `Dataset` resource.\n",
"- `df_source`: The pandas dataframe to import the data items into the `Dataset` resource.\n",
"- `staging_path`: The BigQuery table to store the imported data."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "import_file:covid,csv,forecast"
"id": "3805f945ffdd"
},
"outputs": [],
"source": [
"IMPORT_FILE = \"gs://cloud-samples-data/ai-platform/covid/bigquery-public-covid-nyt-us-counties-train.csv\""
"# Download the table.\n",
"table = bigquery.TableReference.from_string(BQ_TABLE)\n",
"\n",
"rows = bqclient.list_rows(\n",
" table,\n",
" max_results=10000,\n",
" selected_fields=[\n",
" bigquery.SchemaField(\"station_number\", \"STRING\"),\n",
" bigquery.SchemaField(\"year\", \"INTEGER\"),\n",
" bigquery.SchemaField(\"month\", \"INTEGER\"),\n",
" bigquery.SchemaField(\"day\", \"INTEGER\"),\n",
" bigquery.SchemaField(\"mean_temp\", \"FLOAT\"),\n",
" ],\n",
")\n",
"\n",
"dataframe = rows.to_dataframe()\n",
"print(dataframe.head())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "create_dataset:tabular,bq,lrg,v2"
},
"outputs": [],
"source": [
"dataset = aip.TabularDataset.create_from_dataframe(\n",
" display_name=\"example\" + \"_\" + TIMESTAMP,\n",
" df_source=dataframe,\n",
" staging_path=f\"bq://{PROJECT_ID}.samples.gsod\",\n",
")\n",
"\n",
"print(dataset.resource_name)"
]
},
{
@@ -823,7 +911,7 @@
"id": "create_dataset:tabular,forecast,v2"
},
"source": [
"### Create the Dataset\n",
"### Create a Time Series Dataset\n",
"\n",
"Next, create the `Dataset` resource using the `create` method for the `TimeSeriesDataset` class, which takes the following parameters:\n",
"\n",
@@ -834,6 +922,17 @@
"Learn more about [TimeSeriesDataset](https://cloud.google.com/vertex-ai/docs/datasets/prepare-tabular)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "import_file:covid,csv,forecast"
},
"outputs": [],
"source": [
"IMPORT_FILE = \"gs://cloud-samples-data/ai-platform/covid/bigquery-public-covid-nyt-us-counties-train.csv\""
]
},
{
"cell_type": "code",
"execution_count": null,
@@ -1279,7 +1378,7 @@
"import os\n",
"import sys\n",
"\n",
"# If on Google Cloud Notebook, then don't execute this code\n",
"# If on Workbench AI Notebook, then don't execute this code\n",
"if not os.path.exists(\"/opt/deeplearning/metadata/env_version\"):\n",
" if \"google.colab\" in sys.modules:\n",
" ! pip3 install fsspec\n",
@@ -1620,11 +1719,15 @@
},
"outputs": [],
"source": [
"delete_bucket = False\n",
"\n",
"# Delete the dataset using the Vertex dataset object\n",
"dataset.delete()\n",
"datasets = aip.TabularDataset.list(filter=f'display_name=\"example_{TIMESTAMP}\"')\n",
"for dataset in datasets:\n",
" dataset.delete()\n",
"\n",
"# Delete the bucket\n",
"if os.getenv(\"IS_TESTING\"):\n",
"if delete_bucket or os.getenv(\"IS_TESTING\"):\n",
" ! gsutil rm -r $BUCKET_URI"
]
}
@@ -146,7 +146,7 @@
"import os\n",
"\n",
"# The Vertex AI Workbench Notebook product has specific requirements\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\")\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\") and not os.getenv(\"VIRTUAL_ENV\")\n",
"IS_USER_MANAGED_WORKBENCH_NOTEBOOK = os.path.exists(\n",
" \"/opt/deeplearning/metadata/env_version\"\n",
")\n",
@@ -142,7 +142,7 @@
"import os\n",
"\n",
"# The Vertex AI Workbench Notebook product has specific requirements\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\")\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\") and not os.getenv(\"VIRTUAL_ENV\")\n",
"IS_USER_MANAGED_WORKBENCH_NOTEBOOK = os.path.exists(\n",
" \"/opt/deeplearning/metadata/env_version\"\n",
")\n",
Binary file not shown.

Before

Width:  |  Height:  |  Size: 39 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 76 KiB

+31 -14
View File
@@ -25,7 +25,11 @@ The second stage in MLOps is experimenting in developing one or more baseline mo
- Use the What-if-Tool (WIT) to explore how the trained model would make predictions in different scenarios.
<img src='stage2.png'>
<img src='stage2v3.png'>
<br/>
<br/>
<br/>
<img src='stage2.2v1.png'>
## Notebooks
@@ -115,6 +119,32 @@ The steps performed include:
- Train a R model using `Vertex AI Trainingh` service with the R-to-Python training package.
```
[Get Started with Custom Training Packages (R) and Deployment in R environment](get_started_vertex_training_r_using_r_kernel.ipynb)
```
The steps performed include:
- Create a custom R training script
- Create a custom R serving script
- Create a custom R deployment (serving) container.
- Train the model using `Vertex AI` custom training.
- Create an `Endpoint` resource.
- Deploy the `Model` resource (trained R model) to the `Endpoint` resource.
- Make an online prediction.
```
[Get Started with Custom Training Packages (LightGBM)](get_started_vertex_training_lightgbm.ipynb)
```
The steps performed include:
- Training using a Python package.
- Save the model artifacts to Cloud Storage using GCSFuse.
- Construct a FastAPI prediction server.
- Construct a Dockerfile deployment image.
- Test the deployment image locally.
- Create a `Vertex AI Model` resource.
```
[Get Started with Distributed Training](get_started_vertex_distributed_training.ipynb)
```
@@ -205,19 +235,6 @@ The steps performed include:
```
The steps performed include:
- Get the training data.
- Configure training parameters for the Vertex AI TabNet container.
- Train the model using Vertex AI Training using CSV data.
- Upload the model as a Vertex AI Model resource.
- Deploy the Vertex AI Model resource to a Vertex AI Endpoint resource.
- Make a prediction with the deployed model.
- Hyperparameter tuning the Vertex AI TabNet model.
- Train the model using Vertex AI Training using BigQuery table.
```
[Get Started with Vertex AI TabNet builtin algorithm](get_started_with_tabnet.ipynb)
```
The steps performed include:
- Get the training data.
- Configure training parameters for the Vertex AI TabNet container.
- Train the model using Vertex AI Training using CSV data.
@@ -40,7 +40,7 @@
" </td>\n",
" <td>\n",
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/community/ml_ops/stage2/get_started_automl_training.ipynb\">\n",
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\\\" alt=\"Colab logo\"> Run in Colab\n",
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"> Run in Colab\n",
" </a>\n",
" </td>\n",
" <td>\n",
@@ -187,7 +187,7 @@
"import os\n",
"\n",
"# The Vertex AI Workbench Notebook product has specific requirements\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\")\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\") and not os.getenv(\"VIRTUAL_ENV\")\n",
"IS_USER_MANAGED_WORKBENCH_NOTEBOOK = os.path.exists(\n",
" \"/opt/deeplearning/metadata/env_version\"\n",
")\n",
@@ -134,7 +134,7 @@
"import os\n",
"\n",
"# The Vertex AI Workbench Notebook product has specific requirements\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\")\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\") and not os.getenv(\"VIRTUAL_ENV\")\n",
"IS_USER_MANAGED_WORKBENCH_NOTEBOOK = os.path.exists(\"/opt/deeplearning/metadata/env_version\")\n",
"\n",
"# Vertex AI Notebook requires dependencies to be installed with '--user'\n",
@@ -442,6 +442,54 @@
"! gsutil ls -al $BUCKET_URI"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "set_service_account"
},
"source": [
"#### Service Account\n",
"\n",
"You use a service account to create Vertex AI Pipeline jobs. If you do not want to use your project's Compute Engine service account, set `SERVICE_ACCOUNT` to another service account ID."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "set_service_account"
},
"outputs": [],
"source": [
"SERVICE_ACCOUNT = \"[your-service-account]\" # @param {type:\"string\"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "autoset_service_account"
},
"outputs": [],
"source": [
"if (\n",
" SERVICE_ACCOUNT == \"\"\n",
" or SERVICE_ACCOUNT is None\n",
" or SERVICE_ACCOUNT == \"[your-service-account]\"\n",
"):\n",
" # Get your service account from gcloud\n",
" if not IS_COLAB:\n",
" shell_output = !gcloud auth list 2>/dev/null\n",
" SERVICE_ACCOUNT = shell_output[2].replace(\"*\", \"\").strip()\n",
"\n",
" if IS_COLAB:\n",
" shell_output = ! gcloud projects describe $PROJECT_ID\n",
" project_number = shell_output[-1].split(\":\")[1].strip().replace(\"'\", \"\")\n",
" SERVICE_ACCOUNT = f\"{project_number}-compute@developer.gserviceaccount.com\"\n",
"\n",
" print(\"Service Account:\", SERVICE_ACCOUNT)"
]
},
{
"cell_type": "markdown",
"metadata": {
@@ -730,32 +778,6 @@
"print(\"{} created in {}\".format(tblname, job.ended - job.started))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "3b3aa4481cd7"
},
"outputs": [],
"source": [
"MODEL_QUERY = f\"\"\"\n",
"DROP MODEL `{BQ_DATASET_NAME}.{MODEL_NAME}`\n",
"\"\"\"\n",
"\n",
"job = bqclient.query(MODEL_QUERY)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "ef9e14b91475"
},
"outputs": [],
"source": [
"job"
]
},
{
"cell_type": "markdown",
"metadata": {
@@ -1185,24 +1207,21 @@
"\n",
"### Setting permissions to automatically register the model\n",
"\n",
"You need to set some additional IAM permissions for BigQuery ML to automatically upload and register the model after training. Depending on your service account, the setting of the permissions below may fail. In this case, we recommend executing the permissions in a Cloud Shell."
"You need to set some additional IAM permissions for BigQuery ML to automatically upload and register the model after training. Depending on your service account, the setting of the permissions below may fail. In this case, we recommend executing the permissions in a Cloud Shell.\n",
"\n",
"Learn more about [Setting permissions for Model Registry](https://cloud.devsite.corp.google.com/bigquery-ml/docs/managing-models-vertex\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "0472e888105e"
"id": "29229f72d13d"
},
"outputs": [],
"source": [
"! gcloud projects add-iam-policy-binding $PROJECT_ID \\\n",
" --member='serviceAccount:cloud-dataengine@system.gserviceaccount.com' \\\n",
" --role='roles/aiplatform.admin'\n",
"\n",
"! gcloud projects add-iam-policy-binding $PROJECT_ID \\\n",
" --member='user:cloud-dataengine@prod.google.com' \\\n",
" --role='roles/aiplatform.admin'"
" --member=serviceAccount:$SERVICE_ACCOUNT --role=roles/aiplatform.admin --condition=None"
]
},
{
@@ -1283,6 +1302,20 @@
"print(model.gca_resource)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "48e6ef5d5ffa"
},
"outputs": [],
"source": [
"models = aiplatform.Model.list()\n",
"for model in models:\n",
" if model.gca_resource.display_name.startswith(\"bqml\"):\n",
" print(model.gca_resource.display_name)"
]
},
{
"cell_type": "markdown",
"metadata": {
@@ -160,7 +160,7 @@
"import os\n",
"\n",
"# The Vertex AI Workbench Notebook product has specific requirements\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\")\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\") and not os.getenv(\"VIRTUAL_ENV\")\n",
"IS_USER_MANAGED_WORKBENCH_NOTEBOOK = os.path.exists(\n",
" \"/opt/deeplearning/metadata/env_version\"\n",
")\n",
@@ -40,7 +40,7 @@
" </td>\n",
" <td>\n",
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/community/ml_ops/stage2/get_started_vertex_experiments.ipynb\">\n",
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\\\" alt=\"Colab logo\"> Run in Colab\n",
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"> Run in Colab\n",
" </a>\n",
" </td>\n",
" <td>\n",
@@ -143,7 +143,7 @@
"import os\n",
"\n",
"# The Vertex AI Workbench Notebook product has specific requirements\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\")\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\") and not os.getenv(\"VIRTUAL_ENV\")\n",
"IS_USER_MANAGED_WORKBENCH_NOTEBOOK = os.path.exists(\n",
" \"/opt/deeplearning/metadata/env_version\"\n",
")\n",
@@ -41,7 +41,7 @@
" \n",
" <td>\n",
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/community/ml_ops/stage2/get_started_vertex_feature_store.ipynb\">\n",
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\\\" alt=\"Colab logo\"> Run in Colab\n",
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"> Run in Colab\n",
" </a>\n",
" </td>\n",
" \n",
@@ -145,7 +145,7 @@
"import os\n",
"\n",
"# The Vertex AI Workbench Notebook product has specific requirements\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\")\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\") and not os.getenv(\"VIRTUAL_ENV\")\n",
"IS_USER_MANAGED_WORKBENCH_NOTEBOOK = os.path.exists(\n",
" \"/opt/deeplearning/metadata/env_version\"\n",
")\n",
@@ -362,12 +362,11 @@
"import sys\n",
"\n",
"# If on Vertex AI Workbench, then don't execute this code\n",
"IS_COLAB = False\n",
"IS_COLAB = \"google.colab\" in sys.modules\n",
"if not os.path.exists(\"/opt/deeplearning/metadata/env_version\") and not os.getenv(\n",
" \"DL_ANACONDA_HOME\"\n",
"):\n",
" if \"google.colab\" in sys.modules:\n",
" IS_COLAB = True\n",
" from google.colab import auth as google_auth\n",
"\n",
" google_auth.authenticate_user()\n",
@@ -490,7 +489,7 @@
"outputs": [],
"source": [
"# Represents featurestore resource path.\n",
"FEATURESTORE_NAME = \"movies\"\n",
"FEATURESTORE_NAME = \"movies_\" + TIMESTAMP\n",
"\n",
"featurestore = aiplatform.Featurestore.create(\n",
" featurestore_id=FEATURESTORE_NAME,\n",
@@ -194,7 +194,7 @@
"import os\n",
"\n",
"# The Vertex AI Workbench Notebook product has specific requirements\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\")\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\") and not os.getenv(\"VIRTUAL_ENV\")\n",
"IS_USER_MANAGED_WORKBENCH_NOTEBOOK = os.path.exists(\n",
" \"/opt/deeplearning/metadata/env_version\"\n",
")\n",
@@ -40,7 +40,7 @@
" </td>\n",
" <td>\n",
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/community/ml_ops/stage2/get_started_vertex_training.ipynb\">\n",
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\\\" alt=\"Colab logo\"> Run in Colab\n",
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"> Run in Colab\n",
" </a>\n",
" </td>\n",
" <td>\n",
@@ -147,7 +147,7 @@
"import os\n",
"\n",
"# The Vertex AI Workbench Notebook product has specific requirements\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\")\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\") and not os.getenv(\"VIRTUAL_ENV\")\n",
"IS_USER_MANAGED_WORKBENCH_NOTEBOOK = os.path.exists(\n",
" \"/opt/deeplearning/metadata/env_version\"\n",
")\n",
File diff suppressed because it is too large Load Diff
@@ -191,7 +191,7 @@
"import os\n",
"\n",
"# The Vertex AI Workbench Notebook product has specific requirements\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\")\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\") and not os.getenv(\"VIRTUAL_ENV\")\n",
"IS_USER_MANAGED_WORKBENCH_NOTEBOOK = os.path.exists(\n",
" \"/opt/deeplearning/metadata/env_version\"\n",
")\n",
@@ -40,7 +40,7 @@
" </td>\n",
" <td>\n",
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/community/ml_ops/stage2/get_started_vertex_training_r.ipynb\">\n",
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\\\" alt=\"Colab logo\"> Run in Colab\n",
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"> Run in Colab\n",
" </a>\n",
" </td>\n",
" <td>\n",
@@ -148,7 +148,7 @@
"import os\n",
"\n",
"# The Vertex AI Workbench Notebook product has specific requirements\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\")\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\") and not os.getenv(\"VIRTUAL_ENV\")\n",
"IS_USER_MANAGED_WORKBENCH_NOTEBOOK = os.path.exists(\n",
" \"/opt/deeplearning/metadata/env_version\"\n",
")\n",
@@ -50,9 +50,6 @@
" </a>\n",
" </td>\n",
"</table>\n",
" </a>\n",
" </td>\n",
"</table>\n",
"<br/><br/><br/>"
]
},
@@ -186,7 +183,7 @@
"import os\n",
"\n",
"# The Vertex AI Workbench Notebook product has specific requirements\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\")\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\") and not os.getenv(\"VIRTUAL_ENV\")\n",
"IS_USER_MANAGED_WORKBENCH_NOTEBOOK = os.path.exists(\n",
" \"/opt/deeplearning/metadata/env_version\"\n",
")\n",
@@ -136,7 +136,7 @@
"import os\n",
"\n",
"# The Vertex AI Workbench Notebook product has specific requirements\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\")\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\") and not os.getenv(\"VIRTUAL_ENV\")\n",
"IS_USER_MANAGED_WORKBENCH_NOTEBOOK = os.path.exists(\n",
" \"/opt/deeplearning/metadata/env_version\"\n",
")\n",
@@ -40,7 +40,7 @@
" </td>\n",
" <td>\n",
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/community/ml_ops/stage2/get_started_vertex_vizier.ipynb\">\n",
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\\\" alt=\"Colab logo\"> Run in Colab\n",
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"> Run in Colab\n",
" </a>\n",
" </td>\n",
" <td>\n",
@@ -156,7 +156,7 @@
"import os\n",
"\n",
"# The Vertex AI Workbench Notebook product has specific requirements\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\")\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\") and not os.getenv(\"VIRTUAL_ENV\")\n",
"IS_USER_MANAGED_WORKBENCH_NOTEBOOK = os.path.exists(\n",
" \"/opt/deeplearning/metadata/env_version\"\n",
")\n",
@@ -39,7 +39,7 @@
" </td>\n",
" <td>\n",
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/community/ml_ops/stage2/get_started_with_cmek_training.ipynb\">\n",
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\\\" alt=\"Colab logo\"> Run in Colab\n",
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"> Run in Colab\n",
" </a>\n",
" </td>\n",
" <td>\n",
@@ -139,7 +139,7 @@
"import os\n",
"\n",
"# The Vertex AI Workbench Notebook product has specific requirements\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\")\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\") and not os.getenv(\"VIRTUAL_ENV\")\n",
"IS_USER_MANAGED_WORKBENCH_NOTEBOOK = os.path.exists(\n",
" \"/opt/deeplearning/metadata/env_version\"\n",
")\n",
@@ -133,7 +133,7 @@
"import os\n",
"\n",
"# The Vertex AI Workbench Notebook product has specific requirements\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\")\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\") and not os.getenv(\"VIRTUAL_ENV\")\n",
"IS_USER_MANAGED_WORKBENCH_NOTEBOOK = os.path.exists(\n",
" \"/opt/deeplearning/metadata/env_version\"\n",
")\n",
@@ -40,7 +40,7 @@
" </td>\n",
" <td>\n",
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/community/ml_ops/stage2/get_started_with_tfhub_models.ipynb\">\n",
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\\\" alt=\"Colab logo\"> Run in Colab\n",
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"> Run in Colab\n",
" </a>\n",
" </td>\n",
" <td>\n",
@@ -145,7 +145,7 @@
"import os\n",
"\n",
"# The Vertex AI Workbench Notebook product has specific requirements\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\")\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\") and not os.getenv(\"VIRTUAL_ENV\")\n",
"IS_USER_MANAGED_WORKBENCH_NOTEBOOK = os.path.exists(\n",
" \"/opt/deeplearning/metadata/env_version\"\n",
")\n",
@@ -84,11 +84,11 @@
"source": [
"### Dataset\n",
"\n",
"The dataset used for this tutorial is the [Patent PDF Samples with Extracted Structured Data](https://pantheon.corp.google.com/marketplace/details/global-patents/labeled-patents?project=kudos-333820) from Google Public Data Sets. \n",
"The dataset used for this tutorial is the [Patent PDF Samples with Extracted Structured Data](https://console.cloud.google.com/marketplace/product/global-patents/labeled-patents) from Google Public Data Sets. \n",
"\n",
"This dataset includes data extracted from over 300 patent documents issued in the US and EU. The dataset includes links to Google Cloud Storage blobs for the first page of each patent, in addition to a number of extracted entities. \n",
"\n",
"The data is published as a public dataset on `BigQuery`."
"The data is published as a [public dataset](https://cloud.google.com/bigquery/public-data) on `BigQuery`."
]
},
{
@@ -201,7 +201,7 @@
"import os\n",
"\n",
"# The Vertex AI Workbench Notebook product has specific requirements\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\")\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\") and not os.getenv(\"VIRTUAL_ENV\")\n",
"IS_USER_MANAGED_WORKBENCH_NOTEBOOK = os.path.exists(\n",
" \"/opt/deeplearning/metadata/env_version\"\n",
")\n",
@@ -40,7 +40,7 @@
" </td>\n",
" <td>\n",
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/community/ml_ops/stage2/mlops_experimentation.ipynb\">\n",
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\\\" alt=\"Colab logo\"> Run in Colab\n",
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"> Run in Colab\n",
" </a>\n",
" </td>\n",
" <td>\n",
@@ -182,7 +182,7 @@
"import os\n",
"\n",
"# The Vertex AI Workbench Notebook product has specific requirements\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\")\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\") and not os.getenv(\"VIRTUAL_ENV\")\n",
"IS_USER_MANAGED_WORKBENCH_NOTEBOOK = os.path.exists(\n",
" \"/opt/deeplearning/metadata/env_version\"\n",
")\n",
Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 103 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 130 KiB

+12 -1
View File
@@ -27,7 +27,7 @@ The third stage in MLOps is formalization to develop an automated pipeline proce
- Use early stop procedure in training script to detect failure to achieve training objective.
- Store the results of the trained model evaluation in Vertex AI ML Metadata.
<img src='stage3.png'>
<img src='stage3v3.png'>
## Notebooks
@@ -166,6 +166,17 @@ The steps performed in this tutorial include:
- Execute pipeline using customjob-level settings for machine resources
```
[Get Started with Apache Airflow and Vertex AI Pipelines](get_started_with_airflow_and_vertex_pipelines.ipynb)
```
The steps performed in this tutorial include:
- Create Cloud Composer environment.
- Upload Airflow DAG to Composer environment that performs data processing -- i.e., creates a BigQuery table from a CSV file.
- Create a Vertex Pipeline that triggers the Airflow DAG.
- Execute the `Vertex AI Pipeline`.
```
### E2E Stage Example
[Stage 3: Formalization](mlops_formalization.ipynb)
@@ -40,7 +40,7 @@
" </td>\n",
" <td>\n",
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/community/ml_ops/stage3/get_started_with_automl_pipeline_components.ipynb\">\n",
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\\\" alt=\"Colab logo\"> Run in Colab\n",
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"> Run in Colab\n",
" </a>\n",
" </td>\n",
" <td>\n",
@@ -133,7 +133,7 @@
"import os\n",
"\n",
"# The Vertex AI Workbench Notebook product has specific requirements\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\")\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\") and not os.getenv(\"VIRTUAL_ENV\")\n",
"IS_USER_MANAGED_WORKBENCH_NOTEBOOK = os.path.exists(\n",
" \"/opt/deeplearning/metadata/env_version\"\n",
")\n",
@@ -143,7 +143,7 @@
"import os\n",
"\n",
"# The Vertex AI Workbench Notebook product has specific requirements\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\")\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\") and not os.getenv(\"VIRTUAL_ENV\")\n",
"IS_USER_MANAGED_WORKBENCH_NOTEBOOK = os.path.exists(\n",
" \"/opt/deeplearning/metadata/env_version\"\n",
")\n",
@@ -149,7 +149,7 @@
"import os\n",
"\n",
"# The Vertex AI Workbench Notebook product has specific requirements\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\")\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\") and not os.getenv(\"VIRTUAL_ENV\")\n",
"IS_USER_MANAGED_WORKBENCH_NOTEBOOK = os.path.exists(\n",
" \"/opt/deeplearning/metadata/env_version\"\n",
")\n",
@@ -153,7 +153,7 @@
"import os\n",
"\n",
"# The Vertex AI Workbench Notebook product has specific requirements\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\")\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\") and not os.getenv(\"VIRTUAL_ENV\")\n",
"IS_USER_MANAGED_WORKBENCH_NOTEBOOK = os.path.exists(\n",
" \"/opt/deeplearning/metadata/env_version\"\n",
")\n",
Binary file not shown.

After

Width:  |  Height:  |  Size: 63 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 68 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 39 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 152 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 69 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 92 KiB

+20 -1
View File
@@ -36,7 +36,7 @@ This stage may be done entirely by MLOps. We recommend:
<img src='stage4.png'>
<img src='stage4v3.png'>
## Notebooks
@@ -75,6 +75,25 @@ The steps performed include:
- Query your pipeline run metadata.
```
[Get started with Vertex ML Metadata and AutoML](get_started_with_vertex_ml_metadata_and_automl.ipynb)
```
The steps performed include:
- Create a `Dataset` resource.
- Create a corresponding `google.VertexDataset` artifact.
- Train a model using `AutoML`.
- Create a corresponding `google.VertexModel` artifact.
- Create an `Endpoint` resource.
- Create a corresponding `google.Endpoint` artifact.
- Deploy the train model to the `Endpoint`.
- Create an execution and context for the `AutoML` training job and deployment.
- Add the corresponding artifacts and context to the execution.
- Add artifact links (event) to the execution.
- Display the execution graph.
```
Get started with custom model evaluation
Get started with A/B Testing
Binary file not shown.

After

Width:  |  Height:  |  Size: 352 KiB

@@ -576,7 +576,7 @@
"\n",
"Setup up the following constants for Vertex AI:\n",
"\n",
"- `API_ENDPOINT`: The Vertex AI API service endpoint for `FeatureStore` services."
"- `API_ENDPOINT`: The Vertex AI API service endpoint for `ML Metadata` services."
]
},
{
Binary file not shown.

Before

Width:  |  Height:  |  Size: 78 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 84 KiB

+45 -1
View File
@@ -2,11 +2,23 @@
## Purpose
Configure compute and networking requirements for containerized serving binaries for a production load.
## Recommendations
The fifth stage in MLOps is deployment to production of the blessed model, which will replace the previous blessed model in production. This stage may be done entirely by MLOps. We recommend:
<img src='stage5.png'>
- Deploy the blessed model from the Vertex Model Registry.
- Use the Google Container Registry for the deployment container.
- Attach, if any, serving function from the Vertex Model Registry to the deployed model.
- Use Vertex Pipelines for the deployment.
- For cloud models, deploy within the Google Cloud infrastructure.
- Use Vertex Prediction traffic split for production rollout.
- Use Vertex Prediction to set your criteria for scaling and load balancing.
<img src='stage5v3.png'>
## Notebooks
@@ -45,3 +57,35 @@ The steps performed include:
- Deploying a `Model` resource to a `Private Endpoint` resource.
- Send a prediction request to a `Private Endpoint`
```
[Get started with Vertex AI Endpoints and co-hosting models on shared VM](get_started_with_vertex_endpoint_and_shared_vm.ipynb)
```
The steps performed include:
- Upload a pre-trained image classification model as a `Model` resource (model A).
- Upload a pre-trained text sentence encoder model as a `Model` resource (model B).
- Create a shared VM deployment resource pool.
- List shared VM deployment resource pools.
- Create two `Endpoint` resources.
- Deploy first model (model A) to first `Endpoint` resource using shared VM deployment resource pool.
- Deploy second model (model B) to second `Endpoint` resource using shared VM deployment resource pool.
- Make a prediction request with first deployed model (model A).
- Make a prediction request with second deployed model (model B).
```
[Get started with Auto-Scaling for Vertex AI Endpoints](get_started_with_autoscaling.ipynb)
```
The steps performed include:
- Download a pretrained image classification model from TensorFlow Hub.
- Upload the pretrained model as a `Model` resource.
- Create an `Endpoint` resource.
- Deploy `Model` resource for no-scaling (single node).
- Deploy `Model` resource for manual scaling.
- Deploy `Model` resource for auto-scaling.
- Fine-tune scaling thresholds for CPU utilization.
- Fine-tune scaling thresholds for GPU utilization.
- Deploy mix of CPU and GPU model instances with auto-scaling to an `Endpoint` resource.
```
File diff suppressed because it is too large Load Diff
Binary file not shown.

After

Width:  |  Height:  |  Size: 84 KiB

+27 -3
View File
@@ -23,9 +23,7 @@ This stage may be done entirely by MLOps. We recommend:
- Features that dynamically change per example (e.g., bank balance) are stored in Vertex Feature Store.
<img src='stage6a.png'>
<img src='stage6b.png'>
<img src='stage6c.png'>
<img src='stage6v2.png'>
## Notebooks
@@ -177,4 +175,30 @@ The steps performed include:
```
The steps performed include:
1. Train the `Swivel` algorithm to generate embeddings (encoder) for the dataset.
2. Hyperparameter tune the trained `Swivel` encoder.
3. Make example predictions (embeddings) from then trained encoder.
4. Generate embeddings using the trained `Swivel` builtin algorithm.
5. Store embeddings to format supported by `Matching Engine`.
6. Create a `Matching Engine Index` for the embeddings.
7. Deploy the `Matching Engine Index` to a `Index Endpoint`.
8. Make a matching engine prediction request.
```
[Get started with Explainable AI and custom model server](get_started_with_xai_and_custom_server.ipynb)
```
The steps performed include:
- Locally train a Pytorch tabular classifier.
- Locally test the trained model.
- Build a HTTP server using FastAPI.
- Create a custom serving container with the trained model and FastAPI server.
- Locally test the custom serving container.
- Push the custom serving container to the Artifact Registry.
- Upload the custom serving container as a `Model` resource.
- Deploy the `Model` resource to an `Endpoint` resource.
- Make a prediction request to the deployed custom serving container.
- Make an explanation request to the deployed custom serving container.
```
File diff suppressed because it is too large Load Diff
@@ -1951,15 +1951,6 @@
"full_network_name = f\"projects/{PROJECT_NUMBER}/global/networks/{NETWORK}\""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Z4f5yEmatu8P"
},
"source": [
"The following function calls the deployed Vertex Prediction model using the sample query object input file. Note that it uses the model resource directly and doesn't require a deployed endpoint. Once you start the job, you can track its status on the [Cloud Console](https://console.cloud.google.com/vertex-ai/batch-predictions)."
]
},
{
"cell_type": "markdown",
"metadata": {
File diff suppressed because it is too large Load Diff
Binary file not shown.

Before

Width:  |  Height:  |  Size: 41 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 74 KiB

@@ -0,0 +1,36 @@
# Stage 7: Monitoring
## Purpose
Monitor predict requests to detect model degradation and alert or trigger degradation response procedures.
## Recommendations
Degradation includes, but not limited to:
1. Health - deterioration in the operational performance of the serving binary.
2. Latency - deterioration in the elapsed time to transmit a prediction response from the serving binary.
3. Serving skew - detection of a distribution difference between the training data and the data seen at serving. This may be either or both the features of the input or the prediction of the output.
4. Data drift - detection of a change of distribution in the input features of the serving data over time.
5. Concept drift - degradation of the business objective.
For cases of skew and drift, random samples of the serving requests/responses are collected in the serving binary. Another process continuously inspects the distribution of the random collected samples. This process may either alter or initiate retraining of the model once skew or drift exceeds pre-specified thresholds.
In the case of concept drift, one may initiate a rollback of the blessed model and/or renewed A/B testing of the blessed model and previous blessed models.
This stage may be done entirely by MLOps. We recommend:
- When manually inspecting the operation of the serving binary, attach to the serving binary using the Vertex Serving Binary Debugger.
- Use Google Cloud network monitoring to monitor the operational health of the serving binary.
- Store network monitoring logs in Cloud Storage and view logs using StackDriver.
- Use Vertex AI Model Monitoring to random sample prediction requests/responses and to measure distributions for skew and drift.
- Store sampled prediction requests/responses in Big Query.
- Use Vertex ML Metadata to periodically record serving distribution statistics.
- Use Vertex Explainable AI to manually inspect for concept drift in business objectives.
- Use Cloud Pub/Sub to automatically trigger re-training pipeline.
<img src='stage7v2.png'>
## Notebooks
### Get Started
Binary file not shown.

After

Width:  |  Height:  |  Size: 74 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 64 KiB

@@ -8,7 +8,7 @@
},
"outputs": [],
"source": [
"# Copyright 2021 Google LLC\n",
"# Copyright 2022 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",
@@ -26,25 +26,99 @@
{
"cell_type": "markdown",
"metadata": {
"id": "976753012196"
"id": "et4hRnB9mrau"
},
"source": [
"# Feedback or issues?\n",
"For any feedback or questions, please open an [issue](https://github.com/googleapis/python-aiplatform/issues)."
"<table align=\"left\">\n",
"\n",
" <td>\n",
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/master/notebooks/community/sdk/SDK_BigQuery_Custom_Container_Training.ipynb\">\n",
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"> Run in Colab\n",
" </a>\n",
" </td>\n",
" <td>\n",
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/master/notebooks/community/sdk/SDK_BigQuery_Custom_Container_Training.ipynb\">\n",
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\">\n",
" View on GitHub\n",
" </a>\n",
" </td>\n",
" <td>\n",
" <a href=\"https://console.cloud.google.com/vertex-ai/workbench/deploy-notebook?download_url=https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/master/notebooks/community/sdk/SDK_BigQuery_Custom_Container_Training.ipynb\">\n",
" <img src=\"https://lh3.googleusercontent.com/UiNooY4LUgW_oTvpsNhPpQzsstV5W8F7rYgxgGBD85cWJoLmrOzhVs_ksK_vgx40SHs7jCqkTkCk=e14-rj-sc0xffffff-h130-w32\" alt=\"Vertex AI logo\">\n",
" Open in Vertex AI Workbench\n",
" </a>\n",
" </td> \n",
"</table>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "8c3048cd1427"
"id": "wLMxmUTwn1td"
},
"source": [
"# Vertex SDK for Python: BigQuery Custom Container Training Example\n",
"To use this Jupyter notebook, copy the notebook to a Google Cloud Notebooks instance and open it. You can run each step, or cell, and see its results. To run a cell, use Shift+Enter. Jupyter automatically displays the return value of the last line in each cell. For more information about running notebooks in Google Cloud Notebook, see the [Google Cloud Notebook guide](https://cloud.google.com/vertex-ai/docs/general/notebooks).\n",
"### Overview \n",
"To use this Jupyter notebook, copy the notebook to a Google Cloud Notebooks instance and open it. You can run each step, or cell, and see its results. To run a cell, use Shift+Enter. Jupyter automatically displays the return value of the last line in each cell. For more information about running notebooks in Google Cloud Notebook, see the Google Cloud Notebook guide.. \n",
"\n",
"### Objective \n",
"\n",
"This notebook demonstrate how to create a Custom Model using Custom Container Training and a Big Query Dataset. It will require you provide a bucket where the dataset will be stored.\n",
"\n",
"Note: you may incur charges for training, prediction, storage or usage of other GCP products in connection with testing this SDK."
"Costs \n",
"This tutorial uses billable components of Google Cloud: \n",
"\n",
"Vertex AI\n",
"Cloud Storage\n",
"Learn about Vertex AI pricing and Cloud Storage pricing, and use the Pricing Calculator to generate a cost estimate based on your projected usage."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "g5dkyDy1obku"
},
"source": [
"### Set up your local development environment\n",
"\n",
"**If you are using Colab or Google Cloud Notebooks**, your environment already meets\n",
"all the requirements to run this notebook. You can skip this step."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "gyja44LCozU_"
},
"source": [
"**Otherwise**, make sure your environment meets this notebook's requirements.\n",
"You need the following:\n",
"\n",
"* The Google Cloud SDK\n",
"* Git\n",
"* Python 3\n",
"* virtualenv\n",
"* Jupyter notebook running in a virtual environment with Python 3\n",
"\n",
"The Google Cloud guide to [Setting up a Python development\n",
"environment](https://cloud.google.com/python/setup) and the [Jupyter\n",
"installation guide](https://jupyter.org/install) provide detailed instructions\n",
"for meeting these requirements. The following steps provide a condensed set of\n",
"instructions:\n",
"\n",
"1. [Install and initialize the Cloud SDK.](https://cloud.google.com/sdk/docs/)\n",
"\n",
"1. [Install Python 3.](https://cloud.google.com/python/setup#installing_python)\n",
"\n",
"1. [Install\n",
" virtualenv](https://cloud.google.com/python/setup#installing_and_using_virtualenv)\n",
" and create a virtual environment that uses Python 3. Activate the virtual environment.\n",
"\n",
"1. To install Jupyter, run `pip3 install jupyter` on the\n",
"command-line in a terminal shell.\n",
"\n",
"1. To launch Jupyter, run `jupyter notebook` on the command-line in a terminal shell.\n",
"\n",
"1. Open this notebook in the Jupyter Notebook Dashboard."
]
},
{
@@ -65,7 +139,7 @@
"id": "xOMNWzTbftDr"
},
"source": [
"# Install Vertex SDK for Python\n",
"# Install Vertex AI SDK for Python\n",
"\n",
"\n",
"After the SDK installation the kernel will be automatically restarted."
@@ -106,10 +180,223 @@
},
"outputs": [],
"source": [
"MY_PROJECT = \"YOUR PROJECT ID\"\n",
"import os\n",
"\n",
"PROJECT_ID = \"\"\n",
"\n",
"# Get your Google Cloud project ID from gcloud\n",
"if not os.getenv(\"IS_TESTING\"):\n",
" shell_output = !gcloud config list --format 'value(core.project)' 2>/dev/null\n",
" PROJECT_ID = shell_output[0]\n",
" print(\"Project ID: \", PROJECT_ID)\n",
"\n",
"MY_STAGING_BUCKET = \"gs://YOUR BUCKET\" # bucket should be in same region as ucaip"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "_HRQg6eXiolk"
},
"source": [
"Otherwise, set your project ID here."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "Sg6AbQRviolk"
},
"outputs": [],
"source": [
"if PROJECT_ID == \"\" or PROJECT_ID is None:\n",
" PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}\n",
" print(\"Project ID: \", PROJECT_ID)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "6x6CSodKjMmg"
},
"source": [
"### Authenticate your Google Cloud account\n",
"\n",
"**If you are using Google Cloud Notebooks**, your environment is already\n",
"authenticated. Skip this step"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ZaQd5jNwjP_0"
},
"source": [
"**If you are using Colab**, run the cell below and follow the instructions\n",
"when prompted to authenticate your account via oAuth.\n",
"\n",
"**Otherwise**, follow these steps:\n",
"\n",
"1. In the Cloud Console, go to the [**Create service account key**\n",
" page](https://console.cloud.google.com/apis/credentials/serviceaccountkey).\n",
"\n",
"2. Click **Create service account**.\n",
"\n",
"3. In the **Service account name** field, enter a name, and\n",
" click **Create**.\n",
"\n",
"4. In the **Grant this service account access to project** section, click the **Role** drop-down list. Type \"Vertex AI\"\n",
"into the filter box, and select\n",
" **Vertex AI Administrator**. Type \"Storage Object Admin\" into the filter box, and select **Storage Object Admin**.\n",
"\n",
"5. Click *Create*. A JSON file that contains your key downloads to your\n",
"local environment.\n",
"\n",
"6. Enter the path to your service account key as the\n",
"`GOOGLE_APPLICATION_CREDENTIALS` variable in the cell below and run the cell."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "idpLDmlyjWyU"
},
"outputs": [],
"source": [
"import os\n",
"import sys\n",
"\n",
"# If you are running this notebook in Colab, run this cell and follow the\n",
"# instructions to authenticate your GCP account. This provides access to your\n",
"# Cloud Storage bucket and lets you submit training jobs and prediction\n",
"# requests.\n",
"\n",
"# The Google Cloud Notebook product has specific requirements\n",
"IS_GOOGLE_CLOUD_NOTEBOOK = os.path.exists(\"/opt/deeplearning/metadata/env_version\")\n",
"\n",
"# If on Google Cloud Notebooks, then don't execute this code\n",
"if not IS_GOOGLE_CLOUD_NOTEBOOK:\n",
" if \"google.colab\" in sys.modules:\n",
" from google.colab import auth as google_auth\n",
"\n",
" google_auth.authenticate_user()\n",
"\n",
" # If you are running this notebook locally, replace the string below with the\n",
" # path to your service account key and run this cell to authenticate your GCP\n",
" # account.\n",
" elif not os.getenv(\"IS_TESTING\"):\n",
" %env GOOGLE_APPLICATION_CREDENTIALS ''"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "r2lr6-MVpXLP"
},
"source": [
"#### Timestamp\n",
"\n",
"If you are in a live tutorial session, you might be using a shared test account or project. To avoid name collisions between users on resources created, you create a timestamp for each instance session, and append it onto the name of resources you create in this tutorial."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "he2lcG3Jpdxu"
},
"outputs": [],
"source": [
"from datetime import datetime\n",
"\n",
"TIMESTAMP = datetime.now().strftime(\"%Y%m%d%H%M%S\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "VxkQeJLyjgyr"
},
"source": [
"### Create a Cloud Storage bucket\n",
"\n",
"**The following steps are required, regardless of your notebook environment.**\n",
"\n",
"When you submit a training job using the Cloud SDK, you upload a Python package\n",
"containing your training code to a Cloud Storage bucket. Vertex AI runs\n",
"the code from this package. In this tutorial, Vertex AI also saves the\n",
"trained model that results from your job in the same bucket. Using this model artifact, you can then\n",
"create Vertex AI model and endpoint resources in order to serve\n",
"online predictions.\n",
"\n",
"Set the name of your Cloud Storage bucket below. It must be unique across all\n",
"Cloud Storage buckets.\n",
"\n",
"You may also change the `REGION` variable, which is used for operations\n",
"throughout the rest of this notebook. We suggest that you [choose a region where Vertex AI services are\n",
"available](https://cloud.google.com/vertex-ai/docs/general/locations#available_regions)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "GF076Vmoioll"
},
"outputs": [],
"source": [
"BUCKET_URI = \"gs://[your-bucket-name]\" # @param {type:\"string\"}\n",
"REGION = \"[your-region]\" # @param {type:\"string\"}\n",
"\n",
"if BUCKET_URI == \"\" or BUCKET_URI is None or BUCKET_URI == \"gs://[your-bucket-name]\":\n",
" BUCKET_URI = \"gs://\" + PROJECT_ID + \"aip-\" + TIMESTAMP\n",
"\n",
"if REGION == \"[your-region]\":\n",
" REGION = \"us-central1\""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ElTizrkXiolm"
},
"source": [
"**Only if your bucket doesn't already exist**: Run the following cell to create your Cloud Storage bucket."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "3ar8qPT6iolm"
},
"outputs": [],
"source": [
"! gsutil mb -l $REGION -p $PROJECT_ID $BUCKET_URI"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "T_Hq3rtPiolm"
},
"source": [
"Finally, validate access to your Cloud Storage bucket by examining its contents:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "_KfwVmnIiolm"
},
"outputs": [],
"source": [
"! gsutil ls -al $BUCKET_URI"
]
},
{
"cell_type": "markdown",
"metadata": {
@@ -139,8 +426,8 @@
"source": [
"import os\n",
"\n",
"os.environ[\"GOOGLE_CLOUD_PROJECT\"] = MY_PROJECT\n",
"!bq mk {MY_PROJECT}:ml_datasets"
"os.environ[\"GOOGLE_CLOUD_PROJECT\"] = PROJECT_ID\n",
"!bq mk {PROJECT_ID}:ml_datasets"
]
},
{
@@ -160,7 +447,7 @@
},
"outputs": [],
"source": [
"!bq cp -n bigquery-public-data:ml_datasets.iris {MY_PROJECT}:ml_datasets.iris"
"!bq cp -n --project_id={PROJECT_ID} bigquery-public-data:ml_datasets.iris {PROJECT_ID}:ml_datasets.iris "
]
},
{
@@ -205,9 +492,9 @@
"source": [
"cloudbuild_yaml = \"\"\"steps:\n",
"- name: 'gcr.io/cloud-builders/docker'\n",
" args: [ 'build', '-t', 'gcr.io/{MY_PROJECT}/test-custom-container', '.' ]\n",
"images: ['gcr.io/{MY_PROJECT}/test-custom-container']\"\"\".format(\n",
" MY_PROJECT=MY_PROJECT\n",
" args: [ 'build', '-t', 'gcr.io/{PROJECT_ID}/test-custom-container', '.' ]\n",
"images: ['gcr.io/{PROJECT_ID}/test-custom-container']\"\"\".format(\n",
" PROJECT_ID=PROJECT_ID\n",
")\n",
"\n",
"with open(f\"{CONTAINER_ARTIFACTS_DIR}/cloudbuild.yaml\", \"w\") as fp:\n",
@@ -220,7 +507,7 @@
"id": "gQ_GUCtZftDz"
},
"source": [
"### Write Dockerfile"
"### Write The Dockerfile"
]
},
{
@@ -253,7 +540,7 @@
"id": "9dfrLShaftDz"
},
"source": [
"### Write entrypoint script to invoke trainer"
"### Write the entrypoint script to invoke trainer"
]
},
{
@@ -357,7 +644,7 @@
"id": "6LYlV4D2ftD0"
},
"source": [
"### Build Container"
"### Build The Container"
]
},
{
@@ -368,7 +655,7 @@
},
"outputs": [],
"source": [
"!gcloud builds submit --config {CONTAINER_ARTIFACTS_DIR}/cloudbuild.yaml {CONTAINER_ARTIFACTS_DIR}"
"!gcloud builds submit --project={PROJECT_ID} --config {CONTAINER_ARTIFACTS_DIR}/cloudbuild.yaml {CONTAINER_ARTIFACTS_DIR}"
]
},
{
@@ -377,7 +664,7 @@
"id": "Pf0pugbvftD1"
},
"source": [
"# Run Custom Container Training"
"# Run The Custom Container Training"
]
},
{
@@ -386,7 +673,7 @@
"id": "7ee691569d8d"
},
"source": [
"## Initialize Vertex SDK for Python\n",
"## Initialize The Vertex AI SDK for Python\n",
"\n",
"Initialize the *client* for Vertex AI"
]
@@ -401,7 +688,7 @@
"source": [
"from google.cloud import aiplatform\n",
"\n",
"aiplatform.init(project=MY_PROJECT, staging_bucket=MY_STAGING_BUCKET)"
"aiplatform.init(project=PROJECT_ID, staging_bucket=BUCKET_URI)"
]
},
{
@@ -424,7 +711,7 @@
"outputs": [],
"source": [
"ds = aiplatform.TabularDataset.create(\n",
" display_name=\"bq_iris_dataset\", bq_source=f\"bq://{MY_PROJECT}.ml_datasets.iris\"\n",
" display_name=\"bq_iris_dataset\", bq_source=f\"bq://{PROJECT_ID}.ml_datasets.iris\"\n",
")"
]
},
@@ -434,7 +721,7 @@
"id": "ee242cc1f74c"
},
"source": [
"# Launch a Training Job to Create a Model\n",
"# Launch The Training Job to Create a Model\n",
"\n",
"We will train a model with the container we built above."
]
@@ -449,14 +736,14 @@
"source": [
"job = aiplatform.CustomContainerTrainingJob(\n",
" display_name=\"train-bq-iris\",\n",
" container_uri=f\"gcr.io/{MY_PROJECT}/test-custom-container:latest\",\n",
" container_uri=f\"gcr.io/{PROJECT_ID}/test-custom-container:latest\",\n",
" model_serving_container_image_uri=\"gcr.io/cloud-aiplatform/prediction/tf2-cpu.2-2:latest\",\n",
")\n",
"model = job.run(\n",
" ds,\n",
" replica_count=1,\n",
" model_display_name=\"bq-iris-model\",\n",
" bigquery_destination=f\"bq://{MY_PROJECT}\",\n",
" bigquery_destination=f\"bq://{PROJECT_ID}\",\n",
")"
]
},
@@ -466,7 +753,7 @@
"id": "a7fa9b59f919"
},
"source": [
"# Deploy Your Model\n",
"# Deploy The Model\n",
"\n",
"Deploy your model, then wait until the model FINISHES deployment before proceeding to prediction."
]
@@ -488,7 +775,7 @@
"id": "4dbd6c650a03"
},
"source": [
"# Predict on the Endpoint"
"# Make a prediction\n"
]
},
{
@@ -503,12 +790,46 @@
" [{\"sepal_length\": 5.1, \"sepal_width\": 2.5, \"petal_length\": 3.0, \"petal_width\": 1.1}]\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "MaoIczP8qu--"
},
"source": [
"## Cleaning up\n",
"\n",
"To clean up all Google Cloud resources used in this project, you can [delete the Google Cloud\n",
"project](https://cloud.google.com/resource-manager/docs/creating-managing-projects#shutting_down_projects) you used for the tutorial.\n",
"\n",
"Otherwise, you can delete the individual resources you created in this tutorial:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "-UaP-qoKqzc1"
},
"outputs": [],
"source": [
"import os\n",
"\n",
"# Delete endpoint resource\n",
"! gcloud ai endpoints delete $ENDPOINT_NAME --quiet --region $REGION_NAME\n",
"\n",
"# Delete Cloud Storage objects that were created\n",
"! gsutil -m rm -r $JOB_DIR\n",
"\n",
"if os.getenv(\"IS_TESTING\"):\n",
"! gsutil -m rm -r $BUCKET_URI "
]
}
],
"metadata": {
"colab": {
"collapsed_sections": [],
"name": "AI_Platform_(Unified)_SDK_BigQuery_Custom_Container_Training.ipynb",
"name": "SDK_BigQuery_Custom_Container_Training.ipynb",
"toc_visible": true
},
"kernelspec": {
@@ -67,9 +67,9 @@
"source": [
"## Overview\n",
"\n",
"In this sample we demonstrate how to fine-tune a BERT base classification model for sentiment analysis.\n",
"In this sample you learn how to fine-tune a BERT base classification model for sentiment analysis.\n",
"\n",
"Then we export a trained model to Vertex AI Prediction service using an open source based TensorFlow 2.7 container and the optimized TensorFlow runtime container. We benchmark those models so we can compare their predictions.\n",
"Then you export a trained model to Vertex AI Prediction service using an open source based TensorFlow 2.7 container and the optimized TensorFlow runtime container, run performance evaluation for those models and compare their predictions.\n",
"\n",
"For additional information about Vertex AI Prediction optimized TensorFlow runtime containers, see https://cloud.google.com/vertex-ai/docs/predictions/optimized-tensorflow-runtime.\n",
"\n",
@@ -116,7 +116,7 @@
"source": [
"### Set up your local development environment\n",
"\n",
"**If you are using Colab or Google Cloud Notebooks**, your environment already meets\n",
"**If you are using Colab or Vertex AI Workbench Notebooks**, your environment already meets\n",
"all the requirements to run this notebook. You can skip this step."
]
},
@@ -179,10 +179,10 @@
"source": [
"import os\n",
"\n",
"# The Google Cloud Notebook product has specific requirements\n",
"# The Vertex AI Workbench Notebook product has specific requirements\n",
"IS_GOOGLE_CLOUD_NOTEBOOK = os.path.exists(\"/opt/deeplearning/metadata/env_version\")\n",
"\n",
"# Google Cloud Notebook requires dependencies to be installed with '--user'\n",
"# Vertex AI Workbench Notebook requires dependencies to be installed with '--user'\n",
"USER_FLAG = \"\"\n",
"if IS_GOOGLE_CLOUD_NOTEBOOK:\n",
" USER_FLAG = \"--user\""
@@ -196,12 +196,12 @@
},
"outputs": [],
"source": [
"! pip3 install {USER_FLAG} --upgrade tensorflow==2.7.0\n",
"! pip3 install {USER_FLAG} --upgrade tensorflow-text==2.7.0\n",
"! pip3 install {USER_FLAG} --upgrade tensorflow-serving-api==2.7.0\n",
"! pip3 install {USER_FLAG} --upgrade tf-models-official==2.7.0\n",
"! pip3 install {USER_FLAG} --upgrade google-cloud-aiplatform\n",
"! pip3 install {USER_FLAG} --upgrade google-cloud-storage"
"! pip3 install {USER_FLAG} --upgrade tensorflow==2.7.0 -q\n",
"! pip3 install {USER_FLAG} --upgrade tensorflow-text==2.7.0 -q\n",
"! pip3 install {USER_FLAG} --upgrade tensorflow-serving-api==2.7.0 -q\n",
"! pip3 install {USER_FLAG} --upgrade tf-models-official==2.7.0 -q\n",
"! pip3 install {USER_FLAG} --upgrade google-cloud-aiplatform -q\n",
"! pip3 install {USER_FLAG} --upgrade google-cloud-storage -q"
]
},
{
@@ -363,7 +363,7 @@
"source": [
"### Authenticate your Google Cloud account\n",
"\n",
"**If you are using Google Cloud Notebooks**, your environment is already\n",
"**If you are using Vertex AI Workbench Notebooks**, your environment is already\n",
"authenticated. Skip this step."
]
},
@@ -412,10 +412,10 @@
"# Cloud Storage bucket and lets you submit training jobs and prediction\n",
"# requests.\n",
"\n",
"# The Google Cloud Notebook product has specific requirements\n",
"# The Vertex AI Workbench Notebook product has specific requirements\n",
"IS_GOOGLE_CLOUD_NOTEBOOK = os.path.exists(\"/opt/deeplearning/metadata/env_version\")\n",
"\n",
"# If on Google Cloud Notebooks, then don't execute this code\n",
"# If on Vertex AI Workbench Notebooks, then don't execute this code\n",
"if not IS_GOOGLE_CLOUD_NOTEBOOK:\n",
" if \"google.colab\" in sys.modules:\n",
" from google.colab import auth as google_auth\n",
@@ -571,7 +571,7 @@
"id": "9cbb5302a3c4"
},
"source": [
"## Downloading dataset"
"## Download the dataset"
]
},
{
@@ -612,7 +612,7 @@
"id": "8b7051896d19"
},
"source": [
"## Preprocess dataset"
"## Preprocess the dataset"
]
},
{
@@ -703,7 +703,7 @@
"id": "d56746554dd6"
},
"source": [
"As a base for our model we take uncased BERT-Base model from TensorFlow Hub:\n",
"As a base for our model you take uncased BERT-Base model from TensorFlow Hub:\n",
"https://tfhub.dev/tensorflow/bert_en_uncased_L-12_H-768_A-12/3"
]
},
@@ -757,7 +757,7 @@
},
"source": [
"Define a classification model by feeding the output BERT encoder model into dropout and dense layers.\n",
"For details, see the https://www.tensorflow.org/text/tutorials/classify_text_with_bert tutorial."
"Learn more about [Classify text with BERT]( https://www.tensorflow.org/text/tutorials/classify_text_with_bert)."
]
},
{
@@ -917,7 +917,7 @@
"id": "11cc837b06f9"
},
"source": [
"Check the model signature.\n",
"Check the model signature to see which fields prediction request should have.\n",
"\n",
"Note that you might see a stacktrace message about a missing 'CaseFoldUTF8' op. This is a known issue with `saved_model_cli` that you can ignore."
]
@@ -948,7 +948,7 @@
"id": "953eb5e1e86e"
},
"source": [
"Now we can generate requests to send to our model for inference. Requests are generated in the JSON Lines format, one request per line."
"Now you can generate requests to send to our model for inference. Requests are generated in the JSON Lines format, one request per line."
]
},
{
@@ -1047,7 +1047,7 @@
"id": "5bbdded5d2ab"
},
"source": [
"The TensorFlow runtime has components that are lazily initialized. Lazy initialization might result in high latency for the first requests that are sent to a model after it's loaded. This latency can be several orders of magnitude higher than that of a single inference request. \n",
"The TensorFlow runtime has components that are lazily initialized. Lazy initialization might result in high latency for the first requests that are sent to a model after it's loaded. This latency can be several orders of magnitude higher than that of a single inference request.\n",
"\n",
"For more information about SavedModel warmup, see https://www.tensorflow.org/tfx/serving/saved_model_warmup.\n",
"\n",
@@ -1209,9 +1209,9 @@
"id": "83f62a939359"
},
"source": [
"The AI Platform Python client library works as a client/server model. \n",
"The AI Platform Python client library works as a client/server model.\n",
"\n",
"We are going to use following clients in this sample:\n",
"You are going to use following clients in this sample:\n",
"- Model Service for managing models.\n",
"- Endpoint Service for deployment.\n",
"- Prediction Service for serving."
@@ -1254,7 +1254,7 @@
"\n",
"`artifact_uri` argument should point to a GCS path where `saved_model.pb` file is located for your model.\n",
"\n",
"`image_uri` specifies which docker image to use. Here we upload the same model using TF2.7 GPU and Vertex AI Prediction optimized TensorFlow runtime images."
"`image_uri` specifies which docker image to use. Here you upload the same model using TF2.7 GPU and Vertex AI Prediction optimized TensorFlow runtime images."
]
},
{
@@ -1386,7 +1386,7 @@
"id": "5ff288387d03"
},
"source": [
"See [endpoint_service.create_endpoint](https://cloud.google.com/python/docs/reference/aiplatform/latest/google.cloud.aiplatform_v1.services.endpoint_service.EndpointServiceClient#google_cloud_aiplatform_v1_services_endpoint_service_EndpointServiceClient_create_endpoint) API description for details."
"Learn more about [endpoint_service.create_endpoint](https://cloud.google.com/python/docs/reference/aiplatform/latest/google.cloud.aiplatform_v1.services.endpoint_service.EndpointServiceClient#google_cloud_aiplatform_v1_services_endpoint_service_EndpointServiceClient_create_endpoint)."
]
},
{
@@ -1467,7 +1467,7 @@
"id": "ebb2d55b45e0"
},
"source": [
"For details about the `enpoint_service.deploy_model` API, see [enpoint_service.deploy_model](https://cloud.google.com/python/docs/reference/aiplatform/latest/google.cloud.aiplatform_v1.services.endpoint_service.EndpointServiceClient#google_cloud_aiplatform_v1_services_endpoint_service_EndpointServiceClient_deploy_model)."
"Learn more about [enpoint_service.deploy_model](https://cloud.google.com/python/docs/reference/aiplatform/latest/google.cloud.aiplatform_v1.services.endpoint_service.EndpointServiceClient#google_cloud_aiplatform_v1_services_endpoint_service_EndpointServiceClient_deploy_model)."
]
},
{
@@ -1510,7 +1510,7 @@
"source": [
"tf_opt_gpu_deployed_model_dict = {\n",
" \"model\": tf_opt_gpu_model,\n",
" \"display_name\": \"Criteo Kaggle optimized TensorFlow runtime GPU model\",\n",
" \"display_name\": \"BERT Base optimized TensorFlow runtime GPU model\",\n",
" \"dedicated_resources\": {\n",
" \"min_replica_count\": 1,\n",
" \"max_replica_count\": 1,\n",
@@ -1540,7 +1540,7 @@
"source": [
"tf_opt_lossy_gpu_deployed_model_dict = {\n",
" \"model\": tf_opt_lossy_gpu_model,\n",
" \"display_name\": \"Criteo Kaggle optimized TensorFlow runtime GPU model with lossy optimizations\",\n",
" \"display_name\": \"BERT Base optimized TensorFlow runtime GPU model with lossy optimizations\",\n",
" \"dedicated_resources\": {\n",
" \"min_replica_count\": 1,\n",
" \"max_replica_count\": 1,\n",
@@ -1598,7 +1598,7 @@
"id": "ca1dfbcdf81e"
},
"source": [
"Alternatively you can send POST REST requests without using the SDK. For more details, see https://cloud.google.com/vertex-ai/docs/predictions/online-predictions-custom-models#online_predict_custom_trained-drest.\n",
"Alternatively you can send POST REST requests without using the SDK. Learn more about https://cloud.google.com/vertex-ai/docs/predictions/online-predictions-custom-models#online_predict_custom_trained-drest.\n",
"This method is slightly faster."
]
},
@@ -1904,6 +1904,24 @@
"You can see that the Vertex AI Prediction optimized TensorFlow runtime has signficantly higher throughput and lower latency compared to TensorFlow 2.7."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "06f2711e6221"
},
"source": [
"## (Optional) Compare performance of deployed models using MLPerf Inference loadgen"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "adcd7c46de26"
},
"source": [
"MLPerf Inference is a benchmark suite for measuring how fast systems can run models in a variety of deployment scenarios. MLPerf is now an industry standard way of measuring model performance. You can follow instructions at https://github.com/tensorflow/tpu/tree/master/models/experimental/inference/load_test to run MLPerf Inferenence benchmark for deployed models."
]
},
{
"cell_type": "markdown",
"metadata": {
@@ -1921,7 +1939,7 @@
"source": [
"In this sample the Vertex Prediction optimized TensorFlow runtime is used with the `allow_precision_affecting_optimizations` flag set to `true` to gain additional speedup. Now let's check how those optimizations effect prediction results.\n",
"\n",
"We compare the results of predictions for 32,000 requests for a model running on the optimized TensorFlow runtime with lossy optimizations and on TF2.7."
"Compare the results of predictions for 32,000 requests for a model running on the optimized TensorFlow runtime with lossy optimizations and on TF2.7."
]
},
{
@@ -2083,7 +2101,10 @@
},
"outputs": [],
"source": [
"!gsutil rm -r $BUCKET_URI"
"# Set this to true only if you'd like to delete your bucket\n",
"delete_bucket = False\n",
"if delete_bucket or os.getenv(\"IS_TESTING\"):\n",
" !gsutil rm -r $BUCKET_URI"
]
}
],
@@ -67,14 +67,14 @@
"source": [
"## Overview\n",
"\n",
"In this sample we demonstrate how to train a tabular model using TensorFlow Keras or Estimator API using Criteo Kaggle dataset.\n",
"Next, we export a trained model to the Vertex AI Prediction service using open source based TensorFlow 2.7 container and the optimized TensorFlow runtime container. We benchmark those models side by side and compare predictions.\n",
"In this sample you learn how to train a tabular model using TensorFlow Keras or Estimator API using Criteo Kaggle dataset.\n",
"Next, you export a trained model to the Vertex AI Prediction service using open source based TensorFlow 2.7 container and the optimized TensorFlow runtime container, run performance evaluation for those models side by side and compare predictions.\n",
"\n",
"For additional information about Vertex AI Prediction optimized TensorFlow runtime containers, please refer to https://cloud.google.com/vertex-ai/docs/predictions/optimized-tensorflow-runtime.\n",
"\n",
"### Dataset\n",
"\n",
"We use Criteo Kaggle dataset, which takes about 4GB in this sample.\n",
"In this sample you use Criteo Kaggle dataset, which takes about 4GB.\n",
"\n",
"\n",
"### Objective\n",
@@ -113,7 +113,7 @@
"source": [
"### Set up your local development environment\n",
"\n",
"**If you are using Colab or Google Cloud Notebooks**, your environment meets the requirements to run this notebook. You can skip this step."
"**If you are using Colab or Vertex AI Workbench Notebooks**, your environment meets the requirements to run this notebook. You can skip this step."
]
},
{
@@ -122,7 +122,7 @@
"id": "gCuSR8GkAgzl"
},
"source": [
"**If you are not using Colab or Google Cloud Notebooks**, you must have the following in your environment to meet this notebook's requirements.\n",
"**If you are not using Colab or Vertex AI Workbench Notebooks**, you must have the following in your environment to meet this notebook's requirements.\n",
"\n",
"* The Google Cloud SDK\n",
"* Git\n",
@@ -172,10 +172,10 @@
"source": [
"import os\n",
"\n",
"# The Google Cloud Notebook product has specific requirements\n",
"# The Vertex AI Workbench Notebook product has specific requirements\n",
"IS_GOOGLE_CLOUD_NOTEBOOK = os.path.exists(\"/opt/deeplearning/metadata/env_version\")\n",
"\n",
"# Google Cloud Notebook requires dependencies to be installed with '--user'\n",
"# Vertex AI Workbench Notebook requires dependencies to be installed with '--user'\n",
"USER_FLAG = \"\"\n",
"if IS_GOOGLE_CLOUD_NOTEBOOK:\n",
" USER_FLAG = \"--user\""
@@ -189,10 +189,10 @@
},
"outputs": [],
"source": [
"! pip3 install {USER_FLAG} --upgrade tensorflow==2.7.0\n",
"! pip3 install {USER_FLAG} --upgrade tensorflow-serving-api==2.7.0\n",
"! pip3 install {USER_FLAG} --upgrade google-cloud-aiplatform\n",
"! pip3 install {USER_FLAG} --upgrade google-cloud-storage"
"! pip3 install {USER_FLAG} --upgrade tensorflow==2.7.0 -q\n",
"! pip3 install {USER_FLAG} --upgrade tensorflow-serving-api==2.7.0 -q\n",
"! pip3 install {USER_FLAG} --upgrade google-cloud-aiplatform -q\n",
"! pip3 install {USER_FLAG} --upgrade google-cloud-storage -q"
]
},
{
@@ -345,7 +345,7 @@
"source": [
"### Authenticate your Google Cloud account\n",
"\n",
"**If you are using Google Cloud Notebooks**, your environment is already\n",
"**If you are using Vertex AI Workbench Notebooks**, your environment is already\n",
"authenticated. Skip this step."
]
},
@@ -395,10 +395,10 @@
"# Cloud Storage bucket and lets you submit training jobs and prediction\n",
"# requests.\n",
"\n",
"# The Google Cloud Notebook product has specific requirements\n",
"# The Vertex AI Workbench Notebook product has specific requirements\n",
"IS_GOOGLE_CLOUD_NOTEBOOK = os.path.exists(\"/opt/deeplearning/metadata/env_version\")\n",
"\n",
"# If on Google Cloud Notebooks, then don't execute this code\n",
"# If on Vertex AI Workbench Notebooks, then don't execute this code\n",
"if not IS_GOOGLE_CLOUD_NOTEBOOK:\n",
" if \"google.colab\" in sys.modules:\n",
" from google.colab import auth as google_auth\n",
@@ -556,7 +556,7 @@
"id": "9ee07345bcc6"
},
"source": [
"## Downloading Dataset"
"## Download the dataset"
]
},
{
@@ -620,7 +620,7 @@
"id": "ng-Nrc8UdbP2"
},
"source": [
"## Reading and transforming dataset\n",
"## Read and transform the dataset\n",
"\n",
"Before the model can be trained, the variables must be pre-processed.\n",
"\n",
@@ -954,7 +954,7 @@
"id": "AVibdmrufYRA"
},
"source": [
"Check the model signature."
"Check the model signature to see which fields prediction request should have."
]
},
{
@@ -986,7 +986,7 @@
"Another option to train a model is to use the TensorFlow Estimator API. For more information, see\n",
"https://github.com/tensorflow/docs/blob/r2.4/site/en/tutorials/estimator/premade.ipynb\n",
"\n",
"The following code is provided only for illustration purposes. We use the Keras model for deployment."
"The following code is provided only for illustration purposes. You use the Keras model for deployment."
]
},
{
@@ -1232,7 +1232,7 @@
"id": "9c75a550809d"
},
"source": [
"The TensorFlow runtime has components that are lazily initialized. Lazy initialization might result in high latency for the first requests that are sent to a model after it's loaded. This latency can be several orders of magnitude higher than that of a single inference request. \n",
"The TensorFlow runtime has components that are lazily initialized. Lazy initialization might result in high latency for the first requests that are sent to a model after it's loaded. This latency can be several orders of magnitude higher than that of a single inference request.\n",
"\n",
"For more information about SavedModel warmup, see https://www.tensorflow.org/tfx/serving/saved_model_warmup.\n",
"\n",
@@ -1435,7 +1435,7 @@
"id": "9d259d62f8a4"
},
"source": [
"The throughput and latency of the Criteo model we trained is sensitive to network performance."
"The throughput and latency of the Criteo model you trained is sensitive to network performance."
]
},
{
@@ -1488,7 +1488,7 @@
"id": "743b3af8f747"
},
"source": [
"For simplicity, we setup VPC peering to the default network. You can create a different network for your project.\n",
"For simplicity, you setup VPC peering to the default network. You can create a different network for your project.\n",
"\n",
"If you setup VPC peering with any other network, make sure that the network already exists and that your VM is running on that network."
]
@@ -1598,12 +1598,16 @@
"id": "lYFkS2H9dDfd"
},
"source": [
"See [model_service.upload_model](https://cloud.google.com/python/docs/reference/aiplatform/latest/google.cloud.aiplatform_v1.services.model_service.ModelServiceClient#google_cloud_aiplatform_v1_services_model_service_ModelServiceClient_upload_model) documentation for details.\n",
"Learn more about [model_service.upload_model](https://cloud.google.com/python/docs/reference/aiplatform/latest/google.cloud.aiplatform_v1.services.model_service.ModelServiceClient#google_cloud_aiplatform_v1_services_model_service_ModelServiceClient_upload_model).\n",
"\n",
"\n",
"`artifact_uri` argument should point to a GCS path where `saved_model.pb` file is located for your model.\n",
"\n",
"`image_uri` specifies which docker image to use. Here we upload the same model using TF2.7 GPU and Vertex AI Prediction optimized TensorFlow runtime images."
"`image_uri` specifies which docker image to use. Here we upload the same model using TF2.7 GPU and Vertex AI Prediction optimized TensorFlow runtime images.\n",
"\n",
"In order to be able to send requests to your models over gRPC, you need to set `model_name` argument and update `predict_route` and `health_route` accordingly.\n",
"\n",
"Please note that gRPC support in Vertex AI Prediction is still experimental."
]
},
{
@@ -1619,6 +1623,15 @@
" \"artifact_uri\": BUCKET_URI,\n",
" \"container_spec\": {\n",
" \"image_uri\": \"us-docker.pkg.dev/vertex-ai/prediction/tf2-cpu.2-7:latest\",\n",
" \"args\": [\n",
" \"--port=8500\",\n",
" \"--rest_api_port=8080\",\n",
" \"--model_name=default\",\n",
" \"--model_base_path=$(AIP_STORAGE_URI)\",\n",
" ],\n",
" \"ports\": [{\"container_port\": 8080}],\n",
" \"predict_route\": \"/v1/models/default:predict\",\n",
" \"health_route\": \"/v1/models/default\",\n",
" },\n",
"}\n",
"tf27_cpu_model = (\n",
@@ -1642,6 +1655,15 @@
" \"artifact_uri\": BUCKET_URI,\n",
" \"container_spec\": {\n",
" \"image_uri\": \"us-docker.pkg.dev/vertex-ai/prediction/tf2-gpu.2-7:latest\",\n",
" \"args\": [\n",
" \"--port=8500\",\n",
" \"--rest_api_port=8080\",\n",
" \"--model_name=default\",\n",
" \"--model_base_path=$(AIP_STORAGE_URI)\",\n",
" ],\n",
" \"ports\": [{\"container_port\": 8080}],\n",
" \"predict_route\": \"/v1/models/default:predict\",\n",
" \"health_route\": \"/v1/models/default\",\n",
" },\n",
"}\n",
"tf27_gpu_model = (\n",
@@ -1664,11 +1686,7 @@
"- *allow_precompilation* - turns on model pre-compilation for better performance. Note that model precompilation happens when the first request with the new batch size arrives, and the response for that request is sent after precompilation is complete. To mitigate this, specify a warmup file (see the section earlier in this colab). Model precompilation works for different kinds of models, and in most cases has a positive effect on performance. However, we recommend that you try it out for your model before you enable it in production.\n",
"- *allow_precision_affecting_optimizations* - enables precision affecting optimizations. In some cases this makes the model run significantly faster at the cost of very minimal loss to model prediction power. You should assess the precision impact to your model when using this optimization.\n",
"\n",
"For the list of available optimized TensorFlow runtimer containers and options, see https://cloud.google.com/vertex-ai/docs/predictions/optimized-tensorflow-runtime.\n",
"\n",
"In order to be able to send requests to your models over gRPC, you need to set `model_name` argument and update `predict_route` and `health_route` accordingly.\n",
"\n",
"Please note that gRPC support in Vertex AI Prediction is still experimental and only available for models deployed using optimized TensorFlow runtime."
"For the list of available optimized TensorFlow runtimer containers and options, see https://cloud.google.com/vertex-ai/docs/predictions/optimized-tensorflow-runtime."
]
},
{
@@ -1768,7 +1786,7 @@
"id": "e4279643c2cf"
},
"source": [
"See [endpoint_service.create_endpoint](https://cloud.google.com/python/docs/reference/aiplatform/latest/google.cloud.aiplatform_v1.services.endpoint_service.EndpointServiceClient#google_cloud_aiplatform_v1_services_endpoint_service_EndpointServiceClient_create_endpoint) API description for details."
"Learn more about [endpoint_service.create_endpoint](https://cloud.google.com/python/docs/reference/aiplatform/latest/google.cloud.aiplatform_v1.services.endpoint_service.EndpointServiceClient#google_cloud_aiplatform_v1_services_endpoint_service_EndpointServiceClient_create_endpoint)."
]
},
{
@@ -1887,7 +1905,7 @@
"id": "f8586711566b"
},
"source": [
"For details about the `enpoint_service.deploy_model` API, see [enpoint_service.deploy_model](https://cloud.google.com/python/docs/reference/aiplatform/latest/google.cloud.aiplatform_v1.services.endpoint_service.EndpointServiceClient#google_cloud_aiplatform_v1_services_endpoint_service_EndpointServiceClient_deploy_model)."
"Learn more about [enpoint_service.deploy_model](https://cloud.google.com/python/docs/reference/aiplatform/latest/google.cloud.aiplatform_v1.services.endpoint_service.EndpointServiceClient#google_cloud_aiplatform_v1_services_endpoint_service_EndpointServiceClient_deploy_model)."
]
},
{
@@ -2244,7 +2262,7 @@
},
"outputs": [],
"source": [
"tf27_cpu_results = benchmark_rest_private_endpoint(\n",
"tf27_cpu_results = benchmark_grpc_private_endpoint(\n",
" tf27_cpu_endpoint, [10, 20, 30, 40, 50, 55]\n",
")\n",
"tf27_cpu_results"
@@ -2258,7 +2276,7 @@
},
"outputs": [],
"source": [
"tf27_gpu_results = benchmark_rest_private_endpoint(\n",
"tf27_gpu_results = benchmark_grpc_private_endpoint(\n",
" tf27_gpu_endpoint, [10, 20, 30, 40, 50, 60, 70, 75]\n",
")\n",
"tf27_gpu_results"
@@ -2382,6 +2400,24 @@
"You can see that the Vertex AI Prediction optimized TensorFlow runtime has signficantly higher throughput and lower latency compared to TensorFlow 2.7."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "3f369366c0af"
},
"source": [
"## (Optional) Compare performance of deployed models using MLPerf Inference loadgen"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "bfd210c97934"
},
"source": [
"MLPerf Inference is a benchmark suite for measuring how fast systems can run models in a variety of deployment scenarios. MLPerf is now an industry standard way of measuring model performance. You can follow instructions at https://github.com/tensorflow/tpu/tree/master/models/experimental/inference/load_test to run MLPerf Inferenence benchmark for deployed models.\n"
]
},
{
"cell_type": "markdown",
"metadata": {
@@ -2559,7 +2595,10 @@
},
"outputs": [],
"source": [
"!gsutil rm -r $BUCKET_URI"
"# Set this to true only if you'd like to delete your bucket\n",
"delete_bucket = False\n",
"if delete_bucket or os.getenv(\"IS_TESTING\"):\n",
" !gsutil rm -r $BUCKET_URI"
]
}
],
+1 -1
View File
@@ -65,7 +65,7 @@
"\n",
"{TODO: Include a paragraph with Dataset information and where to obtain it.} \n",
"\n",
"{TODO: Make sure the dataset is accessible to the public. **Googlers**: Add your dataset to the [public samples bucket](http://goto/cloudsamples#sample-storage-bucket) within gs://cloud-samples-data/ai-platform-unified, if it doesn't already exist there.}\n",
"{TODO: Make sure the dataset is accessible to the public. **Googlers**: Add your dataset to the [public samples bucket](http://goto/cloudsamples#sample-storage-bucket) within gs://cloud-samples-data/vertex-ai, if it doesn't already exist there.}\n",
"\n",
"### Objective\n",
"\n",
+2 -1
View File
@@ -21,4 +21,5 @@
/feature_store/gapic-feature-store.ipynb @protorganizer @diemtvu
/managed_notebooks @GoogleCloudPlatform/notebooks-team
/pipelines/google_cloud_pipeline_components_bqml_text.ipynb @inardini
/pipelines/google_cloud_pipelines_dataproc_tabular @inardini
/pipelines/google_cloud_pipelines_dataproc_tabular @inardini
/automl/automl_forecasting_bqml_arima_plus_comparison.ipynb @TheMichaelHu
+510 -1
View File
@@ -1,4 +1,513 @@
# Google Cloud Vertex AI Official Notebooks
The official notebooks are organized by Google Cloud Vertex AI products.
The official notebooks are a collection of curated and non-curated notebooks authored by Google Cloud staff members. The curated notebooks are linked to in the [Vertex AI online web documentation](https://cloud.google.com/vertex-ai/docs/tutorials/jupyter-notebooks).
The official notebooks are organized by Google Cloud Vertex AI services.
## Manifest of Curated Notebooks
### AutoML
[AutoML text classification model](https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/automl/automl-text-classification.ipynb)
<blockquote>
In this tutorial, you learn how to use `AutoML` to train a text classification model.
This tutorial uses the following Google Cloud ML services:
- `AutoML Training`
- `Vertex AI Model resource`
The steps performed include:
- Create a `Vertex AI Dataset`
- Train an `AutoML` text classification `Model` resource.
- Obtain the evaluation metrics for the `Model` resource.
- Create an `Endpoint` resource.
- Deploy the `Model` resource to the `Endpoint` resource.
- Make an online prediction.
- Make a batch prediction.
</blockquote>
[AutoML tabular forecasting model](https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/automl/sdk_automl_tabular_forecasting_batch.ipynb)
<blockquote>
In this tutorial, you create an `AutoML` tabular forecasting model from a Python script, and then do a batch prediction using the Vertex AI SDK.
This tutorial uses the following Google Cloud ML services:
- `AutoML Training`
- `Vertex AI Batch Prediction`
- `Vertex AI Model` resource
The steps performed include:
- Create a `Vertex AI Dataset` resource.
- Train an `AutoML` tabular forecasting `Model` resource.
- Obtain the evaluation metrics for the `Model` resource.
- Make a batch prediction.
</blockquote>
### Vertex AI Training
[Custom image classification model training and batch prediction](https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/custom/sdk-custom-image-classification-batch.ipynb)
<blockquote>
In this tutorial, you learn to use `Vertex AI Training` to create a custom trained model and use `Vertex AI Batch Prediction` to do a batch prediction on the trained model.
This tutorial uses the following Google Cloud ML services:
- `Vertex AI Training`
- `Vertex AI Batch Prediction`
- `Vertex AI Model` resource
The steps performed include:
- Create a `Vertex AI` custom job for training a TensorFlow model.
- Upload the trained model artifacts as a `Model` resource.
- Make a batch prediction.
</blockquote>
[Custom image classification model training and online prediction](https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/custom/sdk-custom-image-classification-online.ipynb)
<blockquote>
In this tutorial, you learn to use `Vertex AI Training` to create a custom-trained model from a Python script in a Docker container, and learn to use `Vertex AI Prediction` to do a prediction on the deployed model by sending data.
This tutorial uses the following Google Cloud ML services:
- `Vertex AI Training`
- `Vertex AI Prediction`
- `Vertex AI Model` resource
- `Vertex AI Endpoint` resource
The steps performed include:
- Create a `Vertex AI` custom job for training a TensorFlow model.
- Upload the trained model artifacts to a `Model` resource.
- Create a serving `Endpoint` resource.
- Deploy the Model resource to a serving `Endpoint` resource.
- Make a prediction.
- Undeploy the `Model` resource.
</blockquote>
### Vertex Explainable AI
[AutoML tabular binary classification model with batch explanations](https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/explainable_ai/sdk_automl_tabular_binary_classification_batch_explain.ipynb)
<blockquote>
In this tutorial, you learn to use `AutoML` to create a tabular binary classification model from a Python script, and then learn to use `Vertex AI Batch Prediction` to make predictions with explanations.
This tutorial uses the following Google Cloud ML services:
- `Vertex AI AutoML`
- `Vertex AI Batch Prediction`
- `Vertex Explainable AI`
- `Vertex AI Model` resource
The steps performed include:
- Create a `Vertex Dataset` resource.
- Train an `AutoML` tabular binary classification model.
- View the model evaluation metrics for the trained model.
- Make a batch prediction request with explainability.
</blockquote>
[AutoML tabular binary classification model with online explanations](https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/explainable_ai/sdk_automl_tabular_classification_online_explain.ipynb)
<blockquote>
In this tutorial, you learn to use `AutoML` to create a tabular binary classification model from a Python script, and then learn to use `Vertex AI Online Prediction` to make online predictions with explanations.
This tutorial uses the following Google Cloud ML services:
- `Vertex AI AutoML`
- `Vertex AI Prediction`
- `Vertex Explainable AI`
- `Vertex AI Model` resource
- `Vertex AI Endpoint` resource
The steps performed include:
- Create a `Vertex AI Dataset` resource.
- Train an `AutoML` tabular binary classification model.
- View the model evaluation metrics for the trained model.
- Create a serving `Endpoint` resource.
- Deploy the `Model` resource to a serving `Endpoint` resource.
- Make an online prediction request with explainability.
- Undeploy the `Model` resource.
</blockquote>
[Custom tabular regression model with batch explanations](https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/explainable_ai/sdk_custom_tabular_regression_batch_explain.ipynb)
<blockquote>
In this tutorial, you learn to use `Vertex AI Training` and `Explainable AI` to create a custom image classification model with explanations, and then you learn to use `Vertex AI Batch Prediction` to make a batch prediction request with explanations.
This tutorial uses the following Google Cloud ML services:
- `Vertex AI Training`
- `Vertex AI Batch Prediction`
- `Vertex Explainable AI`
- `Vertex AI Mode`l resource
The steps performed include:
- Create a `Vertex AI` custom job for training a TensorFlow model.
- View the model evaluation for the trained model.
- Set explanation parameters for when the model is deployed.
- Upload the trained model artifacts and explanations as a `Model` resource.
- Make a batch prediction with explanations.
</blockquote>
[Custom tabular regression model with online explanations](https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/explainable_ai/sdk_custom_tabular_regression_online_explain.ipynb)
<blockquote>
In this tutorial, you learn to use `Vertex AI Training` and `Explainable AI` to create a custom image classification model with explanations, and then you learn to use `Vertex AI Prediction` to make an online prediction request with explanations.
This tutorial uses the following Google Cloud ML services:
- `Vertex AI Training`
- `Vertex AI Prediction`
- `Vertex Explainable AI`
- `Vertex AI Model` resource
- `Vertex AI Endpoint` resource
The steps performed include:
- Create a `Vertex AI` custom job for training a TensorFlow model.
- View the model evaluation for the trained model.
- Set explanation parameters for when the model is deployed.
- Upload the trained model artifacts and explanations as a `Model` resource.
- Create a serving `Endpoint` resource.
- Deploy the `Model` resource to a serving `Endpoint` resource.
- Make a prediction with explanation.
- Undeploy the `Model` resource.
</blockquote>
[Custom image classification model with batch explanations](https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/explainable_ai/sdk_custom_image_classification_batch_explain.ipynb)
<blockquote>
In this tutorial, you learn to use `Vertex AI Training and Explainable AI` to create a custom image classification model with explanations, and then you learn to use `Vertex AI Batch Prediction` to make a batch prediction request with explanations.
This tutorial uses the following Google Cloud ML services:
- `Vertex AI Training`
- `Vertex AI Batch Prediction`
- `Vertex Explainable AI`
- `Vertex AI Model` resource
The steps performed include:
- Create a `Vertex AI` custom job for training a TensorFlow model.
- View the model evaluation for the trained model.
- Set explanation parameters for when the model is deployed.
- Upload the trained model artifacts and explanation parameters as a `Model` resource.
- Make a batch prediction with explanations.
</blockquote>
[Custom image classification model with online explanations](https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/explainable_ai/sdk_custom_image_classification_online_explain.ipynb)
<blockquote>
In this tutorial, you learn to use `Vertex AI Training and Explainable AI` to create a custom image classification model with explanations, and then you learn to use `Vertex AI Prediction` to make an online prediction request with explanations.
This tutorial uses the following Google Cloud ML services:
- `Vertex AI Training`
- `Vertex AI Online Prediction`
- `Vertex Explainable AI`
- `Vertex AI Model` resource
- `Vertex AI Endpoint` resource
The steps performed include:
- Create a `Vertex AI` custom job for training a TensorFlow model.
- View the model evaluation for the trained model.
- Set explanation parameters for when the model is deployed.
- Upload the trained model artifacts and explanations as a `Model` resource.
- Create a serving `Endpoint` resource.
- Deploy the `Model` resource to a serving `Endpoint` resource.
- Make a prediction with explanation.
- Undeploy the `Model` resource.
</blockquote>
### Vertex Feature Store
[Managing features in a feature store](https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/feature_store/gapic-feature-store.ipynb)
<blockquote>
In this notebook, you will learn how to use `Vertex AI Feature Store` to import feature data, and to access the feature data for both online serving and offline tasks, such as training.
This tutorial uses the following Google Cloud ML services:
- `Vertex AI Feature Store`
The steps performed include:
- Create featurestore, entity type, and feature resources.
- Import feature data into `Vertex AI Feature Store` resource.
- Serve online prediction requests using the imported features.
- Access imported features in offline jobs, such as training jobs.
</blockquote>
### Vertex Model Monitoring
[Monitoring drift detection in online serving](https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/model_monitoring/model_monitoring.ipynb)
<blockquote>
In this notebook, you learn to use the `Vertex AI Model Monitoring` service to detect drift and anomalies in prediction requests from a deployed `Vertex AI Model` resource.
This tutorial uses the following Google Cloud ML services:
- `Vertex AI Model Monitoring`
- `Vertex AI Prediction`
- `Vertex AI Model` resource
- `Vertex AI Endpoint` resource
The steps performed include:
- Upload a pre-trained model as a `Vertex AI Model` resource.
- Create an `Vertex AI Endpoint` resource.
- Deploy the `Model` resource to the `Endpoint` resource.
- Configure the `Endpoint` resource for model monitoring.
- Generate synthetic prediction requests.
- Understand how to interpret the statistics, visualizations, other data reported by the model monitoring feature.
</blockquote>
### Vertex ML Metadata
[Tracking hyperparameters and metrics in custom training job](https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/ml_metadata/sdk-metric-parameter-tracking-for-custom-jobs.ipynb)
<blockquote>
In this notebook, you learn how to use `Vertex ML Metadata` to track training parameters and evaluation metrics.
This tutorial uses the following Google Cloud ML services:
- `Vertex ML Metadata`
- `Vertex AI Experiments`
The steps performed include:
- Track parameters and metrics for a `Vertex AI` custom trained model.
- Extract and perform analysis for all parameters and metrics within an Experiment.
</blockquote>
[Tracking hyperparameters and metrics in locally trained job](https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/ml_metadata/sdk-metric-parameter-tracking-for-locally-trained-models.ipynb)
<blockquote>
In this notebook, you learn how to use `Vertex ML Metadata` to track training parameters and evaluation metrics.
This tutorial uses the following Google Cloud ML services:
- `Vertex ML Metadata`
- `Vertex AI Experiments`
The steps performed include:
- Track parameters and metrics for a locally trained model.
- Extract and perform analysis for all parameters and metrics within an Experiment.
</blockquote>
### Vertex AI Pipelines
[Creating Python function KFP components](https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/pipelines/lightweight_functions_component_io_kfp.ipynb)
<blockquote>
In this tutorial, you learn to use the KFP SDK to build lightweight Python function-based components, and then you learn to use `Vertex AI Pipelines` to execute the pipeline.
This tutorial uses the following Google Cloud ML services:
- `Vertex AI Pipelines`
The steps performed include:
- Build Python function-based KFP components.
- Construct a KFP pipeline.
- Pass Artifacts and parameters between components, both by path reference and by value.
- Use the kfp.dsl.importer method.
- Compile the KFP pipeline.
- Execute the KFP pipeline using `Vertex AI Pipelines`
</blockquote>
[AutoML image classification model pipeline](https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/pipelines/google_cloud_pipeline_components_automl_images.ipynb)
<blockquote>
In this tutorial, you learn to use `Vertex AI Pipelines` and `Google Cloud Pipeline Components` to build an `AutoML` image classification model.
This tutorial uses the following Google Cloud ML services:
- `Vertex AI Pipelines`
- `Google Cloud Pipeline Components`
- `Vertex AutoML`
- `Vertex AI Model` resource
- `Vertex AI Endpoint` resource
The steps performed include:
- Create a KFP pipeline:
- Create a `Vertex AI Dataset` resource.
- Train an `AutoML` image classification `Model` resource.
- Create an `Endpoint` resource.
- Deploys the `Model` resource to the `Endpoint` resource.
- Compile the KFP pipeline.
- Execute the KFP pipeline using `Vertex AI Pipelines`
</blockquote>
[AutoML tabular classification model pipeline](https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/pipelines/automl_tabular_classification_beans.ipynb)
<blockquote>
In this tutorial, you learn to use `Vertex AI Pipelines` and `Google Cloud Pipeline Components` to build an AutoML tabular classification model.
This tutorial uses the following Google Cloud ML services:
- `Vertex AI Pipelines`
- `Google Cloud Pipeline Components`
- `Vertex AutoML`
- `Vertex AI Model` resource
- `Vertex AI Endpoint` resource
The steps performed include:
- Create a KFP pipeline:
- Create a `Dataset` resource.
- Train an `AutoML` tabular classification `Model` resource.
- Create an `Endpoint` resource.
- Deploys the `Model` resource to the `Endpoint` resource.
- Compile the KFP pipeline.
- Execute the KFP pipeline using `Vertex AI Pipelines`
</blockquote>
[AutoML tabular regression model pipeline](https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/pipelines/google_cloud_pipeline_components_automl_tabular.ipynb)
<blockquote>
In this tutorial, you learn to use `Vertex AI Pipelines` and `Google Cloud Pipeline Components` to build an `AutoML` tabular regression model.
This tutorial uses the following Google Cloud ML services:
- `Vertex AI Pipelines`
- `Google Cloud Pipeline Components`
- `Vertex AutoML`
- `Vertex AI Model` resource
- `Vertex AI Endpoint` resource
The steps performed include:
- Create a KFP pipeline:
- Create a `Dataset` resource.
- Train an `AutoML` tabular regression `Model` resource.
- Create an `Endpoint` resource.
- Deploys the `Model` resource to the `Endpoint` resource.
- Compile the KFP pipeline.
- Execute the KFP pipeline using `Vertex AI Pipelines`
</blockquote>
[AutoML text classification model pipeline](https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/pipelines/google_cloud_pipeline_components_automl_text.ipynb)
<blockquote>
In this tutorial, you learn to use `Vertex AI Pipelines` and `Google Cloud Pipeline Components` to build an `AutoML` text classification model.
This tutorial uses the following Google Cloud ML services:
- `Vertex AI Pipelines`
- `Google Cloud Pipeline Components`
- `Vertex AutoML`
- `Vertex AI Model` resource
"- `Vertex AI Endpoint` resource
The steps performed include:
- Create a KFP pipeline:
- Create a `Dataset` resource.
- Train an AutoML text classification `Model` resource.
- Create an `Endpoint` resource.
- Deploys the `Model` resource to the `Endpoint` resource.
- Compile the KFP pipeline.
- Execute the KFP pipeline using `Vertex AI Pipelines`
</blockquote>
[Custom training and batch prediction using prebuilt components pipeline](https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/pipelines/custom_model_training_and_batch_prediction.ipynb)
<blockquote>
In this tutorial, you learn to use `Vertex AI Pipelines` and `Google Cloud Pipeline Components` to build a custom model.
This tutorial uses the following Google Cloud ML services:
- `Vertex AI Pipelines`
- `Google Cloud Pipeline Components`
- `Vertex AI Training`
- `Vertex AI Model` resource
- `Vertex AI Endpoint` resource
The steps performed include:
- Create a KFP pipeline:
- Train a custom model.
- Upload the trained model as a `Model` resource.
- Create an `Endpoint` resource.
- Deploy the `Model` resource to the `Endpoint` resource.
- Make a batch prediction request.
</blockquote>
[Custom training using prebuilt and custom components pipeline](https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/pipelines/google_cloud_pipeline_components_model_train_upload_deploy.ipynb)
<blockquote>
In this tutorial, you learn to use `Vertex AI Pipelines` and `Google Cloud Pipeline Components` to build and deploy a custom model.
This tutorial uses the following Google Cloud ML services:
- `Vertex AI Pipelines`
- `Google Cloud Pipeline Components`
- `Vertex AI Training`
- `Vertex AI Model` resource
- `Vertex AI Endpoint` resource
The steps performed include:
- Create a KFP pipeline:
- Train a custom model.
- Uploads the trained model as a `Model` resource.
- Creates an `Endpoint` resource.
- Deploys the `Model` resource to the `Endpoint` resource.
- Compile the KFP pipeline.
- Execute the KFP pipeline using `Vertex AI Pipelines`
</blockquote>
[Introduction to control flow in pipelines](https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/pipelines/control_flow_kfp.ipynb)
<blockquote>
In this tutorial, you use the KFP SDK to build pipelines that use loops and conditionals, including nested examples.
This tutorial uses the following Google Cloud ML services:
- `Vertex AI Pipelines`
The steps performed include:
- Create a KFP pipeline:
- Use control flow components
- Compile the KFP pipeline.
- Execute the KFP pipeline using `Vertex AI Pipelines`
</blockquote>
[Introduction to KFP components and pipelines](https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/pipelines/pipelines_intro_kfp.ipynb)
<blockquote>
In this tutorial, you use the KFP SDK to build pipelines.
This tutorial uses the following Google Cloud ML services:
- `Vertex AI Pipelines`
The steps performed include:
- Define and compile a `Vertex AI` pipeline.
- Schedule a recurring pipeline run.
- Specify which service account to use for a pipeline run.
</blockquote>
### Vertex AI Vizier
[Using Vizier for multi-objective study](https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/vizier/gapic-vizier-multi-objective-optimization.ipynb)
@@ -32,21 +32,22 @@
"<table align=\"left\">\n",
"\n",
" <td>\n",
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/master/notebooks/official/automl/automl-text-classification.ipynb\">\n",
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/automl/automl-text-classification.ipynb\">\n",
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"> Run in Colab\n",
" </a>\n",
" </td>\n",
" <td>\n",
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/master/notebooks/official/automl/automl-text-classification.ipynb\">\n",
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/automl/automl-text-classification.ipynb\">\n",
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\">\n",
" View on GitHub\n",
" </a>\n",
" </td>\n",
" <td>\n",
" <a href=\"https://console.cloud.google.com/vertex-ai/notebooks/deploy-notebook?download_url=https://github.com/GoogleCloudPlatform/vertex-ai-samples/raw/master/notebooks/official/automl/automl-text-classification.ipynb\">\n",
" <td>\n",
" <a href=\"https://console.cloud.google.com/ai/platform/notebooks/deploy-notebook?download_url=https://github.com/GoogleCloudPlatform/vertex-ai-samples/tree/main/notebooks/official/automl/automl-text-classification.ipynb\">\n",
" <img src=\"https://lh3.googleusercontent.com/UiNooY4LUgW_oTvpsNhPpQzsstV5W8F7rYgxgGBD85cWJoLmrOzhVs_ksK_vgx40SHs7jCqkTkCk=e14-rj-sc0xffffff-h130-w32\" alt=\"Vertex AI logo\">\n",
" Open in Vertex AI Workbench\n",
" </a>\n",
" </td> \n",
" </td>\n",
"</table>"
]
},
@@ -60,19 +61,30 @@
"\n",
"## Overview\n",
"\n",
"This notebook walks you through the major phases of building and using a text classification model on [Vertex AI](https://cloud.google.com/vertex-ai/docs/). In this notebook, you use the \"Happy Moments\" sample dataset to train a model. The resulting model classifies happy moments into categores that reflect the causes of happiness. \n",
"This notebook walks you through the major phases of building and using a text classification model on [Vertex AI](https://cloud.google.com/vertex-ai/docs/). \n",
"\n",
"### Dataset\n",
"\n",
"In this notebook, you use the \"Happy Moments\" sample dataset to train a model. The resulting model classifies happy moments into categores that reflect the causes of happiness. \n",
"\n",
"### Objective\n",
"\n",
"In this notebook, you learn how to:\n",
"In this tutorial, you learn how to use `AutoML` to train a text classification model.\n",
"\n",
"* Set up your development environment\n",
"* Create a dataset and import data\n",
"* Train an AutoML model\n",
"* Get and review evaluations for the model\n",
"* Deploy a model to an endpoint\n",
"* Get online predictions\n",
"* Get batch predictions\n",
"This tutorial uses the following Google Cloud ML services:\n",
"\n",
"- `AutoML Training`\n",
"- `Vertex AI Model resource`\n",
"\n",
"The steps performed include:\n",
"\n",
"* Create a `Vertex AI Dataset`.\n",
"* Train an `AutoML` text classification `Model` resource.\n",
"* Obtain the evaluation metrics for the `Model` resource.\n",
"* Create an `Endpoint` resource.\n",
"* Deploy the `Model` resource to the `Endpoint` resource.\n",
"* Make an online prediction\n",
"* Make a batch prediction\n",
"\n",
"### Costs\n",
"\n",
@@ -107,7 +119,7 @@
"source": [
"### Set up your local development environment\n",
"\n",
"**If you are using Colab or Google Cloud Notebooks**, your environment already meets\n",
"**If you are using Colab or Workbench AI Notebooks**, your environment already meets\n",
"all the requirements to run this notebook. You can skip this step."
]
},
@@ -148,32 +160,6 @@
"1. Open this notebook in the Jupyter Notebook Dashboard."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "i1VRlEu-l0BW"
},
"source": [
"### Set up your Google Cloud project\n",
"\n",
"**The following steps are required, regardless of your notebook environment.**\n",
"\n",
"1. [Select or create a Google Cloud project](https://console.cloud.google.com/cloud-resource-manager). When you first create an account, you get a $300 free credit towards your compute/storage costs.\n",
"\n",
"1. [Make sure that billing is enabled for your project](https://cloud.google.com/billing/docs/how-to/modify-project).\n",
"\n",
"1. [Enable the Vertex AI, Cloud Storage, and Compute Engine APIs](https://console.cloud.google.com/flows/enableapi?apiid=aiplatform.googleapis.com,compute_component,storage-component.googleapis.com). \n",
"\n",
"1. Follow the \"**Configuring your project**\" instructions from the Vertex Pipelines documentation.\n",
"\n",
"1. If you are running this notebook locally, you will need to install the [Cloud SDK](https://cloud.google.com/sdk).\n",
"\n",
"1. Enter your project ID in the cell below. Then run the cell to make sure the\n",
"Cloud SDK uses the right project for all the commands in this notebook.\n",
"\n",
"**Note**: Jupyter runs lines prefixed with `!` as shell commands, and it interpolates Python variables prefixed with `$` into these commands."
]
},
{
"cell_type": "markdown",
"metadata": {
@@ -182,7 +168,7 @@
"source": [
"### Install additional packages\n",
"\n",
"This notebook uses the Python SDK for Vertex AI, which is contained in the `python-aiplatform` package. You must first install the package into your development environment."
"Install the following packages for executing this notebook."
]
},
{
@@ -195,28 +181,73 @@
"source": [
"import os\n",
"\n",
"# The Google Cloud Notebook product has specific requirements\n",
"IS_GOOGLE_CLOUD_NOTEBOOK = os.path.exists(\"/opt/deeplearning/metadata/env_version\")\n",
"# The Vertex AI Workbench Notebook product has specific requirements\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\") and not os.getenv(\"VIRTUAL_ENV\")\n",
"IS_USER_MANAGED_WORKBENCH_NOTEBOOK = os.path.exists(\n",
" \"/opt/deeplearning/metadata/env_version\"\n",
")\n",
"\n",
"# Google Cloud Notebook requires dependencies to be installed with '--user'\n",
"# Vertex AI Notebook requires dependencies to be installed with '--user'\n",
"USER_FLAG = \"\"\n",
"if IS_GOOGLE_CLOUD_NOTEBOOK:\n",
"if IS_WORKBENCH_NOTEBOOK:\n",
" USER_FLAG = \"--user\"\n",
"\n",
"! pip install {USER_FLAG} --upgrade google-cloud-aiplatform google-cloud-storage jsonlines"
"! pip3 install {USER_FLAG} --upgrade google-cloud-aiplatform google-cloud-storage jsonlines"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "WReHDGG5g0XY"
"id": "e9255e3b156f"
},
"source": [
"### Set your project ID\n",
"### Restart the kernel\n",
"\n",
"Finally, you must initialize the client library before you can send requests to the Vertex AI service. With the Python SDK, you initialize the client library as shown in the following cell. This tutorial also uses the Cloud Storage Python library for accessing batch prediction results.\n",
"Once you've installed the additional packages, you need to restart the notebook kernel so it can find the packages."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "0c0b2427998a"
},
"outputs": [],
"source": [
"import os\n",
"\n",
"Be sure to provide the ID for your Google Cloud project in the `project` variable. This notebook uses the `us-central1` region, although you can change it to another region. \n",
"if not os.getenv(\"IS_TESTING\"):\n",
" # Automatically restart kernel after installs\n",
" import IPython\n",
"\n",
" app = IPython.Application.instance()\n",
" app.kernel.do_shutdown(True)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "435b8e413535"
},
"source": [
"### Set up your Google Cloud project\n",
"\n",
"**The following steps are required, regardless of your notebook environment.**\n",
"\n",
"1. [Select or create a Google Cloud project](https://console.cloud.google.com/cloud-resource-manager). When you first create an account, you get a $300 free credit towards your compute/storage costs.\n",
"\n",
"1. [Make sure that billing is enabled for your project](https://cloud.google.com/billing/docs/how-to/modify-project).\n",
"\n",
"1. [Enable the Vertex AI, BigQuery, Compute Engine and Cloud Storage APIs](https://console.cloud.google.com/flows/enableapi?apiid=aiplatform.googleapis.com,bigquery,compute_component,storage_component).\n",
"\n",
"1. If you are running this notebook locally, you need to install the [Cloud SDK](https://cloud.google.com/sdk).\n",
"\n",
"1. Enter your project ID in the cell below. Then run the cell to make sure the\n",
"Cloud SDK uses the right project for all the commands in this notebook.\n",
"\n",
"**Note**: Jupyter runs lines prefixed with `!` as shell commands, and it interpolates Python variables prefixed with `$` into these commands.\n",
"\n",
"#### Set your project ID\n",
"\n",
"**If you don't know your project ID**, you may be able to get your project ID using `gcloud`."
]
@@ -229,24 +260,7 @@
},
"outputs": [],
"source": [
"import os\n",
"\n",
"PROJECT_ID = \"\"\n",
"\n",
"if not os.getenv(\"IS_TESTING\"):\n",
" # Get your Google Cloud project ID from gcloud\n",
" shell_output = !gcloud config list --format 'value(core.project)' 2>/dev/null\n",
" PROJECT_ID = shell_output[0]\n",
" print(\"Project ID: \", PROJECT_ID)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "a82659ac2487"
},
"source": [
"Otherwise, set your project ID here."
"PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}"
]
},
{
@@ -257,8 +271,107 @@
},
"outputs": [],
"source": [
"if PROJECT_ID == \"\" or PROJECT_ID is None:\n",
" PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}"
"if PROJECT_ID == \"\" or PROJECT_ID is None or PROJECT_ID == \"[your-project-id]\":\n",
" # Get your GCP project id from gcloud\n",
" shell_output = ! gcloud config list --format 'value(core.project)' 2>/dev/null\n",
" PROJECT_ID = shell_output[0]\n",
" print(\"Project ID:\", PROJECT_ID)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "ea86e5a1da1d"
},
"outputs": [],
"source": [
"! gcloud config set project $PROJECT_ID"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "2e6b8b324ce1"
},
"source": [
"#### Region\n",
"\n",
"You can also change the `REGION` variable, which is used for operations\n",
"throughout the rest of this notebook. Below are regions supported for Vertex AI. We recommend that you choose the region closest to you.\n",
"\n",
"- Americas: `us-central1`\n",
"- Europe: `europe-west4`\n",
"- Asia Pacific: `asia-east1`\n",
"\n",
"You may not use a multi-regional bucket for training with Vertex AI. Not all regions provide support for all Vertex AI services.\n",
"\n",
"Learn more about [Vertex AI regions](https://cloud.google.com/vertex-ai/docs/general/locations)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "ae43d96c4b1b"
},
"outputs": [],
"source": [
"REGION = \"[your-region]\" # @param {type: \"string\"}\n",
"\n",
"if REGION == \"[your-region]\":\n",
" REGION = \"us-central1\""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "5f4f5cccf897"
},
"source": [
"#### Timestamp\n",
"\n",
"If you are in a live tutorial session, you might be using a shared test account or project. To avoid name collisions between users on resources created, you create a timestamp for each instance session, and append the timestamp onto the name of resources you create in this tutorial."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "953fa6e5ddda"
},
"outputs": [],
"source": [
"from datetime import datetime\n",
"\n",
"TIMESTAMP = datetime.now().strftime(\"%Y%m%d%H%M%S\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "6c43a8673066"
},
"source": [
"### Authenticate your Google Cloud account\n",
"\n",
"**If you are using Vertex AI Workbench Notebooks**, your environment is already authenticated. Skip this step.\n",
"\n",
"**If you are using Colab**, run the cell below and follow the instructions when prompted to authenticate your account via oAuth.\n",
"\n",
"**Otherwise**, follow these steps:\n",
"\n",
"In the Cloud Console, go to the [Create service account key](https://console.cloud.google.com/apis/credentials/serviceaccountkey) page.\n",
"\n",
"1. **Click Create service account**.\n",
"\n",
"2. In the **Service account name** field, enter a name, and click **Create**.\n",
"\n",
"3. In the **Grant this service account access to project** section, click the Role drop-down list. Type \"Vertex AI\" into the filter box, and select **Vertex AI Administrator**. Type \"Storage Object Admin\" into the filter box, and select **Storage Object Admin**.\n",
"\n",
"4. Click Create. A JSON file that contains your key downloads to your local environment.\n",
"\n",
"5. Enter the path to your service account key as the GOOGLE_APPLICATION_CREDENTIALS variable in the cell below and run the cell."
]
},
{
@@ -269,22 +382,19 @@
},
"outputs": [],
"source": [
"import sys\n",
"from datetime import datetime\n",
"\n",
"import jsonlines\n",
"from google.cloud import aiplatform, storage\n",
"from google.protobuf import json_format\n",
"\n",
"REGION = \"us-central1\"\n",
"\n",
"# If you are running this notebook in Colab, run this cell and follow the\n",
"# instructions to authenticate your GCP account. This provides access to your\n",
"# Cloud Storage bucket and lets you submit training jobs and prediction\n",
"# requests.\n",
"\n",
"# If on Google Cloud Notebooks, then don't execute this code\n",
"if not IS_GOOGLE_CLOUD_NOTEBOOK:\n",
"import os\n",
"import sys\n",
"\n",
"# If on Vertex AI Workbench, then don't execute this code\n",
"IS_COLAB = \"google.colab\" in sys.modules\n",
"if not os.path.exists(\"/opt/deeplearning/metadata/env_version\") and not os.getenv(\n",
" \"DL_ANACONDA_HOME\"\n",
"):\n",
" if \"google.colab\" in sys.modules:\n",
" from google.colab import auth as google_auth\n",
"\n",
@@ -294,8 +404,129 @@
" # path to your service account key and run this cell to authenticate your GCP\n",
" # account.\n",
" elif not os.getenv(\"IS_TESTING\"):\n",
" %env GOOGLE_APPLICATION_CREDENTIALS ''\n",
" %env GOOGLE_APPLICATION_CREDENTIALS ''"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "e5755d1a554f"
},
"source": [
"### Create a Cloud Storage bucket\n",
"\n",
"**The following steps are required, regardless of your notebook environment.**\n",
"\n",
"When you initialize the Vertex SDK for Python, you specify a Cloud Storage staging bucket. The staging bucket is where all the data associated with your dataset and model resources are retained across sessions.\n",
"\n",
"Set the name of your Cloud Storage bucket below. Bucket names must be globally unique across all Google Cloud projects, including those outside of your organization."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "d2de92accb67"
},
"outputs": [],
"source": [
"BUCKET_NAME = \"[your-bucket-name]\" # @param {type:\"string\"}\n",
"BUCKET_URI = f\"gs://{BUCKET_NAME}\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "5ba09496accc"
},
"outputs": [],
"source": [
"if BUCKET_URI == \"\" or BUCKET_URI is None or BUCKET_URI == \"gs://[your-bucket-name]\":\n",
" BUCKET_NAME = PROJECT_ID + \"aip-\" + TIMESTAMP\n",
" BUCKET_URI = \"gs://\" + BUCKET_NAME"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "b72bfdf29dae"
},
"source": [
"**Only if your bucket doesn't already exist**: Run the following cell to create your Cloud Storage bucket."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "a4453435d115"
},
"outputs": [],
"source": [
"! gsutil mb -l $REGION $BUCKET_URI"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "c4cf2cdebb50"
},
"source": [
"Finally, validate access to your Cloud Storage bucket by examining its contents:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "96ad3d416327"
},
"outputs": [],
"source": [
"! gsutil ls -al $BUCKET_URI"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "93d685084cf2"
},
"source": [
"### Import libraries and define constants"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "152013538e59"
},
"outputs": [],
"source": [
"import jsonlines\n",
"from google.cloud import aiplatform, storage"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "03101a4492f3"
},
"source": [
"### Initialize Vertex AI \n",
"\n",
"Initialize the Vertex AI SDK for Python for your project."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "740cd5c67c79"
},
"outputs": [],
"source": [
"aiplatform.init(project=PROJECT_ID, location=REGION)"
]
},
@@ -309,9 +540,9 @@
"\n",
"The notebook uses the 'Happy Moments' dataset for demonstration purposes. You can change it to another text classification dataset that [conforms to the data preparation requirements](https://cloud.google.com/vertex-ai/docs/datasets/prepare-text#classification).\n",
"\n",
"Using the Python SDK, you can create a dataset and import the dataset in one call to `TextDataset.create()`, as shown in the following cell.\n",
"Using the Python SDK, you create a dataset and import the dataset in one call to `TextDataset.create()`, as shown in the following cell.\n",
"\n",
"Creating and importing data is a long-running operation. This next step can take a while. The sample waits for the operation to complete, outputting statements as the operation progresses. The statements contain the full name of the dataset that you will use in the following section.\n",
"Creating and importing data is a long-running operation. This next step can take a while. The `create()` method waits for the operation to complete, outputting statements as the operation progresses. The statements contain the full name of the dataset that you will use in the following section.\n",
"\n",
"**Note**: You can close the noteboook while you wait for this operation to complete. "
]
@@ -325,8 +556,6 @@
"outputs": [],
"source": [
"# Use a timestamp to ensure unique resources\n",
"TIMESTAMP = datetime.now().strftime(\"%Y%m%d%H%M%S\")\n",
"\n",
"src_uris = \"gs://cloud-ml-data/NL-classification/happiness.csv\"\n",
"display_name = f\"e2e-text-dataset-{TIMESTAMP}\""
]
@@ -466,11 +695,9 @@
"id": "caaa3f32b12e"
},
"source": [
"## Get and review model evaluation scores\n",
"## Review model evaluation scores\n",
"\n",
"After your model has finished training, you can review the evaluation scores for it.\n",
"\n",
"First, you need to get a reference to the new model. As with datasets, you can either use the reference to the `model` variable you created when deployed the model or you can list all of the models in your project. When listing your models, you can provide filter criteria to narrow down your search."
"After your model training has finished, you can review the evaluation scores for it using the `list_model_evaluations()` method. This method will return an iterator for each evaluation slice."
]
},
{
@@ -481,94 +708,10 @@
},
"outputs": [],
"source": [
"models = aiplatform.Model.list(filter=f'display_name=\"{model_display_name}\"')\n",
"print(models)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "8481b6878ed2"
},
"source": [
"Using the model name (in the format `projects/[PROJECT_NAME]/locations/us-central1/models/[MODEL_ID]`), you can get its model evaluations. To get model evaluations, you must use the underlying service client.\n",
"model_evaluations = model.list_model_evaluations()\n",
"\n",
"Building a service client requires that you provide the name of the regionalized hostname used for your model. In this tutorial, the hostname is `us-central1-aiplatform.googleapis.com` because the model was created in the `us-central1` location."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "a8443fc8861f"
},
"outputs": [],
"source": [
"# Get the ID of the model\n",
"model_name = \"[your-model-resource-name]\"\n",
"if model_name == \"[your-model-resource-name]\":\n",
" # Use the `resource_name` of the Model instance you created previously\n",
" model_name = model.resource_name\n",
" print(f\"Model name: {model_name}\")\n",
"\n",
"\n",
"# Get a reference to the Model Service client\n",
"client_options = {\"api_endpoint\": \"us-central1-aiplatform.googleapis.com\"}\n",
"model_service_client = aiplatform.gapic.ModelServiceClient(\n",
" client_options=client_options\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "b8a788593609"
},
"source": [
"Before you can view the model evaluation you must first list all of the evaluations for that model. Each model can have multiple evaluations, although a new model is likely to only have one. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "fdcb045e29f2"
},
"outputs": [],
"source": [
"model_evaluations = model_service_client.list_model_evaluations(parent=model_name)\n",
"model_evaluation = list(model_evaluations)[0]"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "cd7d3afae05c"
},
"source": [
"Now that you have the model evaluation, you can look at your model's scores. If you have questions about what the scores mean, review the [public documentation](https://cloud.google.com/vertex-ai/docs/training/evaluating-automl-models#text).\n",
"\n",
"The results returned from the service are formatted as [`google.protobuf.Value`](https://googleapis.dev/python/protobuf/latest/google/protobuf/struct_pb2.html) objects. You can transform the return object as a `dict` for easier reading and parsing."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "6eb9ccb0a0a0"
},
"outputs": [],
"source": [
"model_eval_dict = json_format.MessageToDict(model_evaluation._pb)\n",
"metrics = model_eval_dict[\"metrics\"]\n",
"confidence_metrics = metrics[\"confidenceMetrics\"]\n",
"\n",
"print(f'Area under precision-recall curve (AuPRC): {metrics[\"auPrc\"]}')\n",
"for confidence_scores in confidence_metrics:\n",
" metrics = confidence_scores.keys()\n",
" print(\"\\n\")\n",
" for metric in metrics:\n",
" print(f\"\\t{metric}: {confidence_scores[metric]}\")"
"for model_evaluation in model_evaluations:\n",
" print(model_evaluation.to_dict())"
]
},
{
@@ -720,25 +863,6 @@
"For this tutorial, the following cells create a new Storage bucket, upload individual prediction instances as text files to the bucket, and then create the JSONL file with the URIs of your prediction instances."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "1e0759fbb219"
},
"outputs": [],
"source": [
"TIMESTAMP = datetime.now().strftime(\"%Y%m%d%H%M%S\")\n",
"BUCKET_NAME = \"[your-bucket-name]\"\n",
"\n",
"if BUCKET_NAME == \"\" or BUCKET_NAME is None or BUCKET_NAME == \"[your-bucket-name]\":\n",
" BUCKET_NAME = f\"automl-text-notebook-{TIMESTAMP}\"\n",
"\n",
"BUCKET_URI = f\"gs://{BUCKET_NAME}\"\n",
"\n",
"! gsutil mb -l $REGION $BUCKET_URI"
]
},
{
"cell_type": "code",
"execution_count": null,
@@ -749,7 +873,7 @@
"source": [
"# Instantiate the Storage client and create the new bucket\n",
"storage = storage.Client()\n",
"bucket = storage.bucket(BUCKET_NAME)\n",
"bucket = storage.bucket(BUCKET_URI)\n",
"\n",
"# Iterate over the prediction instances, creating a new TXT file\n",
"# for each.\n",
@@ -964,7 +1088,9 @@
},
"outputs": [],
"source": [
"if os.getenv(\"IS_TESTING\"):\n",
"delete_bucket = False\n",
"\n",
"if delete_bucket or os.getenv(\"IS_TESTING\"):\n",
" ! gsutil rm -r $BUCKET_URI\n",
"\n",
"batch_job.delete()\n",
File diff suppressed because it is too large Load Diff
@@ -32,23 +32,24 @@
"# Vertex AI SDK : AutoML training image object detection model for batch prediction\n",
"\n",
"<table align=\"left\">\n",
" \n",
" <td>\n",
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/master/notebooks/official/automl/sdk_automl_image_object_detection_batch.ipynb\">\n",
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/automl/sdk_automl_image_object_detection_batch.ipynb\">\n",
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"> Run in Colab\n",
" </a>\n",
" </td>\n",
" <td>\n",
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/master/notebooks/official/automl/sdk_automl_image_object_detection_batch.ipynb\">\n",
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/automl/sdk_automl_image_object_detection_batch.ipynb\">\n",
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\">\n",
" View on GitHub\n",
" </a>\n",
" </td>\n",
" <td>\n",
" <a href=\"https://console.cloud.google.com/ai/platform/notebooks/deploy-notebook?download_url=https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/master/notebooks/official/automl/sdk_automl_image_object_detection_batch.ipynb\">\n",
" <img src=\"https://lh3.googleusercontent.com/UiNooY4LUgW_oTvpsNhPpQzsstV5W8F7rYgxgGBD85cWJoLmrOzhVs_ksK_vgx40SHs7jCqkTkCk=e14-rj-sc0xffffff-h130-w32\" alt=\"Vertex AI logo\">\n",
" <a href=\"https://console.cloud.google.com/vertex-ai/workbench/deploy-notebook?download_url=https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/automl/sdk_automl_image_object_detection_batch.ipynb\">\n",
" <img src=\"https://lh3.googleusercontent.com/UiNooY4LUgW_oTvpsNhPpQzsstV5W8F7rYgxgGBD85cWJoLmrOzhVs_ksK_vgx40SHs7jCqkTkCk=e14-rj-sc0xffffff-h130-w32\" alt=\"Vertex AI logo\">\n",
" Open in Vertex AI Workbench\n",
" </a>\n",
" </td>\n",
" </td> \n",
"</table>\n",
"<br/><br/><br/>"
]
@@ -168,7 +169,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "install_aip:mbsdk"
"id": "hw7H6ADSv5mI"
},
"outputs": [],
"source": [
@@ -189,20 +190,29 @@
"id": "install_storage"
},
"source": [
"Install the latest GA version of *google-cloud-storage* library as well."
"Install the latest GA version of *google-cloud-storage* library."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "install_storage"
"id": "d6Pa6Sybv5mK"
},
"outputs": [],
"source": [
"! pip3 install -U google-cloud-storage $USER_FLAG"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "9_zWlX10v5mL"
},
"source": [
"Install the latest version of *tensorflow* library."
]
},
{
"cell_type": "code",
"execution_count": null,
@@ -211,8 +221,7 @@
},
"outputs": [],
"source": [
"if os.getenv(\"IS_TESTING\"):\n",
" ! pip3 install --upgrade tensorflow $USER_FLAG"
"! pip3 install --upgrade tensorflow $USER_FLAG"
]
},
{
@@ -230,7 +239,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "restart"
"id": "D-ZBOjErv5mM"
},
"outputs": [],
"source": [
@@ -282,7 +291,9 @@
},
"outputs": [],
"source": [
"PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}"
"import os\n",
"\n",
"PROJECT_ID = \"\""
]
},
{
@@ -293,11 +304,32 @@
},
"outputs": [],
"source": [
"if PROJECT_ID == \"\" or PROJECT_ID is None or PROJECT_ID == \"[your-project-id]\":\n",
" # Get your GCP project id from gcloud\n",
" shell_output = ! gcloud config list --format 'value(core.project)' 2>/dev/null\n",
"# Get your Google Cloud project ID from gcloud\n",
"if not os.getenv(\"IS_TESTING\"):\n",
" shell_output = !gcloud config list --format 'value(core.project)' 2>/dev/null\n",
" PROJECT_ID = shell_output[0]\n",
" print(\"Project ID:\", PROJECT_ID)"
" print(\"Project ID: \", PROJECT_ID)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "W2F5WRyhv5mO"
},
"source": [
"Otherwise, set your project ID here."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "d7-MjQafv5mO"
},
"outputs": [],
"source": [
"if PROJECT_ID == \"\" or PROJECT_ID is None:\n",
" PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}"
]
},
{
@@ -335,11 +367,14 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "region"
"id": "kAfG6tDAv5mQ"
},
"outputs": [],
"source": [
"REGION = \"us-central1\" # @param {type: \"string\"}"
"REGION = \"[your-region]\" # @param {type:\"string\"}\n",
"\n",
"if REGION == \"[your-region]\":\n",
" REGION = \"us-central1\""
]
},
{
@@ -357,7 +392,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "timestamp"
"id": "PzKW-zT_v5mR"
},
"outputs": [],
"source": [
@@ -397,7 +432,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "gcp_authenticate"
"id": "FvQeFm3Gv5mR"
},
"outputs": [],
"source": [
@@ -475,7 +510,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "create_bucket"
"id": "09kHSsKmv5mT"
},
"outputs": [],
"source": [
@@ -495,7 +530,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "validate_bucket"
"id": "N9JY-esPv5mU"
},
"outputs": [],
"source": [
@@ -540,7 +575,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "init_aip:mbsdk"
"id": "w2Oa_jZSv5mV"
},
"outputs": [],
"source": [
@@ -597,7 +632,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "quick_peek:csv"
"id": "ITshYFagv5mZ"
},
"outputs": [],
"source": [
@@ -629,7 +664,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "create_dataset:image,iod"
"id": "FYyaPzfRv5mc"
},
"outputs": [],
"source": [
@@ -677,7 +712,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "create_automl_pipeline:image,iod"
"id": "vtAgY1Nmv5md"
},
"outputs": [],
"source": [
@@ -719,7 +754,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "run_automl_pipeline:image"
"id": "SvV1nFDTv5md"
},
"outputs": [],
"source": [
@@ -750,7 +785,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "evaluate_the_model:mbsdk"
"id": "RcpDJMgev5me"
},
"outputs": [],
"source": [
@@ -833,7 +868,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "copy_test_items:batch_prediction"
"id": "JXpg67zjv5mf"
},
"outputs": [],
"source": [
@@ -869,7 +904,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "make_batch_file:automl,image"
"id": "hFjX62hvv5mg"
},
"outputs": [],
"source": [
@@ -901,14 +936,27 @@
"- `job_display_name`: The human readable name for the batch prediction job.\n",
"- `gcs_source`: A list of one or more batch request input files.\n",
"- `gcs_destination_prefix`: The Cloud Storage location for storing the batch prediction resuls.\n",
"- `machine_type`: The type of machine for running batch prediction on dedicated resources. Not specifying machine type will result in batch prediction job being run with automatic resources.\n",
"- `starting_replica_count`: The number of machine replicas used at the start of the batch operation. If not set, Vertex AI decides starting number, not greater than `max_replica_count`. Only used if `machine_type` is set.\n",
"- `max_replica_count`: The maximum number of machine replicas the batch operation may be scaled to. Only used if `machine_type` is set. Default is 10.\n",
"- `sync`: If set to True, the call will block while waiting for the asynchronous batch job to complete."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "axn-CW7xv5mg"
},
"source": [
"For AutoML models, only manual scaling is supported. In manual scaling both starting_replica_count and max_replica_count have the same value.\n",
"For this batch job we are using manual scaling. Here we are setting both starting_replica_count and max_replica_count to the same value that is 1. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "batch_request:mbsdk"
"id": "5VMMaJhbv5mh"
},
"outputs": [],
"source": [
@@ -916,6 +964,9 @@
" job_display_name=\"salads_\" + TIMESTAMP,\n",
" gcs_source=gcs_input_uri,\n",
" gcs_destination_prefix=BUCKET_URI,\n",
" machine_type=\"n1-standard-4\",\n",
" starting_replica_count=1,\n",
" max_replica_count=1,\n",
" sync=False,\n",
")\n",
"\n",
@@ -937,7 +988,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "batch_request_wait:mbsdk"
"id": "Yc3YaEqGv5mh"
},
"outputs": [],
"source": [
@@ -967,7 +1018,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "get_batch_prediction:mbsdk,iod"
"id": "UGGJxFjEv5mh"
},
"outputs": [],
"source": [
@@ -986,8 +1037,7 @@
" with tf.io.gfile.GFile(name=gfile_name, mode=\"r\") as gfile:\n",
" for line in gfile.readlines():\n",
" line = json.loads(line)\n",
" print(line)\n",
" break"
" print(line)"
]
},
{
@@ -1014,27 +1064,26 @@
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "cleanup:mbsdk"
"id": "_Olkhs6xv5mi"
},
"outputs": [],
"source": [
"delete_all = True\n",
"delete_bucket = False\n",
"\n",
"if delete_all:\n",
" # Delete the dataset using the Vertex dataset object\n",
" dataset.delete()\n",
"# Delete the dataset using the Vertex dataset object\n",
"dataset.delete()\n",
"\n",
" # Delete the model using the Vertex model object\n",
" model.delete()\n",
"# Delete the model using the Vertex model object\n",
"model.delete()\n",
"\n",
" # Delete the AutoML or Pipeline trainig job\n",
" job.delete()\n",
"# Delete the AutoML or Pipeline trainig job\n",
"job.delete()\n",
"\n",
" # Delete the batch prediction job using the Vertex batch prediction object\n",
" batch_predict_job.delete()\n",
"# Delete the batch prediction job using the Vertex batch prediction object\n",
"batch_predict_job.delete()\n",
"\n",
" if os.getenv(\"IS_TESTING\"):\n",
" ! gsutil rm -r $BUCKET_URI"
"if delete_bucket or os.getenv(\"IS_TESTING\"):\n",
" ! gsutil rm -r $BUCKET_URI"
]
}
],
@@ -29,22 +29,22 @@
"id": "title"
},
"source": [
"# Vertex SDK: AutoML tabular forecasting model for batch prediction\n",
"# Vertex AI SDK: AutoML tabular forecasting model for batch prediction\n",
"\n",
"<table align=\"left\">\n",
" <td>\n",
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/tree/master/notebooks/official/automl/sdk_automl_tabular_forecasting_batch.ipynb\">\n",
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/automl/sdk_automl_tabular_forecasting_batch.ipynb\">\n",
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"> Run in Colab\n",
" </a>\n",
" </td>\n",
" <td>\n",
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/tree/master/notebooks/official/automl/sdk_automl_tabular_forecasting_batch.ipynb\">\n",
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/automl/sdk_automl_tabular_forecasting_batch.ipynb\">\n",
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\">\n",
" View on GitHub\n",
" </a>\n",
" </td>\n",
" <td>\n",
" <a href=\"https://console.cloud.google.com/vertex-ai/notebooks/deploy-notebook?download_url=https://github.com/GoogleCloudPlatform/vertex-ai-samples/tree/master/notebooks/official/automl/sdk_automl_tabular_forecasting_batch.ipynb\">\n",
" <a href=\"https://console.cloud.google.com/vertex-ai/notebooks/deploy-notebook?download_url=https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/automl/sdk_automl_tabular_forecasting_batch.ipynb\">\n",
" <img src=\"https://lh3.googleusercontent.com/UiNooY4LUgW_oTvpsNhPpQzsstV5W8F7rYgxgGBD85cWJoLmrOzhVs_ksK_vgx40SHs7jCqkTkCk=e14-rj-sc0xffffff-h130-w32\" alt=\"Vertex AI logo\">\n",
" Open in Vertex AI Workbench\n",
" </a>\n",
@@ -62,7 +62,7 @@
"## Overview\n",
"\n",
"\n",
"This tutorial demonstrates how to use the Vertex SDK to create tabular forecasting models and do batch prediction using a Google Cloud [AutoML](https://cloud.google.com/vertex-ai/docs/start/automl-users) model."
"This tutorial demonstrates how to use the Vertex AI SDK to create tabular forecasting models and do batch prediction using a Google Cloud [AutoML](https://cloud.google.com/vertex-ai/docs/start/automl-users) model."
]
},
{
@@ -84,13 +84,19 @@
"source": [
"### Objective\n",
"\n",
"In this tutorial, you create an AutoML tabular forecasting model from a Python script, and then do a batch prediction using the Vertex SDK. You can alternatively create and deploy models using the `gcloud` command-line tool or online using the Cloud Console.\n",
"In this tutorial, you create an `AutoML` tabular forecasting model from a Python script, and then do a batch prediction using the Vertex AI SDK. You can alternatively create and deploy models using the `gcloud` command-line tool or online using the Cloud Console.\n",
"\n",
"This tutorial uses the following Google Cloud ML services:\n",
"\n",
"- `AutoML Training`\n",
"- `Vertex AI Batch Prediction`\n",
"- `Vertex AI Model` resource\n",
"\n",
"The steps performed include:\n",
"\n",
"- Create a Vertex `Dataset` resource.\n",
"- Train the model.\n",
"- View the model evaluation.\n",
"- Create a `Vertex AI Dataset` resource.\n",
"- Train an `AutoML` tabular forecasting `Model` resource.\n",
"- Obtain the evaluation metrics for the `Model` resource.\n",
"- Make a batch prediction.\n"
]
},
@@ -122,7 +128,7 @@
"source": [
"### Set up your local development environment\n",
"\n",
"If you are using Colab or Google Cloud Notebooks, your environment already meets all the requirements to run this notebook. You can skip this step.\n",
"If you are using Colab or Workbench AI Notebooks, your environment already meets all the requirements to run this notebook. You can skip this step.\n",
"\n",
"Otherwise, make sure your environment meets this notebook's requirements. You need the following:\n",
"\n",
@@ -155,7 +161,7 @@
"source": [
"## Installation\n",
"\n",
"Install the latest version of Vertex SDK for Python."
"Install the latest version of Vertex AI SDK for Python."
]
},
{
@@ -168,14 +174,19 @@
"source": [
"import os\n",
"\n",
"# Google Cloud Notebook\n",
"if os.path.exists(\"/opt/deeplearning/metadata/env_version\"):\n",
" USER_FLAG = \"--user\"\n",
"else:\n",
" USER_FLAG = \"\"\n",
"# The Vertex AI Workbench Notebook product has specific requirements\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\") and not os.getenv(\"VIRTUAL_ENV\")\n",
"IS_USER_MANAGED_WORKBENCH_NOTEBOOK = os.path.exists(\n",
" \"/opt/deeplearning/metadata/env_version\"\n",
")\n",
"\n",
"! pip3 install --upgrade google-cloud-aiplatform $USER_FLAG\n",
"! pip3 install --upgrade tensorflow $USER_FLAG"
"# Vertex AI Notebook requires dependencies to be installed with '--user'\n",
"USER_FLAG = \"\"\n",
"if IS_WORKBENCH_NOTEBOOK:\n",
" USER_FLAG = \"--user\"\n",
"\n",
"! pip3 install --upgrade google-cloud-aiplatform $USER_FLAG -q\n",
"! pip3 install --upgrade tensorflow $USER_FLAG -q"
]
},
{
@@ -302,7 +313,10 @@
},
"outputs": [],
"source": [
"REGION = \"[your-region]\" # @param {type: \"string\"}"
"REGION = \"[your-region]\" # @param {type: \"string\"}\n",
"\n",
"if REGION == \"[your-region]\":\n",
" REGION = \"us-central1\""
]
},
{
@@ -337,7 +351,7 @@
"source": [
"### Authenticate your Google Cloud account\n",
"\n",
"**If you are using Google Cloud Notebooks**, your environment is already authenticated. Skip this step.\n",
"**If you are using Workbench AI Notebooks**, your environment is already authenticated. Skip this step.\n",
"\n",
"**If you are using Colab**, run the cell below and follow the instructions when prompted to authenticate your account via oAuth.\n",
"\n",
@@ -372,8 +386,11 @@
"import os\n",
"import sys\n",
"\n",
"# If on Google Cloud Notebook, then don't execute this code\n",
"if not os.path.exists(\"/opt/deeplearning/metadata/env_version\"):\n",
"# If on Vertex AI Workbench, then don't execute this code\n",
"IS_COLAB = \"google.colab\" in sys.modules\n",
"if not os.path.exists(\"/opt/deeplearning/metadata/env_version\") and not os.getenv(\n",
" \"DL_ANACONDA_HOME\"\n",
"):\n",
" if \"google.colab\" in sys.modules:\n",
" from google.colab import auth as google_auth\n",
"\n",
@@ -409,7 +426,8 @@
},
"outputs": [],
"source": [
"BUCKET_URI = \"gs://[your-bucket-name]\" # @param {type:\"string\"}"
"BUCKET_NAME = \"[your-bucket-name]\" # @param {type:\"string\"}\n",
"BUCKET_URI = f\"gs://{BUCKET_NAME}\""
]
},
{
@@ -421,7 +439,8 @@
"outputs": [],
"source": [
"if BUCKET_URI == \"\" or BUCKET_URI is None or BUCKET_URI == \"gs://[your-bucket-name]\":\n",
" BUCKET_URI = \"gs://\" + PROJECT_ID + \"aip-\" + TIMESTAMP"
" BUCKET_NAME = PROJECT_ID + \"aip-\" + TIMESTAMP\n",
" BUCKET_URI = \"gs://\" + BUCKET_NAME"
]
},
{
@@ -493,9 +512,9 @@
"id": "init_aip:mbsdk"
},
"source": [
"## Initialize Vertex SDK for Python\n",
"## Initialize Vertex AI SDK for Python\n",
"\n",
"Initialize the Vertex SDK for Python for your project and corresponding bucket."
"Initialize the Vertex AI SDK for Python for your project and corresponding bucket."
]
},
{
@@ -655,8 +674,8 @@
"- `test_fraction_split`: The percentage of the dataset to use for test (holdout data).\n",
"- `target_column`: The name of the column to train as the label.\n",
"- `budget_milli_node_hours`: (optional) Maximum training time specified in unit of millihours (1000 = hour).\n",
"- `time_column`:\n",
"- `time_series_identifier_column`:\n",
"- `time_column`: Time-series column for the forecast model.\n",
"- `time_series_identifier_column`: ID column for the time-series column.\n",
"\n",
"The `run` method when completed returns the `Model` resource.\n",
"\n",
@@ -697,9 +716,8 @@
},
"source": [
"## Review model evaluation scores\n",
"After your model has finished training, you can review the evaluation scores for it.\n",
"\n",
"First, you need to get a reference to the new model. As with datasets, you can either use the reference to the model variable you created when you deployed the model or you can list all of the models in your project."
"After your model training has finished, you can review the evaluation scores for "
]
},
{
@@ -710,21 +728,10 @@
},
"outputs": [],
"source": [
"# Get model resource ID\n",
"models = aiplatform.Model.list(filter=f\"display_name={MODEL_DISPLAY_NAME}\")\n",
"model = models[0]\n",
"model_evaluations = model.list_model_evaluations()\n",
"\n",
"# Get a reference to the Model Service client\n",
"client_options = aiplatform.initializer.global_config.get_client_options()\n",
"model_service_client = aiplatform.gapic.ModelServiceClient(\n",
" client_options=client_options\n",
")\n",
"\n",
"model_evaluations = model_service_client.list_model_evaluations(\n",
" parent=model.resource_name\n",
")\n",
"model_evaluation = list(model_evaluations)[0]\n",
"print(model_evaluation)"
"for model_evaluation in model_evaluations:\n",
" print(model_evaluation.to_dict())"
]
},
{
@@ -746,13 +753,14 @@
"source": [
"### Make the batch prediction request\n",
"\n",
"Now that your Model resource is trained, you can make a batch prediction by invoking the batch_predict() method, with the following parameters:\n",
"Now that your Model resource is trained, you can make a batch prediction by invoking the batch_predict() method using a BigQuery source and destination, with the following parameters:\n",
"\n",
"- `job_display_name`: The human readable name for the batch prediction job.\n",
"- `gcs_source`: A list of one or more batch request input files.\n",
"- `gcs_destination_prefix`: The Cloud Storage location for storing the batch prediction resuls.\n",
"- `instances_format`: The format for the input instances, either 'csv' or 'jsonl'. Defaults to 'jsonl'.\n",
"- `predictions_format`: The format for the output predictions, either 'csv' or 'jsonl'. Defaults to 'jsonl'.\n",
"- `bigquery_source`: BigQuery URI to a table, up to 2000 characters long. For example: `bq://projectId.bqDatasetId.bqTableId`\n",
"- `bigquery_destination_prefix`: The BigQuery dataset or table for storing the batch prediction resuls.\n",
"- `instances_format`: The format for the input instances. Since a BigQuery source is used here, this should be set to `bigquery`.\n",
"- `predictions_format`: The format for the output predictions, `bigquery` is used here to output to a BigQuery table.\n",
"- `generate_explanations`: Set to `True` to generate explanations.\n",
"- `sync`: If set to True, the call will block while waiting for the asynchronous batch job to complete."
]
},
@@ -794,7 +802,9 @@
"id": "99b7a9287ba6"
},
"source": [
"`batch_predict` can export predictions either to BigQuery or GCS. The BQ option is commented out below and the predictions will be exported to the BUCKET_URI."
"For AutoML models, manual scaling can be adjusted by setting both min and max nodes i.e., `starting_replica_count` and `max_replica_count` as the same value(in this example, set to 1). The node count can be increased or decreased as required by load.\n",
" \n",
"`batch_predict` can export predictions either to BigQuery or GCS. The BigQuery options are commented out below and the predictions will be exported to the BUCKET_URI."
]
},
{
@@ -813,9 +823,9 @@
" job_display_name=f\"iowa_liquor_sales_forecasting_predictions_{TIMESTAMP}\",\n",
" bigquery_source=PREDICTION_DATASET_BQ_PATH,\n",
" instances_format=\"bigquery\",\n",
" # bigquery_destination_prefix=batch_predict_bq_output_uri_prefix,\n",
" gcs_destination_prefix=BUCKET_URI,\n",
" bigquery_destination_prefix=batch_predict_bq_output_uri_prefix,\n",
" predictions_format=\"bigquery\",\n",
" generate_explanation=True,\n",
" sync=False,\n",
")\n",
"\n",
@@ -850,14 +860,9 @@
"id": "get_batch_prediction:mbsdk,forecast"
},
"source": [
"### Get the predictions\n",
"### Get the predictions and explanations\n",
"\n",
"Next, get the results from the completed batch prediction job.\n",
"\n",
"The results are written to the Cloud Storage output bucket you specified in the batch prediction request. You call the method iter_outputs() to get a list of each Cloud Storage file generated with the results. Each file contains one or more prediction requests in a CSV format:\n",
"\n",
"- CSV header + predicted_label\n",
"- CSV row + prediction, per prediction request"
"Next, get the results from the completed batch prediction job and print them out. Each result row will include the prediction and explanation."
]
},
{
@@ -868,21 +873,8 @@
},
"outputs": [],
"source": [
"import tensorflow as tf\n",
"\n",
"bp_iter_outputs = batch_prediction_job.iter_outputs()\n",
"\n",
"prediction_results = list()\n",
"for blob in bp_iter_outputs:\n",
" if blob.name.split(\"/\")[-1].startswith(\"prediction\"):\n",
" prediction_results.append(blob.name)\n",
"\n",
"tags = list()\n",
"for prediction_result in prediction_results:\n",
" gfile_name = f\"gs://{bp_iter_outputs.bucket.name}/{prediction_result}\"\n",
" with tf.io.gfile.GFile(name=gfile_name, mode=\"r\") as gfile:\n",
" for line in gfile.readlines():\n",
" print(line)"
"for row in batch_prediction_job.iter_outputs():\n",
" print(row)"
]
},
{
@@ -894,9 +886,11 @@
"### Visualize the forecasts\n",
"\n",
"Lastly, follow the given link to visualize the generated forecasts in [Data Studio](https://support.google.com/datastudio/answer/6283323?hl=en).\n",
"The code block included in this section dynamically generates a Data Studio link that specifies the template, the location of the forecasts, and the query to generate the chart. The data is populated from the forecasts generated earlier.\n",
"The code block included in this section dynamically generates a Data Studio link that specifies the template, the location of the forecasts, and the query to generate the chart. The data is populated from the forecasts generated using BigQuery options where the destination dataset is `batch_predict_bq_output_dataset_path`.\n",
"\n",
"You can inspect the used template at https://datastudio.google.com/c/u/0/reporting/067f70d2-8cd6-4a4c-a099-292acd1053e8. This was created by Google specifically to view forecasting predictions."
"You can inspect the used template at https://datastudio.google.com/c/u/0/reporting/067f70d2-8cd6-4a4c-a099-292acd1053e8. This was created by Google specifically to view forecasting predictions.\n",
"\n",
"**Note:** The Data Studio dashboard can only show the charts properly when the `batch_predict` job is run successfully using the BigQuery options."
]
},
{
@@ -0,0 +1,956 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "copyright"
},
"outputs": [],
"source": [
"# Copyright 2022 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."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "title"
},
"source": [
"# Vertex AI SDK for Python: AutoML training tabular regression model for batch prediction using BigQuery\n",
"\n",
"<table align=\"left\">\n",
" <td>\n",
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/automl/sdk_automl_tabular_regression_batch_bq.ipynb\">\n",
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"> Run in Colab\n",
" </a>\n",
" </td>\n",
" <td>\n",
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/automl/sdk_automl_tabular_regression_batch_bq.ipynb\">\n",
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\">\n",
" View on GitHub\n",
" </a>\n",
" </td>\n",
" <td>\n",
" <a href=\"https://console.cloud.google.com/vertex-ai/workbench/deploy-notebook?download_url=https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/automl/sdk_automl_tabular_regression_batch_bq.ipynb\">\n",
" <img src=\"https://lh3.googleusercontent.com/UiNooY4LUgW_oTvpsNhPpQzsstV5W8F7rYgxgGBD85cWJoLmrOzhVs_ksK_vgx40SHs7jCqkTkCk=e14-rj-sc0xffffff-h130-w32\" alt=\"Vertex AI logo\">\n",
" Open in Vertex AI Workbench\n",
" </a>\n",
" </td> \n",
"</table>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "overview:automl"
},
"source": [
"## Overview\n",
"\n",
"\n",
"This tutorial demonstrates how to use the Vertex AI SDK for Python to create tabular regression models and do batch prediction using a Google Cloud [AutoML](https://cloud.google.com/vertex-ai/docs/start/automl-users) model."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "dataset:gsod,lrg"
},
"source": [
"### Dataset\n",
"\n",
"The dataset used for this tutorial is the [GSOD dataset](https://console.cloud.google.com/marketplace/product/noaa-public/gsod) from [BigQuery public datasets](https://cloud.google.com/bigquery/public-data). In this version of the dataset, you use the year, month, and day fields to predict the mean daily temperature (mean_temp)."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "objective:automl,training,online_prediction"
},
"source": [
"### Objective\n",
"\n",
"In this tutorial, you create an AutoML tabular regression model and deploy it for batch prediction using the Vertex AI SDK for Python. You can alternatively create and deploy models using the `gcloud` command-line tool or batch using the Cloud Console.\n",
"\n",
"The steps performed include:\n",
"\n",
"- Create a Vertex AI `Dataset` resource.\n",
"- Train the model.\n",
"- View the model evaluation.\n",
"- Deploy the `Model` resource to a serving `Endpoint` resource.\n",
"- Make a prediction.\n",
"- Undeploy the `Model`."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "costs"
},
"source": [
"### Costs\n",
"\n",
"This tutorial uses billable components of Google Cloud:\n",
"\n",
"* Vertex AI\n",
"* Cloud Storage\n",
"\n",
"Learn about [Vertex AI\n",
"pricing](https://cloud.google.com/vertex-ai/pricing) and [Cloud Storage\n",
"pricing](https://cloud.google.com/storage/pricing), and use the [Pricing\n",
"Calculator](https://cloud.google.com/products/calculator/)\n",
"to generate a cost estimate based on your projected usage."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "setup_local"
},
"source": [
"### Set up your local development environment\n",
"\n",
"If you are using Colab or Vertex AI Workbench, your environment already meets all the requirements to run this notebook. You can skip this step.\n",
"\n",
"Otherwise, make sure your environment meets this notebook's requirements. You need the following:\n",
"\n",
"- The Cloud Storage SDK\n",
"- Git\n",
"- Python 3\n",
"- virtualenv\n",
"- Jupyter notebook running in a virtual environment with Python 3\n",
"\n",
"The Cloud Storage guide to [Setting up a Python development environment](https://cloud.google.com/python/setup) and the [Jupyter installation guide](https://jupyter.org/install) provide detailed instructions for meeting these requirements. The following steps provide a condensed set of instructions:\n",
"\n",
"1. [Install and initialize the SDK](https://cloud.google.com/sdk/docs/).\n",
"\n",
"2. [Install Python 3](https://cloud.google.com/python/setup#installing_python).\n",
"\n",
"3. [Install virtualenv](https://cloud.google.com/python/setup#installing_and_using_virtualenv) and create a virtual environment that uses Python 3. Activate the virtual environment.\n",
"\n",
"4. To install Jupyter, run `pip3 install jupyter` on the command-line in a terminal shell.\n",
"\n",
"5. To launch Jupyter, run `jupyter notebook` on the command-line in a terminal shell.\n",
"\n",
"6. Open this notebook in the Jupyter Notebook Dashboard.\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "install_aip:mbsdk"
},
"source": [
"## Installation\n",
"\n",
"Install the latest version of the Vertex AI SDK for Python."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "install_aip:mbsdk"
},
"outputs": [],
"source": [
"import os\n",
"\n",
"# Google Cloud Notebook\n",
"if os.path.exists(\"/opt/deeplearning/metadata/env_version\"):\n",
" USER_FLAG = \"--user\"\n",
"else:\n",
" USER_FLAG = \"\"\n",
"\n",
"! pip3 install --upgrade google-cloud-aiplatform $USER_FLAG"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "install_storage"
},
"source": [
"Install the latest version of *google-cloud-storage*."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "install_storage"
},
"outputs": [],
"source": [
"! pip3 install -U google-cloud-storage $USER_FLAG"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "b2f8bf1a1c31"
},
"source": [
"Install the latest version of *google-cloud-bigquery*."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "fb18bb35a386"
},
"outputs": [],
"source": [
"! pip3 install -U \"google-cloud-bigquery[pandas]\" $USER_FLAG"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "restart"
},
"source": [
"### Restart the kernel\n",
"\n",
"After installing the packages, restart the notebook kernel."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "restart"
},
"outputs": [],
"source": [
"import os\n",
"\n",
"if not os.getenv(\"IS_TESTING\"):\n",
" # Automatically restart kernel after installs\n",
" import IPython\n",
"\n",
" app = IPython.Application.instance()\n",
" app.kernel.do_shutdown(True)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "before_you_begin:nogpu"
},
"source": [
"## Before you begin\n",
"\n",
"### GPU runtime\n",
"\n",
"This tutorial does not require a GPU runtime.\n",
"\n",
"### Set up your Google Cloud project\n",
"\n",
"**The following steps are required, regardless of your notebook environment.**\n",
"\n",
"1. [Select or create a Google Cloud project](https://console.cloud.google.com/cloud-resource-manager). When you first create an account, you get a $300 free credit towards your compute/storage costs.\n",
"\n",
"2. [Make sure that billing is enabled for your project.](https://cloud.google.com/billing/docs/how-to/modify-project)\n",
"\n",
"3. [Enable the following APIs: Vertex AI APIs, Compute Engine APIs, and Cloud Storage.](https://console.cloud.google.com/flows/enableapi?apiid=ml.googleapis.com,compute_component,storage-component.googleapis.com)\n",
"\n",
"4. If you are running this notebook locally, install the [Cloud SDK]((https://cloud.google.com/sdk)).\n",
"\n",
"5. Enter your project ID in the cell below. Then run the cell to make sure the\n",
"Cloud SDK uses the right project for all the commands in this notebook.\n",
"\n",
"**Note**: Jupyter runs lines prefixed with `!` as shell commands, and it interpolates Python variables prefixed with `$`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "set_project_id"
},
"outputs": [],
"source": [
"PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "autoset_project_id"
},
"outputs": [],
"source": [
"if PROJECT_ID == \"\" or PROJECT_ID is None or PROJECT_ID == \"[your-project-id]\":\n",
" # Get your GCP project id from gcloud\n",
" shell_output = ! gcloud config list --format 'value(core.project)' 2>/dev/null\n",
" PROJECT_ID = shell_output[0]\n",
" print(\"Project ID:\", PROJECT_ID)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "set_gcloud_project_id"
},
"outputs": [],
"source": [
"! gcloud config set project $PROJECT_ID"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "region"
},
"source": [
"#### Region\n",
"\n",
"You can also change the `REGION` variable, which is used for operations throughout the rest of this notebook. The following regions supported for Vertex AI. We recommend that you choose the region closest to you.\n",
"\n",
"- Americas: `us-central1`\n",
"- Europe: `europe-west4`\n",
"- Asia Pacific: `asia-east1`\n",
"\n",
"You may not use a multi-regional bucket for training with Vertex AI. Not all regions provide support for all Vertex AI services.\n",
"\n",
"Learn more about [Vertex AI regions](https://cloud.google.com/vertex-ai/docs/general/locations)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "region"
},
"outputs": [],
"source": [
"REGION = \"[your-region]\" # @param {type: \"string\"}"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "timestamp"
},
"source": [
"#### Timestamp\n",
"\n",
"If you are in a live tutorial session, you might be using a shared test account or project. To avoid name collisions between users on resources created, create a timestamp for each instance session, and append the timestamp onto the name of resources you create in this tutorial."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "timestamp"
},
"outputs": [],
"source": [
"from datetime import datetime\n",
"\n",
"TIMESTAMP = datetime.now().strftime(\"%Y%m%d%H%M%S\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "gcp_authenticate"
},
"source": [
"### Authenticate your Google Cloud account\n",
"\n",
"**If you are using Google Cloud Notebooks**, your environment is already authenticated. Skip this step.\n",
"\n",
"**If you are using Colab**, run the cell below and follow the instructions when prompted to authenticate your account via oAuth.\n",
"\n",
"**Otherwise**, follow these steps:\n",
"\n",
"In the Cloud Console, go to the [Create service account key](https://console.cloud.google.com/apis/credentials/serviceaccountkey) page.\n",
"\n",
"**Click Create service account**.\n",
"\n",
"In the **Service account name** field, enter a name, and click **Create**.\n",
"\n",
"In the **Grant this service account access to project** section, click the Role drop-down list. Type \"Vertex\" into the filter box, and select **Vertex Administrator**. Type \"Storage Object Admin\" into the filter box, and select **Storage Object Admin**.\n",
"\n",
"Click Create. A JSON file that contains your key downloads to your local environment.\n",
"\n",
"Enter the path to your service account key as the GOOGLE_APPLICATION_CREDENTIALS variable in the cell below and run the cell."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "gcp_authenticate"
},
"outputs": [],
"source": [
"# If you are running this notebook in Colab, run this cell and follow the\n",
"# instructions to authenticate your GCP account. This provides access to your\n",
"# Cloud Storage bucket and lets you submit training jobs and prediction\n",
"# requests.\n",
"\n",
"import os\n",
"import sys\n",
"\n",
"# If on Google Cloud Notebook, then don't execute this code\n",
"if not os.path.exists(\"/opt/deeplearning/metadata/env_version\"):\n",
" if \"google.colab\" in sys.modules:\n",
" from google.colab import auth as google_auth\n",
"\n",
" google_auth.authenticate_user()\n",
"\n",
" # If you are running this notebook locally, replace the string below with the\n",
" # path to your service account key and run this cell to authenticate your GCP\n",
" # account.\n",
" elif not os.getenv(\"IS_TESTING\"):\n",
" ! gcloud auth login"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "setup_vars"
},
"source": [
"### Set up variables\n",
"\n",
"Next, set up some variables used throughout the tutorial.\n",
"### Import libraries and define constants"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "import_aip:mbsdk"
},
"outputs": [],
"source": [
"import google.cloud.aiplatform as aiplatform"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "init_aip:mbsdk"
},
"source": [
"## Initialize Vertex AI SDK for Python\n",
"\n",
"Initialize the Vertex AI SDK for Python for your project and the corresponding bucket."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "init_aip:mbsdk"
},
"outputs": [],
"source": [
"aiplatform.init(project=PROJECT_ID, location=REGION)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "tutorial_start:automl"
},
"source": [
"# Tutorial\n",
"\n",
"Now you are ready to start creating your own AutoML tabular regression model."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "import_file:u_dataset,bq"
},
"source": [
"#### Location of BigQuery training data.\n",
"\n",
"Set the `IMPORT_File` variable to the location of the data table in BigQuery."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "import_file:gsod,bq,lrg"
},
"outputs": [],
"source": [
"IMPORT_FILE = \"bigquery-public-data.samples.gsod\""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "07d8e5973590"
},
"source": [
"#### Prepare the batch prediction data\n",
"\n",
"Create two datasets from the original data."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "63a447565c9c"
},
"outputs": [],
"source": [
"from google.cloud import bigquery\n",
"\n",
"# Create client in default region\n",
"bq_client = bigquery.Client(\n",
" project=PROJECT_ID,\n",
" credentials=aiplatform.initializer.global_config.credentials,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "56ade963c098"
},
"outputs": [],
"source": [
"# Create training dataset in default region\n",
"TRAINING_INPUT_DATASET_ID = f\"gsod_training_{TIMESTAMP}\"\n",
"bq_dataset = bigquery.Dataset(f\"{PROJECT_ID}.{TRAINING_INPUT_DATASET_ID}\")\n",
"bq_dataset = bq_client.create_dataset(bq_dataset)\n",
"print(f\"Created dataset {bq_client.project}.{bq_dataset.dataset_id}\")\n",
"\n",
"# Create test dataset in default region\n",
"PREDICTION_INPUT_DATASET_ID = f\"gsod_prediction_{TIMESTAMP}\"\n",
"bq_dataset = bigquery.Dataset(f\"{PROJECT_ID}.{PREDICTION_INPUT_DATASET_ID}\")\n",
"bq_dataset = bq_client.create_dataset(bq_dataset)\n",
"print(f\"Created dataset {bq_client.project}.{bq_dataset.dataset_id}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "5b790c91182f"
},
"outputs": [],
"source": [
"# Select top 3000 rows of dataset\n",
"TRAINING_SIZE = 3000\n",
"query = f\"\"\"\n",
" SELECT *\n",
" FROM {IMPORT_FILE}\n",
" LIMIT {TRAINING_SIZE}\n",
" \"\"\"\n",
"\n",
"TRAINING_INPUT_TABLE_ID = f\"{PROJECT_ID}.{TRAINING_INPUT_DATASET_ID}.test\"\n",
"job_config = bigquery.QueryJobConfig(destination=TRAINING_INPUT_TABLE_ID)\n",
"\n",
"query_job = bq_client.query(query, job_config=job_config) # API request\n",
"query_job.result() # Waits for query to finish"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "8f841da4574b"
},
"outputs": [],
"source": [
"# Select a subset of the original dataset for testing\n",
"PREDICTION_SIZE = 100\n",
"query = f\"\"\"\n",
" SELECT *\n",
" FROM {IMPORT_FILE}\n",
" LIMIT {PREDICTION_SIZE}\n",
" OFFSET {TRAINING_SIZE} \n",
" \"\"\"\n",
"\n",
"PREDICTION_INPUT_TABLE_ID = f\"{PROJECT_ID}.{PREDICTION_INPUT_DATASET_ID}.prediction\"\n",
"job_config = bigquery.QueryJobConfig(destination=PREDICTION_INPUT_TABLE_ID)\n",
"\n",
"query_job = bq_client.query(query, job_config=job_config) # API request\n",
"query_job.result() # Waits for query to finish"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "create_dataset:tabular,bq,lrg"
},
"source": [
"### Create the Dataset\n",
"\n",
"Use `TabularDataset.create()` to create a `TabularDataset` resource, which takes the following parameters:\n",
"\n",
"- `display_name`: The human readable name for the `Dataset` resource.\n",
"- `gcs_source`: A list of one or more dataset index files to import the data items into the `Dataset` resource.\n",
"- `bq_source`: Alternatively, import data items from a BigQuery table into the `Dataset` resource.\n",
"\n",
"This operation may take several minutes."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "create_dataset:tabular,bq,lrg"
},
"outputs": [],
"source": [
"dataset = aiplatform.TabularDataset.create(\n",
" display_name=\"NOAA historical weather data\" + \"_\" + TIMESTAMP,\n",
" bq_source=[f\"bq://{TRAINING_INPUT_TABLE_ID}\"],\n",
")\n",
"\n",
"label_column = \"mean_temp\"\n",
"\n",
"print(dataset.resource_name)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "set_transformations:gsod"
},
"outputs": [],
"source": [
"COLUMN_SPECS = {\n",
" \"year\": \"auto\",\n",
" \"month\": \"auto\",\n",
" \"day\": \"auto\",\n",
"}\n",
"\n",
"label_column = \"mean_temp\""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "create_automl_pipeline:tabular,lrg,transformations"
},
"source": [
"### Create and run training pipeline\n",
"\n",
"To train an AutoML model, create and run a training pipeline.\n",
"\n",
"#### Create training pipeline\n",
"\n",
"Create an AutoML training pipeline using the `AutoMLTabularTrainingJob` class, with the following parameters:\n",
"\n",
"- `display_name`: The human readable name for the `TrainingJob` resource.\n",
"- `optimization_prediction_type`: The type task to train the model for.\n",
" - `classification`: A tabular classification model.\n",
" - `regression`: A tabular regression model.\n",
"- `column_transformations`: (Optional): Transformations to apply to the input columns\n",
"- `optimization_objective`: The optimization objective (minimize or maximize).\n",
" - binary classification:\n",
" - `minimize-log-loss`\n",
" - `maximize-au-roc`\n",
" - `maximize-au-prc`\n",
" - `maximize-precision-at-recall`\n",
" - `maximize-recall-at-precision`\n",
" - multi-class classification:\n",
" - `minimize-log-loss`\n",
" - regression:\n",
" - `minimize-rmse`\n",
" - `minimize-mae`\n",
" - `minimize-rmsle`\n",
"\n",
"The instantiated object is the DAG (directed acyclic graph) for the training pipeline."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "create_automl_pipeline:tabular,lrg,transformations"
},
"outputs": [],
"source": [
"training_job = aiplatform.AutoMLTabularTrainingJob(\n",
" display_name=\"gsod_\" + TIMESTAMP,\n",
" optimization_prediction_type=\"regression\",\n",
" optimization_objective=\"minimize-rmse\",\n",
" column_specs=COLUMN_SPECS,\n",
")\n",
"\n",
"print(training_job)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "run_automl_pipeline:tabular"
},
"source": [
"#### Run the training pipeline\n",
"\n",
"Run the training job by invoking the `run` method with the following parameters:\n",
"\n",
"- `dataset`: The `Dataset` resource to train the model.\n",
"- `model_display_name`: The human readable name for the trained model.\n",
"- `training_fraction_split`: The percentage of the dataset to use for training.\n",
"- `test_fraction_split`: The percentage of the dataset to use for test (holdout data).\n",
"- `validation_fraction_split`: The percentage of the dataset to use for validation.\n",
"- `target_column`: The name of the column to train as the label.\n",
"- `budget_milli_node_hours`: (optional) Maximum training time specified in unit of millihours (1000 = hour).\n",
"- `disable_early_stopping`: If `True`, training maybe completed before using the entire budget if the service believes it cannot further improve on the model objective measurements.\n",
"\n",
"The `run` method when completed returns the `Model` resource.\n",
"\n",
"The execution of the training pipeline will take upto 20 minutes."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "run_automl_pipeline:tabular"
},
"outputs": [],
"source": [
"model = training_job.run(\n",
" dataset=dataset,\n",
" model_display_name=\"gsod_\" + TIMESTAMP,\n",
" training_fraction_split=0.6,\n",
" validation_fraction_split=0.2,\n",
" test_fraction_split=0.2,\n",
" budget_milli_node_hours=1000,\n",
" disable_early_stopping=False,\n",
" target_column=label_column,\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "evaluate_the_model:mbsdk"
},
"source": [
"## Review model evaluation scores\n",
"After your model has finished training, you can review its evaluation scores."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "evaluate_the_model:mbsdk"
},
"outputs": [],
"source": [
"# Get evaluations\n",
"model_evaluations = model.list_model_evaluations()\n",
"\n",
"model_evaluation = list(model_evaluations)[0]\n",
"print(model_evaluation)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "337625a4c33c"
},
"source": [
"## Send a batch prediction request\n",
"\n",
"Now you can make a batch prediction."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "93bc53cc960d"
},
"source": [
"### Create a results dataset\n",
"\n",
"Create a dataset to store the prediction results."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "f49e90431fcd"
},
"outputs": [],
"source": [
"# Create results dataset in default region\n",
"RESULTS_DATASET_ID = f\"gsod_results_{TIMESTAMP}\"\n",
"bq_dataset = bigquery.Dataset(f\"{PROJECT_ID}.{RESULTS_DATASET_ID}\")\n",
"bq_dataset = bq_client.create_dataset(bq_dataset)\n",
"print(f\"Created dataset {bq_client.project}.{bq_dataset.dataset_id}\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "36c070503d2f"
},
"source": [
"### Make the batch prediction request\n",
"\n",
"You can make a batch prediction by invoking the batch_predict() method, with the following parameters:\n",
"\n",
"- `job_display_name`: The human readable name for the batch prediction job.\n",
"- `gcs_source`: A list of one or more batch request input files.\n",
"- `gcs_destination_prefix`: The Cloud Storage location for storing the batch prediction resuls.\n",
"- `instances_format`: The format for the input instances, either 'bigquery', 'csv' or 'jsonl'. Defaults to 'jsonl'.\n",
"- `predictions_format`: The format for the output predictions, either 'csv' or 'jsonl'. Defaults to 'jsonl'.\n",
"- `machine_type`: The type of machine to use for training.\n",
"- `accelerator_type`: The hardware accelerator type.\n",
"- `accelerator_count`: The number of accelerators to attach to a worker replica.\n",
"- `sync`: If set to True, the call will block while waiting for the asynchronous batch job to complete."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "bbd40d78ea46"
},
"outputs": [],
"source": [
"# Note: The bigquery_source and bigquery_destination_prefix must be in the same region\n",
"PREDICTION_RESULTS_DATASET_ID = f\"{PROJECT_ID}.{RESULTS_DATASET_ID}\"\n",
"\n",
"batch_predict_job = model.batch_predict(\n",
" job_display_name=\"tabular_regression_batch_predict_job\",\n",
" bigquery_source=f\"bq://{PREDICTION_INPUT_TABLE_ID}\",\n",
" instances_format=\"bigquery\",\n",
" predictions_format=\"bigquery\",\n",
" bigquery_destination_prefix=f\"bq://{PREDICTION_RESULTS_DATASET_ID}\",\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "4fc95aae00b0"
},
"source": [
"### View the batch prediction results\n",
"\n",
"Use the BigQuery Python client to query the destination table and return results as a Pandas dataframe."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "fafe1b0f654b"
},
"outputs": [],
"source": [
"dataframe = (\n",
" bq_client.query(f\"SELECT * FROM `{PREDICTION_RESULTS_DATASET_ID}.*`\")\n",
" .result()\n",
" .to_dataframe()\n",
")\n",
"\n",
"print(dataframe.head())"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "cleanup:mbsdk"
},
"source": [
"# Cleaning up\n",
"\n",
"To clean up all Google Cloud resources used in this project, you can [delete the Google Cloud\n",
"project](https://cloud.google.com/resource-manager/docs/creating-managing-projects#shutting_down_projects) you used for the tutorial.\n",
"\n",
"Otherwise, you can delete the individual resources you created in this tutorial:\n",
"\n",
"- Model\n",
"- AutoML Training Job\n",
"- Batch Job\n",
"- Cloud Storage Bucket"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "3be2c2bf9146"
},
"outputs": [],
"source": [
"# Delete BigQuery datasets\n",
"bq_client.delete_dataset(\n",
" f\"{PROJECT_ID}.{TRAINING_INPUT_DATASET_ID}\",\n",
" delete_contents=True,\n",
" not_found_ok=True,\n",
")\n",
"\n",
"bq_client.delete_dataset(\n",
" f\"{PROJECT_ID}.{PREDICTION_INPUT_DATASET_ID}\",\n",
" delete_contents=True,\n",
" not_found_ok=True,\n",
")\n",
"\n",
"bq_client.delete_dataset(\n",
" f\"{PROJECT_ID}.{RESULTS_DATASET_ID}\", delete_contents=True, not_found_ok=True\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "cleanup:mbsdk"
},
"outputs": [],
"source": [
"# Delete Vertex AI resources\n",
"dataset.delete()\n",
"model.delete()\n",
"training_job.delete()\n",
"batch_predict_job.delete()"
]
}
],
"metadata": {
"colab": {
"name": "sdk_automl_tabular_regression_batch_bq.ipynb",
"toc_visible": true
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
@@ -33,16 +33,22 @@
"\n",
"<table align=\"left\">\n",
" <td>\n",
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/master/notebooks/official/custom/sdk-custom-image-classification-batch.ipynb\">\n",
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/custom/sdk-custom-image-classification-batch.ipynb\">\n",
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"> Run in Colab\n",
" </a>\n",
" </td>\n",
" <td>\n",
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/master/notebooks/official/custom/sdk-custom-image-classification-batch.ipynb\">\n",
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/custom/sdk-custom-image-classification-batch.ipynb\">\n",
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\">\n",
" View on GitHub\n",
" </a>\n",
" </td>\n",
" <td>\n",
" <a href=\"https://console.cloud.google.com/ai/platform/notebooks/deploy-notebook?download_url=https://github.com/GoogleCloudPlatform/vertex-ai-samples/tree/main/notebooks/official/automl/automl-text-classification.ipynb\">\n",
" <img src=\"https://lh3.googleusercontent.com/UiNooY4LUgW_oTvpsNhPpQzsstV5W8F7rYgxgGBD85cWJoLmrOzhVs_ksK_vgx40SHs7jCqkTkCk=e14-rj-sc0xffffff-h130-w32\" alt=\"Vertex AI logo\">\n",
" Open in Vertex AI Workbench\n",
" </a>\n",
" </td>\n",
"</table>\n",
"<br/><br/><br/>"
]
@@ -56,7 +62,7 @@
"## Overview\n",
"\n",
"\n",
"This tutorial demonstrates how to use the Vertex SDK for Python to train and deploy a custom image classification model for batch prediction."
"This tutorial demonstrates how to use the Vertex AI SDK for Python to train and deploy a custom image classification model for batch prediction."
]
},
{
@@ -78,14 +84,23 @@
"source": [
"### Objective\n",
"\n",
"In this notebook, you create a custom-trained model from a Python script in a Docker container using the Vertex SDK for Python, and then do a prediction on the deployed model by sending data. Alternatively, you can create custom-trained models using `gcloud` command-line tool, or online using the Cloud Console.\n",
"In this tutorial, you learn to use `Vertex AI Training` to create a custom trained model and use `Vertex AI Batch Prediction` to do a batch prediction on the trained model.\n",
"\n",
"\n",
"create a custom-trained model from a Python script in a Docker container using the Vertex AI SDK for Python, and then do a prediction on the deployed model by sending data. Alternatively, you can create custom-trained models using `gcloud` command-line tool, or online using the Cloud Console.\n",
"\n",
"This tutorial uses the following Google Cloud ML services:\n",
"\n",
"- `Vertex AI Training`\n",
"- `Vertex AI Batch Prediction`\n",
"- `Vertex AI Model` resource\n",
"\n",
"\n",
"The steps performed include:\n",
"\n",
"- Create a Vertex AI custom job for training a model.\n",
"- Train a TensorFlow model.\n",
"- Make a batch prediction.\n",
"- Cleanup resources."
"- Create a `Vertex AI` custom job for training a TensorFlow model.\n",
"- Upload the trained model artifacts as a `Model` resource.\n",
"- Make a batch prediction."
]
},
{
@@ -108,15 +123,57 @@
"to generate a cost estimate based on your projected usage."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "a5cb73702a9b"
},
"source": [
"### Set up your local development environment\n",
"\n",
"**If you are using Colab or Workbench AI Notebooks**, your environment already meets\n",
"all the requirements to run this notebook. You can skip this step.\n",
"\n",
"**Otherwise**, make sure your environment meets this notebook's requirements.\n",
"You need the following:\n",
"\n",
"* The Google Cloud SDK\n",
"* Git\n",
"* Python 3\n",
"* virtualenv\n",
"* Jupyter notebook running in a virtual environment with Python 3\n",
"\n",
"The Google Cloud guide to [Setting up a Python development\n",
"environment](https://cloud.google.com/python/setup) and the [Jupyter\n",
"installation guide](https://jupyter.org/install) provide detailed instructions\n",
"for meeting these requirements. The following steps provide a condensed set of\n",
"instructions:\n",
"\n",
"1. [Install and initialize the Cloud SDK.](https://cloud.google.com/sdk/docs/)\n",
"\n",
"1. [Install Python 3.](https://cloud.google.com/python/setup#installing_python)\n",
"\n",
"1. [Install\n",
" virtualenv](https://cloud.google.com/python/setup#installing_and_using_virtualenv)\n",
" and create a virtual environment that uses Python 3. Activate the virtual environment.\n",
"\n",
"1. To install Jupyter, run `pip install jupyter` on the\n",
"command-line in a terminal shell.\n",
"\n",
"1. To launch Jupyter, run `jupyter notebook` on the command-line in a terminal shell.\n",
"\n",
"1. Open this notebook in the Jupyter Notebook Dashboard."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "install_aip"
},
"source": [
"## Installation\n",
"### Install additional packages\n",
"\n",
"Install the latest (preview) version of Vertex SDK for Python."
"Install the following packages for executing this notebook."
]
},
{
@@ -129,84 +186,21 @@
"source": [
"import os\n",
"\n",
"# The Google Cloud Notebook product has specific requirements\n",
"IS_GOOGLE_CLOUD_NOTEBOOK = os.path.exists(\"/opt/deeplearning/metadata/env_version\")\n",
"# The Vertex AI Workbench Notebook product has specific requirements\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\") and not os.getenv(\"VIRTUAL_ENV\")\n",
"IS_USER_MANAGED_WORKBENCH_NOTEBOOK = os.path.exists(\n",
" \"/opt/deeplearning/metadata/env_version\"\n",
")\n",
"\n",
"# Google Cloud Notebook requires dependencies to be installed with '--user'\n",
"# Vertex AI Notebook requires dependencies to be installed with '--user'\n",
"USER_FLAG = \"\"\n",
"if IS_GOOGLE_CLOUD_NOTEBOOK:\n",
" USER_FLAG = \"--user\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "YsxCgt1zlugo"
},
"outputs": [],
"source": [
"! pip install {USER_FLAG} --upgrade google-cloud-aiplatform"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "install_storage"
},
"source": [
"Install the latest GA version of *google-cloud-storage* library as well."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "qssss-KSlugo"
},
"outputs": [],
"source": [
"! pip install {USER_FLAG} --upgrade google-cloud-storage"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "install_pillow"
},
"source": [
"Install the *pillow* library for loading images."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "vhP4dtWUlugp"
},
"outputs": [],
"source": [
"! pip install {USER_FLAG} --upgrade pillow"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "install_numpy"
},
"source": [
"Install the *numpy* library for manipulation of image data."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "80-_pO4olugp"
},
"outputs": [],
"source": [
"! pip install {USER_FLAG} --upgrade numpy"
"if IS_WORKBENCH_NOTEBOOK:\n",
" USER_FLAG = \"--user\"\n",
"\n",
"! pip3 install {USER_FLAG} --upgrade google-cloud-aiplatform -q\n",
"! pip3 install {USER_FLAG} --upgrade google-cloud-storage -q\n",
"! pip3 install {USER_FLAG} --upgrade pillow -q\n",
"! pip3 install {USER_FLAG} --upgrade numpy -q"
]
},
{
@@ -287,24 +281,7 @@
},
"outputs": [],
"source": [
"import os\n",
"\n",
"PROJECT_ID = \"\"\n",
"\n",
"if not os.getenv(\"IS_TESTING\"):\n",
" # Get your Google Cloud project ID from gcloud\n",
" shell_output=!gcloud config list --format 'value(core.project)' 2>/dev/null\n",
" PROJECT_ID = shell_output[0]\n",
" print(\"Project ID: \", PROJECT_ID)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "set_project_id"
},
"source": [
"Otherwise, set your project ID here."
"PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}"
]
},
{
@@ -315,8 +292,56 @@
},
"outputs": [],
"source": [
"if PROJECT_ID == \"\" or PROJECT_ID is None:\n",
" PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}"
"if PROJECT_ID == \"\" or PROJECT_ID is None or PROJECT_ID == \"[your-project-id]\":\n",
" # Get your GCP project id from gcloud\n",
" shell_output = ! gcloud config list --format 'value(core.project)' 2>/dev/null\n",
" PROJECT_ID = shell_output[0]\n",
" print(\"Project ID:\", PROJECT_ID)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "09021c90b34c"
},
"outputs": [],
"source": [
"! gcloud config set project $PROJECT_ID"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "88dd74c4c84e"
},
"source": [
"#### Region\n",
"\n",
"You can also change the `REGION` variable, which is used for operations\n",
"throughout the rest of this notebook. Below are regions supported for Vertex AI. We recommend that you choose the region closest to you.\n",
"\n",
"- Americas: `us-central1`\n",
"- Europe: `europe-west4`\n",
"- Asia Pacific: `asia-east1`\n",
"\n",
"You may not use a multi-regional bucket for training with Vertex AI. Not all regions provide support for all Vertex AI services.\n",
"\n",
"Learn more about [Vertex AI regions](https://cloud.google.com/vertex-ai/docs/general/locations)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "5c615e53149f"
},
"outputs": [],
"source": [
"REGION = \"[your-region]\" # @param {type: \"string\"}\n",
"\n",
"if REGION == \"[your-region]\":\n",
" REGION = \"us-central1\""
]
},
{
@@ -351,7 +376,7 @@
"source": [
"### Authenticate your Google Cloud account\n",
"\n",
"**If you are using Google Cloud Notebooks**, your environment is already\n",
"**If you are using Workbench AI Notebooks**, your environment is already\n",
"authenticated. Skip this step.\n",
"\n",
"**If you are using Colab**, run the cell below and follow the instructions\n",
@@ -386,18 +411,19 @@
},
"outputs": [],
"source": [
"import sys\n",
"\n",
"# If you are running this notebook in Colab, run this cell and follow the\n",
"# instructions to authenticate your GCP account. This provides access to your\n",
"# Cloud Storage bucket and lets you submit training jobs and prediction\n",
"# requests.\n",
"\n",
"# The Google Cloud Notebook product has specific requirements\n",
"IS_GOOGLE_CLOUD_NOTEBOOK = os.path.exists(\"/opt/deeplearning/metadata/env_version\")\n",
"import os\n",
"import sys\n",
"\n",
"# If on Google Cloud Notebooks, then don't execute this code\n",
"if not IS_GOOGLE_CLOUD_NOTEBOOK:\n",
"# If on Vertex AI Workbench, then don't execute this code\n",
"IS_COLAB = \"google.colab\" in sys.modules\n",
"if not os.path.exists(\"/opt/deeplearning/metadata/env_version\") and not os.getenv(\n",
" \"DL_ANACONDA_HOME\"\n",
"):\n",
" if \"google.colab\" in sys.modules:\n",
" from google.colab import auth as google_auth\n",
"\n",
@@ -426,12 +452,7 @@
"trained model that results from your job in the same bucket. Using this model artifact, you can then create Vertex AI model resources.\n",
"\n",
"Set the name of your Cloud Storage bucket below. It must be unique across all\n",
"Cloud Storage buckets.\n",
"\n",
"You may also change the `REGION` variable, which is used for operations\n",
"throughout the rest of this notebook. Make sure to [choose a region where Vertex AI services are\n",
"available](https://cloud.google.com/vertex-ai/docs/general/locations#available_regions). You may\n",
"not use a Multi-Regional Storage bucket for training with Vertex AI."
"Cloud Storage buckets."
]
},
{
@@ -442,8 +463,8 @@
},
"outputs": [],
"source": [
"BUCKET_NAME = \"gs://[your-bucket-name]\" # @param {type:\"string\"}\n",
"REGION = \"[your-region]\" # @param {type:\"string\"}"
"BUCKET_NAME = \"[your-bucket-name]\" # @param {type:\"string\"}\n",
"BUCKET_URI = f\"gs://{BUCKET_NAME}\""
]
},
{
@@ -454,8 +475,9 @@
},
"outputs": [],
"source": [
"if BUCKET_NAME == \"\" or BUCKET_NAME is None or BUCKET_NAME == \"gs://[your-bucket-name]\":\n",
" BUCKET_NAME = \"gs://\" + PROJECT_ID + \"aip-\" + TIMESTAMP"
"if BUCKET_URI == \"\" or BUCKET_URI is None or BUCKET_URI == \"gs://[your-bucket-name]\":\n",
" BUCKET_NAME = PROJECT_ID + \"aip-\" + TIMESTAMP\n",
" BUCKET_URI = \"gs://\" + BUCKET_NAME"
]
},
{
@@ -475,7 +497,7 @@
},
"outputs": [],
"source": [
"! gsutil mb -l $REGION $BUCKET_NAME"
"! gsutil mb -l $REGION $BUCKET_URI"
]
},
{
@@ -495,7 +517,7 @@
},
"outputs": [],
"source": [
"! gsutil ls -al $BUCKET_NAME"
"! gsutil ls -al $BUCKET_URI"
]
},
{
@@ -504,9 +526,21 @@
"id": "setup_vars"
},
"source": [
"### Set up variables\n",
"### Import libraries and define constants"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "9dcd3eedadfb"
},
"outputs": [],
"source": [
"import os\n",
"import sys\n",
"\n",
"Next, set up some variables used throughout the tutorial."
"from google.cloud import aiplatform"
]
},
{
@@ -515,9 +549,9 @@
"id": "import_aip"
},
"source": [
"#### Import Vertex SDK for Python\n",
"### Initialize Vertex AI \n",
"\n",
"Import the Vertex SDK for Python into your Python environment and initialize it."
"Initialize the Vertex AI SDK for Python for your project."
]
},
{
@@ -528,13 +562,7 @@
},
"outputs": [],
"source": [
"import os\n",
"import sys\n",
"\n",
"from google.cloud import aiplatform\n",
"from google.cloud.aiplatform import gapic as aip\n",
"\n",
"aiplatform.init(project=PROJECT_ID, location=REGION, staging_bucket=BUCKET_NAME)"
"aiplatform.init(project=PROJECT_ID, location=REGION, staging_bucket=BUCKET_URI)"
]
},
{
@@ -545,17 +573,18 @@
"source": [
"#### Set hardware accelerators\n",
"\n",
"You can set hardware accelerators for both training and prediction.\n",
"You can set hardware accelerators for training and prediction.\n",
"\n",
"Set the variables `TRAIN_GPU/TRAIN_NGPU` and `DEPLOY_GPU/DEPLOY_NGPU` to use a container image supporting a GPU and the number of GPUs allocated to the virtual machine (VM) instance. For example, to use a GPU container image with 4 Nvidia Tesla K80 GPUs allocated to each VM, you would specify:\n",
"Set the variables `TRAIN_GPU/TRAIN_NGPU` and `DEPLOY_GPU/DEPLOY_NGPU` to use a container image supporting a GPU and the number of GPUs allocated to the virtual machine (VM) instance. For example, to use a GPU container image with 4 Nvidia Telsa K80 GPUs allocated to each VM, you would specify:\n",
"\n",
" (aip.AcceleratorType.NVIDIA_TESLA_K80, 4)\n",
" (aiplatform.gapic.AcceleratorType.NVIDIA_TESLA_K80, 4)\n",
"\n",
"See the [locations where accelerators are available](https://cloud.google.com/vertex-ai/docs/general/locations#accelerators).\n",
"\n",
"Otherwise specify `(None, None)` to use a container image to run on a CPU.\n",
"\n",
"*Note*: TensorFlow releases earlier than 2.3 for GPU support fail to load the custom model in this tutorial. This issue is caused by static graph operations that are generated in the serving function. This is a known issue, which is fixed in TensorFlow 2.3. If you encounter this issue with your own custom models, use a container image for TensorFlow 2.3 or later with GPU support."
"Learn more about [hardware accelerator support for your region](https://cloud.google.com/vertex-ai/docs/general/locations#accelerators).\n",
"\n",
"*Note*: TF releases before 2.3 for GPU support will fail to load the custom model in this tutorial. It is a known issue and fixed in TF 2.3. This is caused by static graph ops that are generated in the serving function. If you encounter this issue on your own custom models, use a container image for TF 2.3 with GPU support."
]
},
{
@@ -566,9 +595,9 @@
},
"outputs": [],
"source": [
"TRAIN_GPU, TRAIN_NGPU = (aip.AcceleratorType.NVIDIA_TESLA_K80, 1)\n",
"TRAIN_GPU, TRAIN_NGPU = (aiplatform.gapic.AcceleratorType.NVIDIA_TESLA_K80, 1)\n",
"\n",
"DEPLOY_GPU, DEPLOY_NGPU = (aip.AcceleratorType.NVIDIA_TESLA_K80, 1)"
"DEPLOY_GPU, DEPLOY_NGPU = (aiplatform.gapic.AcceleratorType.NVIDIA_TESLA_K80, 1)"
]
},
{
@@ -579,9 +608,11 @@
"source": [
"#### Set pre-built containers\n",
"\n",
"Vertex AI provides pre-built containers to run training and prediction.\n",
"Set the pre-built Docker container image for training and prediction.\n",
"\n",
"For the latest list, see [Pre-built containers for training](https://cloud.google.com/vertex-ai/docs/training/pre-built-containers) and [Pre-built containers for prediction](https://cloud.google.com/vertex-ai/docs/predictions/pre-built-containers)"
"For the latest list, see [Pre-built containers for training](https://cloud.google.com/ai-platform-unified/docs/training/pre-built-containers).\n",
"\n",
"For the latest list, see [Pre-built containers for prediction](https://cloud.google.com/ai-platform-unified/docs/predictions/pre-built-containers)."
]
},
{
@@ -595,8 +626,12 @@
"TRAIN_VERSION = \"tf-gpu.2-1\"\n",
"DEPLOY_VERSION = \"tf2-gpu.2-1\"\n",
"\n",
"TRAIN_IMAGE = \"gcr.io/cloud-aiplatform/training/{}:latest\".format(TRAIN_VERSION)\n",
"DEPLOY_IMAGE = \"gcr.io/cloud-aiplatform/prediction/{}:latest\".format(DEPLOY_VERSION)\n",
"TRAIN_IMAGE = \"{}-docker.pkg.dev/vertex-ai/training/{}:latest\".format(\n",
" REGION.split(\"-\")[0], TRAIN_VERSION\n",
")\n",
"DEPLOY_IMAGE = \"{}-docker.pkg.dev/vertex-ai/prediction/{}:latest\".format(\n",
" REGION.split(\"-\")[0], DEPLOY_VERSION\n",
")\n",
"\n",
"print(\"Training:\", TRAIN_IMAGE, TRAIN_GPU, TRAIN_NGPU)\n",
"print(\"Deployment:\", DEPLOY_IMAGE, DEPLOY_GPU, DEPLOY_NGPU)"
@@ -701,7 +736,7 @@
"outputs": [],
"source": [
"JOB_NAME = \"custom_job_\" + TIMESTAMP\n",
"MODEL_DIR = \"{}/{}\".format(BUCKET_NAME, JOB_NAME)\n",
"MODEL_DIR = \"{}/{}\".format(BUCKET_URI, JOB_NAME)\n",
"\n",
"if not TRAIN_NGPU or TRAIN_NGPU < 2:\n",
" TRAIN_STRATEGY = \"single\"\n",
@@ -1022,7 +1057,7 @@
"BATCH_PREDICTION_INSTANCES_FILE = \"batch_prediction_instances.jsonl\"\n",
"\n",
"BATCH_PREDICTION_GCS_SOURCE = (\n",
" BUCKET_NAME + \"/batch_prediction_instances/\" + BATCH_PREDICTION_INSTANCES_FILE\n",
" BUCKET_URI + \"/batch_prediction_instances/\" + BATCH_PREDICTION_INSTANCES_FILE\n",
")\n",
"\n",
"# Write instances at JSONL\n",
@@ -1082,7 +1117,7 @@
"DESTINATION_FOLDER = \"batch_prediction_results\"\n",
"\n",
"# The Cloud Storage bucket to upload results to\n",
"BATCH_PREDICTION_GCS_DEST_PREFIX = BUCKET_NAME + \"/\" + DESTINATION_FOLDER\n",
"BATCH_PREDICTION_GCS_DEST_PREFIX = BUCKET_URI + \"/\" + DESTINATION_FOLDER\n",
"\n",
"# Make SDK batch_predict method call\n",
"batch_prediction_job = model.batch_predict(\n",
@@ -1132,10 +1167,10 @@
"\n",
"# Get most recently modified directory\n",
"latest_directory = max(\n",
" [\n",
" (\n",
" os.path.join(RESULTS_DIRECTORY_FULL, d)\n",
" for d in os.listdir(RESULTS_DIRECTORY_FULL)\n",
" ],\n",
" ),\n",
" key=os.path.getmtime,\n",
")\n",
"\n",
@@ -1225,8 +1260,8 @@
"# Delete the model\n",
"model.delete()\n",
"\n",
"if delete_bucket and \"BUCKET_NAME\" in globals():\n",
" ! gsutil -m rm -r $BUCKET_NAME"
"if delete_bucket or os.getenv(\"IS_TESTING\"):\n",
" ! gsutil -m rm -r $BUCKET_URI"
]
}
],
@@ -33,16 +33,22 @@
"\n",
"<table align=\"left\">\n",
" <td>\n",
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/master/notebooks/official/custom/sdk-custom-image-classification-online.ipynb\">\n",
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/custom/sdk-custom-image-classification-online.ipynb\">\n",
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"> Run in Colab\n",
" </a>\n",
" </td>\n",
" <td>\n",
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/master/notebooks/official/custom/sdk-custom-image-classification-online.ipynb\">\n",
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/custom/sdk-custom-image-classification-online.ipynb\">\n",
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\">\n",
" View on GitHub\n",
" </a>\n",
" </td>\n",
" <td>\n",
" <a href=\"https://console.cloud.google.com/vertex-ai/workbench/deploy-notebook?download_url=https://raw.githubusercontent.com/GoogleCloudPlatform/vertex-ai-samples/main/notebooks/official/custom/sdk-custom-image-classification-online.ipynb\">\n",
" <img src=\"https://lh3.googleusercontent.com/UiNooY4LUgW_oTvpsNhPpQzsstV5W8F7rYgxgGBD85cWJoLmrOzhVs_ksK_vgx40SHs7jCqkTkCk=e14-rj-sc0xffffff-h130-w32\" alt=\"Vertex AI logo\">\n",
" Open in Vertex AI Workbench\n",
" </a>\n",
" </td>\n",
"</table>\n",
"<br/><br/><br/>"
]
@@ -56,7 +62,7 @@
"## Overview\n",
"\n",
"\n",
"This tutorial demonstrates how to use the Vertex SDK for Python to train and deploy a custom image classification model for online prediction."
"This tutorial demonstrates how to use the Vertex AI SDK for Python to train and deploy a custom image classification model for online prediction."
]
},
{
@@ -78,12 +84,20 @@
"source": [
"### Objective\n",
"\n",
"In this notebook, you create a custom-trained model from a Python script in a Docker container using the Vertex SDK for Python, and then do a prediction on the deployed model by sending data. Alternatively, you can create custom-trained models using `gcloud` command-line tool, or online using the Cloud Console.\n",
"In this tutorial, you learn to use `Vertex AI Training` to create a custom-trained model from a Python script in a Docker container, and learn to use `Vertex AI Prediction` to do a prediction on the deployed model by sending data. Alternatively, you can create custom-trained models using `gcloud` command-line tool, or online using the Cloud Console.\n",
"\n",
"This tutorial uses the following Google Cloud ML services:\n",
"\n",
"- `Vertex AI Training`\n",
"- `Vertex AI Prediction`\n",
"- `Vertex AI Model` resource\n",
"- `Vertex AI Endpoint` resource\n",
"\n",
"The steps performed include:\n",
"\n",
"- Create a Vertex AI custom job for training a model.\n",
"- Train a TensorFlow model.\n",
"- Create a `Vertex AI` custom job for training a TensorFlow model.\n",
"- Upload the trained model artifacts to a `Model` resource.\n",
"- Create a serving `Endpoint` resource.\n",
"- Deploy the `Model` resource to a serving `Endpoint` resource.\n",
"- Make a prediction.\n",
"- Undeploy the `Model` resource."
@@ -117,7 +131,7 @@
"source": [
"## Installation\n",
"\n",
"Install the latest (preview) version of Vertex SDK for Python."
"Install the packages required for executing this notebook."
]
},
{
@@ -130,84 +144,21 @@
"source": [
"import os\n",
"\n",
"# The Google Cloud Notebook product has specific requirements\n",
"IS_GOOGLE_CLOUD_NOTEBOOK = os.path.exists(\"/opt/deeplearning/metadata/env_version\")\n",
"# The Vertex AI Workbench Notebook product has specific requirements\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\") and not os.getenv(\"VIRTUAL_ENV\")\n",
"IS_USER_MANAGED_WORKBENCH_NOTEBOOK = os.path.exists(\n",
" \"/opt/deeplearning/metadata/env_version\"\n",
")\n",
"\n",
"# Google Cloud Notebook requires dependencies to be installed with '--user'\n",
"# Vertex AI Notebook requires dependencies to be installed with '--user'\n",
"USER_FLAG = \"\"\n",
"if IS_GOOGLE_CLOUD_NOTEBOOK:\n",
" USER_FLAG = \"--user\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "YsxCgt1zlugo"
},
"outputs": [],
"source": [
"! pip install {USER_FLAG} --upgrade google-cloud-aiplatform"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "install_storage"
},
"source": [
"Install the latest GA version of *google-cloud-storage* library as well."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "qssss-KSlugo"
},
"outputs": [],
"source": [
"! pip install {USER_FLAG} --upgrade google-cloud-storage"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "install_pillow"
},
"source": [
"Install the *pillow* library for loading images."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "vhP4dtWUlugp"
},
"outputs": [],
"source": [
"! pip install {USER_FLAG} --upgrade pillow"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "install_numpy"
},
"source": [
"Install the *numpy* library for manipulation of image data."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "80-_pO4olugp"
},
"outputs": [],
"source": [
"! pip install {USER_FLAG} --upgrade numpy"
"if IS_WORKBENCH_NOTEBOOK:\n",
" USER_FLAG = \"--user\"\n",
"\n",
"! pip3 install --upgrade google-cloud-aiplatform $USER_FLAG -q\n",
"! pip3 install {USER_FLAG} --upgrade google-cloud-storage -q\n",
"! pip3 install {USER_FLAG} --upgrade pillow -q\n",
"! pip3 install {USER_FLAG} --upgrade numpy -q"
]
},
{
@@ -288,24 +239,7 @@
},
"outputs": [],
"source": [
"import os\n",
"\n",
"PROJECT_ID = \"\"\n",
"\n",
"if not os.getenv(\"IS_TESTING\"):\n",
" # Get your Google Cloud project ID from gcloud\n",
" shell_output=!gcloud config list --format 'value(core.project)' 2>/dev/null\n",
" PROJECT_ID = shell_output[0]\n",
" print(\"Project ID: \", PROJECT_ID)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "set_project_id"
},
"source": [
"Otherwise, set your project ID here."
"PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}"
]
},
{
@@ -316,8 +250,56 @@
},
"outputs": [],
"source": [
"if PROJECT_ID == \"\" or PROJECT_ID is None:\n",
" PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}"
"if PROJECT_ID == \"\" or PROJECT_ID is None or PROJECT_ID == \"[your-project-id]\":\n",
" # Get your GCP project id from gcloud\n",
" shell_output = ! gcloud config list --format 'value(core.project)' 2>/dev/null\n",
" PROJECT_ID = shell_output[0]\n",
" print(\"Project ID:\", PROJECT_ID)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "250cb8c648d5"
},
"outputs": [],
"source": [
"! gcloud config set project $PROJECT_ID"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "2aa333eca058"
},
"source": [
"#### Region\n",
"\n",
"You can also change the `REGION` variable, which is used for operations\n",
"throughout the rest of this notebook. Below are regions supported for Vertex AI. We recommend that you choose the region closest to you.\n",
"\n",
"- Americas: `us-central1`\n",
"- Europe: `europe-west4`\n",
"- Asia Pacific: `asia-east1`\n",
"\n",
"You may not use a multi-regional bucket for training with Vertex AI. Not all regions provide support for all Vertex AI services.\n",
"\n",
"Learn more about [Vertex AI regions](https://cloud.google.com/vertex-ai/docs/general/locations)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "d8b34ef9a3d0"
},
"outputs": [],
"source": [
"REGION = \"[your-region]\" # @param {type: \"string\"}\n",
"\n",
"if REGION == \"[your-region]\":\n",
" REGION = \"us-central1\""
]
},
{
@@ -352,7 +334,7 @@
"source": [
"### Authenticate your Google Cloud account\n",
"\n",
"**If you are using Google Cloud Notebooks**, your environment is already\n",
"**If you are using Workbench AI Notebooks**, your environment is already\n",
"authenticated. Skip this step.\n",
"\n",
"**If you are using Colab**, run the cell below and follow the instructions\n",
@@ -387,19 +369,19 @@
},
"outputs": [],
"source": [
"import os\n",
"import sys\n",
"\n",
"# If you are running this notebook in Colab, run this cell and follow the\n",
"# instructions to authenticate your GCP account. This provides access to your\n",
"# Cloud Storage bucket and lets you submit training jobs and prediction\n",
"# requests.\n",
"\n",
"# The Google Cloud Notebook product has specific requirements\n",
"IS_GOOGLE_CLOUD_NOTEBOOK = os.path.exists(\"/opt/deeplearning/metadata/env_version\")\n",
"import os\n",
"import sys\n",
"\n",
"# If on Google Cloud Notebooks, then don't execute this code\n",
"if not IS_GOOGLE_CLOUD_NOTEBOOK:\n",
"# If on Vertex AI Workbench, then don't execute this code\n",
"IS_COLAB = \"google.colab\" in sys.modules\n",
"if not os.path.exists(\"/opt/deeplearning/metadata/env_version\") and not os.getenv(\n",
" \"DL_ANACONDA_HOME\"\n",
"):\n",
" if \"google.colab\" in sys.modules:\n",
" from google.colab import auth as google_auth\n",
"\n",
@@ -430,12 +412,7 @@
"online predictions.\n",
"\n",
"Set the name of your Cloud Storage bucket below. It must be unique across all\n",
"Cloud Storage buckets.\n",
"\n",
"You may also change the `REGION` variable, which is used for operations\n",
"throughout the rest of this notebook. Make sure to [choose a region where Vertex AI services are\n",
"available](https://cloud.google.com/vertex-ai/docs/general/locations#available_regions). You may\n",
"not use a Multi-Regional Storage bucket for training with Vertex AI."
"Cloud Storage buckets."
]
},
{
@@ -446,8 +423,8 @@
},
"outputs": [],
"source": [
"BUCKET_NAME = \"gs://[your-bucket-name]\" # @param {type:\"string\"}\n",
"REGION = \"[your-region]\" # @param {type:\"string\"}"
"BUCKET_NAME = \"[your-bucket-name]\" # @param {type:\"string\"}\n",
"BUCKET_URI = f\"gs://{BUCKET_NAME}\""
]
},
{
@@ -458,8 +435,9 @@
},
"outputs": [],
"source": [
"if BUCKET_NAME == \"\" or BUCKET_NAME is None or BUCKET_NAME == \"gs://[your-bucket-name]\":\n",
" BUCKET_NAME = \"gs://\" + PROJECT_ID + \"aip-\" + TIMESTAMP"
"if BUCKET_NAME == \"\" or BUCKET_NAME is None or BUCKET_NAME == \"[your-bucket-name]\":\n",
" BUCKET_NAME = PROJECT_ID + \"aip-\" + TIMESTAMP\n",
" BUCKET_URI = \"gs://\" + BUCKET_NAME"
]
},
{
@@ -479,7 +457,7 @@
},
"outputs": [],
"source": [
"! gsutil mb -l $REGION $BUCKET_NAME"
"! gsutil mb -l $REGION $BUCKET_URI"
]
},
{
@@ -499,7 +477,7 @@
},
"outputs": [],
"source": [
"! gsutil ls -al $BUCKET_NAME"
"! gsutil ls -al $BUCKET_URI"
]
},
{
@@ -510,7 +488,23 @@
"source": [
"### Set up variables\n",
"\n",
"Next, set up some variables used throughout the tutorial."
"Next, set up some variables used throughout the tutorial.\n",
"\n",
"### Import libraries and define constants"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "8a9846bc4a2e"
},
"outputs": [],
"source": [
"import os\n",
"import sys\n",
"\n",
"from google.cloud import aiplatform"
]
},
{
@@ -519,9 +513,9 @@
"id": "import_aip"
},
"source": [
"#### Import Vertex SDK for Python\n",
"### Initialize Vertex AI SDK for Python\n",
"\n",
"Import the Vertex SDK for Python into your Python environment and initialize it."
"Initialize the Vertex AI SDK for Python for your project and corresponding bucket."
]
},
{
@@ -532,13 +526,7 @@
},
"outputs": [],
"source": [
"import os\n",
"import sys\n",
"\n",
"from google.cloud import aiplatform\n",
"from google.cloud.aiplatform import gapic as aip\n",
"\n",
"aiplatform.init(project=PROJECT_ID, location=REGION, staging_bucket=BUCKET_NAME)"
"aiplatform.init(project=PROJECT_ID, location=REGION, staging_bucket=BUCKET_URI)"
]
},
{
@@ -553,7 +541,7 @@
"\n",
"Set the variables `TRAIN_GPU/TRAIN_NGPU` and `DEPLOY_GPU/DEPLOY_NGPU` to use a container image supporting a GPU and the number of GPUs allocated to the virtual machine (VM) instance. For example, to use a GPU container image with 4 Nvidia Tesla K80 GPUs allocated to each VM, you would specify:\n",
"\n",
" (aip.AcceleratorType.NVIDIA_TESLA_K80, 4)\n",
" (aiplatform.gapic.AcceleratorType.NVIDIA_TESLA_K80, 4)\n",
"\n",
"See the [locations where accelerators are available](https://cloud.google.com/vertex-ai/docs/general/locations#accelerators).\n",
"\n",
@@ -570,9 +558,9 @@
},
"outputs": [],
"source": [
"TRAIN_GPU, TRAIN_NGPU = (aip.AcceleratorType.NVIDIA_TESLA_K80, 1)\n",
"TRAIN_GPU, TRAIN_NGPU = (aiplatform.gapic.AcceleratorType.NVIDIA_TESLA_K80, 1)\n",
"\n",
"DEPLOY_GPU, DEPLOY_NGPU = (aip.AcceleratorType.NVIDIA_TESLA_K80, 1)"
"DEPLOY_GPU, DEPLOY_NGPU = (aiplatform.gapic.AcceleratorType.NVIDIA_TESLA_K80, 1)"
]
},
{
@@ -583,9 +571,14 @@
"source": [
"#### Set pre-built containers\n",
"\n",
"Vertex AI provides pre-built containers to run training and prediction.\n",
"\n",
"For the latest list, see [Pre-built containers for training](https://cloud.google.com/vertex-ai/docs/training/pre-built-containers) and [Pre-built containers for prediction](https://cloud.google.com/vertex-ai/docs/predictions/pre-built-containers)"
"Set the pre-built Docker container image for training and prediction.\n",
"\n",
"\n",
"For the latest list, see [Pre-built containers for training](https://cloud.google.com/ai-platform-unified/docs/training/pre-built-containers).\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)."
]
},
{
@@ -599,8 +592,12 @@
"TRAIN_VERSION = \"tf-gpu.2-1\"\n",
"DEPLOY_VERSION = \"tf2-gpu.2-1\"\n",
"\n",
"TRAIN_IMAGE = \"gcr.io/cloud-aiplatform/training/{}:latest\".format(TRAIN_VERSION)\n",
"DEPLOY_IMAGE = \"gcr.io/cloud-aiplatform/prediction/{}:latest\".format(DEPLOY_VERSION)\n",
"TRAIN_IMAGE = \"{}-docker.pkg.dev/vertex-ai/training/{}:latest\".format(\n",
" REGION.split(\"-\")[0], TRAIN_VERSION\n",
")\n",
"DEPLOY_IMAGE = \"{}-docker.pkg.dev/vertex-ai/prediction/{}:latest\".format(\n",
" REGION.split(\"-\")[0], DEPLOY_VERSION\n",
")\n",
"\n",
"print(\"Training:\", TRAIN_IMAGE, TRAIN_GPU, TRAIN_NGPU)\n",
"print(\"Deployment:\", DEPLOY_IMAGE, DEPLOY_GPU, DEPLOY_NGPU)"
@@ -705,7 +702,7 @@
"outputs": [],
"source": [
"JOB_NAME = \"custom_job_\" + TIMESTAMP\n",
"MODEL_DIR = \"{}/{}\".format(BUCKET_NAME, JOB_NAME)\n",
"MODEL_DIR = \"{}/{}\".format(BUCKET_URI, JOB_NAME)\n",
"\n",
"if not TRAIN_NGPU or TRAIN_NGPU < 2:\n",
" TRAIN_STRATEGY = \"single\"\n",
@@ -1185,8 +1182,8 @@
"# Delete the endpoint\n",
"endpoint.delete()\n",
"\n",
"if delete_bucket and \"BUCKET_NAME\" in globals():\n",
" ! gsutil -m rm -r $BUCKET_NAME"
"if delete_bucket or os.getenv(\"IS_TESTING\"):\n",
" ! gsutil -m rm -r $BUCKET_URI"
]
}
],
@@ -33,18 +33,19 @@
"\n",
"<table align=\"left\">\n",
" <td>\n",
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/tree/master/notebooks/official/automl/sdk_automl_tabular_binary_classification_batch_explain.ipynb\">\n",
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/explainable_ai/sdk_automl_tabular_binary_classification_batch_explain.ipynb\">\n",
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"> Run in Colab\n",
" </a>\n",
" </td>\n",
" <td>\n",
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/tree/master/notebooks/official/automl/sdk_automl_tabular_binary_classification_batch_explain.ipynb\">\n",
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/explainable_ai/sdk_automl_tabular_binary_classification_batch_explain.ipynb\">\n",
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\">\n",
" View on GitHub\n",
" </a>\n",
" </td>\n",
" <td>\n",
" <a href=\"https://console.cloud.google.com/vertex-ai/platform/notebooks/deploy-notebook?download_url=https://github.com/GoogleCloudPlatform/vertex-ai-samples/tree/master/notebooks/official/automl/sdk_automl_tabular_binary_classification_batch_explain.ipynb\">\n",
" <a href=\"https://console.cloud.google.com/vertex-ai/workbench/deploy-notebook?download_url=https://raw.githubusercontent.com/GoogleCloudPlatform/vertex-ai-samples/tree/main/notebooks/official/explainable_ai/sdk_automl_tabular_binary_classification_batch_explain.ipynb\">\n",
" <img src=\"https://lh3.googleusercontent.com/UiNooY4LUgW_oTvpsNhPpQzsstV5W8F7rYgxgGBD85cWJoLmrOzhVs_ksK_vgx40SHs7jCqkTkCk=e14-rj-sc0xffffff-h130-w32\" alt=\"Vertex AI logo\">\n",
" Open in Vertex AI Workbench\n",
" </a>\n",
" </td>\n",
@@ -61,7 +62,7 @@
"## Overview\n",
"\n",
"\n",
"This tutorial demonstrates how to use the Vertex SDK to create tabular binary classification models and do batch prediction with explanation using a Google Cloud [AutoML](https://cloud.google.com/vertex-ai/docs/start/automl-users) model."
"This tutorial demonstrates how to use the Vertex AI SDK to create tabular binary classification models and do batch prediction with explanation using a Google Cloud [AutoML](https://cloud.google.com/vertex-ai/docs/start/automl-users) model."
]
},
{
@@ -83,13 +84,21 @@
"source": [
"### Objective\n",
"\n",
"In this tutorial, you create an AutoML tabular binary classification model from a Python script, and send a batch prediction request with explainability using the Vertex SDK. You can alternatively create and deploy models using the `gcloud` command-line tool or online using the Cloud Console.\n",
"In this tutorial, you learn to use `AutoML` to create a tabular binary classification model from a Python script, and then learn to use `Vertex AI Batch Prediction` to make predictions with explanations. You can alternatively create and deploy models using the `gcloud` command-line tool or online using the Cloud Console.\n",
"\n",
"This tutorial uses the following Google Cloud ML services:\n",
"\n",
"- `Vertex AI AutoML`\n",
"- `Vertex AI Batch Prediction`\n",
"- `Vertex Explainable AI`\n",
"- `Vertex AI Model` resource\n",
"\n",
"\n",
"The steps performed include:\n",
"\n",
"- Create a Vertex `Dataset` resource.\n",
"- Train the model.\n",
"- View the model evaluation.\n",
"- Create a `Vertex Dataset` resource.\n",
"- Train an `AutoML` tabular binary classification model.\n",
"- View the model evaluation metrics for the trained model.\n",
"- Make a batch prediction request with explainability.\n",
"\n",
"There is one key difference between using batch prediction and using online prediction:\n",
@@ -127,7 +136,7 @@
"source": [
"### Set up your local development environment\n",
"\n",
"If you are using Colab or Google Cloud Notebook, your environment already meets all the requirements to run this notebook. You can skip this step.\n",
"If you are using Colab or Vertex Workbench Notebook, your environment already meets all the requirements to run this notebook. You can skip this step.\n",
"\n",
"Otherwise, make sure your environment meets this notebook's requirements. You need the following:\n",
"\n",
@@ -160,7 +169,7 @@
"source": [
"## Installation\n",
"\n",
"Install the latest version of Vertex SDK for Python."
"Install the packages required for executing this notebook."
]
},
{
@@ -173,45 +182,20 @@
"source": [
"import os\n",
"\n",
"# Google Cloud Notebook\n",
"if os.path.exists(\"/opt/deeplearning/metadata/env_version\"):\n",
" USER_FLAG = \"--user\"\n",
"else:\n",
" USER_FLAG = \"\"\n",
"# The Vertex AI Workbench Notebook product has specific requirements\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\") and not os.getenv(\"VIRTUAL_ENV\")\n",
"IS_USER_MANAGED_WORKBENCH_NOTEBOOK = os.path.exists(\n",
" \"/opt/deeplearning/metadata/env_version\"\n",
")\n",
"\n",
"! pip3 install --upgrade google-cloud-aiplatform $USER_FLAG"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "install_storage"
},
"source": [
"Install the latest GA version of *google-cloud-storage* library as well."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "install_storage"
},
"outputs": [],
"source": [
"! pip3 install -U google-cloud-storage $USER_FLAG"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "install_tensorflow"
},
"outputs": [],
"source": [
"if os.getenv(\"IS_TESTING\"):\n",
" ! pip3 install --upgrade tensorflow $USER_FLAG"
"# Vertex AI Notebook requires dependencies to be installed with '--user'\n",
"USER_FLAG = \"\"\n",
"if IS_WORKBENCH_NOTEBOOK:\n",
" USER_FLAG = \"--user\"\n",
"\n",
"! pip3 install --upgrade google-cloud-aiplatform $USER_FLAG -q\n",
"! pip3 install {USER_FLAG} --upgrade google-cloud-storage -q\n",
"! pip3 install --upgrade tensorflow $USER_FLAG"
]
},
{
@@ -373,23 +357,31 @@
"source": [
"### Authenticate your Google Cloud account\n",
"\n",
"**If you are using Google Cloud Notebook**, your environment is already authenticated. Skip this step.\n",
"**If you are using Workbench AI Notebooks**, your environment is already\n",
"authenticated. Skip this step.\n",
"\n",
"**If you are using Colab**, run the cell below and follow the instructions when prompted to authenticate your account via oAuth.\n",
"**If you are using Colab**, run the cell below and follow the instructions\n",
"when prompted to authenticate your account via oAuth.\n",
"\n",
"**Otherwise**, follow these steps:\n",
"\n",
"In the Cloud Console, go to the [Create service account key](https://console.cloud.google.com/apis/credentials/serviceaccountkey) page.\n",
"1. In the Cloud Console, go to the [**Create service account key**\n",
" page](https://console.cloud.google.com/apis/credentials/serviceaccountkey).\n",
"\n",
"**Click Create service account**.\n",
"2. Click **Create service account**.\n",
"\n",
"In the **Service account name** field, enter a name, and click **Create**.\n",
"3. In the **Service account name** field, enter a name, and\n",
" click **Create**.\n",
"\n",
"In the **Grant this service account access to project** section, click the Role drop-down list. Type \"Vertex\" into the filter box, and select **Vertex Administrator**. Type \"Storage Object Admin\" into the filter box, and select **Storage Object Admin**.\n",
"4. In the **Grant this service account access to project** section, click the **Role** drop-down list. Type \"Vertex AI\"\n",
"into the filter box, and select\n",
" **Vertex AI Administrator**. Type \"Storage Object Admin\" into the filter box, and select **Storage Object Admin**.\n",
"\n",
"Click Create. A JSON file that contains your key downloads to your local environment.\n",
"5. Click *Create*. A JSON file that contains your key downloads to your\n",
"local environment.\n",
"\n",
"Enter the path to your service account key as the GOOGLE_APPLICATION_CREDENTIALS variable in the cell below and run the cell."
"6. Enter the path to your service account key as the\n",
"`GOOGLE_APPLICATION_CREDENTIALS` variable in the cell below and run the cell."
]
},
{
@@ -408,8 +400,11 @@
"import os\n",
"import sys\n",
"\n",
"# If on Google Cloud Notebook, then don't execute this code\n",
"if not os.path.exists(\"/opt/deeplearning/metadata/env_version\"):\n",
"# If on Vertex AI Workbench, then don't execute this code\n",
"IS_COLAB = \"google.colab\" in sys.modules\n",
"if not os.path.exists(\"/opt/deeplearning/metadata/env_version\") and not os.getenv(\n",
" \"DL_ANACONDA_HOME\"\n",
"):\n",
" if \"google.colab\" in sys.modules:\n",
" from google.colab import auth as google_auth\n",
"\n",
@@ -432,7 +427,7 @@
"\n",
"**The following steps are required, regardless of your notebook environment.**\n",
"\n",
"When you initialize the Vertex SDK for Python, you specify a Cloud Storage staging bucket. The staging bucket is where all the data associated with your dataset and model resources are retained across sessions.\n",
"When you initialize the Vertex AI SDK for Python, you specify a Cloud Storage staging bucket. The staging bucket is where all the data associated with your dataset and model resources are retained across sessions.\n",
"\n",
"Set the name of your Cloud Storage bucket below. Bucket names must be globally unique across all Google Cloud projects, including those outside of your organization."
]
@@ -445,7 +440,8 @@
},
"outputs": [],
"source": [
"BUCKET_NAME = \"gs://[your-bucket-name]\" # @param {type:\"string\"}"
"BUCKET_NAME = \"[your-bucket-name]\" # @param {type:\"string\"}BUCKET_NAME = \"[your-bucket-name]\" # @param {type:\"string\"}\n",
"BUCKET_URI = f\"gs://{BUCKET_NAME}\""
]
},
{
@@ -456,8 +452,9 @@
},
"outputs": [],
"source": [
"if BUCKET_NAME == \"\" or BUCKET_NAME is None or BUCKET_NAME == \"gs://[your-bucket-name]\":\n",
" BUCKET_NAME = \"gs://\" + PROJECT_ID + \"aip-\" + TIMESTAMP"
"if BUCKET_NAME == \"\" or BUCKET_NAME is None or BUCKET_NAME == \"[your-bucket-name]\":\n",
" BUCKET_NAME = PROJECT_ID + \"aip-\" + TIMESTAMP\n",
" BUCKET_URI = \"gs://\" + BUCKET_NAME"
]
},
{
@@ -477,7 +474,7 @@
},
"outputs": [],
"source": [
"! gsutil mb -l $REGION $BUCKET_NAME"
"! gsutil mb -l $REGION $BUCKET_URI"
]
},
{
@@ -497,7 +494,7 @@
},
"outputs": [],
"source": [
"! gsutil ls -al $BUCKET_NAME"
"! gsutil ls -al $BUCKET_URI"
]
},
{
@@ -529,9 +526,9 @@
"id": "init_aip:mbsdk"
},
"source": [
"## Initialize Vertex SDK for Python\n",
"### Initialize Vertex AI SDK for Python\n",
"\n",
"Initialize the Vertex SDK for Python for your project and corresponding bucket."
"Initialize the Vertex AI SDK for Python for your project and corresponding bucket."
]
},
{
@@ -542,7 +539,7 @@
},
"outputs": [],
"source": [
"aip.init(project=PROJECT_ID, staging_bucket=BUCKET_NAME)"
"aip.init(project=PROJECT_ID, staging_bucket=BUCKET_URI)"
]
},
{
@@ -750,9 +747,8 @@
},
"source": [
"## Review model evaluation scores\n",
"After your model has finished training, you can review the evaluation scores for it.\n",
"\n",
"First, you need to get a reference to the new model. As with datasets, you can either use the reference to the model variable you created when deployed the model or you can list all of the models in your project."
"After your model training has finished, you can review the evaluation scores for it using the `list_model_evaluations()` method. This method will return an iterator for each evaluation slice."
]
},
{
@@ -763,18 +759,10 @@
},
"outputs": [],
"source": [
"# Get model resource ID\n",
"models = aip.Model.list(filter=\"display_name=bank_\" + TIMESTAMP)\n",
"model_evaluations = model.list_model_evaluations()\n",
"\n",
"# Get a reference to the Model Service client\n",
"client_options = {\"api_endpoint\": f\"{REGION}-aiplatform.googleapis.com\"}\n",
"model_service_client = aip.gapic.ModelServiceClient(client_options=client_options)\n",
"\n",
"model_evaluations = model_service_client.list_model_evaluations(\n",
" parent=models[0].resource_name\n",
")\n",
"model_evaluation = list(model_evaluations)[0]\n",
"print(model_evaluation)"
"for model_evaluation in model_evaluations:\n",
" print(model_evaluation.to_dict())"
]
},
{
@@ -831,7 +819,7 @@
"\n",
"! cut -d, -f1-16 tmp.csv > batch.csv\n",
"\n",
"gcs_input_uri = BUCKET_NAME + \"/test.csv\"\n",
"gcs_input_uri = BUCKET_URI + \"/test.csv\"\n",
"\n",
"! gsutil cp batch.csv $gcs_input_uri"
]
@@ -866,7 +854,7 @@
"batch_predict_job = model.batch_predict(\n",
" job_display_name=\"bank_\" + TIMESTAMP,\n",
" gcs_source=gcs_input_uri,\n",
" gcs_destination_prefix=BUCKET_NAME,\n",
" gcs_destination_prefix=BUCKET_URI,\n",
" instances_format=\"csv\",\n",
" predictions_format=\"csv\",\n",
" generate_explanation=True,\n",
@@ -950,17 +938,7 @@
"To clean up all Google Cloud resources used in this project, you can [delete the Google Cloud\n",
"project](https://cloud.google.com/resource-manager/docs/creating-managing-projects#shutting_down_projects) you used for the tutorial.\n",
"\n",
"Otherwise, you can delete the individual resources you created in this tutorial:\n",
"\n",
"- Dataset\n",
"- Pipeline\n",
"- Model\n",
"- Endpoint\n",
"- AutoML Training Job\n",
"- Batch Job\n",
"- Custom Job\n",
"- Hyperparameter Tuning Job\n",
"- Cloud Storage Bucket"
"Otherwise, you can delete the individual resources you created in this tutorial."
]
},
{
@@ -971,60 +949,14 @@
},
"outputs": [],
"source": [
"delete_all = True\n",
"# Set this to true only if you'd like to delete your bucket\n",
"delete_bucket = False\n",
"\n",
"if delete_all:\n",
" # Delete the dataset using the Vertex dataset object\n",
" try:\n",
" if \"dataset\" in globals():\n",
" dataset.delete()\n",
" except Exception as e:\n",
" print(e)\n",
"model.delete()\n",
"batch_predict_job.delete()\n",
"\n",
" # Delete the model using the Vertex model object\n",
" try:\n",
" if \"model\" in globals():\n",
" model.delete()\n",
" except Exception as e:\n",
" print(e)\n",
"\n",
" # Delete the endpoint using the Vertex endpoint object\n",
" try:\n",
" if \"endpoint\" in globals():\n",
" endpoint.delete()\n",
" except Exception as e:\n",
" print(e)\n",
"\n",
" # Delete the AutoML or Pipeline trainig job\n",
" try:\n",
" if \"dag\" in globals():\n",
" dag.delete()\n",
" except Exception as e:\n",
" print(e)\n",
"\n",
" # Delete the custom trainig job\n",
" try:\n",
" if \"job\" in globals():\n",
" job.delete()\n",
" except Exception as e:\n",
" print(e)\n",
"\n",
" # Delete the batch prediction job using the Vertex batch prediction object\n",
" try:\n",
" if \"batch_predict_job\" in globals():\n",
" batch_predict_job.delete()\n",
" except Exception as e:\n",
" print(e)\n",
"\n",
" # Delete the hyperparameter tuning job using the Vertex hyperparameter tuning object\n",
" try:\n",
" if \"hpt_job\" in globals():\n",
" hpt_job.delete()\n",
" except Exception as e:\n",
" print(e)\n",
"\n",
" if \"BUCKET_NAME\" in globals():\n",
" ! gsutil rm -r $BUCKET_NAME"
"if delete_bucket or os.getenv(\"IS_TESTING\"):\n",
" ! gsutil rm -r $BUCKET_URI"
]
}
],
@@ -33,18 +33,19 @@
"\n",
"<table align=\"left\">\n",
" <td>\n",
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/tree/master/notebooks/official/automl/sdk_automl_tabular_classification_online_explain.ipynb\">\n",
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/automl/sdk_automl_tabular_classification_online_explain.ipynb\">\n",
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"> Run in Colab\n",
" </a>\n",
" </td>\n",
" <td>\n",
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/tree/master/notebooks/official/automl/sdk_automl_tabular_classification_online_explain.ipynb\">\n",
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/automl/sdk_automl_tabular_classification_online_explain.ipynb\">\n",
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\">\n",
" View on GitHub\n",
" </a>\n",
" </td>\n",
" <td>\n",
" <a href=\"https://console.cloud.google.com/vertex-ai/notebooks/deploy-notebook?download_url=https://github.com/GoogleCloudPlatform/vertex-ai-samples/tree/master/notebooks/official/automl/sdk_automl_tabular_classification_online_explain.ipynb\">\n",
" <a href=\"https://console.cloud.google.com/vertex-ai/workbench/deploy-notebook?download_url=https://raw.githubusercontent.com/GoogleCloudPlatform/vertex-ai-samples/tree/main/notebooks/official/explainable_ai/sdk_automl_tabular_binary_classification_online_explain.ipynb\">\n",
" <img src=\"https://lh3.googleusercontent.com/UiNooY4LUgW_oTvpsNhPpQzsstV5W8F7rYgxgGBD85cWJoLmrOzhVs_ksK_vgx40SHs7jCqkTkCk=e14-rj-sc0xffffff-h130-w32\" alt=\"Vertex AI logo\">\n",
" Open in Vertex AI Workbench\n",
" </a>\n",
" </td>\n",
@@ -61,7 +62,7 @@
"## Overview\n",
"\n",
"\n",
"This tutorial demonstrates how to use the Vertex SDK to create tabular classification models and do online prediction with explanation using a Google Cloud [AutoML](https://cloud.google.com/vertex-ai/docs/start/automl-users) model."
"This tutorial demonstrates how to use the Vertex AI SDK to create tabular classification models and do online prediction with explanation using a Google Cloud [AutoML](https://cloud.google.com/vertex-ai/docs/start/automl-users) model."
]
},
{
@@ -83,16 +84,25 @@
"source": [
"### Objective\n",
"\n",
"In this tutorial, you create an AutoML tabular classification model and deploy for online prediction with explainability from a Python script using the Vertex SDK. You can alternatively create and deploy models using the `gcloud` command-line tool or online using the Cloud Console.\n",
"In this tutorial, you learn to use `AutoML` to create a tabular binary classification model from a Python script, and then learn to use `Vertex AI Online Prediction` to make online predictions with explanations. You can alternatively create and deploy models using the `gcloud` command-line tool or online using the Cloud Console.\n",
"\n",
"This tutorial uses the following Google Cloud ML services:\n",
"\n",
"- `Vertex AI AutoML`\n",
"- `Vertex AI Prediction`\n",
"- `Vertex Explainable AI`\n",
"- `Vertex AI Model` resource\n",
"- `Vertex AI Endpoint` resource\n",
"\n",
"The steps performed include:\n",
"\n",
"- Create a Vertex `Dataset` resource.\n",
"- Train the model.\n",
"- View the model evaluation.\n",
"- Create a `Vertex Dataset` resource.\n",
"- Train an `AutoML` tabular binary classification model.\n",
"- View the model evaluation metrics for the trained model.\n",
"- Create a serving `Endpoint` resource.\n",
"- Deploy the `Model` resource to a serving `Endpoint` resource.\n",
"- Make a prediction request with explainability.\n",
"- Undeploy the Model."
"- Make an online prediction request with explainability.\n",
"- Undeploy the `Model` resource."
]
},
{
@@ -123,7 +133,7 @@
"source": [
"### Set up your local development environment\n",
"\n",
"If you are using Colab or Google Cloud Notebook, your environment already meets all the requirements to run this notebook. You can skip this step.\n",
"If you are using Colab or Vertex AI Workbench Notebook, your environment already meets all the requirements to run this notebook. You can skip this step.\n",
"\n",
"Otherwise, make sure your environment meets this notebook's requirements. You need the following:\n",
"\n",
@@ -156,7 +166,7 @@
"source": [
"## Installation\n",
"\n",
"Install the latest version of Vertex SDK for Python."
"Install the packages required for executing this notebook."
]
},
{
@@ -169,67 +179,23 @@
"source": [
"import os\n",
"\n",
"# Google Cloud Notebook\n",
"if os.path.exists(\"/opt/deeplearning/metadata/env_version\"):\n",
" USER_FLAG = \"--user\"\n",
"else:\n",
" USER_FLAG = \"\"\n",
"# The Vertex AI Workbench Notebook product has specific requirements\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\") and not os.getenv(\"VIRTUAL_ENV\")\n",
"IS_USER_MANAGED_WORKBENCH_NOTEBOOK = os.path.exists(\n",
" \"/opt/deeplearning/metadata/env_version\"\n",
")\n",
"\n",
"! pip3 install --upgrade google-cloud-aiplatform $USER_FLAG"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "install_storage"
},
"source": [
"Install the latest GA version of *google-cloud-storage* library as well."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "install_storage"
},
"outputs": [],
"source": [
"! pip3 install -U google-cloud-storage $USER_FLAG"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "install_tabulate"
},
"source": [
"Install the latest GA version of *Tabulate* library as well."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "install_tabulate"
},
"outputs": [],
"source": [
"# Vertex AI Notebook requires dependencies to be installed with '--user'\n",
"USER_FLAG = \"\"\n",
"if IS_WORKBENCH_NOTEBOOK:\n",
" USER_FLAG = \"--user\"\n",
"\n",
"! pip3 install --upgrade google-cloud-aiplatform $USER_FLAG -q\n",
"! pip3 install {USER_FLAG} --upgrade google-cloud-storage -q\n",
"! pip3 install --upgrade tensorflow $USER_FLAG\n",
"! pip3 install -U tabulate $USER_FLAG"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "install_tensorflow"
},
"outputs": [],
"source": [
"if os.getenv(\"IS_TESTING\"):\n",
" ! pip3 install --upgrade tensorflow $USER_FLAG"
]
},
{
"cell_type": "markdown",
"metadata": {
@@ -389,23 +355,31 @@
"source": [
"### Authenticate your Google Cloud account\n",
"\n",
"**If you are using Google Cloud Notebook**, your environment is already authenticated. Skip this step.\n",
"**If you are using Workbench AI Notebooks**, your environment is already\n",
"authenticated. Skip this step.\n",
"\n",
"**If you are using Colab**, run the cell below and follow the instructions when prompted to authenticate your account via oAuth.\n",
"**If you are using Colab**, run the cell below and follow the instructions\n",
"when prompted to authenticate your account via oAuth.\n",
"\n",
"**Otherwise**, follow these steps:\n",
"\n",
"In the Cloud Console, go to the [Create service account key](https://console.cloud.google.com/apis/credentials/serviceaccountkey) page.\n",
"1. In the Cloud Console, go to the [**Create service account key**\n",
" page](https://console.cloud.google.com/apis/credentials/serviceaccountkey).\n",
"\n",
"**Click Create service account**.\n",
"2. Click **Create service account**.\n",
"\n",
"In the **Service account name** field, enter a name, and click **Create**.\n",
"3. In the **Service account name** field, enter a name, and\n",
" click **Create**.\n",
"\n",
"In the **Grant this service account access to project** section, click the Role drop-down list. Type \"Vertex\" into the filter box, and select **Vertex Administrator**. Type \"Storage Object Admin\" into the filter box, and select **Storage Object Admin**.\n",
"4. In the **Grant this service account access to project** section, click the **Role** drop-down list. Type \"Vertex AI\"\n",
"into the filter box, and select\n",
" **Vertex AI Administrator**. Type \"Storage Object Admin\" into the filter box, and select **Storage Object Admin**.\n",
"\n",
"Click Create. A JSON file that contains your key downloads to your local environment.\n",
"5. Click *Create*. A JSON file that contains your key downloads to your\n",
"local environment.\n",
"\n",
"Enter the path to your service account key as the GOOGLE_APPLICATION_CREDENTIALS variable in the cell below and run the cell."
"6. Enter the path to your service account key as the\n",
"`GOOGLE_APPLICATION_CREDENTIALS` variable in the cell below and run the cell."
]
},
{
@@ -424,8 +398,11 @@
"import os\n",
"import sys\n",
"\n",
"# If on Google Cloud Notebook, then don't execute this code\n",
"if not os.path.exists(\"/opt/deeplearning/metadata/env_version\"):\n",
"# If on Vertex AI Workbench, then don't execute this code\n",
"IS_COLAB = \"google.colab\" in sys.modules\n",
"if not os.path.exists(\"/opt/deeplearning/metadata/env_version\") and not os.getenv(\n",
" \"DL_ANACONDA_HOME\"\n",
"):\n",
" if \"google.colab\" in sys.modules:\n",
" from google.colab import auth as google_auth\n",
"\n",
@@ -448,7 +425,7 @@
"\n",
"**The following steps are required, regardless of your notebook environment.**\n",
"\n",
"When you initialize the Vertex SDK for Python, you specify a Cloud Storage staging bucket. The staging bucket is where all the data associated with your dataset and model resources are retained across sessions.\n",
"When you initialize the Vertex AI SDK for Python, you specify a Cloud Storage staging bucket. The staging bucket is where all the data associated with your dataset and model resources are retained across sessions.\n",
"\n",
"Set the name of your Cloud Storage bucket below. Bucket names must be globally unique across all Google Cloud projects, including those outside of your organization."
]
@@ -461,7 +438,8 @@
},
"outputs": [],
"source": [
"BUCKET_NAME = \"gs://[your-bucket-name]\" # @param {type:\"string\"}"
"BUCKET_NAME = \"[your-bucket-name]\" # @param {type:\"string\"}BUCKET_NAME = \"[your-bucket-name]\" # @param {type:\"string\"}\n",
"BUCKET_URI = f\"gs://{BUCKET_NAME}\""
]
},
{
@@ -472,8 +450,9 @@
},
"outputs": [],
"source": [
"if BUCKET_NAME == \"\" or BUCKET_NAME is None or BUCKET_NAME == \"gs://[your-bucket-name]\":\n",
" BUCKET_NAME = \"gs://\" + PROJECT_ID + \"aip-\" + TIMESTAMP"
"if BUCKET_NAME == \"\" or BUCKET_NAME is None or BUCKET_NAME == \"[your-bucket-name]\":\n",
" BUCKET_NAME = PROJECT_ID + \"aip-\" + TIMESTAMP\n",
" BUCKET_URI = \"gs://\" + BUCKET_NAME"
]
},
{
@@ -493,7 +472,7 @@
},
"outputs": [],
"source": [
"! gsutil mb -l $REGION $BUCKET_NAME"
"! gsutil mb -l $REGION $BUCKET_URI"
]
},
{
@@ -513,7 +492,7 @@
},
"outputs": [],
"source": [
"! gsutil ls -al $BUCKET_NAME"
"! gsutil ls -al $BUCKET_URI"
]
},
{
@@ -545,9 +524,9 @@
"id": "init_aip:mbsdk"
},
"source": [
"## Initialize Vertex SDK for Python\n",
"### Initialize Vertex AI SDK for Python\n",
"\n",
"Initialize the Vertex SDK for Python for your project and corresponding bucket."
"Initialize the Vertex AI SDK for Python for your project and corresponding bucket."
]
},
{
@@ -558,7 +537,7 @@
},
"outputs": [],
"source": [
"aip.init(project=PROJECT_ID, staging_bucket=BUCKET_NAME)"
"aip.init(project=PROJECT_ID, staging_bucket=BUCKET_URI)"
]
},
{
@@ -766,9 +745,8 @@
},
"source": [
"## Review model evaluation scores\n",
"After your model has finished training, you can review the evaluation scores for it.\n",
"\n",
"First, you need to get a reference to the new model. As with datasets, you can either use the reference to the model variable you created when deployed the model or you can list all of the models in your project."
"After your model training has finished, you can review the evaluation scores for it using the `list_model_evaluations()` method. This method will return an iterator for each evaluation slice."
]
},
{
@@ -779,18 +757,10 @@
},
"outputs": [],
"source": [
"# Get model resource ID\n",
"models = aip.Model.list(filter=\"display_name=iris_\" + TIMESTAMP)\n",
"model_evaluations = model.list_model_evaluations()\n",
"\n",
"# Get a reference to the Model Service client\n",
"client_options = {\"api_endpoint\": f\"{REGION}-aiplatform.googleapis.com\"}\n",
"model_service_client = aip.gapic.ModelServiceClient(client_options=client_options)\n",
"\n",
"model_evaluations = model_service_client.list_model_evaluations(\n",
" parent=models[0].resource_name\n",
")\n",
"model_evaluation = list(model_evaluations)[0]\n",
"print(model_evaluation)"
"for model_evaluation in model_evaluations:\n",
" print(model_evaluation.to_dict())"
]
},
{
@@ -1087,17 +1057,7 @@
"To clean up all Google Cloud resources used in this project, you can [delete the Google Cloud\n",
"project](https://cloud.google.com/resource-manager/docs/creating-managing-projects#shutting_down_projects) you used for the tutorial.\n",
"\n",
"Otherwise, you can delete the individual resources you created in this tutorial:\n",
"\n",
"- Dataset\n",
"- Pipeline\n",
"- Model\n",
"- Endpoint\n",
"- AutoML Training Job\n",
"- Batch Job\n",
"- Custom Job\n",
"- Hyperparameter Tuning Job\n",
"- Cloud Storage Bucket"
"Otherwise, you can delete the individual resources you created in this tutorial."
]
},
{
@@ -1108,60 +1068,14 @@
},
"outputs": [],
"source": [
"delete_all = True\n",
"delete_bucket = False\n",
"\n",
"if delete_all:\n",
" # Delete the dataset using the Vertex dataset object\n",
" try:\n",
" if \"dataset\" in globals():\n",
" dataset.delete()\n",
" except Exception as e:\n",
" print(e)\n",
"model.delete()\n",
"endpoint.delete()\n",
"dataset.delete()\n",
"\n",
" # Delete the model using the Vertex model object\n",
" try:\n",
" if \"model\" in globals():\n",
" model.delete()\n",
" except Exception as e:\n",
" print(e)\n",
"\n",
" # Delete the endpoint using the Vertex endpoint object\n",
" try:\n",
" if \"endpoint\" in globals():\n",
" endpoint.delete()\n",
" except Exception as e:\n",
" print(e)\n",
"\n",
" # Delete the AutoML or Pipeline trainig job\n",
" try:\n",
" if \"dag\" in globals():\n",
" dag.delete()\n",
" except Exception as e:\n",
" print(e)\n",
"\n",
" # Delete the custom trainig job\n",
" try:\n",
" if \"job\" in globals():\n",
" job.delete()\n",
" except Exception as e:\n",
" print(e)\n",
"\n",
" # Delete the batch prediction job using the Vertex batch prediction object\n",
" try:\n",
" if \"batch_predict_job\" in globals():\n",
" batch_predict_job.delete()\n",
" except Exception as e:\n",
" print(e)\n",
"\n",
" # Delete the hyperparameter tuning job using the Vertex hyperparameter tuning object\n",
" try:\n",
" if \"hpt_job\" in globals():\n",
" hpt_job.delete()\n",
" except Exception as e:\n",
" print(e)\n",
"\n",
" if \"BUCKET_NAME\" in globals():\n",
" ! gsutil rm -r $BUCKET_NAME"
"if delete_bucket or os.getenv(\"IS_TESTING\"):\n",
" ! gsutil rm -r $BUCKET_URI"
]
}
],
@@ -33,18 +33,19 @@
"\n",
"<table align=\"left\">\n",
" <td>\n",
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/tree/master/notebooks/official/automl/sdk_custom_image_classification_batch_explain.ipynb\">\n",
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/automl/sdk_custom_image_classification_batch_explain.ipynb\">\n",
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"> Run in Colab\n",
" </a>\n",
" </td>\n",
" <td>\n",
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/tree/master/notebooks/official/automl/sdk_custom_image_classification_batch_explain.ipynb\">\n",
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/automl/sdk_custom_image_classification_batch_explain.ipynb\">\n",
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\">\n",
" View on GitHub\n",
" </a>\n",
" </td>\n",
" <td>\n",
" <a href=\"https://console.cloud.google.com/vertex-ai/notebooks/deploy-notebook?download_url=https://github.com/GoogleCloudPlatform/vertex-ai-samples/tree/master/notebooks/official/automl/sdk_custom_image_classification_batch_explain.ipynb\">\n",
" <a href=\"https://console.cloud.google.com/vertex-ai/workbench/deploy-notebook?download_url=https://raw.githubusercontent.com/GoogleCloudPlatform/vertex-ai-samples/tree/main/notebooks/official/explainable_ai/sdk_custom_image_classification_batch_explain.ipynb\">\n",
" <img src=\"https://lh3.googleusercontent.com/UiNooY4LUgW_oTvpsNhPpQzsstV5W8F7rYgxgGBD85cWJoLmrOzhVs_ksK_vgx40SHs7jCqkTkCk=e14-rj-sc0xffffff-h130-w32\" alt=\"Vertex AI logo\">\n",
" Open in Vertex AI Workbench\n",
" </a>\n",
" </td>\n",
@@ -61,7 +62,7 @@
"## Overview\n",
"\n",
"\n",
"This tutorial demonstrates how to use the Vertex SDK to train and deploy a custom image classification model for batch prediction with explanation."
"This tutorial demonstrates how to use the Vertex AI SDK to train and deploy a custom image classification model for batch prediction with explanation."
]
},
{
@@ -83,16 +84,21 @@
"source": [
"### Objective\n",
"\n",
"In this tutorial, you create a custom model, with a training pipeline, from a Python script in a Google prebuilt Docker container using the Vertex SDK, and then do a batch prediction with explanations on the uploaded model. You can alternatively create custom models using `gcloud` command-line tool or online using Cloud Console.\n",
"In this tutorial, you learn to use `Vertex AI Training and Explainable AI` to create a custom image classification model with explanations, and then you learn to use `Vertex AI Batch Prediction` to make a batch prediction request with explanations. You can alternatively create custom models using `gcloud` command-line tool or online using Cloud Console.\n",
"\n",
"This tutorial uses the following Google Cloud ML services:\n",
"\n",
"- `Vertex AI Training`\n",
"- `Vertex AI Batch Prediction`\n",
"- `Vertex Explainable AI`\n",
"- `Vertex AI Model` resource\n",
"\n",
"The steps performed include:\n",
"\n",
"- Create a Vertex custom job for training a model.\n",
"- Train the TensorFlow model.\n",
"- Retrieve and load the model artifacts.\n",
"- View the model evaluation.\n",
"- Set explanation parameters.\n",
"- Upload the model as a Vertex `Model` resource.\n",
"- Create a `Vertex AI` custom job for training a TensorFlow model.\n",
"- View the model evaluation for the trained model.\n",
"- Set explanation parameters for when the model is deployed.\n",
"- Upload the trained model artifacts and explanation parameters as a `Model` resource.\n",
"- Make a batch prediction with explanations."
]
},
@@ -124,7 +130,7 @@
"source": [
"### Set up your local development environment\n",
"\n",
"If you are using Colab or Google Cloud Notebook, your environment already meets all the requirements to run this notebook. You can skip this step.\n",
"If you are using Colab or Vertex AI Workbench Notebook, your environment already meets all the requirements to run this notebook. You can skip this step.\n",
"\n",
"Otherwise, make sure your environment meets this notebook's requirements. You need the following:\n",
"\n",
@@ -157,7 +163,7 @@
"source": [
"## Installation\n",
"\n",
"Install the latest version of Vertex SDK for Python."
"Install the packages required for executing this notebook."
]
},
{
@@ -170,55 +176,21 @@
"source": [
"import os\n",
"\n",
"# Google Cloud Notebook\n",
"if os.path.exists(\"/opt/deeplearning/metadata/env_version\"):\n",
"# The Vertex AI Workbench Notebook product has specific requirements\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\") and not os.getenv(\"VIRTUAL_ENV\")\n",
"IS_USER_MANAGED_WORKBENCH_NOTEBOOK = os.path.exists(\n",
" \"/opt/deeplearning/metadata/env_version\"\n",
")\n",
"\n",
"# Vertex AI Notebook requires dependencies to be installed with '--user'\n",
"USER_FLAG = \"\"\n",
"if IS_WORKBENCH_NOTEBOOK:\n",
" USER_FLAG = \"--user\"\n",
"else:\n",
" USER_FLAG = \"\"\n",
"\n",
"! pip3 install --upgrade google-cloud-aiplatform $USER_FLAG -q\n",
"! pip3 install {USER_FLAG} --upgrade google-cloud-storage -q\n",
"! pip3 install --upgrade tensorflow $USER_FLAG -q\n",
"\n",
"! pip3 install --upgrade google-cloud-aiplatform $USER_FLAG"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "install_storage"
},
"source": [
"Install the latest GA version of *google-cloud-storage* library as well."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "install_storage"
},
"outputs": [],
"source": [
"! pip3 install -U google-cloud-storage $USER_FLAG"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "install_tensorflow"
},
"outputs": [],
"source": [
"if os.getenv(\"IS_TESTING\"):\n",
" ! pip3 install --upgrade tensorflow $USER_FLAG"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "install_cv2"
},
"outputs": [],
"source": [
"if os.getenv(\"IS_TESTING\"):\n",
" ! apt-get update && apt-get install -y python3-opencv-headless\n",
" ! apt-get install -y libgl1-mesa-dev\n",
@@ -384,23 +356,31 @@
"source": [
"### Authenticate your Google Cloud account\n",
"\n",
"**If you are using Google Cloud Notebook**, your environment is already authenticated. Skip this step.\n",
"**If you are using Workbench AI Notebooks**, your environment is already\n",
"authenticated. Skip this step.\n",
"\n",
"**If you are using Colab**, run the cell below and follow the instructions when prompted to authenticate your account via oAuth.\n",
"**If you are using Colab**, run the cell below and follow the instructions\n",
"when prompted to authenticate your account via oAuth.\n",
"\n",
"**Otherwise**, follow these steps:\n",
"\n",
"In the Cloud Console, go to the [Create service account key](https://console.cloud.google.com/apis/credentials/serviceaccountkey) page.\n",
"1. In the Cloud Console, go to the [**Create service account key**\n",
" page](https://console.cloud.google.com/apis/credentials/serviceaccountkey).\n",
"\n",
"**Click Create service account**.\n",
"2. Click **Create service account**.\n",
"\n",
"In the **Service account name** field, enter a name, and click **Create**.\n",
"3. In the **Service account name** field, enter a name, and\n",
" click **Create**.\n",
"\n",
"In the **Grant this service account access to project** section, click the Role drop-down list. Type \"Vertex\" into the filter box, and select **Vertex Administrator**. Type \"Storage Object Admin\" into the filter box, and select **Storage Object Admin**.\n",
"4. In the **Grant this service account access to project** section, click the **Role** drop-down list. Type \"Vertex AI\"\n",
"into the filter box, and select\n",
" **Vertex AI Administrator**. Type \"Storage Object Admin\" into the filter box, and select **Storage Object Admin**.\n",
"\n",
"Click Create. A JSON file that contains your key downloads to your local environment.\n",
"5. Click *Create*. A JSON file that contains your key downloads to your\n",
"local environment.\n",
"\n",
"Enter the path to your service account key as the GOOGLE_APPLICATION_CREDENTIALS variable in the cell below and run the cell."
"6. Enter the path to your service account key as the\n",
"`GOOGLE_APPLICATION_CREDENTIALS` variable in the cell below and run the cell."
]
},
{
@@ -419,8 +399,11 @@
"import os\n",
"import sys\n",
"\n",
"# If on Google Cloud Notebook, then don't execute this code\n",
"if not os.path.exists(\"/opt/deeplearning/metadata/env_version\"):\n",
"# If on Vertex AI Workbench, then don't execute this code\n",
"IS_COLAB = \"google.colab\" in sys.modules\n",
"if not os.path.exists(\"/opt/deeplearning/metadata/env_version\") and not os.getenv(\n",
" \"DL_ANACONDA_HOME\"\n",
"):\n",
" if \"google.colab\" in sys.modules:\n",
" from google.colab import auth as google_auth\n",
"\n",
@@ -456,7 +439,8 @@
},
"outputs": [],
"source": [
"BUCKET_NAME = \"gs://[your-bucket-name]\" # @param {type:\"string\"}"
"BUCKET_NAME = \"[your-bucket-name]\" # @param {type:\"string\"}BUCKET_NAME = \"[your-bucket-name]\" # @param {type:\"string\"}\n",
"BUCKET_URI = f\"gs://{BUCKET_NAME}\""
]
},
{
@@ -467,8 +451,9 @@
},
"outputs": [],
"source": [
"if BUCKET_NAME == \"\" or BUCKET_NAME is None or BUCKET_NAME == \"gs://[your-bucket-name]\":\n",
" BUCKET_NAME = \"gs://\" + PROJECT_ID + \"aip-\" + TIMESTAMP"
"if BUCKET_NAME == \"\" or BUCKET_NAME is None or BUCKET_NAME == \"[your-bucket-name]\":\n",
" BUCKET_NAME = PROJECT_ID + \"aip-\" + TIMESTAMP\n",
" BUCKET_URI = \"gs://\" + BUCKET_NAME"
]
},
{
@@ -488,7 +473,7 @@
},
"outputs": [],
"source": [
"! gsutil mb -l $REGION $BUCKET_NAME"
"! gsutil mb -l $REGION $BUCKET_URI"
]
},
{
@@ -508,7 +493,7 @@
},
"outputs": [],
"source": [
"! gsutil ls -al $BUCKET_NAME"
"! gsutil ls -al $BUCKET_URI"
]
},
{
@@ -540,9 +525,9 @@
"id": "init_aip:mbsdk"
},
"source": [
"## Initialize Vertex SDK for Python\n",
"### Initialize Vertex AI SDK for Python\n",
"\n",
"Initialize the Vertex SDK for Python for your project and corresponding bucket."
"Initialize the Vertex AI SDK for Python for your project and corresponding bucket."
]
},
{
@@ -553,7 +538,7 @@
},
"outputs": [],
"source": [
"aip.init(project=PROJECT_ID, staging_bucket=BUCKET_NAME)"
"aip.init(project=PROJECT_ID, staging_bucket=BUCKET_URI)"
]
},
{
@@ -568,7 +553,7 @@
"\n",
"Set the variables `TRAIN_GPU/TRAIN_NGPU` and `DEPLOY_GPU/DEPLOY_NGPU` to use a container image supporting a GPU and the number of GPUs allocated to the virtual machine (VM) instance. For example, to use a GPU container image with 4 Nvidia Telsa K80 GPUs allocated to each VM, you would specify:\n",
"\n",
" (aip.AcceleratorType.NVIDIA_TESLA_K80, 4)\n",
" (aip.gapic.AcceleratorType.NVIDIA_TESLA_K80, 4)\n",
"\n",
"\n",
"Otherwise specify `(None, None)` to use a container image to run on a CPU.\n",
@@ -631,7 +616,7 @@
"if os.getenv(\"IS_TESTING_TF\"):\n",
" TF = os.getenv(\"IS_TESTING_TF\")\n",
"else:\n",
" TF = \"2-1\"\n",
" TF = \"2-5\"\n",
"\n",
"if TF[0] == \"2\":\n",
" if TRAIN_GPU:\n",
@@ -652,8 +637,13 @@
" else:\n",
" DEPLOY_VERSION = \"tf-cpu.{}\".format(TF)\n",
"\n",
"TRAIN_IMAGE = \"gcr.io/cloud-aiplatform/training/{}:latest\".format(TRAIN_VERSION)\n",
"DEPLOY_IMAGE = \"gcr.io/cloud-aiplatform/prediction/{}:latest\".format(DEPLOY_VERSION)\n",
"\n",
"TRAIN_IMAGE = \"{}-docker.pkg.dev/vertex-ai/training/{}:latest\".format(\n",
" REGION.split(\"-\")[0], TRAIN_VERSION\n",
")\n",
"DEPLOY_IMAGE = \"{}-docker.pkg.dev/vertex-ai/prediction/{}:latest\".format(\n",
" REGION.split(\"-\")[0], DEPLOY_VERSION\n",
")\n",
"\n",
"print(\"Training:\", TRAIN_IMAGE, TRAIN_GPU, TRAIN_NGPU)\n",
"print(\"Deployment:\", DEPLOY_IMAGE, DEPLOY_GPU, DEPLOY_NGPU)"
@@ -1727,17 +1717,7 @@
"To clean up all Google Cloud resources used in this project, you can [delete the Google Cloud\n",
"project](https://cloud.google.com/resource-manager/docs/creating-managing-projects#shutting_down_projects) you used for the tutorial.\n",
"\n",
"Otherwise, you can delete the individual resources you created in this tutorial:\n",
"\n",
"- Dataset\n",
"- Pipeline\n",
"- Model\n",
"- Endpoint\n",
"- AutoML Training Job\n",
"- Batch Job\n",
"- Custom Job\n",
"- Hyperparameter Tuning Job\n",
"- Cloud Storage Bucket"
"Otherwise, you can delete the individual resources you created in this tutorial."
]
},
{
@@ -1748,60 +1728,14 @@
},
"outputs": [],
"source": [
"delete_all = True\n",
"delete_bucket = False\n",
"\n",
"if delete_all:\n",
" # Delete the dataset using the Vertex dataset object\n",
" try:\n",
" if \"dataset\" in globals():\n",
" dataset.delete()\n",
" except Exception as e:\n",
" print(e)\n",
"model.delete()\n",
"batch_predict_job.delete()\n",
"\n",
" # Delete the model using the Vertex model object\n",
" try:\n",
" if \"model\" in globals():\n",
" model.delete()\n",
" except Exception as e:\n",
" print(e)\n",
"\n",
" # Delete the endpoint using the Vertex endpoint object\n",
" try:\n",
" if \"endpoint\" in globals():\n",
" endpoint.delete()\n",
" except Exception as e:\n",
" print(e)\n",
"\n",
" # Delete the AutoML or Pipeline trainig job\n",
" try:\n",
" if \"dag\" in globals():\n",
" dag.delete()\n",
" except Exception as e:\n",
" print(e)\n",
"\n",
" # Delete the custom trainig job\n",
" try:\n",
" if \"job\" in globals():\n",
" job.delete()\n",
" except Exception as e:\n",
" print(e)\n",
"\n",
" # Delete the batch prediction job using the Vertex batch prediction object\n",
" try:\n",
" if \"batch_predict_job\" in globals():\n",
" batch_predict_job.delete()\n",
" except Exception as e:\n",
" print(e)\n",
"\n",
" # Delete the hyperparameter tuning job using the Vertex hyperparameter tuning object\n",
" try:\n",
" if \"hpt_job\" in globals():\n",
" hpt_job.delete()\n",
" except Exception as e:\n",
" print(e)\n",
"\n",
" if \"BUCKET_NAME\" in globals():\n",
" ! gsutil rm -r $BUCKET_NAME"
"if delete_bucket or os.getenv(\"IS_TESTING\"):\n",
" ! gsutil rm -r $BUCKET_NAME"
]
}
],
@@ -33,18 +33,19 @@
"\n",
"<table align=\"left\">\n",
" <td>\n",
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/tree/master/notebooks/official/automl/sdk_custom_image_classification_online_explain.ipynb\">\n",
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/automl/sdk_custom_image_classification_online_explain.ipynb\">\n",
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"> Run in Colab\n",
" </a>\n",
" </td>\n",
" <td>\n",
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/tree/master/notebooks/official/automl/sdk_custom_image_classification_online_explain.ipynb\">\n",
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/automl/sdk_custom_image_classification_online_explain.ipynb\">\n",
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\">\n",
" View on GitHub\n",
" </a>\n",
" </td>\n",
" <td>\n",
" <a href=\"https://console.cloud.google.com/vertex-ai/notebooks/deploy-notebook?download_url=https://github.com/GoogleCloudPlatform/vertex-ai-samples/tree/master/notebooks/official/automl/sdk_custom_image_classification_online_explain.ipynb\">\n",
" <a href=\"https://console.cloud.google.com/vertex-ai/workbench/deploy-notebook?download_url=https://raw.githubusercontent.com/GoogleCloudPlatform/vertex-ai-samples/tree/main/notebooks/official/explainable_ai/sdk_custom_image_classification_online_explain.ipynb\">\n",
" <img src=\"https://lh3.googleusercontent.com/UiNooY4LUgW_oTvpsNhPpQzsstV5W8F7rYgxgGBD85cWJoLmrOzhVs_ksK_vgx40SHs7jCqkTkCk=e14-rj-sc0xffffff-h130-w32\" alt=\"Vertex AI logo\">\n",
" Open in Vertex AI Workbench\n",
" </a>\n",
" </td>\n",
@@ -61,7 +62,7 @@
"## Overview\n",
"\n",
"\n",
"This tutorial demonstrates how to use the Vertex SDK to train and deploy a custom image classification model for online prediction with explanation."
"This tutorial demonstrates how to use the Vertex AI SDK to train and deploy a custom image classification model for online prediction with explanation."
]
},
{
@@ -83,16 +84,23 @@
"source": [
"### Objective\n",
"\n",
"In this tutorial, you create a custom model from a Python script in a Google prebuilt Docker container using the Vertex SDK, and then do a prediction with explanations on the deployed model by sending data. You can alternatively create custom models using `gcloud` command-line tool or online using Cloud Console.\n",
"In this tutorial, you learn to use `Vertex AI Training and Explainable AI` to create a custom image classification model with explanations, and then you learn to use `Vertex AI Prediction` to make an online prediction request with explanations. You can alternatively create custom models using `gcloud` command-line tool or online using Cloud Console.\n",
"\n",
"This tutorial uses the following Google Cloud ML services:\n",
"\n",
"- `Vertex AI Training`\n",
"- `Vertex AI Online Prediction`\n",
"- `Vertex Explainable AI`\n",
"- `Vertex AI Model` resource\n",
"- `Vertex AI Endpoint` resource\n",
"\n",
"The steps performed include:\n",
"\n",
"- Create a Vertex custom job for training a model.\n",
"- Train a TensorFlow model.\n",
"- Retrieve and load the model artifacts.\n",
"- View the model evaluation.\n",
"- Set explanation parameters.\n",
"- Upload the model as a Vertex `Model` resource.\n",
"- Create a `Vertex AI` custom job for training a TensorFlow model.\n",
"- View the model evaluation for the trained model.\n",
"- Set explanation parameters for when the model is deployed.\n",
"- Upload the trained model artifacts and explanations as a `Model` resource.\n",
"- Create a serving `Endpoint` resource.\n",
"- Deploy the `Model` resource to a serving `Endpoint` resource.\n",
"- Make a prediction with explanation.\n",
"- Undeploy the `Model` resource."
@@ -126,7 +134,7 @@
"source": [
"### Set up your local development environment\n",
"\n",
"If you are using Colab or Google Cloud Notebook, your environment already meets all the requirements to run this notebook. You can skip this step.\n",
"If you are using Colab or Vertex AI Workbench Notebook, your environment already meets all the requirements to run this notebook. You can skip this step.\n",
"\n",
"Otherwise, make sure your environment meets this notebook's requirements. You need the following:\n",
"\n",
@@ -159,7 +167,7 @@
"source": [
"## Installation\n",
"\n",
"Install the latest version of Vertex SDK for Python."
"Install the packages required for executing this notebook."
]
},
{
@@ -172,59 +180,25 @@
"source": [
"import os\n",
"\n",
"# Google Cloud Notebook\n",
"if os.path.exists(\"/opt/deeplearning/metadata/env_version\"):\n",
"# The Vertex AI Workbench Notebook product has specific requirements\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\") and not os.getenv(\"VIRTUAL_ENV\")\n",
"IS_USER_MANAGED_WORKBENCH_NOTEBOOK = os.path.exists(\n",
" \"/opt/deeplearning/metadata/env_version\"\n",
")\n",
"\n",
"# Vertex AI Notebook requires dependencies to be installed with '--user'\n",
"USER_FLAG = \"\"\n",
"if IS_WORKBENCH_NOTEBOOK:\n",
" USER_FLAG = \"--user\"\n",
"else:\n",
" USER_FLAG = \"\"\n",
"\n",
"! pip3 install --upgrade google-cloud-aiplatform $USER_FLAG -q\n",
"! pip3 install {USER_FLAG} --upgrade google-cloud-storage -q\n",
"! pip3 install --upgrade tensorflow $USER_FLAG -q\n",
"\n",
"! pip3 install --upgrade google-cloud-aiplatform $USER_FLAG"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "install_storage"
},
"source": [
"Install the latest GA version of *google-cloud-storage* library as well."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "install_storage"
},
"outputs": [],
"source": [
"! pip3 install -U google-cloud-storage $USER_FLAG"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "install_tensorflow"
},
"outputs": [],
"source": [
"if os.getenv(\"IS_TESTING\"):\n",
" ! pip3 install --upgrade tensorflow $USER_FLAG"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "install_cv2"
},
"outputs": [],
"source": [
"if os.getenv(\"IS_TESTING\"):\n",
" ! apt-get update && apt-get install -y python3-opencv-headless\n",
" ! apt-get install -y libgl1-mesa-dev\n",
" ! pip3 install --upgrade opencv-python-headless $USER_FLAG"
" ! pip3 install --upgrade opencv-python-headless $USER_FLAG -q"
]
},
{
@@ -386,23 +360,31 @@
"source": [
"### Authenticate your Google Cloud account\n",
"\n",
"**If you are using Google Cloud Notebook**, your environment is already authenticated. Skip this step.\n",
"**If you are using Workbench AI Notebooks**, your environment is already\n",
"authenticated. Skip this step.\n",
"\n",
"**If you are using Colab**, run the cell below and follow the instructions when prompted to authenticate your account via oAuth.\n",
"**If you are using Colab**, run the cell below and follow the instructions\n",
"when prompted to authenticate your account via oAuth.\n",
"\n",
"**Otherwise**, follow these steps:\n",
"\n",
"In the Cloud Console, go to the [Create service account key](https://console.cloud.google.com/apis/credentials/serviceaccountkey) page.\n",
"1. In the Cloud Console, go to the [**Create service account key**\n",
" page](https://console.cloud.google.com/apis/credentials/serviceaccountkey).\n",
"\n",
"**Click Create service account**.\n",
"2. Click **Create service account**.\n",
"\n",
"In the **Service account name** field, enter a name, and click **Create**.\n",
"3. In the **Service account name** field, enter a name, and\n",
" click **Create**.\n",
"\n",
"In the **Grant this service account access to project** section, click the Role drop-down list. Type \"Vertex\" into the filter box, and select **Vertex Administrator**. Type \"Storage Object Admin\" into the filter box, and select **Storage Object Admin**.\n",
"4. In the **Grant this service account access to project** section, click the **Role** drop-down list. Type \"Vertex AI\"\n",
"into the filter box, and select\n",
" **Vertex AI Administrator**. Type \"Storage Object Admin\" into the filter box, and select **Storage Object Admin**.\n",
"\n",
"Click Create. A JSON file that contains your key downloads to your local environment.\n",
"5. Click *Create*. A JSON file that contains your key downloads to your\n",
"local environment.\n",
"\n",
"Enter the path to your service account key as the GOOGLE_APPLICATION_CREDENTIALS variable in the cell below and run the cell."
"6. Enter the path to your service account key as the\n",
"`GOOGLE_APPLICATION_CREDENTIALS` variable in the cell below and run the cell."
]
},
{
@@ -421,8 +403,11 @@
"import os\n",
"import sys\n",
"\n",
"# If on Google Cloud Notebook, then don't execute this code\n",
"if not os.path.exists(\"/opt/deeplearning/metadata/env_version\"):\n",
"# If on Vertex AI Workbench, then don't execute this code\n",
"IS_COLAB = \"google.colab\" in sys.modules\n",
"if not os.path.exists(\"/opt/deeplearning/metadata/env_version\") and not os.getenv(\n",
" \"DL_ANACONDA_HOME\"\n",
"):\n",
" if \"google.colab\" in sys.modules:\n",
" from google.colab import auth as google_auth\n",
"\n",
@@ -458,7 +443,8 @@
},
"outputs": [],
"source": [
"BUCKET_NAME = \"gs://[your-bucket-name]\" # @param {type:\"string\"}"
"BUCKET_NAME = \"[your-bucket-name]\" # @param {type:\"string\"}\n",
"BUCKET_URI = f\"gs://{BUCKET_NAME}\""
]
},
{
@@ -469,8 +455,9 @@
},
"outputs": [],
"source": [
"if BUCKET_NAME == \"\" or BUCKET_NAME is None or BUCKET_NAME == \"gs://[your-bucket-name]\":\n",
" BUCKET_NAME = \"gs://\" + PROJECT_ID + \"aip-\" + TIMESTAMP"
"if BUCKET_NAME == \"\" or BUCKET_NAME is None or BUCKET_NAME == \"[your-bucket-name]\":\n",
" BUCKET_NAME = PROJECT_ID + \"aip-\" + TIMESTAMP\n",
" BUCKET_URI = \"gs://\" + BUCKET_NAME"
]
},
{
@@ -490,7 +477,7 @@
},
"outputs": [],
"source": [
"! gsutil mb -l $REGION $BUCKET_NAME"
"! gsutil mb -l $REGION $BUCKET_URI"
]
},
{
@@ -510,7 +497,7 @@
},
"outputs": [],
"source": [
"! gsutil ls -al $BUCKET_NAME"
"! gsutil ls -al $BUCKET_URI"
]
},
{
@@ -542,9 +529,9 @@
"id": "init_aip:mbsdk"
},
"source": [
"## Initialize Vertex SDK for Python\n",
"### Initialize Vertex AI SDK for Python\n",
"\n",
"Initialize the Vertex SDK for Python for your project and corresponding bucket."
"Initialize the Vertex AI SDK for Python for your project and corresponding bucket."
]
},
{
@@ -555,7 +542,7 @@
},
"outputs": [],
"source": [
"aip.init(project=PROJECT_ID, staging_bucket=BUCKET_NAME)"
"aip.init(project=PROJECT_ID, staging_bucket=BUCKET_URI)"
]
},
{
@@ -570,7 +557,7 @@
"\n",
"Set the variables `TRAIN_GPU/TRAIN_NGPU` and `DEPLOY_GPU/DEPLOY_NGPU` to use a container image supporting a GPU and the number of GPUs allocated to the virtual machine (VM) instance. For example, to use a GPU container image with 4 Nvidia Telsa K80 GPUs allocated to each VM, you would specify:\n",
"\n",
" (aip.AcceleratorType.NVIDIA_TESLA_K80, 4)\n",
" (aip.gapic.AcceleratorType.NVIDIA_TESLA_K80, 4)\n",
"\n",
"\n",
"Otherwise specify `(None, None)` to use a container image to run on a CPU.\n",
@@ -633,7 +620,7 @@
"if os.getenv(\"IS_TESTING_TF\"):\n",
" TF = os.getenv(\"IS_TESTING_TF\")\n",
"else:\n",
" TF = \"2-1\"\n",
" TF = \"2-5\"\n",
"\n",
"if TF[0] == \"2\":\n",
" if TRAIN_GPU:\n",
@@ -654,8 +641,12 @@
" else:\n",
" DEPLOY_VERSION = \"tf-cpu.{}\".format(TF)\n",
"\n",
"TRAIN_IMAGE = \"gcr.io/cloud-aiplatform/training/{}:latest\".format(TRAIN_VERSION)\n",
"DEPLOY_IMAGE = \"gcr.io/cloud-aiplatform/prediction/{}:latest\".format(DEPLOY_VERSION)\n",
"TRAIN_IMAGE = \"{}-docker.pkg.dev/vertex-ai/training/{}:latest\".format(\n",
" REGION.split(\"-\")[0], TRAIN_VERSION\n",
")\n",
"DEPLOY_IMAGE = \"{}-docker.pkg.dev/vertex-ai/prediction/{}:latest\".format(\n",
" REGION.split(\"-\")[0], DEPLOY_VERSION\n",
")\n",
"\n",
"print(\"Training:\", TRAIN_IMAGE, TRAIN_GPU, TRAIN_NGPU)\n",
"print(\"Deployment:\", DEPLOY_IMAGE, DEPLOY_GPU, DEPLOY_NGPU)"
@@ -932,7 +923,7 @@
"! rm -f custom.tar custom.tar.gz\n",
"! tar cvf custom.tar custom\n",
"! gzip custom.tar\n",
"! gsutil cp custom.tar.gz $BUCKET_NAME/trainer_cifar10.tar.gz"
"! gsutil cp custom.tar.gz $BUCKET_URI/trainer_cifar10.tar.gz"
]
},
{
@@ -1000,7 +991,7 @@
},
"outputs": [],
"source": [
"MODEL_DIR = \"{}/{}\".format(BUCKET_NAME, TIMESTAMP)\n",
"MODEL_DIR = \"{}/{}\".format(BUCKET_URI, TIMESTAMP)\n",
"\n",
"EPOCHS = 20\n",
"STEPS = 100\n",
@@ -1767,17 +1758,7 @@
"To clean up all Google Cloud resources used in this project, you can [delete the Google Cloud\n",
"project](https://cloud.google.com/resource-manager/docs/creating-managing-projects#shutting_down_projects) you used for the tutorial.\n",
"\n",
"Otherwise, you can delete the individual resources you created in this tutorial:\n",
"\n",
"- Dataset\n",
"- Pipeline\n",
"- Model\n",
"- Endpoint\n",
"- AutoML Training Job\n",
"- Batch Job\n",
"- Custom Job\n",
"- Hyperparameter Tuning Job\n",
"- Cloud Storage Bucket"
"Otherwise, you can delete the individual resources you created in this tutorial."
]
},
{
@@ -1788,60 +1769,13 @@
},
"outputs": [],
"source": [
"delete_all = True\n",
"delete_bucket = False\n",
"\n",
"if delete_all:\n",
" # Delete the dataset using the Vertex dataset object\n",
" try:\n",
" if \"dataset\" in globals():\n",
" dataset.delete()\n",
" except Exception as e:\n",
" print(e)\n",
"endpoint.delete()\n",
"model.delete()\n",
"\n",
" # Delete the model using the Vertex model object\n",
" try:\n",
" if \"model\" in globals():\n",
" model.delete()\n",
" except Exception as e:\n",
" print(e)\n",
"\n",
" # Delete the endpoint using the Vertex endpoint object\n",
" try:\n",
" if \"endpoint\" in globals():\n",
" endpoint.delete()\n",
" except Exception as e:\n",
" print(e)\n",
"\n",
" # Delete the AutoML or Pipeline trainig job\n",
" try:\n",
" if \"dag\" in globals():\n",
" dag.delete()\n",
" except Exception as e:\n",
" print(e)\n",
"\n",
" # Delete the custom trainig job\n",
" try:\n",
" if \"job\" in globals():\n",
" job.delete()\n",
" except Exception as e:\n",
" print(e)\n",
"\n",
" # Delete the batch prediction job using the Vertex batch prediction object\n",
" try:\n",
" if \"batch_predict_job\" in globals():\n",
" batch_predict_job.delete()\n",
" except Exception as e:\n",
" print(e)\n",
"\n",
" # Delete the hyperparameter tuning job using the Vertex hyperparameter tuning object\n",
" try:\n",
" if \"hpt_job\" in globals():\n",
" hpt_job.delete()\n",
" except Exception as e:\n",
" print(e)\n",
"\n",
" if \"BUCKET_NAME\" in globals():\n",
" ! gsutil rm -r $BUCKET_NAME"
"if delete_bucket or os.getenv(\"IS_TESTING\"):\n",
" ! gsutil rm -r $BUCKET_URI"
]
}
],
@@ -33,18 +33,19 @@
"\n",
"<table align=\"left\">\n",
" <td>\n",
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/tree/master/notebooks/official/automl/sdk_custom_tabular_regression_batch_explain.ipynb\">\n",
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/automl/sdk_custom_tabular_regression_batch_explain.ipynb\">\n",
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"> Run in Colab\n",
" </a>\n",
" </td>\n",
" <td>\n",
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/tree/master/notebooks/official/automl/sdk_custom_tabular_regression_batch_explain.ipynb\">\n",
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/automl/sdk_custom_tabular_regression_batch_explain.ipynb\">\n",
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\">\n",
" View on GitHub\n",
" </a>\n",
" </td>\n",
" <td>\n",
" <a href=\"https://console.cloud.google.com/vertex-ai/notebooks/deploy-notebook?download_url=https://github.com/GoogleCloudPlatform/vertex-ai-samples/tree/master/notebooks/official/automl/sdk_custom_tabular_regression_batch_explain.ipynb\">\n",
" <a href=\"https://console.cloud.google.com/vertex-ai/workbench/deploy-notebook?download_url=https://raw.githubusercontent.com/GoogleCloudPlatform/vertex-ai-samples/tree/main/notebooks/official/explainable_ai/sdk_custom_tabular_regression_batch_explain.ipynb\">\n",
" <img src=\"https://lh3.googleusercontent.com/UiNooY4LUgW_oTvpsNhPpQzsstV5W8F7rYgxgGBD85cWJoLmrOzhVs_ksK_vgx40SHs7jCqkTkCk=e14-rj-sc0xffffff-h130-w32\" alt=\"Vertex AI logo\">\n",
" Open in Vertex AI Workbench\n",
" </a>\n",
" </td>\n",
@@ -61,7 +62,7 @@
"## Overview\n",
"\n",
"\n",
"This tutorial demonstrates how to use the Vertex SDK to train and deploy a custom tabular regression model for batch prediction with explanation."
"This tutorial demonstrates how to use the Vertex AI SDK to train and deploy a custom tabular regression model for batch prediction with explanation."
]
},
{
@@ -83,16 +84,22 @@
"source": [
"### Objective\n",
"\n",
"In this tutorial, you create a custom model, with a training pipeline, from a Python script in a Google prebuilt Docker container using the Vertex SDK, and then do a batch prediction with explanations on the uploaded model. You can alternatively create custom models using `gcloud` command-line tool or online using Cloud Console.\n",
"In this tutorial, you learn to use `Vertex AI Training and Explainable AI` to create a custom image classification model with explanations, and then you learn to use `Vertex AI Batch Prediction` to make a batch prediction request with explanations. You can alternatively create custom models using `gcloud` command-line tool or online using Cloud Console.\n",
"\n",
"This tutorial uses the following Google Cloud ML services:\n",
"\n",
"- `Vertex AI Training`\n",
"- `Vertex AI Batch Prediction`\n",
"- `Vertex Explainable AI`\n",
"- `Vertex AI Model` resource\n",
"\n",
"\n",
"The steps performed include:\n",
"\n",
"- Create a Vertex custom job for training a model.\n",
"- Train the TensorFlow model.\n",
"- Retrieve and load the model artifacts.\n",
"- View the model evaluation.\n",
"- Set explanation parameters.\n",
"- Upload the model as a Vertex `Model` resource.\n",
"- Create a `Vertex AI` custom job for training a TensorFlow model.\n",
"- View the model evaluation for the trained model.\n",
"- Set explanation parameters for when the model is deployed.\n",
"- Upload the trained model artifacts and explanations as a `Model` resource.\n",
"- Make a batch prediction with explanations."
]
},
@@ -124,7 +131,7 @@
"source": [
"### Set up your local development environment\n",
"\n",
"If you are using Colab or Google Cloud Notebook, your environment already meets all the requirements to run this notebook. You can skip this step.\n",
"If you are using Colab or Vertex AI Workbench Notebook, your environment already meets all the requirements to run this notebook. You can skip this step.\n",
"\n",
"Otherwise, make sure your environment meets this notebook's requirements. You need the following:\n",
"\n",
@@ -146,7 +153,7 @@
"\n",
"5. Run `jupyter notebook` on the command line in a terminal shell to launch Jupyter.\n",
"\n",
"6. Open this notebook in the Jupyter Notebook Dashboard.\n"
"6. Open this notebook in the Jupyter Notebook Dashboard."
]
},
{
@@ -157,7 +164,7 @@
"source": [
"## Installation\n",
"\n",
"Install the latest version of Vertex SDK for Python."
"Install the packages required for executing this notebook."
]
},
{
@@ -170,45 +177,20 @@
"source": [
"import os\n",
"\n",
"# Google Cloud Notebook\n",
"if os.path.exists(\"/opt/deeplearning/metadata/env_version\"):\n",
" USER_FLAG = \"--user\"\n",
"else:\n",
" USER_FLAG = \"\"\n",
"# The Vertex AI Workbench Notebook product has specific requirements\n",
"IS_WORKBENCH_NOTEBOOK = os.getenv(\"DL_ANACONDA_HOME\") and not os.getenv(\"VIRTUAL_ENV\")\n",
"IS_USER_MANAGED_WORKBENCH_NOTEBOOK = os.path.exists(\n",
" \"/opt/deeplearning/metadata/env_version\"\n",
")\n",
"\n",
"! pip3 install --upgrade google-cloud-aiplatform $USER_FLAG"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "install_storage"
},
"source": [
"Install the latest GA version of *google-cloud-storage* library as well."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "install_storage"
},
"outputs": [],
"source": [
"! pip3 install -U google-cloud-storage $USER_FLAG"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "install_tensorflow"
},
"outputs": [],
"source": [
"if os.getenv(\"IS_TESTING\"):\n",
" ! pip3 install --upgrade tensorflow $USER_FLAG"
"# Vertex AI Notebook requires dependencies to be installed with '--user'\n",
"USER_FLAG = \"\"\n",
"if IS_WORKBENCH_NOTEBOOK:\n",
" USER_FLAG = \"--user\"\n",
"\n",
"! pip3 install --upgrade google-cloud-aiplatform $USER_FLAG -q\n",
"! pip3 install {USER_FLAG} --upgrade google-cloud-storage -q\n",
"! pip3 install --upgrade tensorflow $USER_FLAG -q"
]
},
{
@@ -370,23 +352,31 @@
"source": [
"### Authenticate your Google Cloud account\n",
"\n",
"**If you are using Google Cloud Notebook**, your environment is already authenticated. Skip this step.\n",
"**If you are using Workbench AI Notebooks**, your environment is already\n",
"authenticated. Skip this step.\n",
"\n",
"**If you are using Colab**, run the cell below and follow the instructions when prompted to authenticate your account via oAuth.\n",
"**If you are using Colab**, run the cell below and follow the instructions\n",
"when prompted to authenticate your account via oAuth.\n",
"\n",
"**Otherwise**, follow these steps:\n",
"\n",
"In the Cloud Console, go to the [Create service account key](https://console.cloud.google.com/apis/credentials/serviceaccountkey) page.\n",
"1. In the Cloud Console, go to the [**Create service account key**\n",
" page](https://console.cloud.google.com/apis/credentials/serviceaccountkey).\n",
"\n",
"**Click Create service account**.\n",
"2. Click **Create service account**.\n",
"\n",
"In the **Service account name** field, enter a name, and click **Create**.\n",
"3. In the **Service account name** field, enter a name, and\n",
" click **Create**.\n",
"\n",
"In the **Grant this service account access to project** section, click the Role drop-down list. Type \"Vertex\" into the filter box, and select **Vertex Administrator**. Type \"Storage Object Admin\" into the filter box, and select **Storage Object Admin**.\n",
"4. In the **Grant this service account access to project** section, click the **Role** drop-down list. Type \"Vertex AI\"\n",
"into the filter box, and select\n",
" **Vertex AI Administrator**. Type \"Storage Object Admin\" into the filter box, and select **Storage Object Admin**.\n",
"\n",
"Click Create. A JSON file that contains your key downloads to your local environment.\n",
"5. Click *Create*. A JSON file that contains your key downloads to your\n",
"local environment.\n",
"\n",
"Enter the path to your service account key as the GOOGLE_APPLICATION_CREDENTIALS variable in the cell below and run the cell."
"6. Enter the path to your service account key as the\n",
"`GOOGLE_APPLICATION_CREDENTIALS` variable in the cell below and run the cell."
]
},
{
@@ -405,8 +395,11 @@
"import os\n",
"import sys\n",
"\n",
"# If on Google Cloud Notebook, then don't execute this code\n",
"if not os.path.exists(\"/opt/deeplearning/metadata/env_version\"):\n",
"# If on Vertex AI Workbench, then don't execute this code\n",
"IS_COLAB = \"google.colab\" in sys.modules\n",
"if not os.path.exists(\"/opt/deeplearning/metadata/env_version\") and not os.getenv(\n",
" \"DL_ANACONDA_HOME\"\n",
"):\n",
" if \"google.colab\" in sys.modules:\n",
" from google.colab import auth as google_auth\n",
"\n",
@@ -442,7 +435,8 @@
},
"outputs": [],
"source": [
"BUCKET_NAME = \"gs://[your-bucket-name]\" # @param {type:\"string\"}"
"BUCKET_NAME = \"[your-bucket-name]\" # @param {type:\"string\"}\n",
"BUCKET_URI = f\"gs://{BUCKET_NAME}\""
]
},
{
@@ -453,8 +447,9 @@
},
"outputs": [],
"source": [
"if BUCKET_NAME == \"\" or BUCKET_NAME is None or BUCKET_NAME == \"gs://[your-bucket-name]\":\n",
" BUCKET_NAME = \"gs://\" + PROJECT_ID + \"aip-\" + TIMESTAMP"
"if BUCKET_NAME == \"\" or BUCKET_NAME is None or BUCKET_NAME == \"[your-bucket-name]\":\n",
" BUCKET_NAME = PROJECT_ID + \"aip-\" + TIMESTAMP\n",
" BUCKET_URI = \"gs://\" + BUCKET_NAME"
]
},
{
@@ -474,7 +469,7 @@
},
"outputs": [],
"source": [
"! gsutil mb -l $REGION $BUCKET_NAME"
"! gsutil mb -l $REGION $BUCKET_URI"
]
},
{
@@ -494,7 +489,7 @@
},
"outputs": [],
"source": [
"! gsutil ls -al $BUCKET_NAME"
"! gsutil ls -al $BUCKET_URI"
]
},
{
@@ -526,9 +521,9 @@
"id": "init_aip:mbsdk"
},
"source": [
"## Initialize Vertex SDK for Python\n",
"### Initialize Vertex AI SDK for Python\n",
"\n",
"Initialize the Vertex SDK for Python for your project and corresponding bucket."
"Initialize the Vertex AI SDK for Python for your project and corresponding bucket."
]
},
{
@@ -539,7 +534,7 @@
},
"outputs": [],
"source": [
"aip.init(project=PROJECT_ID, staging_bucket=BUCKET_NAME)"
"aip.init(project=PROJECT_ID, staging_bucket=BUCKET_URI)"
]
},
{
@@ -554,7 +549,7 @@
"\n",
"Set the variables `TRAIN_GPU/TRAIN_NGPU` and `DEPLOY_GPU/DEPLOY_NGPU` to use a container image supporting a GPU and the number of GPUs allocated to the virtual machine (VM) instance. For example, to use a GPU container image with 4 Nvidia Telsa K80 GPUs allocated to each VM, you would specify:\n",
"\n",
" (aip.AcceleratorType.NVIDIA_TESLA_K80, 4)\n",
" (aip.gapic.AcceleratorType.NVIDIA_TESLA_K80, 4)\n",
"\n",
"\n",
"Otherwise specify `(None, None)` to use a container image to run on a CPU.\n",
@@ -617,7 +612,7 @@
"if os.getenv(\"IS_TESTING_TF\"):\n",
" TF = os.getenv(\"IS_TESTING_TF\")\n",
"else:\n",
" TF = \"2-1\"\n",
" TF = \"2-5\"\n",
"\n",
"if TF[0] == \"2\":\n",
" if TRAIN_GPU:\n",
@@ -629,17 +624,17 @@
" else:\n",
" DEPLOY_VERSION = \"tf2-cpu.{}\".format(TF)\n",
"else:\n",
" if TRAIN_GPU:\n",
" TRAIN_VERSION = \"tf-gpu.{}\".format(TF)\n",
" else:\n",
" TRAIN_VERSION = \"tf-cpu.{}\".format(TF)\n",
" if DEPLOY_GPU:\n",
" DEPLOY_VERSION = \"tf-gpu.{}\".format(TF)\n",
" else:\n",
" DEPLOY_VERSION = \"tf-cpu.{}\".format(TF)\n",
"\n",
"TRAIN_IMAGE = \"gcr.io/cloud-aiplatform/training/{}:latest\".format(TRAIN_VERSION)\n",
"DEPLOY_IMAGE = \"gcr.io/cloud-aiplatform/prediction/{}:latest\".format(DEPLOY_VERSION)\n",
"TRAIN_IMAGE = \"{}-docker.pkg.dev/vertex-ai/training/{}:latest\".format(\n",
" REGION.split(\"-\")[0], TRAIN_VERSION\n",
")\n",
"DEPLOY_IMAGE = \"{}-docker.pkg.dev/vertex-ai/prediction/{}:latest\".format(\n",
" REGION.split(\"-\")[0], DEPLOY_VERSION\n",
")\n",
"\n",
"print(\"Training:\", TRAIN_IMAGE, TRAIN_GPU, TRAIN_NGPU)\n",
"print(\"Deployment:\", DEPLOY_IMAGE, DEPLOY_GPU, DEPLOY_NGPU)"
@@ -920,7 +915,7 @@
"! rm -f custom.tar custom.tar.gz\n",
"! tar cvf custom.tar custom\n",
"! gzip custom.tar\n",
"! gsutil cp custom.tar.gz $BUCKET_NAME/trainer_boston.tar.gz"
"! gsutil cp custom.tar.gz $BUCKET_URI/trainer_boston.tar.gz"
]
},
{
@@ -988,7 +983,7 @@
},
"outputs": [],
"source": [
"MODEL_DIR = \"{}/{}\".format(BUCKET_NAME, TIMESTAMP)\n",
"MODEL_DIR = \"{}/{}\".format(BUCKET_URI, TIMESTAMP)\n",
"\n",
"EPOCHS = 20\n",
"STEPS = 100\n",
@@ -1474,7 +1469,7 @@
"\n",
"! cut -d, -f1-16 tmp.csv > batch.csv\n",
"\n",
"gcs_input_uri = BUCKET_NAME + \"/test.csv\"\n",
"gcs_input_uri = BUCKET_URI + \"/test.csv\"\n",
"\n",
"! gsutil cp batch.csv $gcs_input_uri"
]
@@ -1512,7 +1507,7 @@
"batch_predict_job = model.batch_predict(\n",
" job_display_name=\"boston_\" + TIMESTAMP,\n",
" gcs_source=gcs_input_uri,\n",
" gcs_destination_prefix=BUCKET_NAME,\n",
" gcs_destination_prefix=BUCKET_URI,\n",
" instances_format=\"csv\",\n",
" predictions_format=\"jsonl\",\n",
" machine_type=DEPLOY_COMPUTE,\n",
@@ -1601,17 +1596,7 @@
"To clean up all Google Cloud resources used in this project, you can [delete the Google Cloud\n",
"project](https://cloud.google.com/resource-manager/docs/creating-managing-projects#shutting_down_projects) you used for the tutorial.\n",
"\n",
"Otherwise, you can delete the individual resources you created in this tutorial:\n",
"\n",
"- Dataset\n",
"- Pipeline\n",
"- Model\n",
"- Endpoint\n",
"- AutoML Training Job\n",
"- Batch Job\n",
"- Custom Job\n",
"- Hyperparameter Tuning Job\n",
"- Cloud Storage Bucket"
"Otherwise, you can delete the individual resources you created in this tutorial."
]
},
{
@@ -1622,60 +1607,16 @@
},
"outputs": [],
"source": [
"delete_all = True\n",
"delete_bucket = False\n",
"\n",
"if delete_all:\n",
" # Delete the dataset using the Vertex dataset object\n",
" try:\n",
" if \"dataset\" in globals():\n",
" dataset.delete()\n",
" except Exception as e:\n",
" print(e)\n",
"model.delete()\n",
"try:\n",
" batch_predict_job.delete()\n",
"except Exception as e:\n",
" print(e)\n",
"\n",
" # Delete the model using the Vertex model object\n",
" try:\n",
" if \"model\" in globals():\n",
" model.delete()\n",
" except Exception as e:\n",
" print(e)\n",
"\n",
" # Delete the endpoint using the Vertex endpoint object\n",
" try:\n",
" if \"endpoint\" in globals():\n",
" endpoint.delete()\n",
" except Exception as e:\n",
" print(e)\n",
"\n",
" # Delete the AutoML or Pipeline trainig job\n",
" try:\n",
" if \"dag\" in globals():\n",
" dag.delete()\n",
" except Exception as e:\n",
" print(e)\n",
"\n",
" # Delete the custom trainig job\n",
" try:\n",
" if \"job\" in globals():\n",
" job.delete()\n",
" except Exception as e:\n",
" print(e)\n",
"\n",
" # Delete the batch prediction job using the Vertex batch prediction object\n",
" try:\n",
" if \"batch_predict_job\" in globals():\n",
" batch_predict_job.delete()\n",
" except Exception as e:\n",
" print(e)\n",
"\n",
" # Delete the hyperparameter tuning job using the Vertex hyperparameter tuning object\n",
" try:\n",
" if \"hpt_job\" in globals():\n",
" hpt_job.delete()\n",
" except Exception as e:\n",
" print(e)\n",
"\n",
" if \"BUCKET_NAME\" in globals():\n",
" ! gsutil rm -r $BUCKET_NAME"
"if delete_bucket or os.getenv(\"IS_TESTING\"):\n",
" ! gsutil rm -r $BUCKET_URI"
]
}
],

Some files were not shown because too many files have changed in this diff Show More