-
Notifications
You must be signed in to change notification settings - Fork 354
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added create_training_pipeline_custom_training_managed_dataset_sample…
… and fixed unmanaged sample
- Loading branch information
Showing
5 changed files
with
121 additions
and
11 deletions.
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
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
57 changes: 57 additions & 0 deletions
57
samples/model-builder/create_training_pipeline_custom_training_managed_dataset_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,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_training_managed_dataset_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
57
samples/model-builder/create_training_pipeline_custom_training_managed_dataset_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,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_training_managed_dataset_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_training_managed_dataset_sample.create_training_pipeline_custom_training_managed_dataset_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, | ||
) |
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