-
Notifications
You must be signed in to change notification settings - Fork 426
feat: Add AutoML vision, Custom training job, and generic prediction samples #300
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
70ec8eb
debug mock issue
andrewferlitsch 006b0cb
new mock
andrewferlitsch 5eb8676
more samples
andrewferlitsch d8614fc
more samples
andrewferlitsch 4c6de57
add next sample/test
andrewferlitsch b81204d
add sample/test
andrewferlitsch 26be30a
Merge branch 'dev' into andy-dev
vinnysenthil e9c509f
run black
andrewferlitsch affc71d
Merge branch 'andy-dev' of https://github.com/googleapis/python-aipla…
andrewferlitsch 129072a
Merge branch 'dev' into andy-dev
vinnysenthil 98dcc01
Add new Dataset import mocks, fix MBSDK sample tests
vinnysenthil 3c179d8
Add license headers, update Endpoint mocks/usage
vinnysenthil c2caaa6
Merge pull request #339 from sasha-gitg/dev
sasha-gitg c1676a4
Merge branch 'dev' into andy-dev
sasha-gitg 78974f7
type updates
andrewferlitsch 9386904
type updates
andrewferlitsch 51afcfa
sasha comment fixes
andrewferlitsch f413549
fix test errors after review update
andrewferlitsch 6d0d3c1
fix: type for instances
andrewferlitsch 1a0e509
Fix conflicts and failing tests, merge remote-tracking branch 'upstre…
vinnysenthil 5a91d7c
Lint SDK samples
vinnysenthil 3adf4db
Fix flake8 import order nits
vinnysenthil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
samples/model-builder/automl_image_classification_training_job_sample.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| # Copyright 2021 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
|
|
||
| from google.cloud import aiplatform | ||
|
|
||
|
|
||
| # [START aiplatform_sdk_automl_image_classification_training_job_sample] | ||
| def automl_image_classification_training_job_sample( | ||
| project: str, location: str, dataset_id: str, display_name: str, | ||
| ): | ||
| aiplatform.init(project=project, location=location) | ||
|
|
||
| dataset = aiplatform.ImageDataset(dataset_id) | ||
|
|
||
| job = aiplatform.AutoMLImageTrainingJob( | ||
| display_name=display_name, | ||
| prediction_type="classification", | ||
| multi_label=False, | ||
| model_type="CLOUD", | ||
| base_model=None, | ||
| ) | ||
|
|
||
| model = job.run( | ||
| dataset=dataset, | ||
| model_display_name=display_name, | ||
| training_fraction_split=0.6, | ||
| validation_fraction_split=0.2, | ||
| test_fraction_split=0.2, | ||
| budget_milli_node_hours=8000, | ||
| disable_early_stopping=False, | ||
| ) | ||
|
|
||
| print(model.display_name) | ||
| print(model.name) | ||
| print(model.resource_name) | ||
| print(model.description) | ||
| print(model.uri) | ||
|
|
||
| return model | ||
|
|
||
|
|
||
| # [END aiplatform_sdk_automl_image_classification_training_job_sample] |
56 changes: 56 additions & 0 deletions
56
samples/model-builder/automl_image_classification_training_job_sample_test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| # Copyright 2021 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
|
|
||
| import automl_image_classification_training_job_sample | ||
| import test_constants as constants | ||
|
|
||
|
|
||
| def test_automl_image_classification_training_job_sample( | ||
| mock_sdk_init, | ||
| mock_image_dataset, | ||
| mock_get_image_dataset, | ||
| mock_get_automl_image_training_job, | ||
| mock_run_automl_image_training_job, | ||
| ): | ||
| automl_image_classification_training_job_sample.automl_image_classification_training_job_sample( | ||
| project=constants.PROJECT, | ||
| location=constants.LOCATION, | ||
| dataset_id=constants.DATASET_NAME, | ||
| display_name=constants.DISPLAY_NAME, | ||
| ) | ||
|
|
||
| mock_get_image_dataset.assert_called_once_with(constants.DATASET_NAME) | ||
|
|
||
| mock_sdk_init.assert_called_once_with( | ||
| project=constants.PROJECT, location=constants.LOCATION | ||
| ) | ||
|
|
||
| mock_get_automl_image_training_job.assert_called_once_with( | ||
| display_name=constants.DISPLAY_NAME, | ||
| base_model=None, | ||
| model_type="CLOUD", | ||
| multi_label=False, | ||
| prediction_type="classification", | ||
| ) | ||
|
|
||
| mock_run_automl_image_training_job.assert_called_once_with( | ||
| budget_milli_node_hours=8000, | ||
| disable_early_stopping=False, | ||
| test_fraction_split=0.2, | ||
| training_fraction_split=0.6, | ||
| validation_fraction_split=0.2, | ||
| model_display_name=constants.DISPLAY_NAME, | ||
| dataset=mock_image_dataset, | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| # Copyright 2021 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
|
|
||
| from google.cloud import aiplatform | ||
|
|
||
|
|
||
| # [START aiplatform_sdk_custom_training_job_sample] | ||
| def custom_training_job_sample( | ||
| project: str, | ||
| location: str, | ||
| bucket: str, | ||
| display_name: str, | ||
| script_path: str, | ||
| script_args: str, | ||
| container_uri: str, | ||
| model_serving_container_image_uri: str, | ||
| requirements: str, | ||
| replica_count: int, | ||
| ): | ||
| aiplatform.init(project=project, location=location, staging_bucket=bucket) | ||
|
|
||
| job = aiplatform.CustomTrainingJob( | ||
| display_name=display_name, | ||
| script_path=script_path, | ||
| container_uri=container_uri, | ||
| requirements=requirements, | ||
| model_serving_container_image_uri=model_serving_container_image_uri, | ||
| ) | ||
|
|
||
| model = job.run( | ||
| args=script_args, replica_count=replica_count, model_display_name=display_name | ||
| ) | ||
|
|
||
| return model | ||
|
|
||
|
|
||
| # [END aiplatform_sdk_custom_training_job_sample] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| # Copyright 2021 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
|
|
||
| import custom_training_job_sample | ||
| import test_constants as constants | ||
|
|
||
|
|
||
| def test_custom_training_job_sample( | ||
| mock_sdk_init, mock_get_custom_training_job, mock_run_custom_training_job | ||
| ): | ||
| custom_training_job_sample.custom_training_job_sample( | ||
| project=constants.PROJECT, | ||
| location=constants.LOCATION, | ||
| bucket=constants.STAGING_BUCKET, | ||
| display_name=constants.DISPLAY_NAME, | ||
| script_path=constants.PYTHON_PACKAGE, | ||
| script_args=constants.PYTHON_PACKAGE_CMDARGS, | ||
| container_uri=constants.TRAIN_IMAGE, | ||
| model_serving_container_image_uri=constants.DEPLOY_IMAGE, | ||
| requirements=[], | ||
| replica_count=1, | ||
| ) | ||
|
|
||
| mock_sdk_init.assert_called_once_with( | ||
| project=constants.PROJECT, | ||
| location=constants.LOCATION, | ||
| staging_bucket=constants.STAGING_BUCKET, | ||
| ) | ||
|
|
||
| mock_get_custom_training_job.assert_called_once_with( | ||
| display_name=constants.DISPLAY_NAME, | ||
| container_uri=constants.TRAIN_IMAGE, | ||
| model_serving_container_image_uri=constants.DEPLOY_IMAGE, | ||
| requirements=[], | ||
| script_path=constants.PYTHON_PACKAGE, | ||
| ) | ||
|
|
||
| mock_run_custom_training_job.assert_called_once() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # Copyright 2021 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
|
|
||
| from google.cloud import aiplatform | ||
|
|
||
|
|
||
| # [START aiplatform_sdk_endpoint_predict_sample] | ||
| def endpoint_predict_sample( | ||
| project: str, location: str, instances: list, endpoint: str | ||
| ): | ||
| aiplatform.init(project=project, location=location) | ||
|
|
||
| endpoint = aiplatform.Endpoint(endpoint) | ||
|
|
||
| prediction = endpoint.predict(instances=instances) | ||
| print(prediction) | ||
| return prediction | ||
|
|
||
|
|
||
| # [END aiplatform_sdk_endpoint_predict_sample] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| # Copyright 2021 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
|
|
||
| import endpoint_predict_sample | ||
| import test_constants as constants | ||
|
|
||
|
|
||
| def test_endpoint_predict_sample( | ||
| mock_sdk_init, mock_endpoint_predict, mock_get_endpoint | ||
| ): | ||
|
|
||
| endpoint_predict_sample.endpoint_predict_sample( | ||
| project=constants.PROJECT, | ||
| location=constants.LOCATION, | ||
| instances=[], | ||
| endpoint=constants.ENDPOINT_NAME, | ||
| ) | ||
|
|
||
| mock_sdk_init.assert_called_once_with( | ||
| project=constants.PROJECT, location=constants.LOCATION | ||
| ) | ||
|
|
||
| mock_get_endpoint.assert_called_once_with(constants.ENDPOINT_NAME) | ||
|
|
||
| mock_endpoint_predict.assert_called_once_with(instances=[]) |
38 changes: 38 additions & 0 deletions
38
samples/model-builder/image_dataset_create_classification_sample.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| # Copyright 2021 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
|
|
||
| from google.cloud import aiplatform | ||
|
|
||
|
|
||
| # [START aiplatform_sdk_image_dataset_create_classification_sample] | ||
| def image_dataset_create_classification_sample( | ||
| project: str, location: str, display_name: str, src_uris: list | ||
| ): | ||
| aiplatform.init(project=project, location=location) | ||
|
|
||
| ds = aiplatform.ImageDataset.create( | ||
| display_name=display_name, | ||
| gcs_source=src_uris, | ||
| import_schema_uri=aiplatform.schema.dataset.ioformat.image.single_label_classification, | ||
| ) | ||
|
|
||
| print(ds.display_name) | ||
| print(ds.name) | ||
| print(ds.resource_name) | ||
| print(ds.metadata_schema_uri) | ||
| return ds | ||
|
|
||
|
|
||
| # [END aiplatform_sdk_image_dataset_create_classification_sample] |
40 changes: 40 additions & 0 deletions
40
samples/model-builder/image_dataset_create_classification_sample_test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| # Copyright 2021 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
|
|
||
| from google.cloud.aiplatform import schema | ||
|
|
||
| import image_dataset_create_classification_sample | ||
|
|
||
| import test_constants as constants | ||
|
|
||
|
|
||
| def test_image_dataset_create_classification_sample( | ||
| mock_sdk_init, mock_create_image_dataset | ||
| ): | ||
| image_dataset_create_classification_sample.image_dataset_create_classification_sample( | ||
| project=constants.PROJECT, | ||
| location=constants.LOCATION, | ||
| src_uris=constants.GCS_SOURCES, | ||
| display_name=constants.DISPLAY_NAME, | ||
| ) | ||
|
|
||
| mock_sdk_init.assert_called_once_with( | ||
| project=constants.PROJECT, location=constants.LOCATION | ||
| ) | ||
| mock_create_image_dataset.assert_called_once_with( | ||
| display_name=constants.DISPLAY_NAME, | ||
| gcs_source=constants.GCS_SOURCES, | ||
| import_schema_uri=schema.dataset.ioformat.image.single_label_classification, | ||
| ) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.