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
* Manual Changes-Updated the cell by replacing 'gsutil copy' with the correct 'gcloud storage cp'
* Manual Changes-Updated the cell by replacing 'gsutil copy' with the correct 'gcloud storage cp'
* Revert "Manual Changes-Updated the cell by replacing 'gsutil copy' with the correct 'gcloud storage cp'"
This reverts commit 175eaa4fe8.
* Manual Changes-Updated the cell by replacing 'gsutil copy' with the correct 'gcloud storage cp'
* Changes for 4326
* Changes for 4326
* Linter fix issue for 4326
* removed model garden changes
* Update model_garden_movinet_action_recognition.ipynb
---------
Co-authored-by: bhandarivijay <bhandarivijay@google.com>
Co-authored-by: gurusai-voleti <gvoleti@google.com>
35 KiB
35 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 [ ]:
# install packages
! pip3 install --upgrade --quiet google-cloud-aiplatformIn [ ]:
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 [ ]:
! gcloud storage buckets create --location={LOCATION} --project={PROJECT_ID} {BUCKET_URI}In [ ]:
from google.cloud import aiplatform
aiplatform.init(project=PROJECT_ID, location=LOCATION, staging_bucket=BUCKET_URI)In [ ]:
TRAIN_GPU, TRAIN_NGPU = (None, None)
DEPLOY_GPU, DEPLOY_NGPU = (None, None)In [ ]:
TRAIN_VERSION = "xgboost-cpu.1-1"
DEPLOY_VERSION = "xgboost-cpu.1-1"
TRAIN_IMAGE = "{}-docker.pkg.dev/vertex-ai/training/{}:latest".format(
LOCATION.split("-")[0], TRAIN_VERSION
)
DEPLOY_IMAGE = "{}-docker.pkg.dev/vertex-ai/prediction/{}:latest".format(
LOCATION.split("-")[0], DEPLOY_VERSION
)In [ ]:
TRAIN_COMPUTE = "n1-standard-4"
print("Train machine type", TRAIN_COMPUTE)In [ ]:
# Make folder for Python training script
! rm -rf custom
! mkdir custom
# Add package information
! touch custom/README.md
setup_cfg = "[egg_info]\n\ntag_build =\n\ntag_date = 0"
! echo "$setup_cfg" > custom/setup.cfg
setup_py = "import setuptools\n\nsetuptools.setup(\n\n install_requires=[\n\n 'cloudml-hypertune',\n\n ],\n\n packages=setuptools.find_packages())"
! echo "$setup_py" > custom/setup.py
pkg_info = "Metadata-Version: 1.0\n\nName: Iris tabular classification\n\nVersion: 0.0.0\n\nSummary: Demostration training script\n\nHome-page: www.google.com\n\nAuthor: Google\n\nAuthor-email: aferlitsch@google.com\n\nLicense: Public\n\nDescription: Demo\n\nPlatform: Vertex"
! echo "$pkg_info" > custom/PKG-INFO
# Make the training subfolder
! mkdir custom/trainer
! touch custom/trainer/__init__.pyIn [ ]:
%%writefile custom/trainer/task.py
import datetime
import os
import subprocess
import sys
import pandas as pd
import xgboost as xgb
import hypertune
import argparse
import logging
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
parser = argparse.ArgumentParser()
parser.add_argument('--model-dir', dest='model_dir',
default=os.getenv('AIP_MODEL_DIR'), type=str, help='Model dir.')
parser.add_argument("--dataset-data-url", dest="dataset_data_url",
type=str, help="Download url for the training data.")
parser.add_argument("--dataset-labels-url", dest="dataset_labels_url",
type=str, help="Download url for the training data labels.")
parser.add_argument("--boost-rounds", dest="boost_rounds",
default=20, type=int, help="Number of boosted rounds")
args = parser.parse_args()
logging.getLogger().setLevel(logging.INFO)
def get_data():
logging.info("Downloading training data and labelsfrom: {}, {}".format(args.dataset_data_url, args.dataset_labels_url))
# gsutil outputs everything to stderr. Hence, the need to divert it to stdout.
subprocess.check_call(['gcloud', 'storage', 'cp', args.dataset_data_url, 'data.csv'], stderr=sys.stdout)
subprocess.check_call(['gcloud', 'storage', 'cp', args.dataset_labels_url, 'labels.csv'], stderr=sys.stdout)
# Load data into pandas, then use `.values` to get NumPy arrays
data = pd.read_csv('data.csv').values
labels = pd.read_csv('labels.csv').values
# Convert one-column 2D array into 1D array for use with XGBoost
labels = labels.reshape((labels.size,))
train_data, test_data, train_labels, test_labels = train_test_split(data, labels, test_size=0.2, random_state=7)
# Load data into DMatrix object
dtrain = xgb.DMatrix(train_data, label=train_labels)
return dtrain, test_data, test_labels
def train_model(dtrain):
logging.info("Start training ...")
# Train XGBoost model
model = xgb.train({}, dtrain, num_boost_round=args.boost_rounds)
logging.info("Training completed")
return model
def evaluate_model(model, test_data, test_labels):
dtest = xgb.DMatrix(test_data)
pred = model.predict(dtest)
predictions = [round(value) for value in pred]
# evaluate predictions
accuracy = accuracy_score(test_labels, predictions)
logging.info(f"Evaluation completed with model accuracy: {accuracy}")
# report metric for hyperparameter tuning
hpt = hypertune.HyperTune()
hpt.report_hyperparameter_tuning_metric(
hyperparameter_metric_tag='accuracy',
metric_value=accuracy
)
return accuracy
dtrain, test_data, test_labels = get_data()
model = train_model(dtrain)
accuracy = evaluate_model(model, test_data, test_labels)
# GCSFuse conversion
gs_prefix = 'gs://'
gcsfuse_prefix = '/gcs/'
if args.model_dir.startswith(gs_prefix):
args.model_dir = args.model_dir.replace(gs_prefix, gcsfuse_prefix)
dirpath = os.path.split(args.model_dir)[0]
if not os.path.isdir(dirpath):
os.makedirs(dirpath)
# Export the classifier to a file
gcs_model_path = os.path.join(args.model_dir, 'model.bst')
logging.info("Saving model artifacts to {}". format(gcs_model_path))
model.save_model(gcs_model_path)
logging.info("Saving metrics to {}/metrics.json". format(args.model_dir))
gcs_metrics_path = os.path.join(args.model_dir, 'metrics.json')
with open(gcs_metrics_path, "w") as f:
f.write(f"{'accuracy: {accuracy}'}")In [ ]:
! rm -f custom.tar custom.tar.gz
! tar cvf custom.tar custom
! gzip custom.tar
! gcloud storage cp custom.tar.gz $BUCKET_URI/trainer_iris.tar.gzIn [ ]:
if TRAIN_GPU:
machine_spec = {
"machine_type": TRAIN_COMPUTE,
"accelerator_type": TRAIN_GPU,
"accelerator_count": TRAIN_NGPU,
}
else:
machine_spec = {"machine_type": TRAIN_COMPUTE, "accelerator_count": 0}In [ ]:
DISK_TYPE = "pd-ssd" # [ pd-ssd, pd-standard]
DISK_SIZE = 100 # GB
disk_spec = {"boot_disk_type": DISK_TYPE, "boot_disk_size_gb": DISK_SIZE}In [ ]:
# Set path to save model
MODEL_DIR = "{}/aiplatform-custom-job".format(BUCKET_URI)
# Set the source path to the dataset
DATASET_DIR = "gs://cloud-samples-data/ai-platform/iris"
# Set the command-line arguments
CMDARGS = [
"--dataset-data-url=" + DATASET_DIR + "/iris_data.csv",
"--dataset-labels-url=" + DATASET_DIR + "/iris_target.csv",
]
# Set the worker pool specs
worker_pool_spec = [
{
"replica_count": 1,
"machine_spec": machine_spec,
"disk_spec": disk_spec,
"python_package_spec": {
"executor_image_uri": TRAIN_IMAGE,
"package_uris": [BUCKET_URI + "/trainer_iris.tar.gz"],
"python_module": "trainer.task",
"args": CMDARGS,
},
}
]In [ ]:
job = aiplatform.CustomJob(
display_name="iris",
worker_pool_specs=worker_pool_spec,
base_output_dir=MODEL_DIR,
)In [ ]:
from google.cloud.aiplatform import hyperparameter_tuning as hpt
hpt_job = aiplatform.HyperparameterTuningJob(
display_name="iris",
custom_job=job,
metric_spec={
"accuracy": "maximize",
},
parameter_spec={
"boost-rounds": hpt.IntegerParameterSpec(min=10, max=100, scale="linear"),
},
search_algorithm=None,
max_trial_count=6,
parallel_trial_count=1,
)In [ ]:
hpt_job.run()In [ ]:
print(hpt_job.trials)In [ ]:
# Initialize a tuple to identify the best configuration
best = (None, None, None, 0.0)
# Iterate through the trails and update the best configuration
for trial in hpt_job.trials:
# Keep track of the best outcome
if float(trial.final_measurement.metrics[0].value) > best[3]:
try:
best = (
trial.id,
float(trial.parameters[0].value),
float(trial.parameters[1].value),
float(trial.final_measurement.metrics[0].value),
)
except:
best = (
trial.id,
float(trial.parameters[0].value),
None,
float(trial.final_measurement.metrics[0].value),
)
# print details of the best configuration
print(best)In [ ]:
# Fetch the best model
BEST_MODEL_DIR = MODEL_DIR + "/" + best[0] + "/model"
! gcloud storage ls {BEST_MODEL_DIR}In [ ]:
# Delete the hyperparameter tuning job
hpt_job.delete()
# Delete the Cloud Storage bucket
delete_bucket = False # Set True to delete the bucket
if delete_bucket:
! gcloud storage rm --recursive $BUCKET_URI
# Delete the locally generated files
! rm -rf custom/
! rm custom.tar.gz