From 8fc413d2e96d368dc1f01b7dd71157a54ba901e5 Mon Sep 17 00:00:00 2001 From: Andrew Ferlitsch Date: Tue, 21 Dec 2021 11:40:46 -0800 Subject: [PATCH 01/13] Create predict_image_classification_sample.py --- .../predict_image_classification_sample.py | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 samples/model-builder/predict_image_classification_sample.py diff --git a/samples/model-builder/predict_image_classification_sample.py b/samples/model-builder/predict_image_classification_sample.py new file mode 100644 index 0000000000..771acf3c93 --- /dev/null +++ b/samples/model-builder/predict_image_classification_sample.py @@ -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 typing import Dict, List + +from google.cloud import aiplatform + +import base64 + +import tensorflow as tf + + +# [START aiplatform_sdk_predict_images_classification_sample] +def predict_tabular_classification_sample( + project: str, + location: str, + endpoint_name: str, + images: List, +): + ''' + Args + project: Your project ID or project number. + location: Region where Endpoint is located. For example, 'us-central1'. + endpoint_name: A fully qualified endpoint name or endpoint ID. Example: "projects/123/locations/us-central1/endpoints/456" or + "456" when project and location are initialized or passed. + images: A list of one or more images to return a prediction for. + ''' + aiplatform.init(project=project, location=location) + + endpoint = aiplatform.Endpoint(endpoint_name) + + instances = [] + for image in images: + with tf.io.gfile.GFile(image, "rb") as f: + content = f.read() + instances.append({"content": base64.b64encode(content).decode("utf-8")}) + + response = endpoint.predict(instances=instances) + + for prediction_ in response.predictions: + print(prediction_) + + +# [END aiplatform_sdk_predict_image_classification_sample] From 6100f764b6acf1ce70484525015244afd94f04fe Mon Sep 17 00:00:00 2001 From: Andrew Ferlitsch Date: Tue, 21 Dec 2021 11:44:40 -0800 Subject: [PATCH 02/13] feat: new sample and test --- ...redict_image_classification_sample_test.py | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 samples/model-builder/predict_image_classification_sample_test.py diff --git a/samples/model-builder/predict_image_classification_sample_test.py b/samples/model-builder/predict_image_classification_sample_test.py new file mode 100644 index 0000000000..c031cadedb --- /dev/null +++ b/samples/model-builder/predict_image_classification_sample_test.py @@ -0,0 +1,33 @@ +# 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 predict_image_classification_sample +import test_constants as constants + + +def test_predict_image_classification_sample(mock_sdk_init, mock_get_endpoint): + + predict_image_classification_sample.predict_image_classification_sample( + project=constants.PROJECT, + location=constants.LOCATION, + endpoint_name=constants.ENDPOINT_NAME, + images=[] + ) + + mock_sdk_init.assert_called_once_with( + project=constants.PROJECT, location=constants.LOCATION + ) + + mock_get_endpoint.assert_called_once_with(constants.ENDPOINT_NAME,) From ed92732664b34e840e572c9e8ced57b616a02cff Mon Sep 17 00:00:00 2001 From: Andrew Ferlitsch Date: Tue, 21 Dec 2021 12:23:42 -0800 Subject: [PATCH 03/13] lint: fix wsp --- .../predict_image_classification_sample.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/samples/model-builder/predict_image_classification_sample.py b/samples/model-builder/predict_image_classification_sample.py index 771acf3c93..563b3de78c 100644 --- a/samples/model-builder/predict_image_classification_sample.py +++ b/samples/model-builder/predict_image_classification_sample.py @@ -15,10 +15,10 @@ from typing import Dict, List -from google.cloud import aiplatform - import base64 +from google.cloud import aiplatform + import tensorflow as tf @@ -40,12 +40,12 @@ def predict_tabular_classification_sample( aiplatform.init(project=project, location=location) endpoint = aiplatform.Endpoint(endpoint_name) - + instances = [] for image in images: - with tf.io.gfile.GFile(image, "rb") as f: - content = f.read() - instances.append({"content": base64.b64encode(content).decode("utf-8")}) + with tf.io.gfile.GFile(image, "rb") as f: + content = f.read() + instances.append({"content": base64.b64encode(content).decode("utf-8")}) response = endpoint.predict(instances=instances) From 787b4605764bde031ebdb11d529dbfe154212853 Mon Sep 17 00:00:00 2001 From: Andrew Ferlitsch Date: Tue, 21 Dec 2021 12:29:23 -0800 Subject: [PATCH 04/13] lint: import order --- samples/model-builder/predict_image_classification_sample.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/model-builder/predict_image_classification_sample.py b/samples/model-builder/predict_image_classification_sample.py index 563b3de78c..e46c0b53a3 100644 --- a/samples/model-builder/predict_image_classification_sample.py +++ b/samples/model-builder/predict_image_classification_sample.py @@ -13,10 +13,10 @@ # limitations under the License. -from typing import Dict, List - import base64 +from typing import Dict, List + from google.cloud import aiplatform import tensorflow as tf From 991b878494cdfe97008b19aceb8cb2a953d2ddc3 Mon Sep 17 00:00:00 2001 From: Andrew Ferlitsch Date: Tue, 21 Dec 2021 12:36:23 -0800 Subject: [PATCH 05/13] lint: fix import --- samples/model-builder/predict_image_classification_sample.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/model-builder/predict_image_classification_sample.py b/samples/model-builder/predict_image_classification_sample.py index e46c0b53a3..c39d3f5081 100644 --- a/samples/model-builder/predict_image_classification_sample.py +++ b/samples/model-builder/predict_image_classification_sample.py @@ -15,7 +15,7 @@ import base64 -from typing import Dict, List +from typing import List from google.cloud import aiplatform From 5752cd9a437e3946d1da69589dc8fdd933ad614c Mon Sep 17 00:00:00 2001 From: Andrew Ferlitsch Date: Tue, 21 Dec 2021 12:46:56 -0800 Subject: [PATCH 06/13] tags: fixed start tag --- samples/model-builder/predict_image_classification_sample.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/model-builder/predict_image_classification_sample.py b/samples/model-builder/predict_image_classification_sample.py index c39d3f5081..12f9c71b4d 100644 --- a/samples/model-builder/predict_image_classification_sample.py +++ b/samples/model-builder/predict_image_classification_sample.py @@ -22,7 +22,7 @@ import tensorflow as tf -# [START aiplatform_sdk_predict_images_classification_sample] +# [START aiplatform_sdk_predict_image_classification_sample] def predict_tabular_classification_sample( project: str, location: str, From 37b1c784f8384cb7150a9df2719189e21d876929 Mon Sep 17 00:00:00 2001 From: Andrew Ferlitsch Date: Thu, 17 Mar 2022 13:10:18 -0700 Subject: [PATCH 07/13] samples: change tabular to image in sample function name. --- samples/model-builder/predict_image_classification_sample.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/model-builder/predict_image_classification_sample.py b/samples/model-builder/predict_image_classification_sample.py index 12f9c71b4d..ef64ecde7e 100644 --- a/samples/model-builder/predict_image_classification_sample.py +++ b/samples/model-builder/predict_image_classification_sample.py @@ -23,7 +23,7 @@ # [START aiplatform_sdk_predict_image_classification_sample] -def predict_tabular_classification_sample( +def predict_image_classification_sample( project: str, location: str, endpoint_name: str, From 102b4c51226f387397cc2b6229e7548534d077d4 Mon Sep 17 00:00:00 2001 From: Andrew Ferlitsch Date: Thu, 17 Mar 2022 13:59:22 -0700 Subject: [PATCH 08/13] samples: replace TF version of reading in binary file with Python version --- samples/model-builder/predict_image_classification_sample.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/model-builder/predict_image_classification_sample.py b/samples/model-builder/predict_image_classification_sample.py index ef64ecde7e..3953b38074 100644 --- a/samples/model-builder/predict_image_classification_sample.py +++ b/samples/model-builder/predict_image_classification_sample.py @@ -43,7 +43,7 @@ def predict_image_classification_sample( instances = [] for image in images: - with tf.io.gfile.GFile(image, "rb") as f: + with open(image, "rb") as f: content = f.read() instances.append({"content": base64.b64encode(content).decode("utf-8")}) From bca85e92982086534d0fa37c9267ec6f2b6fd65f Mon Sep 17 00:00:00 2001 From: Andrew Ferlitsch Date: Thu, 17 Mar 2022 14:28:49 -0700 Subject: [PATCH 09/13] samples: delete tf import, move other imports within region tags --- .../model-builder/predict_image_classification_sample.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/samples/model-builder/predict_image_classification_sample.py b/samples/model-builder/predict_image_classification_sample.py index 3953b38074..ebd37f20f5 100644 --- a/samples/model-builder/predict_image_classification_sample.py +++ b/samples/model-builder/predict_image_classification_sample.py @@ -12,17 +12,15 @@ # See the License for the specific language governing permissions and # limitations under the License. +from typing import List -import base64 -from typing import List +# [START aiplatform_sdk_predict_image_classification_sample] +import base64 from google.cloud import aiplatform -import tensorflow as tf - -# [START aiplatform_sdk_predict_image_classification_sample] def predict_image_classification_sample( project: str, location: str, From b8cbfa1af7b886525eb710c261665bc494b5e419 Mon Sep 17 00:00:00 2001 From: Andrew Ferlitsch Date: Thu, 17 Mar 2022 14:57:42 -0700 Subject: [PATCH 10/13] Update predict_image_classification_sample.py --- samples/model-builder/predict_image_classification_sample.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/model-builder/predict_image_classification_sample.py b/samples/model-builder/predict_image_classification_sample.py index ebd37f20f5..42e232abef 100644 --- a/samples/model-builder/predict_image_classification_sample.py +++ b/samples/model-builder/predict_image_classification_sample.py @@ -12,14 +12,14 @@ # See the License for the specific language governing permissions and # limitations under the License. -from typing import List - # [START aiplatform_sdk_predict_image_classification_sample] import base64 from google.cloud import aiplatform +from typing import List + def predict_image_classification_sample( project: str, From 14428d44b590295c9549c19897880cce87c8a9c1 Mon Sep 17 00:00:00 2001 From: Andrew Ferlitsch Date: Thu, 17 Mar 2022 15:19:38 -0700 Subject: [PATCH 11/13] samples: move imports for lint --- samples/model-builder/predict_image_classification_sample.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/model-builder/predict_image_classification_sample.py b/samples/model-builder/predict_image_classification_sample.py index 42e232abef..1bb5a52b55 100644 --- a/samples/model-builder/predict_image_classification_sample.py +++ b/samples/model-builder/predict_image_classification_sample.py @@ -16,10 +16,10 @@ # [START aiplatform_sdk_predict_image_classification_sample] import base64 -from google.cloud import aiplatform - from typing import List +from google.cloud import aiplatform + def predict_image_classification_sample( project: str, From 291695eabf79f9bc27cb82f54daf5bbe1cc5395e Mon Sep 17 00:00:00 2001 From: Andrew Ferlitsch Date: Fri, 1 Jul 2022 07:08:02 -0700 Subject: [PATCH 12/13] Update predict_image_classification_sample.py --- samples/model-builder/predict_image_classification_sample.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/model-builder/predict_image_classification_sample.py b/samples/model-builder/predict_image_classification_sample.py index 1bb5a52b55..4ae05599a5 100644 --- a/samples/model-builder/predict_image_classification_sample.py +++ b/samples/model-builder/predict_image_classification_sample.py @@ -1,4 +1,4 @@ -# Copyright 2021 Google LLC +# Copyright 2022 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. From 2c8186c551970a978e4c6db074a694bc97fb38a5 Mon Sep 17 00:00:00 2001 From: Andrew Ferlitsch Date: Fri, 1 Jul 2022 07:08:30 -0700 Subject: [PATCH 13/13] Update predict_image_classification_sample_test.py --- .../model-builder/predict_image_classification_sample_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/model-builder/predict_image_classification_sample_test.py b/samples/model-builder/predict_image_classification_sample_test.py index c031cadedb..59e926696b 100644 --- a/samples/model-builder/predict_image_classification_sample_test.py +++ b/samples/model-builder/predict_image_classification_sample_test.py @@ -1,4 +1,4 @@ -# Copyright 2021 Google LLC +# Copyright 2022 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License.