From f318768d3ad3273d05cddb8c1c8c918f55578a19 Mon Sep 17 00:00:00 2001 From: nnegrey Date: Thu, 5 Mar 2020 16:01:58 -0700 Subject: [PATCH 1/5] automl: move video object tracking samples out of branch --- .../video_object_tracking_create_dataset.py | 41 +++++++++++++++ ...deo_object_tracking_create_dataset_test.py | 50 +++++++++++++++++++ .../video_object_tracking_create_model.py | 42 ++++++++++++++++ ...video_object_tracking_create_model_test.py | 48 ++++++++++++++++++ 4 files changed, 181 insertions(+) create mode 100644 automl/beta/video_object_tracking_create_dataset.py create mode 100644 automl/beta/video_object_tracking_create_dataset_test.py create mode 100644 automl/beta/video_object_tracking_create_model.py create mode 100644 automl/beta/video_object_tracking_create_model_test.py diff --git a/automl/beta/video_object_tracking_create_dataset.py b/automl/beta/video_object_tracking_create_dataset.py new file mode 100644 index 000000000000..06a06b29775b --- /dev/null +++ b/automl/beta/video_object_tracking_create_dataset.py @@ -0,0 +1,41 @@ +# Copyright 2020 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 +# +# http://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. + +# [START automl_video_object_tracking_create_dataset_beta] +from google.cloud import automl_v1beta1 as automl + + +def create_dataset( + project_id="YOUR_PROJECT_ID", display_name="your_datasets_display_name" +): + """Create a automl video object tracking dataset.""" + client = automl.AutoMlClient() + + # A resource that represents Google Cloud Platform location. + project_location = client.location_path(project_id, "us-central1") + metadata = automl.types.VideoObjectTrackingDatasetMetadata() + dataset = automl.types.Dataset( + display_name=display_name, + video_object_tracking_dataset_metadata=metadata, + ) + + # Create a dataset with the dataset metadata in the region. + response = client.create_dataset(project_location, dataset) + + created_dataset = response.result() + + # Display the dataset information + print("Dataset name: {}".format(created_dataset.name)) + print("Dataset id: {}".format(created_dataset.name.split("/")[-1])) +# [END automl_video_object_tracking_create_dataset_beta] diff --git a/automl/beta/video_object_tracking_create_dataset_test.py b/automl/beta/video_object_tracking_create_dataset_test.py new file mode 100644 index 000000000000..eb67717364ca --- /dev/null +++ b/automl/beta/video_object_tracking_create_dataset_test.py @@ -0,0 +1,50 @@ +# Copyright 2020 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 +# +# http://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 os +import uuid + +from google.cloud import automl_v1beta1 as automl +import pytest + +import video_object_tracking_create_dataset + +PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] +pytest.DATASET_ID = None + + +@pytest.fixture(scope="function", autouse=True) +def teardown(): + yield + + # Delete the created dataset + client = automl.AutoMlClient() + dataset_full_id = client.dataset_path( + PROJECT_ID, "us-central1", pytest.DATASET_ID + ) + response = client.delete_dataset(dataset_full_id) + response.result() + + +def test_video_classification_create_dataset(capsys): + # create dataset + dataset_name = "test_" + uuid.uuid4() + video_object_tracking_create_dataset.create_dataset( + PROJECT_ID, dataset_name + ) + out, _ = capsys.readouterr() + assert "Dataset id: " in out + + # Get the dataset id for deletion + pytest.DATASET_ID = out.splitlines()[1].split()[2] diff --git a/automl/beta/video_object_tracking_create_model.py b/automl/beta/video_object_tracking_create_model.py new file mode 100644 index 000000000000..5ff8be9873ec --- /dev/null +++ b/automl/beta/video_object_tracking_create_model.py @@ -0,0 +1,42 @@ +# Copyright 2020 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 +# +# http://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. + +# [START automl_video_object_tracking_create_model_beta] +from google.cloud import automl_v1beta1 as automl + + +def create_model( + project_id="YOUR_PROJECT_ID", + dataset_id="YOUR_DATASET_ID", + display_name="your_models_display_name", +): + """Create a automl video classification model.""" + client = automl.AutoMlClient() + + # A resource that represents Google Cloud Platform loacation. + project_location = client.location_path(project_id, "us-central1") + # Leave model unset to use the default base model provided by Google + metadata = automl.types.VideoObjectTrackingModelMetadata() + model = automl.types.Model( + display_name=display_name, + dataset_id=dataset_id, + video_object_tracking_model_metadata=metadata, + ) + + # Create a model with the model metadata in the region. + response = client.create_model(project_location, model) + + print("Training operation name: {}".format(response.operation.name)) + print("Training started...") +# [END automl_video_object_tracking_create_model_beta] diff --git a/automl/beta/video_object_tracking_create_model_test.py b/automl/beta/video_object_tracking_create_model_test.py new file mode 100644 index 000000000000..69da08d0978c --- /dev/null +++ b/automl/beta/video_object_tracking_create_model_test.py @@ -0,0 +1,48 @@ +# Copyright 2020 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 +# +# http://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 os +import uuid + +from google.cloud import automl_v1beta1 as automl +import pytest + +import video_object_tracking_create_model + +PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] +DATASET_ID = "VOT2823376535338090496" +pytest.OPERATION_ID = None + + +@pytest.fixture(scope="function", autouse=True) +def teardown(): + yield + + # Cancel the training operation + client = automl.AutoMlClient() + client.transport._operations_client.cancel_operation(pytest.OPERATION_ID) + + +def test_video_classification_create_model(capsys): + model_name = "test_" + uuid.uuid4() + video_object_tracking_create_model.create_model( + PROJECT_ID, DATASET_ID, model_name + ) + out, _ = capsys.readouterr() + assert "Training started" in out + + # Cancel the operation + pytest.OPERATION_ID = out.split("Training operation name: ")[1].split( + "\n" + )[0] From bace2a1e07eada6172bbb903b08ae230f0bcc0dd Mon Sep 17 00:00:00 2001 From: nnegrey Date: Thu, 5 Mar 2020 16:14:56 -0700 Subject: [PATCH 2/5] fix uuid and create test --- automl/beta/video_object_tracking_create_dataset.py | 5 +---- automl/beta/video_object_tracking_create_dataset_test.py | 2 +- automl/beta/video_object_tracking_create_model_test.py | 2 +- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/automl/beta/video_object_tracking_create_dataset.py b/automl/beta/video_object_tracking_create_dataset.py index 06a06b29775b..2a651d0d4064 100644 --- a/automl/beta/video_object_tracking_create_dataset.py +++ b/automl/beta/video_object_tracking_create_dataset.py @@ -31,10 +31,7 @@ def create_dataset( ) # Create a dataset with the dataset metadata in the region. - response = client.create_dataset(project_location, dataset) - - created_dataset = response.result() - + created_dataset = client.create_dataset(project_location, dataset) # Display the dataset information print("Dataset name: {}".format(created_dataset.name)) print("Dataset id: {}".format(created_dataset.name.split("/")[-1])) diff --git a/automl/beta/video_object_tracking_create_dataset_test.py b/automl/beta/video_object_tracking_create_dataset_test.py index eb67717364ca..91e28d58c2ea 100644 --- a/automl/beta/video_object_tracking_create_dataset_test.py +++ b/automl/beta/video_object_tracking_create_dataset_test.py @@ -39,7 +39,7 @@ def teardown(): def test_video_classification_create_dataset(capsys): # create dataset - dataset_name = "test_" + uuid.uuid4() + dataset_name = "test_{}".format(uuid.uuid4()).replace("-", "")[:32] video_object_tracking_create_dataset.create_dataset( PROJECT_ID, dataset_name ) diff --git a/automl/beta/video_object_tracking_create_model_test.py b/automl/beta/video_object_tracking_create_model_test.py index 69da08d0978c..20051c659006 100644 --- a/automl/beta/video_object_tracking_create_model_test.py +++ b/automl/beta/video_object_tracking_create_model_test.py @@ -35,7 +35,7 @@ def teardown(): def test_video_classification_create_model(capsys): - model_name = "test_" + uuid.uuid4() + model_name = "test_{}".format(uuid.uuid4()).replace("-", "")[:32] video_object_tracking_create_model.create_model( PROJECT_ID, DATASET_ID, model_name ) From b639cfd10edea108a5d4d9413e728b4222d8a2fa Mon Sep 17 00:00:00 2001 From: nnegrey Date: Thu, 5 Mar 2020 16:22:30 -0700 Subject: [PATCH 3/5] fix project --- automl/beta/video_object_tracking_create_model_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/automl/beta/video_object_tracking_create_model_test.py b/automl/beta/video_object_tracking_create_model_test.py index 20051c659006..740ef0c1d8a7 100644 --- a/automl/beta/video_object_tracking_create_model_test.py +++ b/automl/beta/video_object_tracking_create_model_test.py @@ -20,7 +20,7 @@ import video_object_tracking_create_model -PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] +PROJECT_ID = os.environ["GCLOUD_PROJECT"] DATASET_ID = "VOT2823376535338090496" pytest.OPERATION_ID = None From 2edf3e9075468eeaaeab91e91ca24987485b02d0 Mon Sep 17 00:00:00 2001 From: nnegrey Date: Fri, 6 Mar 2020 10:48:13 -0700 Subject: [PATCH 4/5] use global var for testing --- automl/beta/video_object_tracking_create_dataset_test.py | 7 ++++--- automl/beta/video_object_tracking_create_model_test.py | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/automl/beta/video_object_tracking_create_dataset_test.py b/automl/beta/video_object_tracking_create_dataset_test.py index 91e28d58c2ea..96957f71013b 100644 --- a/automl/beta/video_object_tracking_create_dataset_test.py +++ b/automl/beta/video_object_tracking_create_dataset_test.py @@ -21,7 +21,7 @@ import video_object_tracking_create_dataset PROJECT_ID = os.environ["AUTOML_PROJECT_ID"] -pytest.DATASET_ID = None +DATASET_ID = None @pytest.fixture(scope="function", autouse=True) @@ -31,7 +31,7 @@ def teardown(): # Delete the created dataset client = automl.AutoMlClient() dataset_full_id = client.dataset_path( - PROJECT_ID, "us-central1", pytest.DATASET_ID + PROJECT_ID, "us-central1", DATASET_ID ) response = client.delete_dataset(dataset_full_id) response.result() @@ -47,4 +47,5 @@ def test_video_classification_create_dataset(capsys): assert "Dataset id: " in out # Get the dataset id for deletion - pytest.DATASET_ID = out.splitlines()[1].split()[2] + global DATASET_ID + DATASET_ID = out.splitlines()[1].split()[2] diff --git a/automl/beta/video_object_tracking_create_model_test.py b/automl/beta/video_object_tracking_create_model_test.py index 740ef0c1d8a7..37693cce2320 100644 --- a/automl/beta/video_object_tracking_create_model_test.py +++ b/automl/beta/video_object_tracking_create_model_test.py @@ -22,7 +22,7 @@ PROJECT_ID = os.environ["GCLOUD_PROJECT"] DATASET_ID = "VOT2823376535338090496" -pytest.OPERATION_ID = None +OPERATION_ID = None @pytest.fixture(scope="function", autouse=True) @@ -31,7 +31,7 @@ def teardown(): # Cancel the training operation client = automl.AutoMlClient() - client.transport._operations_client.cancel_operation(pytest.OPERATION_ID) + client.transport._operations_client.cancel_operation(OPERATION_ID) def test_video_classification_create_model(capsys): @@ -43,6 +43,7 @@ def test_video_classification_create_model(capsys): assert "Training started" in out # Cancel the operation - pytest.OPERATION_ID = out.split("Training operation name: ")[1].split( + global OPERATION_ID + OPERATION_ID = out.split("Training operation name: ")[1].split( "\n" )[0] From 2833dd4e2e9e5be6a053b2dc9be20281d1560529 Mon Sep 17 00:00:00 2001 From: Noah Negrey Date: Fri, 6 Mar 2020 10:50:35 -0700 Subject: [PATCH 5/5] Update video_object_tracking_create_model_test.py --- automl/beta/video_object_tracking_create_model_test.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/automl/beta/video_object_tracking_create_model_test.py b/automl/beta/video_object_tracking_create_model_test.py index 37693cce2320..edfea3e4d13e 100644 --- a/automl/beta/video_object_tracking_create_model_test.py +++ b/automl/beta/video_object_tracking_create_model_test.py @@ -44,6 +44,4 @@ def test_video_classification_create_model(capsys): # Cancel the operation global OPERATION_ID - OPERATION_ID = out.split("Training operation name: ")[1].split( - "\n" - )[0] + OPERATION_ID = out.split("Training operation name: ")[1].split("\n")[0]