<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Feedback or issues?
For any feedback or questions, please open an issue.
Vertex SDK for Python
Step1:  Enter your project and GCS bucket
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.
Step2:  Initialize Vertex SDK for Python
Initialize the client for Vertex AI
Step6:  Write your Training Script
Write this cell as a file which will be used for custom training.
Instead of using a managed dataset, a Tensorflow Dataset URI is passed in through the 'args' parameter of the 'run' function. The script will download the data from the URI at training time.
Step7:  Launch a Training Job to Create a Model
Once we have defined your training script, we will create a model.
Step8:  Deploy Your Model
Deploy your model, then wait until the model FINISHES deployment before proceeding to prediction.
Step9:  Predict on the Endpoint
To do a prediction you will need some flowers images. You can download some photos of flowers or use the ones provided below.
<ASSISTANT_TASK:>
Python Code:
!pip3 uninstall -y google-cloud-aiplatform
!pip3 install google-cloud-aiplatform
import IPython
app = IPython.Application.instance()
app.kernel.do_shutdown(True)
Explanation: Feedback or issues?
For any feedback or questions, please open an issue.
Vertex SDK for Python: Custom Training Example with Unmanaged Image Dataset
To use this Colaboratory notebook, you copy the notebook to your own Google Drive and open it with Colaboratory (or Colab). You can run each step, or cell, and see its results. To run a cell, use Shift+Enter. Colab automatically displays the return value of the last line in each cell. For more information about running notebooks in Colab, see the Colab welcome page.
This notebook demonstrate how to create a custom model based on an image dataset. It will require you provide a bucket where the dataset will be stored.
Note: you may incur charges for training, prediction,  storage or usage of other GCP products in connection with testing this SDK.
Install Vertex SDK for Python, Authenticate, and upload of a Dataset to your GCS bucket
After the SDK installation the kernel will be automatically restarted. You may see this error message Your session crashed for an unknown reason which is normal.
End of explanation
import sys
if "google.colab" in sys.modules:
    from google.colab import auth
    auth.authenticate_user()
MY_PROJECT = "YOUR PROJECT"
MY_STAGING_BUCKET = "gs://YOUR BUCKET"  # bucket should be in same region as ucaip
Explanation: Enter your project and GCS bucket
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.
End of explanation
from google.cloud import aiplatform
aiplatform.init(project=MY_PROJECT, staging_bucket=MY_STAGING_BUCKET)
Explanation: Initialize Vertex SDK for Python
Initialize the client for Vertex AI
End of explanation
%%writefile training_script.py
# Source: https://cloud.google.com/vertex-ai/docs/tutorials/image-recognition-custom
import argparse
import logging
import os
import tensorflow as tf
import tensorflow_datasets as tfds
IMG_WIDTH = 128
def normalize_img(image):
    Normalizes image.
    * Resizes image to IMG_WIDTH x IMG_WIDTH pixels
    * Casts values from `uint8` to `float32`
    * Scales values from [0, 255] to [0, 1]
    Returns:
      A tensor with shape (IMG_WIDTH, IMG_WIDTH, 3). (3 color channels)
    
    image = tf.image.resize_with_pad(image, IMG_WIDTH, IMG_WIDTH)
    return image / 255.
def normalize_img_and_label(image, label):
    Normalizes image and label.
    * Performs normalize_img on image
    * Passes through label unchanged
    Returns:
      Tuple (image, label) where
      * image is a tensor with shape (IMG_WIDTH, IMG_WIDTH, 3). (3 color
        channels)
      * label is an unchanged integer [0, 4] representing flower type
    
    return normalize_img(image), label
def get_args():
  Argument parser.
  Returns:
    Dictionary of arguments.
  
  parser = argparse.ArgumentParser(description='Flower classification sample')
  parser.add_argument(
      '--tfds',
      default=None,
      help='The tfds URI from https://www.tensorflow.org/datasets/ to load the data from')
  args = parser.parse_args()
  return args
# Training settings
args = get_args()
if 'AIP_MODEL_DIR' not in os.environ:
    raise KeyError(
        'The `AIP_MODEL_DIR` environment variable has not been' +
        'set. See https://cloud.google.com/vertex-ai/docs/tutorials/image-recognition-custom/training'
    )
output_directory = os.environ['AIP_MODEL_DIR']
logging.info('Loading and preprocessing data ...')
dataset = tfds.load(args.tfds,
                    split='train',
                    try_gcs=True,
                    shuffle_files=True,
                    as_supervised=True)
dataset = dataset.map(normalize_img_and_label,
                      num_parallel_calls=tf.data.experimental.AUTOTUNE)
dataset = dataset.cache()
dataset = dataset.shuffle(1000)
dataset = dataset.batch(128)
dataset = dataset.prefetch(tf.data.experimental.AUTOTUNE)
logging.info('Creating and training model ...')
model = tf.keras.Sequential([
    tf.keras.layers.Conv2D(16,
                           3,
                           padding='same',
                           activation='relu',
                           input_shape=(IMG_WIDTH, IMG_WIDTH, 3)),
    tf.keras.layers.MaxPooling2D(),
    tf.keras.layers.Conv2D(32, 3, padding='same', activation='relu'),
    tf.keras.layers.MaxPooling2D(),
    tf.keras.layers.Conv2D(64, 3, padding='same', activation='relu'),
    tf.keras.layers.MaxPooling2D(),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(512, activation="relu"),
    tf.keras.layers.Dense(5)  # 5 classes
])
model.compile(
    optimizer='adam',
    loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
    metrics=['accuracy'])
model.fit(dataset, epochs=10)
logging.info(f'Exporting SavedModel to: {output_directory}')
# Add softmax layer for intepretability
probability_model = tf.keras.Sequential([model, tf.keras.layers.Softmax()])
probability_model.save(output_directory)
Explanation: Write your Training Script
Write this cell as a file which will be used for custom training.
Instead of using a managed dataset, a Tensorflow Dataset URI is passed in through the 'args' parameter of the 'run' function. The script will download the data from the URI at training time.
End of explanation
job = aiplatform.CustomTrainingJob(
    display_name="train-flowers-dist-1-replica",
    script_path="training_script.py",
    container_uri="gcr.io/cloud-aiplatform/training/tf-cpu.2-2:latest",
    requirements=["gcsfs==0.7.1"],
    model_serving_container_image_uri="gcr.io/cloud-aiplatform/prediction/tf2-cpu.2-2:latest",
)
model = job.run(
    args=["--tfds", "tf_flowers:3.*.*"],
    replica_count=1,
    model_display_name="flowers-model",
)
Explanation: Launch a Training Job to Create a Model
Once we have defined your training script, we will create a model.
End of explanation
endpoint = model.deploy(machine_type="n1-standard-4")
Explanation: Deploy Your Model
Deploy your model, then wait until the model FINISHES deployment before proceeding to prediction.
End of explanation
!gsutil -m cp -R gs://cloud-ml-data/img/flower_photos/daisy/14221848160_7f0a37c395.jpg .
!gsutil -m cp -R gs://cloud-ml-data/img/flower_photos/tulips/13289268363_b9337d751e.jpg .
!gsutil -m cp -R gs://cloud-ml-data/img/flower_photos/sunflowers/14623719696_1bb7970208_n.jpg .
import numpy as np
from PIL import Image
daisy_floats = np.array(Image.open("14221848160_7f0a37c395.jpg"))
small_image = np.array(Image.fromarray(np.uint8(daisy_floats)).resize((128, 128)))
endpoint.predict(instances=[small_image.tolist()])
Explanation: Predict on the Endpoint
To do a prediction you will need some flowers images. You can download some photos of flowers or use the ones provided below.
End of explanation
<END_TASK>