Skip to content

Commit 1230dc6

Browse files
authored
feat: Add initial Model Builder SDK samples (#265)
* Add inital batch of MBSDK samples and sample tests * Drop unneeded prints * Update to use constants, address reviewers
1 parent ddd54af commit 1230dc6

10 files changed

+526
-0
lines changed

samples/model-builder/conftest.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# Copyright 2021 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import pytest
16+
from unittest import mock
17+
from unittest.mock import MagicMock, patch
18+
19+
from google.cloud import aiplatform
20+
21+
22+
@pytest.fixture
23+
def mock_sdk_init():
24+
with patch.object(aiplatform, "init") as mock:
25+
yield mock
26+
27+
28+
# ----------------------------------------------------------------------------
29+
# Dataset Fixtures
30+
# ----------------------------------------------------------------------------
31+
32+
33+
@pytest.fixture
34+
def mock_dataset():
35+
mock = MagicMock(aiplatform.datasets.Dataset)
36+
yield mock
37+
38+
39+
@pytest.fixture
40+
def mock_new_dataset(mock_dataset):
41+
with patch.object(aiplatform.datasets.Dataset, "__new__") as mock_new_dataset:
42+
mock_new_dataset.return_value = mock_dataset
43+
yield mock_new_dataset
44+
45+
46+
@pytest.fixture
47+
def mock_init_dataset(mock_new_dataset):
48+
with patch.object(aiplatform.datasets.Dataset, "__init__") as mock_init_dataset:
49+
mock_init_dataset.return_value = None
50+
yield mock_init_dataset
51+
52+
53+
@pytest.fixture
54+
def mock_create_dataset():
55+
with patch.object(aiplatform.datasets.Dataset, "create") as mock:
56+
mock.return_value = MagicMock(aiplatform.Dataset)
57+
yield mock
58+
59+
60+
@pytest.fixture
61+
def mock_create_image_dataset():
62+
with patch.object(aiplatform.datasets.ImageDataset, "create") as mock:
63+
mock.return_value = MagicMock(aiplatform.Dataset)
64+
yield mock
65+
66+
67+
# ----------------------------------------------------------------------------
68+
# TrainingJob Fixtures
69+
# ----------------------------------------------------------------------------
70+
71+
72+
@pytest.fixture
73+
def mock_init_automl_image_training_job():
74+
with patch.object(
75+
aiplatform.training_jobs.AutoMLImageTrainingJob, "__init__"
76+
) as mock:
77+
mock.return_value = None
78+
yield mock
79+
80+
81+
@pytest.fixture
82+
def mock_run_automl_image_training_job():
83+
with patch.object(aiplatform.training_jobs.AutoMLImageTrainingJob, "run") as mock:
84+
yield mock
85+
86+
87+
# ----------------------------------------------------------------------------
88+
# Model Fixtures
89+
# ----------------------------------------------------------------------------
90+
91+
92+
@pytest.fixture
93+
def mock_init_model():
94+
with patch.object(aiplatform.models.Model, "__init__") as mock:
95+
mock.return_value = None
96+
yield mock
97+
98+
99+
@pytest.fixture
100+
def mock_batch_predict_model():
101+
with patch.object(aiplatform.models.Model, "batch_predict") as mock:
102+
yield mock
103+
104+
105+
# ----------------------------------------------------------------------------
106+
# Job Fixtures
107+
# ----------------------------------------------------------------------------
108+
109+
110+
@pytest.fixture
111+
def mock_create_batch_prediction_job():
112+
with patch.object(aiplatform.jobs.BatchPredictionJob, "create") as mock:
113+
yield mock
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright 2021 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from typing import List, Union
16+
17+
from google.cloud import aiplatform
18+
19+
# [START aiplatform_sdk_create_and_import_dataset_image_sample]
20+
def create_and_import_dataset_image_sample(
21+
project: str,
22+
location: str,
23+
display_name: str,
24+
src_uris: Union[str, List[str]],
25+
sync: bool = True,
26+
):
27+
aiplatform.init(project=project, location=location)
28+
29+
ds = aiplatform.ImageDataset.create(
30+
display_name=display_name,
31+
gcs_source=src_uris,
32+
import_schema_uri=aiplatform.schema.dataset.ioformat.image.single_label_classification,
33+
sync=sync,
34+
)
35+
36+
ds.wait()
37+
38+
print(ds.display_name)
39+
print(ds.resource_name)
40+
return ds
41+
42+
43+
# [END aiplatform_sdk_create_and_import_dataset_image_sample]
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright 2021 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
import test_constants as constants
17+
import create_and_import_dataset_image_sample
18+
19+
from google.cloud.aiplatform import schema
20+
21+
22+
def test_create_and_import_dataset_image_sample(
23+
mock_sdk_init, mock_create_image_dataset
24+
):
25+
26+
create_and_import_dataset_image_sample.create_and_import_dataset_image_sample(
27+
project=constants.PROJECT,
28+
location=constants.LOCATION,
29+
src_uris=constants.GCS_SOURCES,
30+
display_name=constants.DISPLAY_NAME,
31+
)
32+
33+
mock_sdk_init.assert_called_once_with(
34+
project=constants.PROJECT, location=constants.LOCATION
35+
)
36+
mock_create_image_dataset.assert_called_once_with(
37+
display_name=constants.DISPLAY_NAME,
38+
gcs_source=constants.GCS_SOURCES,
39+
import_schema_uri=schema.dataset.ioformat.image.single_label_classification,
40+
sync=True,
41+
)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Copyright 2021 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from typing import Optional, Sequence, Union
16+
17+
from google.cloud import aiplatform
18+
19+
# [START aiplatform_sdk_create_batch_prediction_job_sample]
20+
def create_batch_prediction_job_sample(
21+
project: str,
22+
location: str,
23+
model_resource_name: str,
24+
job_display_name: str,
25+
gcs_source: Union[str, Sequence[str]],
26+
gcs_destination: str,
27+
sync: bool = True,
28+
):
29+
aiplatform.init(project=project, location=location)
30+
31+
my_model = aiplatform.Model(model_resource_name)
32+
33+
batch_prediction_job = my_model.batch_predict(
34+
job_display_name=job_display_name,
35+
gcs_source=gcs_source,
36+
gcs_destination_prefix=gcs_destination,
37+
sync=sync,
38+
)
39+
40+
batch_prediction_job.wait()
41+
42+
print(batch_prediction_job.display_name)
43+
print(batch_prediction_job.resource_name)
44+
print(batch_prediction_job.state)
45+
return batch_prediction_job
46+
47+
48+
# [END aiplatform_sdk_create_batch_prediction_job_sample]
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright 2021 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
import test_constants as constants
17+
import create_batch_prediction_job_sample
18+
19+
20+
def test_create_batch_prediction_job_sample(
21+
mock_sdk_init, mock_init_model, mock_batch_predict_model
22+
):
23+
24+
create_batch_prediction_job_sample.create_batch_prediction_job_sample(
25+
project=constants.PROJECT,
26+
location=constants.LOCATION,
27+
model_resource_name=constants.MODEL_NAME,
28+
job_display_name=constants.DISPLAY_NAME,
29+
gcs_source=constants.GCS_SOURCES,
30+
gcs_destination=constants.GCS_DESTINATION,
31+
)
32+
33+
mock_sdk_init.assert_called_once_with(
34+
project=constants.PROJECT, location=constants.LOCATION
35+
)
36+
mock_init_model.assert_called_once_with(constants.MODEL_NAME)
37+
mock_batch_predict_model.assert_called_once_with(
38+
job_display_name=constants.DISPLAY_NAME,
39+
gcs_source=constants.GCS_SOURCES,
40+
gcs_destination_prefix=constants.GCS_DESTINATION,
41+
sync=True,
42+
)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Copyright 2021 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from google.cloud import aiplatform
16+
17+
# [START aiplatform_sdk_create_training_pipeline_image_classification_sample]
18+
def create_training_pipeline_image_classification_sample(
19+
project: str,
20+
display_name: str,
21+
dataset_id: int,
22+
location: str = "us-central1",
23+
model_display_name: str = None,
24+
training_fraction_split: float = 0.8,
25+
validation_fraction_split: float = 0.1,
26+
test_fraction_split: float = 0.1,
27+
budget_milli_node_hours: int = 8000,
28+
disable_early_stopping: bool = False,
29+
sync: bool = True,
30+
):
31+
aiplatform.init(project=project, location=location)
32+
33+
job = aiplatform.AutoMLImageTrainingJob(display_name=display_name)
34+
35+
my_image_ds = aiplatform.Dataset(dataset_id)
36+
37+
model = job.run(
38+
dataset=my_image_ds,
39+
model_display_name=model_display_name,
40+
training_fraction_split=training_fraction_split,
41+
validation_fraction_split=validation_fraction_split,
42+
test_fraction_split=test_fraction_split,
43+
budget_milli_node_hours=budget_milli_node_hours,
44+
disable_early_stopping=disable_early_stopping,
45+
sync=sync,
46+
)
47+
48+
model.wait()
49+
50+
print(model.display_name)
51+
print(model.resource_name)
52+
print(model.uri)
53+
return model
54+
55+
56+
# [END aiplatform_sdk_create_training_pipeline_image_classification_sample]

0 commit comments

Comments
 (0)