Fine-tune an image classification model with custom data on Vertex AI Pipelines

This tutorial shows you how to use Vertex AI Pipelines to run an end-to-end ML workflow, including the following tasks:

  • Import and transform data.
  • Fine-tune an image classification model from TFHub using the transformed data.
  • Import the trained model to Vertex AI Model Registry.
  • Optional: Deploy the model for online serving with Vertex AI Prediction.

Before you begin

  1. Ensure that you've completed steps 1-3 in Set up a Google Cloud project and a development environment.

  2. Create an isolated Python environment and install the Vertex AI SDK for Python.

  3. Install the Kubeflow Pipelines SDK:

    python3 -m pip install "kfp<2.0.0" "google-cloud-aiplatform>=1.16.0" --upgrade --quiet

Run the ML model training pipeline

The sample code does the following:

  • Loads components from a component repository to be used as pipeline building blocks.
  • Composes a pipeline by creating component tasks and passing data between them using arguments.
  • Submits the pipeline for execution on Vertex AI Pipelines. See Vertex AI Pipelines pricing.

Copy the following sample code into your development environment and run it.

Image classification

# python3 -m pip install "kfp<2.0.0" "google-cloud-aiplatform>=1.16.0" --upgrade --quiet
from kfp import components
from kfp.v2 import dsl

# %% Loading components
upload_Tensorflow_model_to_Google_Cloud_Vertex_AI_op = components.load_component_from_url('')
deploy_model_to_endpoint_op = components.load_component_from_url('')
transcode_imagedataset_tfrecord_from_csv_op = components.load_component_from_url('')
load_image_classification_model_from_tfhub_op = components.load_component_from_url('')
preprocess_image_data_op = components.load_component_from_url('')
train_tensorflow_image_classification_model_op = components.load_component_from_url('')

# %% Pipeline definition
def image_classification_pipeline():
    class_names = ['daisy', 'dandelion', 'roses', 'sunflowers', 'tulips']
    csv_image_data_path = 'gs://cloud-samples-data/ai-platform/flowers/flowers.csv'
    deploy_model = False

    image_data = dsl.importer(
        artifact_uri=csv_image_data_path, artifact_class=dsl.Dataset).output

    image_tfrecord_data = transcode_imagedataset_tfrecord_from_csv_op(

    loaded_model_outputs = load_image_classification_model_from_tfhub_op(

    preprocessed_data = preprocess_image_data_op(

    trained_model = (train_tensorflow_image_classification_model_op(
        preprocessed_training_data_path = preprocessed_data['preprocessed_training_data_path'],
        preprocessed_validation_data_path = preprocessed_data['preprocessed_validation_data_path'],
                   add_node_selector_constraint('', 'NVIDIA_TESLA_A100').

    vertex_model_name = upload_Tensorflow_model_to_Google_Cloud_Vertex_AI_op(

    # Deploying the model might incur additional costs over time
    if deploy_model:
        vertex_endpoint_name = deploy_model_to_endpoint_op(

pipeline_func = image_classification_pipeline

# %% Pipeline submission
if __name__ == '__main__':
    from import aiplatform

Note the following about the sample code provided:

  • A Kubeflow pipeline is defined as a Python function.
  • The pipeline's workflow steps are created using Kubeflow pipeline components. By using the outputs of a component as an input of another component, you define the pipeline's workflow as a graph. For example, the preprocess_image_data_op component task depends on the tfrecord_image_data_path output from the transcode_imagedataset_tfrecord_from_csv_op component task.
  • You create a pipeline run on Vertex AI Pipelines using the Vertex AI SDK for Python.

Monitor the pipeline

In the Google Cloud console, in the Vertex AI section, go to the Pipelines page and open the Runs tab.

Go to Pipeline runs

What's next