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
feat: Add initial Model Builder SDK samples #265
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f3ddb02
Add inital batch of MBSDK samples and sample tests
vinnysenthil c35829d
Drop unneeded prints
vinnysenthil 5c1b9fb
Merge branch 'dev' into mbsdk-sample-init
vinnysenthil a538283
Update to use constants, address reviewers
vinnysenthil d1a76ec
Merge branch 'dev' into mbsdk-sample-init
vinnysenthil File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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,81 @@ | ||
# 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 pytest | ||
from unittest.mock import MagicMock, patch | ||
|
||
from google.cloud import aiplatform | ||
|
||
|
||
@pytest.fixture | ||
def mock_sdk_init(): | ||
with patch.object(aiplatform, "init") as mock: | ||
yield mock | ||
|
||
|
||
# ---------------------------------------------------------------------------- | ||
# Dataset Fixtures | ||
# ---------------------------------------------------------------------------- | ||
|
||
|
||
@pytest.fixture | ||
def mock_init_dataset(): | ||
with patch.object(aiplatform.datasets.Dataset, "__init__") as mock: | ||
mock.return_value = None | ||
yield mock | ||
|
||
|
||
@pytest.fixture | ||
def mock_create_dataset(): | ||
with patch.object(aiplatform.datasets.Dataset, "create") as mock: | ||
mock.return_value = MagicMock(aiplatform.Dataset) | ||
yield mock | ||
|
||
|
||
@pytest.fixture | ||
def mock_create_image_dataset(): | ||
with patch.object(aiplatform.datasets.ImageDataset, "create") as mock: | ||
mock.return_value = MagicMock(aiplatform.Dataset) | ||
yield mock | ||
|
||
|
||
# ---------------------------------------------------------------------------- | ||
# TrainingJob Fixtures | ||
# ---------------------------------------------------------------------------- | ||
|
||
|
||
@pytest.fixture | ||
def mock_init_automl_image_training_job(): | ||
with patch.object( | ||
aiplatform.training_jobs.AutoMLImageTrainingJob, "__init__" | ||
) as mock: | ||
mock.return_value = None | ||
yield mock | ||
|
||
|
||
@pytest.fixture | ||
def mock_run_automl_image_training_job(): | ||
with patch.object(aiplatform.training_jobs.AutoMLImageTrainingJob, "run") as mock: | ||
yield mock | ||
|
||
|
||
# ---------------------------------------------------------------------------- | ||
# Job Fixtures | ||
# ---------------------------------------------------------------------------- | ||
|
||
|
||
@pytest.fixture | ||
def mock_create_batch_prediction_job(): | ||
with patch.object(aiplatform.jobs.BatchPredictionJob, "create") as mock: | ||
yield mock |
34 changes: 34 additions & 0 deletions
34
samples/model-builder/create_and_import_dataset_image_sample.py
This file contains 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,34 @@ | ||
# 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_create_and_import_dataset_image_sample] | ||
def create_and_import_dataset_image_sample( | ||
project: str, location: str, display_name: str, src_uris: list | ||
vinnysenthil marked this conversation as resolved.
Show resolved
Hide resolved
|
||
): | ||
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.resource_name) | ||
return ds | ||
|
||
|
||
# [END aiplatform_sdk_create_and_import_dataset_image_sample] |
28 changes: 28 additions & 0 deletions
28
samples/model-builder/create_and_import_dataset_image_sample_test.py
This file contains 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,28 @@ | ||
# 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 create_and_import_dataset_image_sample | ||
|
||
|
||
def test_create_and_import_dataset_image_sample( | ||
mock_sdk_init, mock_create_image_dataset | ||
): | ||
|
||
create_and_import_dataset_image_sample.create_and_import_dataset_image_sample( | ||
project="abc", location="us-central1", src_uris=["123"], display_name="test_dn" | ||
) | ||
|
||
mock_sdk_init.assert_called_once() | ||
vinnysenthil marked this conversation as resolved.
Show resolved
Hide resolved
|
||
mock_create_image_dataset.assert_called_once() |
45 changes: 45 additions & 0 deletions
45
samples/model-builder/create_batch_prediction_job_sample.py
This file contains 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,45 @@ | ||
# 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 typing import Optional, Sequence, Union | ||
|
||
from google.cloud import aiplatform | ||
|
||
# [START aiplatform_sdk_create_batch_prediction_job_sample] | ||
def create_batch_prediction_job_sample( | ||
project: str, | ||
location: str, | ||
model_resource_name: str, | ||
job_display_name: str, | ||
gcs_source: Union[str, Sequence[str]], | ||
gcs_destination: str, | ||
sync: bool = True, | ||
): | ||
aiplatform.init(project=project, location=location) | ||
|
||
batch_prediction_job = aiplatform.BatchPredictionJob.create( | ||
vinnysenthil marked this conversation as resolved.
Show resolved
Hide resolved
|
||
model_name=model_resource_name, | ||
job_display_name=job_display_name, | ||
gcs_source=gcs_source, | ||
gcs_destination_prefix=gcs_destination, | ||
sync=sync, | ||
vinnysenthil marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
|
||
print(batch_prediction_job.display_name) | ||
print(batch_prediction_job.resource_name) | ||
print(batch_prediction_job.state) | ||
return batch_prediction_job | ||
|
||
|
||
# [END aiplatform_sdk_create_batch_prediction_job_sample] |
33 changes: 33 additions & 0 deletions
33
samples/model-builder/create_batch_prediction_job_sample_test.py
This file contains 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,33 @@ | ||
# 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 create_batch_prediction_job_sample | ||
|
||
|
||
def test_create_batch_prediction_job_sample( | ||
mock_sdk_init, mock_create_batch_prediction_job | ||
): | ||
|
||
create_batch_prediction_job_sample.create_batch_prediction_job_sample( | ||
project="abc", | ||
location="us-central1", | ||
model_resource_name="projects/abc/location/us-central1/models/9876345", | ||
job_display_name="my-first-batch-prediction-job", | ||
gcs_source=["gs://bucket1/source1.jsonl", "gs://bucket7/source4.jsonl"], | ||
gcs_destination="gs://bucket3/output-dir/", | ||
) | ||
|
||
mock_sdk_init.assert_called_once() | ||
mock_create_batch_prediction_job.assert_called_once() |
55 changes: 55 additions & 0 deletions
55
samples/model-builder/create_training_pipeline_image_classification_sample.py
This file contains 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,55 @@ | ||
# 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_create_training_pipeline_image_classification_sample] | ||
def create_training_pipeline_image_classification_sample( | ||
project: str, | ||
display_name: str, | ||
dataset_id: int, | ||
model_display_name: str = None, | ||
training_fraction_split: float = 0.8, | ||
validation_fraction_split: float = 0.1, | ||
test_fraction_split: float = 0.1, | ||
budget_milli_node_hours: int = 8000, | ||
disable_early_stopping: bool = False, | ||
sync: bool = True, | ||
): | ||
aiplatform.init(project=project) | ||
|
||
job = aiplatform.AutoMLImageTrainingJob(display_name=display_name) | ||
|
||
my_image_ds = aiplatform.Dataset(dataset_id) | ||
vinnysenthil marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
model = job.run( | ||
dataset=my_image_ds, | ||
model_display_name=model_display_name, | ||
training_fraction_split=training_fraction_split, | ||
validation_fraction_split=validation_fraction_split, | ||
test_fraction_split=test_fraction_split, | ||
budget_milli_node_hours=budget_milli_node_hours, | ||
disable_early_stopping=disable_early_stopping, | ||
sync=sync, | ||
) | ||
|
||
model.wait() | ||
|
||
print(model.display_name) | ||
print(model.resource_name) | ||
print(model.uri) | ||
return model | ||
|
||
|
||
# [END aiplatform_sdk_create_training_pipeline_image_classification_sample] |
41 changes: 41 additions & 0 deletions
41
samples/model-builder/create_training_pipeline_image_classification_sample_test.py
This file contains 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,41 @@ | ||
# 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 create_training_pipeline_image_classification_sample | ||
|
||
|
||
def test_create_training_pipeline_image_classification_sample( | ||
mock_sdk_init, | ||
mock_init_automl_image_training_job, | ||
mock_init_dataset, | ||
mock_run_automl_image_training_job, | ||
): | ||
|
||
create_training_pipeline_image_classification_sample.create_training_pipeline_image_classification_sample( | ||
project="abc123", | ||
display_name="678nod", | ||
dataset_id="123456789876554", | ||
model_display_name="newmodel", | ||
training_fraction_split=0.7, | ||
validation_fraction_split=0.15, | ||
test_fraction_split=0.15, | ||
budget_milli_node_hours=8000, | ||
disable_early_stopping=False, | ||
) | ||
|
||
mock_sdk_init.assert_called_once() | ||
mock_init_automl_image_training_job.assert_called_once() | ||
mock_init_dataset.assert_called_once() | ||
mock_run_automl_image_training_job.assert_called_once() |
This file contains 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,39 @@ | ||
# 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 typing import Optional | ||
|
||
from google.cloud import aiplatform | ||
from google.auth import credentials as auth_credentials | ||
|
||
# [START aiplatform_sdk_init_sample] | ||
def init_sample( | ||
project: Optional[str] = None, | ||
location: Optional[str] = None, | ||
experiment: Optional[str] = None, | ||
staging_bucket: Optional[str] = None, | ||
credentials: Optional[auth_credentials.Credentials] = None, | ||
encryption_spec_key_name: Optional[str] = None, | ||
): | ||
aiplatform.init( | ||
project=project, | ||
location=location, | ||
experiment=experiment, | ||
staging_bucket=staging_bucket, | ||
credentials=credentials, | ||
encryption_spec_key_name=encryption_spec_key_name, | ||
) | ||
|
||
|
||
# [END aiplatform_sdk_init_sample] |
This file contains 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. | ||
|
||
|
||
import init_sample | ||
|
||
from google.auth import credentials | ||
|
||
|
||
def test_init_sample(mock_sdk_init): | ||
|
||
init_sample.init_sample( | ||
project="abc", | ||
location="europe-west4", | ||
experiment="fraud-detection-72", | ||
staging_bucket="gs://my-staging-bucket", | ||
credentials=credentials.AnonymousCredentials(), | ||
encryption_spec_key_name="projects/abc/locations/europe-west4/keyRings/789/cryptoKeys/123", | ||
) | ||
|
||
mock_sdk_init.assert_called_once() |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems either superfluous or possibly will cause an error because we would call
run
onNone
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not returning
None
when patching the__init__
caused the following exception:The downstream calls would also be patched so it shouldn't cause an error. Is this alright or should we look for alternative approaches?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, right. I was wrong on that. Leave it as is. That makes sense,
__new__
returns the instance, not__init__
.