mirror of
https://github.com/GoogleCloudPlatform/vertex-ai-samples.git
synced 2026-09-26 14:42:04 +00:00
* Migrate gsutil usage to gcloud storage
* changes for 4301
* linter changes
* Revert "linter changes"
This reverts commit 6665133b2c.
* Apply automated linter fixes
* Update training-multi-class-classification-model-for-ads-targeting-usecase.ipynb
* Update training-multi-class-classification-model-for-ads-targeting-usecase.ipynb
* removed model_garden folder changes
* Update model_garden_mediapipe_object_detection.ipynb
---------
Co-authored-by: gurusai-voleti <gvoleti@google.com>
29 KiB
29 KiB
In [ ]:
# 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.
# 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.In [ ]:
import os
# Install the packages
! pip3 install --upgrade --quiet google-cloud-aiplatform \
google-cloud-storage
if os.getenv("IS_TESTING"):
! pip3 install --upgrade --quiet google-api-core==2.10 In [ ]:
# Automatically restart kernel after installs so that your environment can access the new packages
# import IPython
# app = IPython.Application.instance()
# app.kernel.do_shutdown(True)In [ ]:
PROJECT_ID = "[your-project-id]" # @param {type:"string"}
# Set the project id
! gcloud config set project {PROJECT_ID}In [ ]:
REGION = "us-central1" # @param {type: "string"}In [ ]:
EMAIL = "[your-email-address]" # @param {type: "string"}
if os.getenv("IS_TESTING"):
EMAIL = "noreply@google.com"In [ ]:
if EMAIL == "[your-email-address]":
shell_output = ! gcloud auth list 2>/dev/null
EMAIL = shell_output[2].replace("*", "").strip()
print(EMAIL)In [ ]:
# ! gcloud auth loginIn [ ]:
# from google.colab import auth
# auth.authenticate_user()In [ ]:
BUCKET_URI = f"gs://your-bucket-name-{PROJECT_ID}-unique" # @param {type:"string"}In [ ]:
! gcloud storage buckets create --location=$REGION $BUCKET_URIIn [ ]:
import os
import time
import google.cloud.aiplatform as aip
from google.cloud import storage
from google.cloud.aiplatform import gapic
from google.protobuf.json_format import ParseDict
from google.protobuf.struct_pb2 import ValueIn [ ]:
aip.init(project=PROJECT_ID, location=REGION)In [ ]:
# API Endpoint
API_ENDPOINT = "{}-aiplatform.googleapis.com".format(REGION)
# Vertex AI location root path for your dataset, model and endpoint resources
PARENT = "projects/" + PROJECT_ID + "/locations/" + REGIONIn [ ]:
# Image labeling task
LABELING_SCHEMA_IMAGE = "gs://google-cloud-aiplatform/schema/datalabelingjob/inputs/image_classification_1.0.0.yaml"In [ ]:
# client options same for all services
client_options = {"api_endpoint": API_ENDPOINT}
clients = {}
clients["job"] = gapic.JobServiceClient(client_options=client_options)
# add client for specialist pool
clients["specialist_pool"] = gapic.SpecialistPoolServiceClient(
client_options=client_options
)
for client in clients.items():
print(client)In [ ]:
test_filename = "labeling.csv"
LABELING_FILES = [
"gs://cloud-samples-data/vision/automl_classification/flowers/daisy/100080576_f52e8ee070_n.jpg",
"gs://cloud-samples-data/vision/automl_classification/flowers/daisy/102841525_bd6628ae3c.jpg",
]
IMPORT_FILE = BUCKET_URI + "/labeling.csv"
bucket = storage.Client(project=PROJECT_ID).bucket(BUCKET_URI.replace("gs://", ""))
# creating a blob
blob = bucket.blob(blob_name=test_filename)
# creating data variable
data = LABELING_FILES[0] + "\n" + LABELING_FILES[1] + "\n"
# uploading data variable content to bucket
blob.upload_from_string(data, content_type="text/csv")
# printing path of uploaded file
print(IMPORT_FILE)
# printing content of uploaded file
! gcloud storage cat $IMPORT_FILEIn [ ]:
dataset = aip.ImageDataset.create("labeling")
print(dataset)In [ ]:
dataset.import_data(
gcs_source=[IMPORT_FILE],
import_schema_uri=aip.schema.dataset.ioformat.image.single_label_classification,
)In [ ]:
specialist_pool = {
"name": "labeling",
"display_name": "labeling",
"specialist_manager_emails": [EMAIL],
}
request = clients["specialist_pool"].create_specialist_pool(
parent=PARENT, specialist_pool=specialist_pool
)
result = request.result()
print(result)
specialist_name = result.name
specialist_id = specialist_name.split("/")[-1]
print(specialist_name)In [ ]:
# create placeholder file for instructions for data labeling
! echo "this is instruction" >> instruction.txt | gcloud storage cp instruction.txt $BUCKET_URIIn [ ]:
LABLEING_SCHEMA = LABELING_SCHEMA_IMAGE
INSTRUCTION_FILE = BUCKET_URI + "/instruction.txt"
inputs = ParseDict({"annotation_specs": ["rose"]}, Value())
data_labeling_job = {
"display_name": "labeling",
"datasets": [dataset.resource_name],
"labeler_count": 1,
"instruction_uri": INSTRUCTION_FILE,
"inputs_schema_uri": LABLEING_SCHEMA,
"inputs": inputs,
"annotation_labels": {
"aiplatform.googleapis.com/annotation_set_name": "data_labeling_job_specialist_pool"
},
"specialist_pools": [specialist_name],
}
print(data_labeling_job)
request = clients["job"].create_data_labeling_job(
parent=PARENT, data_labeling_job=data_labeling_job
)
print(request)
labeling_task_name = request.name
print(labeling_task_name)In [ ]:
request = clients["job"].get_data_labeling_job(name=labeling_task_name)
print(request)In [ ]:
request = clients["job"].cancel_data_labeling_job(name=labeling_task_name)
print(request)In [ ]:
while True:
response = clients["job"].get_data_labeling_job(name=labeling_task_name)
if response.state == gapic.JobState.JOB_STATE_CANCELLED:
print("Labeling job CANCELED")
break
else:
print("Canceling labeling job:", response.state)
time.sleep(60)In [ ]:
# Set this to true only if you'd like to delete your bucket
delete_bucket = False
# Delete the dataset using the Vertex AI fully qualified identifier for the dataset
dataset.delete()
# Delete the labeling job using the Vertex AI fully qualified identifier for the dataset
request = clients["job"].delete_data_labeling_job(name=labeling_task_name)
# Delete the specialist pool using the Vertex AI fully qualified identifier for the dataset
clients["specialist_pool"].delete_specialist_pool(name=specialist_name)
# Delete the bucket created
if delete_bucket or os.getenv("IS_TESTING"):
! gcloud storage rm --recursive $BUCKET_URI