-
Notifications
You must be signed in to change notification settings - Fork 348
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
docs(samples): add AutoML image classification sample #923
Merged
Merged
Changes from 7 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
8fc413d
Create predict_image_classification_sample.py
andrewferlitsch 6100f76
feat: new sample and test
andrewferlitsch ed92732
lint: fix wsp
andrewferlitsch 787b460
lint: import order
andrewferlitsch 991b878
lint: fix import
andrewferlitsch 5752cd9
tags: fixed start tag
andrewferlitsch dda6fb7
Merge branch 'main' into issue_202042079
andrewferlitsch 37b1c78
samples: change tabular to image in sample function name.
andrewferlitsch e38c2b4
Merge branch 'main' into issue_202042079
andrewferlitsch 102b4c5
samples: replace TF version of reading in binary file with Python ver…
andrewferlitsch bca85e9
samples: delete tf import, move other imports within region tags
andrewferlitsch b8cbfa1
Update predict_image_classification_sample.py
andrewferlitsch 14428d4
samples: move imports for lint
andrewferlitsch 9d6a9bb
Merge branch 'main' into issue_202042079
kweinmeister 3a22b8a
Merge branch 'main' into issue_202042079
kweinmeister 5ba43af
Merge branch 'main' into issue_202042079
kweinmeister 291695e
Update predict_image_classification_sample.py
andrewferlitsch 2c8186c
Update predict_image_classification_sample_test.py
andrewferlitsch a9fdbf9
Merge branch 'main' into issue_202042079
andrewferlitsch 7656a4c
Merge branch 'main' into issue_202042079
rosiezou e97ca2b
Merge branch 'main' into issue_202042079
nayaknishant cceff6b
Merge branch 'main' into issue_202042079
rosiezou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
samples/model-builder/predict_image_classification_sample.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
|
||
|
||
import base64 | ||
|
||
from typing import List | ||
|
||
from google.cloud import aiplatform | ||
|
||
import tensorflow as tf | ||
|
||
|
||
# [START aiplatform_sdk_predict_image_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] |
33 changes: 33 additions & 0 deletions
33
samples/model-builder/predict_image_classification_sample_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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,) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this read
predict_image_classification_sample
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, that looks like a typo.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I fixed it.