mirror of
https://github.com/GoogleCloudPlatform/vertex-ai-samples.git
synced 2026-09-26 14:42:04 +00:00
* fix,chore,refactor(egen): Changed REGION variable name to LOCATION, modified the import file of flowers dataset, added comments in cleanup section, removed os.getenv(IS_TESTING) from the cleanup section, refactored code according to template guidelines and performed linter test. * chore(egen): Done changes according to @kittylabs and performed linter test. --------- Co-authored-by: sriramya2610 <sriramya.peddapally@egen.ai>
80 KiB
80 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 In [ ]:
import sys
if "google.colab" in sys.modules:
import IPython
app = IPython.Application.instance()
app.kernel.do_shutdown(True)In [ ]:
import sys
if "google.colab" in sys.modules:
from google.colab import auth
auth.authenticate_user()In [ ]:
PROJECT_ID = "[your-project-id]" # @param {type:"string"}
LOCATION = "us-central1" # @param {type: "string"}In [ ]:
BUCKET_URI = f"gs://your-bucket-name-{PROJECT_ID}-unique" # @param {type:"string"}In [ ]:
! gsutil mb -l $LOCATION $BUCKET_URIIn [ ]:
import sys
import google.cloud.aiplatform as aiplatformIn [ ]:
aiplatform.init(project=PROJECT_ID, staging_bucket=BUCKET_URI)In [ ]:
IMPORT_FILE = "gs://cloud-samples-data/ai-platform/flowers/flowers.csv"In [ ]:
FILE = IMPORT_FILE
count = ! gsutil cat $FILE | wc -l
print("Number of Examples", int(count[0]))
print("First 10 rows")
! gsutil cat $FILE | headIn [ ]:
dataset = aiplatform.ImageDataset.create(
display_name="flowers",
gcs_source=[IMPORT_FILE],
import_schema_uri=aiplatform.schema.dataset.ioformat.image.single_label_classification,
)
print(dataset.resource_name)In [ ]:
dag = aiplatform.AutoMLImageTrainingJob(
display_name="flowers",
prediction_type="classification",
multi_label=False,
model_type="MOBILE_TF_LOW_LATENCY_1",
base_model=None,
)
print(dag)In [ ]:
import os
if os.getenv("IS_TESTING"):
sys.exit(0)In [ ]:
model = dag.run(
dataset=dataset,
model_display_name="flowers",
training_fraction_split=0.8,
validation_fraction_split=0.1,
test_fraction_split=0.1,
budget_milli_node_hours=8000,
disable_early_stopping=False,
)In [ ]:
model_evaluations = model.list_model_evaluations()
for model_evaluation in model_evaluations:
print(model_evaluation.to_dict())In [ ]:
endpoint = model.deploy()In [ ]:
test_item = !gsutil cat $IMPORT_FILE | head -n1
if len(str(test_item[0]).split(",")) == 3:
_, test_item, test_label = str(test_item[0]).split(",")
else:
test_item, test_label = str(test_item[0]).split(",")
print(test_item, test_label)In [ ]:
import base64
from google.cloud import storage
# Copy the test image to the Cloud storage bucket as "test.jpg"
test_image_local = "{}/test.jpg".format(BUCKET_URI)
! gsutil cp $test_item $test_image_local
# Download the test image in bytes format
storage_client = storage.Client(project=PROJECT_ID)
bucket = storage_client.bucket(bucket_name=BUCKET_URI[5:])
test_content = bucket.get_blob("test.jpg").download_as_bytes()
# The format of each instance should conform to the deployed model's prediction input schema.
instances = [{"content": base64.b64encode(test_content).decode("utf-8")}]
prediction = endpoint.predict(instances=instances)
print(prediction)In [ ]:
endpoint.undeploy_all()In [ ]:
response = model.export_model(
artifact_destination=BUCKET_URI, export_format_id="tflite", sync=True
)
model_package = response["artifactOutputUri"]In [ ]:
model.delete()In [ ]:
dataset.delete()In [ ]:
endpoint.delete()In [ ]:
IMPORT_FILE = "bq://bigquery-public-data.samples.gsod"
BQ_TABLE = "bigquery-public-data.samples.gsod"In [ ]:
dataset = aiplatform.TabularDataset.create(
display_name="gsod",
bq_source=[IMPORT_FILE],
labels={"user_metadata": BUCKET_URI[5:]},
)
label_column = "mean_temp"
print(dataset.resource_name)In [ ]:
TRANSFORMATIONS = [
{"auto": {"column_name": "year"}},
{"auto": {"column_name": "month"}},
{"auto": {"column_name": "day"}},
]
label_column = "mean_temp"In [ ]:
dag = aiplatform.AutoMLTabularTrainingJob(
display_name="gsod",
optimization_prediction_type="regression",
optimization_objective="minimize-rmse",
column_transformations=TRANSFORMATIONS,
)
print(dag)In [ ]:
model = dag.run(
dataset=dataset,
model_display_name="gsod",
training_fraction_split=0.8,
validation_fraction_split=0.1,
test_fraction_split=0.1,
budget_milli_node_hours=8000,
disable_early_stopping=False,
target_column="mean_temp",
)In [ ]:
model_evaluations = model.list_model_evaluations()
for model_evaluation in model_evaluations:
print(model_evaluation.to_dict())In [ ]:
endpoint = model.deploy(machine_type="n1-standard-4")In [ ]:
endpoint.undeploy_all()In [ ]:
response = model.export_model(
artifact_destination=BUCKET_URI, export_format_id="tf-saved-model", sync=True
)
model_package = response["artifactOutputUri"]In [ ]:
model.delete()In [ ]:
dataset.delete()In [ ]:
endpoint.delete()In [ ]:
IMPORT_FILE = "gs://cloud-ml-data/NL-classification/happiness.csv"In [ ]:
FILE = IMPORT_FILE
count = ! gsutil cat $FILE | wc -l
print("Number of Examples", int(count[0]))
print("First 10 rows")
! gsutil cat $FILE | headIn [ ]:
dataset = aiplatform.TextDataset.create(
display_name="happydb",
gcs_source=[IMPORT_FILE],
import_schema_uri=aiplatform.schema.dataset.ioformat.text.single_label_classification,
)
print(dataset.resource_name)In [ ]:
dag = aiplatform.AutoMLTextTrainingJob(
display_name="happydb",
prediction_type="classification",
multi_label=False,
)
print(dag)Warning:
Output truncated. This notebook contains too many cells to display efficiently.


