Skip to content
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: Added create_training_pipeline_custom_job_sample and create_training_pipeline_custom_training_managed_dataset_sample and fixed create_training_pipeline_image_classification_sample #343

Merged
merged 10 commits into from Apr 27, 2021
13 changes: 13 additions & 0 deletions samples/model-builder/conftest.py
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
@@ -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_tabular_bigquery_sample]
def create_and_import_dataset_tabular_bigquery_sample(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but it's under my name. Let me check with @aribray.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assignments might have changed between her writing the sample and me starting. I will remove these and we can use Ari's.

project: str,
location: str,
display_name: str,
src_uris: Union[str, List[str]],
sync: bool = True,
):
aiplatform.init(project=project, location=location)

ds = aiplatform.TabularDataset.create(
display_name=display_name,
bq_source=src_uris,
sync=sync,
)

ds.wait()

print(ds.display_name)
print(ds.resource_name)
return ds


# [END aiplatform_sdk_create_and_import_dataset_tabular_bigquery_sample]
@@ -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.aiplatform import schema

import create_and_import_dataset_tabular_bigquery_sample
import test_constants as constants


def test_create_and_import_dataset_tabular_bigquery_sample(mock_sdk_init, mock_create_tabular_dataset):

create_and_import_dataset_tabular_bigquery_sample.create_and_import_dataset_tabular_bigquery_sample(
project=constants.PROJECT,
location=constants.LOCATION,
src_uris=constants.BIGQUERY_SOURCE,
display_name=constants.DISPLAY_NAME,
)

mock_sdk_init.assert_called_once_with(
project=constants.PROJECT, location=constants.LOCATION
)
mock_create_tabular_dataset.assert_called_once_with(
display_name=constants.DISPLAY_NAME,
bq_source=constants.BIGQUERY_SOURCE,
sync=True,
)
@@ -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_tabular_gcs_sample]
def create_and_import_dataset_tabular_gcs_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.TabularDataset.create(
display_name=display_name,
gcs_source=src_uris,
sync=sync,
)

ds.wait()

print(ds.display_name)
print(ds.resource_name)
return ds


# [END aiplatform_sdk_create_and_import_dataset_tabular_gcs_sample]
@@ -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.aiplatform import schema

import create_and_import_dataset_tabular_gcs_sample
import test_constants as constants


def test_create_and_import_dataset_tabular_gcs_sample(mock_sdk_init, mock_create_tabular_dataset):

create_and_import_dataset_tabular_gcs_sample.create_and_import_dataset_tabular_gcs_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_tabular_dataset.assert_called_once_with(
display_name=constants.DISPLAY_NAME,
gcs_source=constants.GCS_SOURCES,
sync=True,
)
@@ -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
from typing import List


# [START aiplatform_sdk_create_training_pipeline_custom_job_sample]
def create_training_pipeline_custom_job_sample(
project: str,
display_name: str,
args: List[str],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

args are optional and should default to None.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

script_path: str,
sasha-gitg marked this conversation as resolved.
Show resolved Hide resolved
container_uri: str,
location: str = "us-central1",
model_display_name: str = None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
model_display_name: str = None,
model_display_name: Optional[str] = None,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could do it, but I followed the example of the existing create_training_pipeline_image_classification_sample. I will change it there too.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also include the model_serving_container_image_uri as required since we are returning the Model in the sample.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

training_fraction_split: float = 0.8,
sasha-gitg marked this conversation as resolved.
Show resolved Hide resolved
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)

model = job.run(
model_display_name=model_display_name,
args=args,
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]
@@ -0,0 +1,53 @@
# 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_init_custom_training_job,
mock_run_custom_training_job,
):

create_training_pipeline_custom_job_sample.create_training_pipeline_custom_job_sample(
project=constants.PROJECT,
display_name=constants.DISPLAY_NAME,
args=constants.ARGS,
script_path=constants.SCRIPT_PATH,
container_uri=constants.CONTAINER_URI,
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_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(
model_display_name=constants.DISPLAY_NAME_2,
args=constants.ARGS,
training_fraction_split=constants.TRAINING_FRACTION_SPLIT,
validation_fraction_split=constants.VALIDATION_FRACTION_SPLIT,
test_fraction_split=constants.TEST_FRACTION_SPLIT,
sync=True,
)
@@ -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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comments here as above.

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]