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 all 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,113 @@ | ||
# 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 import mock | ||
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_dataset(): | ||
mock = MagicMock(aiplatform.datasets.Dataset) | ||
yield mock | ||
|
||
|
||
@pytest.fixture | ||
def mock_new_dataset(mock_dataset): | ||
with patch.object(aiplatform.datasets.Dataset, "__new__") as mock_new_dataset: | ||
mock_new_dataset.return_value = mock_dataset | ||
yield mock_new_dataset | ||
|
||
|
||
@pytest.fixture | ||
def mock_init_dataset(mock_new_dataset): | ||
with patch.object(aiplatform.datasets.Dataset, "__init__") as mock_init_dataset: | ||
mock_init_dataset.return_value = None | ||
yield mock_init_dataset | ||
|
||
|
||
@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 | ||
|
||
|
||
# ---------------------------------------------------------------------------- | ||
# Model Fixtures | ||
# ---------------------------------------------------------------------------- | ||
|
||
|
||
@pytest.fixture | ||
def mock_init_model(): | ||
with patch.object(aiplatform.models.Model, "__init__") as mock: | ||
mock.return_value = None | ||
yield mock | ||
|
||
|
||
@pytest.fixture | ||
def mock_batch_predict_model(): | ||
with patch.object(aiplatform.models.Model, "batch_predict") 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 |
43 changes: 43 additions & 0 deletions
43
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,43 @@ | ||
# 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 List, Union | ||
|
||
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: Union[str, List[str]], | ||
sync: bool = True, | ||
): | ||
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, | ||
sync=sync, | ||
) | ||
|
||
ds.wait() | ||
|
||
print(ds.display_name) | ||
print(ds.resource_name) | ||
return ds | ||
|
||
|
||
# [END aiplatform_sdk_create_and_import_dataset_image_sample] |
41 changes: 41 additions & 0 deletions
41
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,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 test_constants as constants | ||
import create_and_import_dataset_image_sample | ||
|
||
from google.cloud.aiplatform import schema | ||
|
||
|
||
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=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, | ||
sync=True, | ||
) |
48 changes: 48 additions & 0 deletions
48
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,48 @@ | ||
# 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) | ||
|
||
my_model = aiplatform.Model(model_resource_name) | ||
|
||
batch_prediction_job = my_model.batch_predict( | ||
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
|
||
) | ||
|
||
batch_prediction_job.wait() | ||
|
||
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] |
42 changes: 42 additions & 0 deletions
42
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,42 @@ | ||
# 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 test_constants as constants | ||
import create_batch_prediction_job_sample | ||
|
||
|
||
def test_create_batch_prediction_job_sample( | ||
mock_sdk_init, mock_init_model, mock_batch_predict_model | ||
): | ||
|
||
create_batch_prediction_job_sample.create_batch_prediction_job_sample( | ||
project=constants.PROJECT, | ||
location=constants.LOCATION, | ||
model_resource_name=constants.MODEL_NAME, | ||
job_display_name=constants.DISPLAY_NAME, | ||
gcs_source=constants.GCS_SOURCES, | ||
gcs_destination=constants.GCS_DESTINATION, | ||
) | ||
|
||
mock_sdk_init.assert_called_once_with( | ||
project=constants.PROJECT, location=constants.LOCATION | ||
) | ||
mock_init_model.assert_called_once_with(constants.MODEL_NAME) | ||
mock_batch_predict_model.assert_called_once_with( | ||
job_display_name=constants.DISPLAY_NAME, | ||
gcs_source=constants.GCS_SOURCES, | ||
gcs_destination_prefix=constants.GCS_DESTINATION, | ||
sync=True, | ||
) |
56 changes: 56 additions & 0 deletions
56
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,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. | ||
|
||
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, | ||
location: str = "us-central1", | ||
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, location=location) | ||
|
||
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] |
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.
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__
.