Skip to content

Commit cc1a708

Browse files
andrewferlitschvinnysenthilsasha-gitg
authored
feat: Add AutoML vision, Custom training job, and generic prediction samples (#300)
* debug mock issue * new mock * more samples * more samples * add next sample/test * add sample/test * run black * Add new Dataset import mocks, fix MBSDK sample tests * Add license headers, update Endpoint mocks/usage * type updates * sasha comment fixes * fix test errors after review update * fix: type for instances * Lint SDK samples * Fix flake8 import order nits Co-authored-by: Vinny Senthil <vinnysenthil@gmail.com> Co-authored-by: sasha-gitg <44654632+sasha-gitg@users.noreply.github.com>
1 parent 56273f7 commit cc1a708

16 files changed

+599
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
from google.cloud import aiplatform
17+
18+
19+
# [START aiplatform_sdk_automl_image_classification_training_job_sample]
20+
def automl_image_classification_training_job_sample(
21+
project: str, location: str, dataset_id: str, display_name: str,
22+
):
23+
aiplatform.init(project=project, location=location)
24+
25+
dataset = aiplatform.ImageDataset(dataset_id)
26+
27+
job = aiplatform.AutoMLImageTrainingJob(
28+
display_name=display_name,
29+
prediction_type="classification",
30+
multi_label=False,
31+
model_type="CLOUD",
32+
base_model=None,
33+
)
34+
35+
model = job.run(
36+
dataset=dataset,
37+
model_display_name=display_name,
38+
training_fraction_split=0.6,
39+
validation_fraction_split=0.2,
40+
test_fraction_split=0.2,
41+
budget_milli_node_hours=8000,
42+
disable_early_stopping=False,
43+
)
44+
45+
print(model.display_name)
46+
print(model.name)
47+
print(model.resource_name)
48+
print(model.description)
49+
print(model.uri)
50+
51+
return model
52+
53+
54+
# [END aiplatform_sdk_automl_image_classification_training_job_sample]
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+
16+
import automl_image_classification_training_job_sample
17+
import test_constants as constants
18+
19+
20+
def test_automl_image_classification_training_job_sample(
21+
mock_sdk_init,
22+
mock_image_dataset,
23+
mock_get_image_dataset,
24+
mock_get_automl_image_training_job,
25+
mock_run_automl_image_training_job,
26+
):
27+
automl_image_classification_training_job_sample.automl_image_classification_training_job_sample(
28+
project=constants.PROJECT,
29+
location=constants.LOCATION,
30+
dataset_id=constants.DATASET_NAME,
31+
display_name=constants.DISPLAY_NAME,
32+
)
33+
34+
mock_get_image_dataset.assert_called_once_with(constants.DATASET_NAME)
35+
36+
mock_sdk_init.assert_called_once_with(
37+
project=constants.PROJECT, location=constants.LOCATION
38+
)
39+
40+
mock_get_automl_image_training_job.assert_called_once_with(
41+
display_name=constants.DISPLAY_NAME,
42+
base_model=None,
43+
model_type="CLOUD",
44+
multi_label=False,
45+
prediction_type="classification",
46+
)
47+
48+
mock_run_automl_image_training_job.assert_called_once_with(
49+
budget_milli_node_hours=8000,
50+
disable_early_stopping=False,
51+
test_fraction_split=0.2,
52+
training_fraction_split=0.6,
53+
validation_fraction_split=0.2,
54+
model_display_name=constants.DISPLAY_NAME,
55+
dataset=mock_image_dataset,
56+
)

samples/model-builder/conftest.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,18 @@ def mock_create_video_dataset(mock_video_dataset):
124124
"""Mocks for SomeDataset.import_data() """
125125

126126

127+
@pytest.fixture
128+
def mock_import_image_dataset(mock_image_dataset):
129+
with patch.object(mock_image_dataset, "import_data") as mock:
130+
yield mock
131+
132+
133+
@pytest.fixture
134+
def mock_import_tabular_dataset(mock_tabular_dataset):
135+
with patch.object(mock_tabular_dataset, "import_data") as mock:
136+
yield mock
137+
138+
127139
@pytest.fixture
128140
def mock_import_text_dataset(mock_text_dataset):
129141
with patch.object(mock_text_dataset, "import_data") as mock:
@@ -327,6 +339,13 @@ def mock_get_endpoint(mock_endpoint):
327339
yield mock_get_endpoint
328340

329341

342+
@pytest.fixture
343+
def mock_endpoint_predict(mock_endpoint):
344+
with patch.object(mock_endpoint, "predict") as mock:
345+
mock.return_value = []
346+
yield mock
347+
348+
330349
@pytest.fixture
331350
def mock_endpoint_explain(mock_endpoint):
332351
with patch.object(mock_endpoint, "explain") as mock_endpoint_explain:
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
from google.cloud import aiplatform
17+
18+
19+
# [START aiplatform_sdk_custom_training_job_sample]
20+
def custom_training_job_sample(
21+
project: str,
22+
location: str,
23+
bucket: str,
24+
display_name: str,
25+
script_path: str,
26+
script_args: str,
27+
container_uri: str,
28+
model_serving_container_image_uri: str,
29+
requirements: str,
30+
replica_count: int,
31+
):
32+
aiplatform.init(project=project, location=location, staging_bucket=bucket)
33+
34+
job = aiplatform.CustomTrainingJob(
35+
display_name=display_name,
36+
script_path=script_path,
37+
container_uri=container_uri,
38+
requirements=requirements,
39+
model_serving_container_image_uri=model_serving_container_image_uri,
40+
)
41+
42+
model = job.run(
43+
args=script_args, replica_count=replica_count, model_display_name=display_name
44+
)
45+
46+
return model
47+
48+
49+
# [END aiplatform_sdk_custom_training_job_sample]
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 custom_training_job_sample
17+
import test_constants as constants
18+
19+
20+
def test_custom_training_job_sample(
21+
mock_sdk_init, mock_get_custom_training_job, mock_run_custom_training_job
22+
):
23+
custom_training_job_sample.custom_training_job_sample(
24+
project=constants.PROJECT,
25+
location=constants.LOCATION,
26+
bucket=constants.STAGING_BUCKET,
27+
display_name=constants.DISPLAY_NAME,
28+
script_path=constants.PYTHON_PACKAGE,
29+
script_args=constants.PYTHON_PACKAGE_CMDARGS,
30+
container_uri=constants.TRAIN_IMAGE,
31+
model_serving_container_image_uri=constants.DEPLOY_IMAGE,
32+
requirements=[],
33+
replica_count=1,
34+
)
35+
36+
mock_sdk_init.assert_called_once_with(
37+
project=constants.PROJECT,
38+
location=constants.LOCATION,
39+
staging_bucket=constants.STAGING_BUCKET,
40+
)
41+
42+
mock_get_custom_training_job.assert_called_once_with(
43+
display_name=constants.DISPLAY_NAME,
44+
container_uri=constants.TRAIN_IMAGE,
45+
model_serving_container_image_uri=constants.DEPLOY_IMAGE,
46+
requirements=[],
47+
script_path=constants.PYTHON_PACKAGE,
48+
)
49+
50+
mock_run_custom_training_job.assert_called_once()
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
from google.cloud import aiplatform
17+
18+
19+
# [START aiplatform_sdk_endpoint_predict_sample]
20+
def endpoint_predict_sample(
21+
project: str, location: str, instances: list, endpoint: str
22+
):
23+
aiplatform.init(project=project, location=location)
24+
25+
endpoint = aiplatform.Endpoint(endpoint)
26+
27+
prediction = endpoint.predict(instances=instances)
28+
print(prediction)
29+
return prediction
30+
31+
32+
# [END aiplatform_sdk_endpoint_predict_sample]
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 endpoint_predict_sample
17+
import test_constants as constants
18+
19+
20+
def test_endpoint_predict_sample(
21+
mock_sdk_init, mock_endpoint_predict, mock_get_endpoint
22+
):
23+
24+
endpoint_predict_sample.endpoint_predict_sample(
25+
project=constants.PROJECT,
26+
location=constants.LOCATION,
27+
instances=[],
28+
endpoint=constants.ENDPOINT_NAME,
29+
)
30+
31+
mock_sdk_init.assert_called_once_with(
32+
project=constants.PROJECT, location=constants.LOCATION
33+
)
34+
35+
mock_get_endpoint.assert_called_once_with(constants.ENDPOINT_NAME)
36+
37+
mock_endpoint_predict.assert_called_once_with(instances=[])
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
from google.cloud import aiplatform
17+
18+
19+
# [START aiplatform_sdk_image_dataset_create_classification_sample]
20+
def image_dataset_create_classification_sample(
21+
project: str, location: str, display_name: str, src_uris: list
22+
):
23+
aiplatform.init(project=project, location=location)
24+
25+
ds = aiplatform.ImageDataset.create(
26+
display_name=display_name,
27+
gcs_source=src_uris,
28+
import_schema_uri=aiplatform.schema.dataset.ioformat.image.single_label_classification,
29+
)
30+
31+
print(ds.display_name)
32+
print(ds.name)
33+
print(ds.resource_name)
34+
print(ds.metadata_schema_uri)
35+
return ds
36+
37+
38+
# [END aiplatform_sdk_image_dataset_create_classification_sample]
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
from google.cloud.aiplatform import schema
17+
18+
import image_dataset_create_classification_sample
19+
20+
import test_constants as constants
21+
22+
23+
def test_image_dataset_create_classification_sample(
24+
mock_sdk_init, mock_create_image_dataset
25+
):
26+
image_dataset_create_classification_sample.image_dataset_create_classification_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+
)

0 commit comments

Comments
 (0)