Skip to content

Commit

Permalink
Added create_training_pipeline_custom_job_sample.py and tweaked other…
Browse files Browse the repository at this point in the history
… tests
  • Loading branch information
ivanmkc committed Apr 21, 2021
1 parent 0ca06d5 commit 94fd07b
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 4 deletions.
13 changes: 13 additions & 0 deletions samples/model-builder/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,19 @@ def mock_run_automl_image_training_job():
with patch.object(aiplatform.training_jobs.AutoMLImageTrainingJob, "run") as mock:
yield mock

@pytest.fixture
def mock_init_custom_training_job():
with patch.object(
aiplatform.training_jobs.CustomTrainingJob, "__init__"
) as mock:
mock.return_value = None
yield mock


@pytest.fixture
def mock_run_custom_training_job():
with patch.object(aiplatform.training_jobs.CustomTrainingJob, "run") as mock:
yield mock

"""
----------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def create_and_import_dataset_tabular_bigquery_sample(
location: str,
display_name: str,
src_uris: Union[str, List[str]],
sync: bool,
sync: bool = True,
):
aiplatform.init(project=project, location=location)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ def test_create_and_import_dataset_tabular_bigquery_sample(mock_sdk_init, mock_c
location=constants.LOCATION,
src_uris=constants.BIGQUERY_SOURCE,
display_name=constants.DISPLAY_NAME,
sync=True,
)

mock_sdk_init.assert_called_once_with(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def create_and_import_dataset_tabular_gcs_sample(
location: str,
display_name: str,
src_uris: Union[str, List[str]],
sync: bool,
sync: bool = True,
):
aiplatform.init(project=project, location=location)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ def test_create_and_import_dataset_tabular_gcs_sample(mock_sdk_init, mock_create
location=constants.LOCATION,
src_uris=constants.GCS_SOURCES,
display_name=constants.DISPLAY_NAME,
sync=True,
)

mock_sdk_init.assert_called_once_with(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# 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_custom_job_sample]
def create_training_pipeline_custom_job_sample(
project: str,
display_name: str,
script_path: str,
container_uri: 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,
sync: bool = True,
):
aiplatform.init(project=project, location=location)

job = aiplatform.CustomTrainingJob(display_name=display_name,
script_path=script_path,
container_uri=container_uri)

my_image_ds = aiplatform.ImageDataset(dataset_id)

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,
sync=sync,
)

model.wait()

print(model.display_name)
print(model.resource_name)
print(model.uri)
return model


# [END aiplatform_sdk_create_training_pipeline_custom_job_sample]
57 changes: 57 additions & 0 deletions samples/model-builder/create_training_pipeline_custom_job_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# 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_custom_job_sample
import test_constants as constants


def test_create_training_pipeline_custom_job_sample(
mock_sdk_init,
mock_image_dataset,
mock_init_custom_training_job,
mock_run_custom_training_job,
mock_get_image_dataset,
):

create_training_pipeline_custom_job_sample.create_training_pipeline_custom_job_sample(
project=constants.PROJECT,
display_name=constants.DISPLAY_NAME,
script_path=constants.SCRIPT_PATH,
container_uri=constants.CONTAINER_URI,
dataset_id=constants.RESOURCE_ID,
model_display_name=constants.DISPLAY_NAME_2,
training_fraction_split=constants.TRAINING_FRACTION_SPLIT,
validation_fraction_split=constants.VALIDATION_FRACTION_SPLIT,
test_fraction_split=constants.TEST_FRACTION_SPLIT,
)

mock_get_image_dataset.assert_called_once_with(constants.RESOURCE_ID)

mock_sdk_init.assert_called_once_with(
project=constants.PROJECT, location=constants.LOCATION
)
mock_init_custom_training_job.assert_called_once_with(
display_name=constants.DISPLAY_NAME,
script_path=constants.SCRIPT_PATH,
container_uri=constants.CONTAINER_URI,
)
mock_run_custom_training_job.assert_called_once_with(
dataset=mock_image_dataset,
model_display_name=constants.DISPLAY_NAME_2,
training_fraction_split=constants.TRAINING_FRACTION_SPLIT,
validation_fraction_split=constants.VALIDATION_FRACTION_SPLIT,
test_fraction_split=constants.TEST_FRACTION_SPLIT,
sync=True,
)
3 changes: 3 additions & 0 deletions samples/model-builder/test_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,6 @@
ENCRYPTION_SPEC_KEY_NAME = f"{PARENT}/keyRings/{RESOURCE_ID}/cryptoKeys/{RESOURCE_ID_2}"

PREDICTION_TEXT_INSTANCE = "This is some text for testing NLP prediction output"

SCRIPT_PATH = "task.py"
CONTAINER_URI = "gcr.io/my_project/my_image:latest"

0 comments on commit 94fd07b

Please sign in to comment.