<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Vertex SDK
Step1:  Restart the Kernel
Once you've installed the Vertex SDK and Google cloud-storage, you need to restart the notebook kernel so it can find the packages.
Step2:  Before you begin
GPU run-time
Make sure you're running this notebook in a GPU runtime if you have that option. In Colab, select Runtime > Change Runtime Type > GPU
Set up your GCP project
The following steps are required, regardless of your notebook environment.
Select or create a GCP project. When you first create an account, you get a $300 free credit towards your compute/storage costs.
Make sure that billing is enabled for your project.
Enable the AutoML APIs and Compute Engine APIs.
Google Cloud SDK is already installed in AutoML Notebooks.
Enter your project ID in the cell below. Then run the  cell to make sure the
Cloud SDK uses the right project for all the commands in this notebook.
Note
Step3:  Region
You can also change the REGION variable, which is used for operations
throughout the rest of this notebook.  Below are regions supported forAutoML. We recommend when possible, to choose the region closest to you. 
Currently project resources must be in the us-central1 region to use this API.
Step4:  Timestamp
If you are in a live tutorial session, you might be using a shared test account or project. To avoid name collisions between users on resources created, you create a timestamp for each instance session, and append onto the name of resources which will be created in this tutorial.
Step5:  Authenticate your GCP account
If you are using AutoML Notebooks, your environment is already
authenticated. Skip this step.
Note
Step6:  Create a Cloud Storage bucket
The following steps are required, regardless of your notebook environment.
This tutorial is designed to use training data that is in a public Cloud Storage bucket and a local Cloud Storage bucket for your batch predictions. You may alternatively use your own training data that you have stored in a local Cloud Storage bucket.
Set the name of your Cloud Storage bucket below. It must be unique across all Cloud Storage buckets.
Step7:  Only if your bucket doesn't already exist
Step8:  Finally, validate access to your Cloud Storage bucket by examining its contents
Step9:  Set up variables
Next, set up some variables used throughout the tutorial.
Import libraries and define constants
Step10:  AutoML constants
Setup up the following constants for AutoML
Step11:  Clients
We use the Google APIs Client Library for Python to call the Vertex Training and Prediction API without manually constructing HTTP requests.
Step12:  Prepare a trainer script
Package assembly
Step13:  Task.py contents
Step14:  Store training script on your Cloud Storage bucket
Step15:  Train a model
projects.jobs.create
Request
Step16:  Example output
Step17:  Response
Step18:  Example output
Step19:  projects.jobs.get
Call
Step20:  Response
Step21:  Example output
Step22:  Serving function for trained model (image data)
Step23:  Example output
Step24:  Example output
Step25:  Example output
Step26:  Response
Step27:  Example output
Step28:  projects.jobs.get
Call
Step29:  Response
Step30:  Example output
Step31:  Example output
Step32:  Example output
Step33:  Response
Step34:  Example output
Step35:  projects.models.versions.create
Request
Step36:  Example output
Step37:  Response
Step38:  Example output
Step39:  Prepare input for online prediction
Step40:  projects.predict
Request
Step41:  Example output
Step42:  Response
Step43:  Example output
Step44:  Response
Step45:  Example output
<ASSISTANT_TASK:>
Python Code:
! pip3 install google-cloud-storage
Explanation: Vertex SDK: Train & deploy a TensorFlow model with hosted runtimes (aka pre-built containers)
Installation
Install the Google cloud-storage library as well.
End of explanation
import os
if not os.getenv("AUTORUN"):
    # Automatically restart kernel after installs
    import IPython
    app = IPython.Application.instance()
    app.kernel.do_shutdown(True)
Explanation: Restart the Kernel
Once you've installed the Vertex SDK and Google cloud-storage, you need to restart the notebook kernel so it can find the packages.
End of explanation
PROJECT_ID = "[your-project-id]"  # @param {type:"string"}
if PROJECT_ID == "" or PROJECT_ID is None or PROJECT_ID == "[your-project-id]":
    # Get your GCP project id from gcloud
    shell_output = !gcloud config list --format 'value(core.project)' 2>/dev/null
    PROJECT_ID = shell_output[0]
    print("Project ID:", PROJECT_ID)
! gcloud config set project $PROJECT_ID
Explanation: Before you begin
GPU run-time
Make sure you're running this notebook in a GPU runtime if you have that option. In Colab, select Runtime > Change Runtime Type > GPU
Set up your GCP project
The following steps are required, regardless of your notebook environment.
Select or create a GCP project. When you first create an account, you get a $300 free credit towards your compute/storage costs.
Make sure that billing is enabled for your project.
Enable the AutoML APIs and Compute Engine APIs.
Google Cloud SDK is already installed in AutoML Notebooks.
Enter your project ID in the cell below. Then run the  cell to make sure the
Cloud SDK uses the right project for all the commands in this notebook.
Note: Jupyter runs lines prefixed with ! as shell commands, and it interpolates Python variables prefixed with $ into these commands.
End of explanation
REGION = "us-central1"  # @param {type: "string"}
Explanation: Region
You can also change the REGION variable, which is used for operations
throughout the rest of this notebook.  Below are regions supported forAutoML. We recommend when possible, to choose the region closest to you. 
Currently project resources must be in the us-central1 region to use this API.
End of explanation
from datetime import datetime
TIMESTAMP = datetime.now().strftime("%Y%m%d%H%M%S")
Explanation: Timestamp
If you are in a live tutorial session, you might be using a shared test account or project. To avoid name collisions between users on resources created, you create a timestamp for each instance session, and append onto the name of resources which will be created in this tutorial.
End of explanation
import os
import sys
# If you are running this notebook in Colab, run this cell and follow the
# instructions to authenticate your Google Cloud account. This provides access
# to your Cloud Storage bucket and lets you submit training jobs and prediction
# requests.
# If on AutoML, then don't execute this code
if not os.path.exists("/opt/deeplearning/metadata/env_version"):
    if "google.colab" in sys.modules:
        from google.colab import auth as google_auth
        google_auth.authenticate_user()
    # If you are running this tutorial in a notebook locally, replace the string
    # below with the path to your service account key and run this cell to
    # authenticate your Google Cloud account.
    else:
        %env GOOGLE_APPLICATION_CREDENTIALS your_path_to_credentials.json
    # Log in to your account on Google Cloud
    ! gcloud auth login
Explanation: Authenticate your GCP account
If you are using AutoML Notebooks, your environment is already
authenticated. Skip this step.
Note: If you are on an AutoML notebook and run the cell, the cell knows to skip executing the authentication steps.
End of explanation
BUCKET_NAME = "[your-bucket-name]"  # @param {type:"string"}
if BUCKET_NAME == "" or BUCKET_NAME is None or BUCKET_NAME == "[your-bucket-name]":
    BUCKET_NAME = PROJECT_ID + "aip-" + TIMESTAMP
Explanation: Create a Cloud Storage bucket
The following steps are required, regardless of your notebook environment.
This tutorial is designed to use training data that is in a public Cloud Storage bucket and a local Cloud Storage bucket for your batch predictions. You may alternatively use your own training data that you have stored in a local Cloud Storage bucket.
Set the name of your Cloud Storage bucket below. It must be unique across all Cloud Storage buckets.
End of explanation
! gsutil mb -l $REGION gs://$BUCKET_NAME
Explanation: Only if your bucket doesn't already exist: Run the following cell to create your Cloud Storage bucket.
End of explanation
! gsutil ls -al gs://$BUCKET_NAME
Explanation: Finally, validate access to your Cloud Storage bucket by examining its contents:
End of explanation
import json
import time
from google.protobuf.json_format import MessageToJson
from google.protobuf.struct_pb2 import Value
from googleapiclient import discovery, errors
Explanation: Set up variables
Next, set up some variables used throughout the tutorial.
Import libraries and define constants
End of explanation
# AutoM location root path for your dataset, model and endpoint resources
PARENT = "projects/" + PROJECT_ID
Explanation: AutoML constants
Setup up the following constants for AutoML:
PARENT: The AutoM location root path for dataset, model and endpoint resources.
End of explanation
cloudml = discovery.build("ml", "v1")
Explanation: Clients
We use the Google APIs Client Library for Python to call the Vertex Training and Prediction API without manually constructing HTTP requests.
End of explanation
! rm -rf cifar
! mkdir cifar
! touch cifar/README.md
setup_cfg = "[egg_info]\n\
tag_build =\n\
tag_date = 0"
! echo "$setup_cfg" > cifar/setup.cfg
setup_py = "import setuptools\n\
# Requires TensorFlow Datasets\n\
setuptools.setup(\n\
    install_requires=[\n\
        'tensorflow_datasets==1.3.0',\n\
    ],\n\
    packages=setuptools.find_packages())"
! echo "$setup_py" > cifar/setup.py
pkg_info = "Metadata-Version: 1.0\n\
Name: Custom Training CIFAR-10\n\
Version: 0.0.0\n\
Summary: Demonstration training script\n\
Home-page: www.google.com\n\
Author: Google\n\
Author-email: aferlitsch@google.com\n\
License: Public\n\
Description: Demo\n\
Platform: Vertex AI"
! echo "$pkg_info" > cifar/PKG-INFO
! mkdir cifar/trainer
! touch cifar/trainer/__init__.py
Explanation: Prepare a trainer script
Package assembly
End of explanation
%%writefile cifar/trainer/task.py
import tensorflow_datasets as tfds
import tensorflow as tf
from tensorflow.python.client import device_lib
import argparse
import os
import sys
tfds.disable_progress_bar()
parser = argparse.ArgumentParser()
parser.add_argument('--model-dir', dest='model_dir',
                    default='/tmp/saved_model', type=str, help='Model dir.')
parser.add_argument('--lr', dest='lr',
                    default=0.01, type=float,
                    help='Learning rate.')
parser.add_argument('--epochs', dest='epochs',
                    default=10, type=int,
                    help='Number of epochs.')
parser.add_argument('--steps', dest='steps',
                    default=200, type=int,
                    help='Number of steps per epoch.')
parser.add_argument('--distribute', dest='distribute', type=str, default='single',
                    help='distributed training strategy')
args = parser.parse_args()
print('Python Version = {}'.format(sys.version))
print('TensorFlow Version = {}'.format(tf.__version__))
print('TF_CONFIG = {}'.format(os.environ.get('TF_CONFIG', 'Not found')))
print('DEVICES', device_lib.list_local_devices())
if args.distribute == 'single':
    if tf.test.is_gpu_available():
        strategy = tf.distribute.OneDeviceStrategy(device="/gpu:0")
    else:
        strategy = tf.distribute.OneDeviceStrategy(device="/cpu:0")
elif args.distribute == 'mirror':
    strategy = tf.distribute.MirroredStrategy()
elif args.distribute == 'multi':
    strategy = tf.distribute.experimental.MultiWorkerMirroredStrategy()
print('num_replicas_in_sync = {}'.format(strategy.num_replicas_in_sync))
BUFFER_SIZE = 10000
BATCH_SIZE = 64
def make_datasets_unbatched():
  def scale(image, label):
    image = tf.cast(image, tf.float32)
    image /= 255.0
    return image, label
  datasets, info = tfds.load(name='cifar10',
                            with_info=True,
                            as_supervised=True)
  return datasets['train'].map(scale).cache().shuffle(BUFFER_SIZE).repeat()
def build_and_compile_cnn_model():
  model = tf.keras.Sequential([
      tf.keras.layers.Conv2D(32, 3, activation='relu', input_shape=(32, 32, 3)),
      tf.keras.layers.MaxPooling2D(),
      tf.keras.layers.Conv2D(32, 3, activation='relu'),
      tf.keras.layers.MaxPooling2D(),
      tf.keras.layers.Flatten(),
      tf.keras.layers.Dense(10, activation='softmax')
  ])
  model.compile(
      loss=tf.keras.losses.sparse_categorical_crossentropy,
      optimizer=tf.keras.optimizers.SGD(learning_rate=args.lr),
      metrics=['accuracy'])
  return model
NUM_WORKERS = strategy.num_replicas_in_sync
GLOBAL_BATCH_SIZE = BATCH_SIZE * NUM_WORKERS
train_dataset = make_datasets_unbatched().batch(GLOBAL_BATCH_SIZE)
with strategy.scope():
  model = build_and_compile_cnn_model()
model.fit(x=train_dataset, epochs=args.epochs, steps_per_epoch=args.steps)
model.save(args.model_dir)
Explanation: Task.py contents
End of explanation
! rm -f cifar.tar cifar.tar.gz
! tar cvf cifar.tar cifar
! gzip cifar.tar
! gsutil cp cifar.tar.gz gs://$BUCKET_NAME/trainer_cifar.tar.gz
Explanation: Store training script on your Cloud Storage bucket
End of explanation
JOB_NAME = "custom_job_TF_" + TIMESTAMP
TRAINING_INPUTS = {
    "scaleTier": "CUSTOM",
    "masterType": "n1-standard-4",
    "masterConfig": {"acceleratorConfig": {"count": "1", "type": "NVIDIA_TESLA_K80"}},
    "packageUris": ["gs://" + BUCKET_NAME + "/trainer_cifar.tar.gz"],
    "pythonModule": "trainer.task",
    "args": [
        "--model-dir=" + "gs://{}/{}".format(BUCKET_NAME, JOB_NAME),
        "--epochs=" + str(20),
        "--steps=" + str(100),
        "--distribute=" + "single",
    ],
    "region": REGION,
    "runtimeVersion": "2.1",
    "pythonVersion": "3.7",
}
body = {"jobId": JOB_NAME, "trainingInput": TRAINING_INPUTS}
request = cloudml.projects().jobs().create(parent=PARENT)
request.body = json.loads(json.dumps(body, indent=2))
print(json.dumps(json.loads(request.to_json()), indent=2))
request = cloudml.projects().jobs().create(parent=PARENT, body=body)
Explanation: Train a model
projects.jobs.create
Request
End of explanation
response = request.execute()
Explanation: Example output:
{
  "uri": "https://ml.googleapis.com/v1/projects/migration-ucaip-training/jobs?alt=json",
  "method": "POST",
  "body": {
    "jobId": "custom_job_TF_20210325211532",
    "trainingInput": {
      "scaleTier": "CUSTOM",
      "masterType": "n1-standard-4",
      "masterConfig": {
        "acceleratorConfig": {
          "count": "1",
          "type": "NVIDIA_TESLA_K80"
        }
      },
      "packageUris": [
        "gs://migration-ucaip-trainingaip-20210325211532/trainer_cifar.tar.gz"
      ],
      "pythonModule": "trainer.task",
      "args": [
        "--model-dir=gs://migration-ucaip-trainingaip-20210325211532/custom_job_TF_20210325211532",
        "--epochs=20",
        "--steps=100",
        "--distribute=single"
      ],
      "region": "us-central1",
      "runtimeVersion": "2.1",
      "pythonVersion": "3.7"
    }
  },
  "headers": {
    "accept": "application/json",
    "accept-encoding": "gzip, deflate",
    "user-agent": "(gzip)",
    "x-goog-api-client": "gdcl/1.12.8 gl-python/3.7.8"
  },
  "methodId": "ml.projects.jobs.create",
  "resumable": null,
  "response_callbacks": [],
  "_in_error_state": false,
  "body_size": 0,
  "resumable_uri": null,
  "resumable_progress": 0
}
Call
End of explanation
print(json.dumps(response, indent=2))
Explanation: Response
End of explanation
# The full unique ID for the custom training job
custom_training_id = f'{PARENT}/jobs/{response["jobId"]}'
# The short numeric ID for the custom training job
custom_training_short_id = response["jobId"]
print(custom_training_id)
Explanation: Example output:
{
  "jobId": "custom_job_TF_20210325211532",
  "trainingInput": {
    "scaleTier": "CUSTOM",
    "masterType": "n1-standard-4",
    "packageUris": [
      "gs://migration-ucaip-trainingaip-20210325211532/trainer_cifar.tar.gz"
    ],
    "pythonModule": "trainer.task",
    "args": [
      "--model-dir=gs://migration-ucaip-trainingaip-20210325211532/custom_job_TF_20210325211532",
      "--epochs=20",
      "--steps=100",
      "--distribute=single"
    ],
    "region": "us-central1",
    "runtimeVersion": "2.1",
    "pythonVersion": "3.7",
    "masterConfig": {
      "acceleratorConfig": {
        "count": "1",
        "type": "NVIDIA_TESLA_K80"
      }
    }
  },
  "createTime": "2021-03-25T21:15:40Z",
  "state": "QUEUED",
  "trainingOutput": {},
  "etag": "dH4whflp8Fg="
}
End of explanation
request = cloudml.projects().jobs().get(name=custom_training_id)
response = request.execute()
Explanation: projects.jobs.get
Call
End of explanation
print(json.dumps(response, indent=2))
Explanation: Response
End of explanation
while True:
    response = cloudml.projects().jobs().get(name=custom_training_id).execute()
    if response["state"] != "SUCCEEDED":
        print("Training job has not completed:", response["state"])
        if response["state"] == "FAILED":
            break
    else:
        break
    time.sleep(20)
# model artifact output directory on Google Cloud Storage
model_artifact_dir = response["trainingInput"]["args"][0].split("=")[-1]
print("artifact location  " + model_artifact_dir)
Explanation: Example output:
{
  "jobId": "custom_job_TF_20210325211532",
  "trainingInput": {
    "scaleTier": "CUSTOM",
    "masterType": "n1-standard-4",
    "packageUris": [
      "gs://migration-ucaip-trainingaip-20210325211532/trainer_cifar.tar.gz"
    ],
    "pythonModule": "trainer.task",
    "args": [
      "--model-dir=gs://migration-ucaip-trainingaip-20210325211532/custom_job_TF_20210325211532",
      "--epochs=20",
      "--steps=100",
      "--distribute=single"
    ],
    "region": "us-central1",
    "runtimeVersion": "2.1",
    "pythonVersion": "3.7",
    "masterConfig": {
      "acceleratorConfig": {
        "count": "1",
        "type": "NVIDIA_TESLA_K80"
      }
    }
  },
  "createTime": "2021-03-25T21:15:40Z",
  "state": "PREPARING",
  "trainingOutput": {},
  "etag": "eLnYfClHtKU="
}
End of explanation
import tensorflow as tf
model = tf.keras.models.load_model(model_artifact_dir)
CONCRETE_INPUT = "numpy_inputs"
def _preprocess(bytes_input):
    decoded = tf.io.decode_jpeg(bytes_input, channels=3)
    decoded = tf.image.convert_image_dtype(decoded, tf.float32)
    resized = tf.image.resize(decoded, size=(32, 32))
    rescale = tf.cast(resized / 255.0, tf.float32)
    return rescale
@tf.function(input_signature=[tf.TensorSpec([None], tf.string)])
def preprocess_fn(bytes_inputs):
    decoded_images = tf.map_fn(
        _preprocess, bytes_inputs, dtype=tf.float32, back_prop=False
    )
    return {
        CONCRETE_INPUT: decoded_images
    }  # User needs to make sure the key matches model's input
m_call = tf.function(model.call).get_concrete_function(
    [tf.TensorSpec(shape=[None, 32, 32, 3], dtype=tf.float32, name=CONCRETE_INPUT)]
)
@tf.function(
    input_signature=[tf.TensorSpec([None], tf.string), tf.TensorSpec([None], tf.string)]
)
def serving_fn(bytes_inputs, key):
    images = preprocess_fn(bytes_inputs)
    prob = m_call(**images)
    return {"prediction": prob, "key": key}
tf.saved_model.save(
    model,
    model_artifact_dir,
    signatures={
        "serving_default": serving_fn,
    },
)
loaded = tf.saved_model.load(model_artifact_dir)
tensors_specs = list(loaded.signatures["serving_default"].structured_input_signature)
print("Tensors specs:", tensors_specs)
input_name = [v for k, v in tensors_specs[1].items() if k != "key"][0].name
print("Bytes input tensor name:", input_name)
Explanation: Serving function for trained model (image data)
End of explanation
import base64
import json
import cv2
import numpy as np
import tensorflow as tf
(_, _), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()
test_image_1, test_label_1 = x_test[0], y_test[0]
test_image_2, test_label_2 = x_test[1], y_test[1]
cv2.imwrite("tmp1.jpg", (test_image_1 * 255).astype(np.uint8))
cv2.imwrite("tmp2.jpg", (test_image_2 * 255).astype(np.uint8))
gcs_input_uri = "gs://" + BUCKET_NAME + "/" + "test.json"
with tf.io.gfile.GFile(gcs_input_uri, "w") as f:
    for img in ["tmp1.jpg", "tmp2.jpg"]:
        bytes = tf.io.read_file(img)
        b64str = base64.b64encode(bytes.numpy()).decode("utf-8")
        f.write(json.dumps({"key": img, input_name: {"b64": b64str}}) + "\n")
! gsutil cat $gcs_input_uri
Explanation: Example output:
Tensors specs: [(), {'bytes_inputs': TensorSpec(shape=(None,), dtype=tf.string, name='bytes_inputs'), 'key': TensorSpec(shape=(None,), dtype=tf.string, name='key')}]
Bytes input tensor name: bytes_inputs
Make batch predictions
Prepare files for batch prediction
End of explanation
body = {
    "jobId": "custom_job_TF_pred_" + TIMESTAMP,
    "prediction_input": {
        "input_paths": gcs_input_uri,
        "output_path": "gs://" + f"{BUCKET_NAME}/batch_output/",
        "data_format": "JSON",
        "runtime_version": "2.1",
        "uri": model_artifact_dir,
        "region": "us-central1",
    },
}
request = (
    cloudml.projects()
    .jobs()
    .create(
        parent=PARENT,
    )
)
request.body = json.loads(json.dumps(body, indent=2))
print(json.dumps(json.loads(request.to_json()), indent=2))
request = cloudml.projects().jobs().create(parent=PARENT, body=body)
Explanation: Example output:
{"key": "tmp1.jpg", "bytes_inputs": {"b64": "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAIBAQEBAQIBAQECAgICAgQDAgICAgUEBAMEBgUGBgYFBgYGBwkIBgcJBwYGCAsICQoKCgoKBggLDAsKDAkKCgr/2wBDAQICAgICAgUDAwUKBwYHCgoKCgoKCgoKCgo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Pn0WDw9piTLLbuUiYGWZsCNYp/tMtqiSbL+b7RMrqvWxVDKamZ89BOg03Q9+deupOpBRotU1CM4OMak/aSUIxkouTbUjmllc0qic60XrGNldX/dtNr/n2+aS5r3XI3ytKz+Jof+CN2r6LYHU/ibqOo2iQzFmmn8eXLfugMbDhwMcdeprg/iV+zX+zx8O9Mu9f8NaRplw9oSr6g0sl0BgdBNMzZ+i9K+svi9P+yv8ADAnRfhl4MfxNdhSDe63fzS2sJHdYpHbfjtu/KvhL9ub4tarruhy2JvJMsdjJFGFj28gKqrgKo9B6VhlvEGMzfDxm8M6N+kpRlJeT5dE/mwoZDiMO+evVb8j/2Q=="}}
projects.jobs.create
Request
End of explanation
response = request.execute()
Explanation: Example output:
{
  "uri": "https://ml.googleapis.com/v1/projects/migration-ucaip-training/jobs?alt=json",
  "method": "POST",
  "body": {
    "jobId": "custom_job_TF_pred_20210325211532",
    "prediction_input": {
      "input_paths": "gs://migration-ucaip-trainingaip-20210325211532/test.json",
      "output_path": "gs://migration-ucaip-trainingaip-20210325211532/batch_output/",
      "data_format": "JSON",
      "runtime_version": "2.1",
      "uri": "gs://migration-ucaip-trainingaip-20210325211532/custom_job_TF_20210325211532",
      "region": "us-central1"
    }
  },
  "headers": {
    "accept": "application/json",
    "accept-encoding": "gzip, deflate",
    "user-agent": "(gzip)",
    "x-goog-api-client": "gdcl/1.12.8 gl-python/3.7.8"
  },
  "methodId": "ml.projects.jobs.create",
  "resumable": null,
  "response_callbacks": [],
  "_in_error_state": false,
  "body_size": 0,
  "resumable_uri": null,
  "resumable_progress": 0
}
Call
End of explanation
print(json.dumps(response, indent=2))
Explanation: Response
End of explanation
# The full unique ID for the batch prediction job
batch_job_id = PARENT + "/jobs/" + response["jobId"]
print(batch_job_id)
Explanation: Example output:
{
  "jobId": "custom_job_TF_pred_20210325211532",
  "predictionInput": {
    "dataFormat": "JSON",
    "inputPaths": [
      "gs://migration-ucaip-trainingaip-20210325211532/test.json"
    ],
    "outputPath": "gs://migration-ucaip-trainingaip-20210325211532/batch_output/",
    "region": "us-central1",
    "runtimeVersion": "2.1",
    "uri": "gs://migration-ucaip-trainingaip-20210325211532/custom_job_TF_20210325211532",
    "framework": "TENSORFLOW"
  },
  "createTime": "2021-03-25T21:34:56Z",
  "state": "QUEUED",
  "predictionOutput": {
    "outputPath": "gs://migration-ucaip-trainingaip-20210325211532/batch_output/"
  },
  "etag": "QwNOFOfoKdY="
}
End of explanation
request = cloudml.projects().jobs().get(name=batch_job_id)
response = request.execute()
Explanation: projects.jobs.get
Call
End of explanation
print(json.dumps(response, indent=2))
Explanation: Response
End of explanation
while True:
    response = request = cloudml.projects().jobs().get(name=batch_job_id).execute()
    if response["state"] != "SUCCEEDED":
        print("The job has not completed:", response["state"])
        if response["state"] == "FAILED":
            break
    else:
        folder = response["predictionInput"]["outputPath"][:-1]
        ! gsutil ls $folder/prediction*
        ! gsutil cat $folder/prediction*
        break
    time.sleep(60)
Explanation: Example output:
{
  "jobId": "custom_job_TF_pred_20210325211532",
  "predictionInput": {
    "dataFormat": "JSON",
    "inputPaths": [
      "gs://migration-ucaip-trainingaip-20210325211532/test.json"
    ],
    "outputPath": "gs://migration-ucaip-trainingaip-20210325211532/batch_output/",
    "region": "us-central1",
    "runtimeVersion": "2.1",
    "uri": "gs://migration-ucaip-trainingaip-20210325211532/custom_job_TF_20210325211532",
    "framework": "TENSORFLOW"
  },
  "createTime": "2021-03-25T21:34:56Z",
  "state": "QUEUED",
  "predictionOutput": {
    "outputPath": "gs://migration-ucaip-trainingaip-20210325211532/batch_output/"
  },
  "etag": "NSbtn4XnbbU="
}
End of explanation
request = cloudml.projects().models().create(parent=PARENT)
request.body = json.loads(json.dumps({"name": "custom_job_TF_" + TIMESTAMP}, indent=2))
print(json.dumps(json.loads(request.to_json()), indent=2))
request = (
    cloudml.projects()
    .models()
    .create(parent=PARENT, body={"name": "custom_job_TF_" + TIMESTAMP})
)
Explanation: Example output:
gs://migration-ucaip-trainingaip-20210325211532/batch_output/prediction.errors_stats-00000-of-00001
gs://migration-ucaip-trainingaip-20210325211532/batch_output/prediction.results-00000-of-00001
{"prediction": [0.033321816474199295, 0.052459586411714554, 0.1548144668340683, 0.11401787400245667, 0.17382358014583588, 0.09015274047851562, 0.19865882396697998, 0.10446511209011078, 0.029874442145228386, 0.048411525785923004], "key": "tmp1.jpg"}
{"prediction": [0.03346974775195122, 0.05255022272467613, 0.15449963510036469, 0.11388237029314041, 0.17408262193202972, 0.08989296853542328, 0.19814379513263702, 0.10520868003368378, 0.02989153563976288, 0.04837837815284729], "key": "tmp2.jpg"}
Make online predictions
Deploy the model
projects.models.create
Request
End of explanation
response = request.execute()
Explanation: Example output:
{
  "uri": "https://ml.googleapis.com/v1/projects/migration-ucaip-training/models?alt=json",
  "method": "POST",
  "body": {
    "name": "custom_job_TF_20210325211532"
  },
  "headers": {
    "accept": "application/json",
    "accept-encoding": "gzip, deflate",
    "user-agent": "(gzip)",
    "x-goog-api-client": "gdcl/1.12.8 gl-python/3.7.8"
  },
  "methodId": "ml.projects.models.create",
  "resumable": null,
  "response_callbacks": [],
  "_in_error_state": false,
  "body_size": 0,
  "resumable_uri": null,
  "resumable_progress": 0
}
Call
End of explanation
print(json.dumps(response, indent=2))
Explanation: Response
End of explanation
# The full unique ID for the training pipeline
model_id = response["name"]
# The short numeric ID for the training pipeline
model_short_name = model_id.split("/")[-1]
print(model_id)
Explanation: Example output:
{
  "name": "projects/migration-ucaip-training/models/custom_job_TF_20210325211532",
  "regions": [
    "us-central1"
  ],
  "etag": "fFH1QQbH3tA="
}
End of explanation
version = {
    "name": "custom_job_TF_" + TIMESTAMP,
    "deploymentUri": model_artifact_dir,
    "runtimeVersion": "2.1",
    "framework": "TENSORFLOW",
    "pythonVersion": "3.7",
    "machineType": "mls1-c1-m2",
}
request = (
    cloudml.projects()
    .models()
    .versions()
    .create(
        parent=model_id,
    )
)
request.body = json.loads(json.dumps(version, indent=2))
print(json.dumps(json.loads(request.to_json()), indent=2))
request = cloudml.projects().models().versions().create(parent=model_id, body=version)
Explanation: projects.models.versions.create
Request
End of explanation
response = request.execute()
Explanation: Example output:
{
  "uri": "https://ml.googleapis.com/v1/projects/migration-ucaip-training/models/custom_job_TF_20210325211532/versions?alt=json",
  "method": "POST",
  "body": {
    "name": "custom_job_TF_20210325211532",
    "deploymentUri": "gs://migration-ucaip-trainingaip-20210325211532/custom_job_TF_20210325211532",
    "runtimeVersion": "2.1",
    "framework": "TENSORFLOW",
    "pythonVersion": "3.7",
    "machineType": "mls1-c1-m2"
  },
  "headers": {
    "accept": "application/json",
    "accept-encoding": "gzip, deflate",
    "user-agent": "(gzip)",
    "x-goog-api-client": "gdcl/1.12.8 gl-python/3.7.8"
  },
  "methodId": "ml.projects.models.versions.create",
  "resumable": null,
  "response_callbacks": [],
  "_in_error_state": false,
  "body_size": 0,
  "resumable_uri": null,
  "resumable_progress": 0
}
Call
End of explanation
print(json.dumps(response, indent=2))
Explanation: Response
End of explanation
# The full unique ID for the model version
model_version_name = response["metadata"]["version"]["name"]
print(model_version_name)
while True:
    response = (
        cloudml.projects().models().versions().get(name=model_version_name).execute()
    )
    if response["state"] == "READY":
        print("Model version created.")
        break
    time.sleep(60)
Explanation: Example output:
{
  "name": "projects/migration-ucaip-training/operations/create_custom_job_TF_20210325211532_custom_job_TF_20210325211532-1616708521927",
  "metadata": {
    "@type": "type.googleapis.com/google.cloud.ml.v1.OperationMetadata",
    "createTime": "2021-03-25T21:42:02Z",
    "operationType": "CREATE_VERSION",
    "modelName": "projects/migration-ucaip-training/models/custom_job_TF_20210325211532",
    "version": {
      "name": "projects/migration-ucaip-training/models/custom_job_TF_20210325211532/versions/custom_job_TF_20210325211532",
      "deploymentUri": "gs://migration-ucaip-trainingaip-20210325211532/custom_job_TF_20210325211532",
      "createTime": "2021-03-25T21:42:01Z",
      "runtimeVersion": "2.1",
      "etag": "3vf44xGDtdw=",
      "framework": "TENSORFLOW",
      "machineType": "mls1-c1-m2",
      "pythonVersion": "3.7"
    }
  }
}
End of explanation
import base64
import json
import cv2
import tensorflow as tf
(_, _), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()
test_image_1, test_label_1 = x_test[0], y_test[0]
test_image_2, test_label_2 = x_test[1], y_test[1]
cv2.imwrite("tmp1.jpg", (test_image_1 * 255).astype(np.uint8))
cv2.imwrite("tmp2.jpg", (test_image_2 * 255).astype(np.uint8))
Explanation: Prepare input for online prediction
End of explanation
instances_list = []
for img in ["tmp1.jpg", "tmp2.jpg"]:
    bytes = tf.io.read_file(img)
    b64str = base64.b64encode(bytes.numpy()).decode("utf-8")
    instances_list.append({"key": img, input_name: {"b64": b64str}})
request = cloudml.projects().predict(name=model_version_name)
request.body = json.loads(json.dumps({"instances": instances_list}, indent=2))
print(json.dumps(json.loads(request.to_json()), indent=2))
request = cloudml.projects().predict(
    name=model_version_name, body={"instances": instances_list}
)
Explanation: projects.predict
Request
End of explanation
response = request.execute()
Explanation: Example output:
{
  "uri": "https://ml.googleapis.com/v1/projects/migration-ucaip-training/models/custom_job_TF_20210325211532/versions/custom_job_TF_20210325211532:predict?alt=json",
  "method": "POST",
  "body": {
    "instances": [
      {
        "key": "tmp1.jpg",
        "bytes_inputs": {
          "b64": "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAIBAQEBAQIBAQECAgICAgQDAgICAgUEBAMEBgUGBgYFBgYGBwkIBgcJBwYGCAsICQoKCgoKBggLDAsKDAkKCgr/2wBDAQICAgICAgUDAwUKBwYHCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgr/wAARCAAgACADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqD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paQtraa1cISXiEqu9tKVydrbMZ5Kkg8jIr234a/Bq7+EngjQPAng3wzB/ZOl6ZFa2tpp/yeWiqFB2Hq2ASeuTz15r9ixHBa+vSp1JXpxXuy6vyfpbXuz8jocUyWCVSirTb1j09V95e+E3hnwXr8dn8QPjLaSWZBguP+EcudKSW6gnSMfLHOrcQh2djCSAxY5BxkzfEDx1H4n8ZyvpEC2WnMAwighMe8hvl3gZyQCB15K5xWNq3iKbVNVk8MW91NZzxLllkt9jL2z0I/DrXCeG47T4seNL3wN4c1nULKPTY2GoX8YYNcSkfKisxwis2ASMnk9AK7f8AiHuQ47CulWlKzfM7S5W+vRfgZQ47zvA4qNako3irK8eZLpfVn//Z"
        }
      },
      {
        "key": "tmp2.jpg",
        "bytes_inputs": {
          "b64": "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAIBAQEBAQIBAQECAgICAgQDAgICAgUEBAMEBgUGBgYFBgYGBwkIBgcJBwYGCAsICQoKCgoKBggLDAsKDAkKCgr/2wBDAQICAgICAgUDAwUKBwYHCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgr/wAARCAAgACADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl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pHbfjtu/KvhL9ub4tarruhy2JvJMsdjJFGFj28gKqrgKo9B6VhlvEGMzfDxm8M6N+kpRlJeT5dE/mwoZDiMO+evVb8j/2Q=="
        }
      }
    ]
  },
  "headers": {
    "accept": "application/json",
    "accept-encoding": "gzip, deflate",
    "user-agent": "(gzip)",
    "x-goog-api-client": "gdcl/1.12.8 gl-python/3.7.8"
  },
  "methodId": "ml.projects.predict",
  "resumable": null,
  "response_callbacks": [],
  "_in_error_state": false,
  "body_size": 0,
  "resumable_uri": null,
  "resumable_progress": 0
}
Call
End of explanation
print(json.dumps(response, indent=2))
Explanation: Response
End of explanation
request = cloudml.projects().models().versions().delete(name=model_version_name)
response = request.execute()
Explanation: Example output:
{
  "predictions": [
    {
      "key": "tmp1.jpg",
      "prediction": [
        0.033321816474199295,
        0.052459586411714554,
        0.1548144668340683,
        0.11401788890361786,
        0.17382356524467468,
        0.09015275537967682,
        0.19865882396697998,
        0.10446509718894958,
        0.02987445704638958,
        0.048411525785923004
      ]
    },
    {
      "key": "tmp2.jpg",
      "prediction": [
        0.03346974775195122,
        0.052550218999385834,
        0.15449965000152588,
        0.11388237029314041,
        0.17408263683319092,
        0.08989296108484268,
        0.19814379513263702,
        0.10520866513252258,
        0.02989153563976288,
        0.04837837815284729
      ]
    }
  ]
}
projects.models.versions.delete
Call
End of explanation
print(json.dumps(response, indent=2))
Explanation: Response
End of explanation
delete_model = True
delete_bucket = True
# Delete the model using the Vertex AI fully qualified identifier for the model
try:
    if delete_model:
        cloudml.projects().models().delete(name=model_id)
except Exception as e:
    print(e)
if delete_bucket and "BUCKET_NAME" in globals():
    ! gsutil rm -r gs://$BUCKET_NAME
Explanation: Example output:
{
  "name": "projects/migration-ucaip-training/operations/delete_custom_job_TF_20210325211532_custom_job_TF_20210325211532-1616708584436",
  "metadata": {
    "@type": "type.googleapis.com/google.cloud.ml.v1.OperationMetadata",
    "createTime": "2021-03-25T21:43:04Z",
    "operationType": "DELETE_VERSION",
    "modelName": "projects/migration-ucaip-training/models/custom_job_TF_20210325211532",
    "version": {
      "name": "projects/migration-ucaip-training/models/custom_job_TF_20210325211532/versions/custom_job_TF_20210325211532",
      "deploymentUri": "gs://migration-ucaip-trainingaip-20210325211532/custom_job_TF_20210325211532",
      "createTime": "2021-03-25T21:42:01Z",
      "runtimeVersion": "2.1",
      "state": "READY",
      "etag": "Nu2QJaCl6vw=",
      "framework": "TENSORFLOW",
      "machineType": "mls1-c1-m2",
      "pythonVersion": "3.7"
    }
  }
}
Cleanup
End of explanation
<END_TASK>