使用 Model Registry 进行模型版本控制

借助模型版本控制,您可以创建同一模型的多个版本。通过模型版本控制,您可以一种适当的方式组织模型,以便于您浏览和了解哪些更改对模型有何影响。借助 Model Registry,您可以在一个视图中查看模型及其所有版本。您可以深入分析特定模型版本,并准确了解它们的表现情况。

导入新模型版本

从 Model Registry 中,您可以将模型作为现有模型的新版本导入。

控制台

  1. 在 Google Cloud 控制台中,进入 Model Registry 页面。
    进入 Model Registry
  2. 在页面顶部,选择导入
  3. 选择作为新版本导入
  4. 从下拉列表中选择此模型的新版本。添加可选的版本说明。
  5. (可选)将此模型设置为默认版本。每当模型用于预测时,系统都会预先选择默认版本(但您仍然可以选择其他版本)。
  6. 选择您的区域。
  7. 选择继续
  8. 在“模型设置”中,定义要使用的容器。您可以选择将模型工件导入到新的预构建容器中,也可以导入到现有的自定义容器中。 如需详细了解容器,请参阅将模型导入 Vertex AI
  9. 选择继续
  10. 可选:为模型添加可解释性支持。
  11. 选择导入

REST

在使用任何请求数据之前,请先进行以下替换:

  • PROJECT_ID:项目 ID。
  • LOCATION:位置。
  • MODEL_DISPLAY_NAME:模型名称。
  • ARTIFACT_URI:包含模型工件及其任何支持文件的目录的路径。
  • IMAGE_URI:要用作执行预测的自定义容器的 Docker 映像。
  • PARENT_MODEL:版本将上传到的模型的资源名称。

HTTP 方法和网址:

POST http://LOCATION_ID-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION_ID/models:upload

请求 JSON 正文:

{

  "model": {
    "displayName": "MODEL_DISPLAY_NAME",
    "artifactUri": "ARTIFACT_URI",
    "containerSpec": {
       "imageUri": "IMAGE_URI"
    }
   },
 "parentModel": "PARENT_MODEL"
}

如需发送您的请求,请展开以下选项之一:

您应该会收到一个成功的状态代码 (2xx) 和一个空响应。

Python

Python


from typing import List

from google.cloud import aiplatform

def upload_new_model_version_using_custom_training_pipeline(
    display_name: str,
    script_path: str,
    container_uri,
    model_serving_container_image_uri: str,
    dataset_id: str,
    replica_count: int,
    machine_type: str,
    accelerator_type: str,
    accelerator_count: int,
    parent_model: str,
    args: List[str],
    model_version_aliases: List[str],
    model_version_description: str,
    is_default_version: bool,
    project: str,
    location: str,
):
    """
    Uploads a new model version using a custom training pipeline.
    Args:
        display_name: The display name of the model version.
        script_path: The path to the Python script that trains the model.
        container_uri: The URI of the container to use for training.
        model_serving_container_image_uri: The URI of the serving container image to use.
        dataset_id: The ID of the dataset to use for training.
        replica_count: The number of replicas to use for training.
        machine_type: The machine type to use for training.
        accelerator_type: The accelerator type to use for training.
        accelerator_count: The number of accelerators to use for training.
        parent_model: The parent resource name of an existing model.
        args: A list of arguments to pass to the training script.
        model_version_aliases: The aliases of the model version to create.
        model_version_description: The description of the model version.
        is_default_version: Whether the model version is the default version.
        project: The project ID.
        location: The region name.
    Returns:
        The new version of the model.
    """
    # Initialize the client.
    aiplatform.init(project=project, location=location)

    # Create the training job.
    # This job will upload a new, non-default version of the my-training-job model
    job = aiplatform.CustomTrainingJob(
        display_name=display_name,
        script_path=script_path,
        container_uri=container_uri,
        model_serving_container_image_uri=model_serving_container_image_uri,
    )

    # Create dataset
    # This examples uses a TabularDataset, but you can use any dataset type.
    dataset = aiplatform.TabularDataset(dataset_id) if dataset_id else None

    # Run the training job.
    model = job.run(
        dataset=dataset,
        args=args,
        replica_count=replica_count,
        machine_type=machine_type,
        accelerator_type=accelerator_type,
        accelerator_count=accelerator_count,
        parent_model=parent_model,
        model_version_aliases=model_version_aliases,
        model_version_description=model_version_description,
        is_default_version=is_default_version,
    )

    return model

训练新模型版本

您可以从 Model Registry 训练模型的新版本。

控制台

您可以从 Model Registry 创建现有模型的一个版本。如需详细了解如何在训练流水线中添加或创建模型版本,请参阅 CustomJob 和模型上传

  1. 在 Google Cloud 控制台中,进入 Model Registry 页面。
    <a{: class="button button-primary" l10n-attrs-original-order="href,target,class,track-name,track-type" l10n-encrypted-href="eE471CdLRMtrJ6UgOX6O4ltigmNHgUGOXn/QVSGplOheMVJU/yDyZ/OoAbOtzTXtQQdc2CaGxVtVGC/5S+Yj6A==" target="console" track-name="consoleLink" track-type="tasks" }="">进入 Model Registry </a{:>
  2. 在页面顶部,选择创建。输入训练方法详情,然后选择模型训练方法。
  3. 点击继续
  4. 在“模型详情”下,选择训练新版本选项。从下拉列表中选择要向其添加新版本的模型。添加版本的说明。点击继续
  5. 计算和价格部分,输入预算,并在准备就绪后选择开始训练。模型训练完成后,新版本会显示在 Model Registry 中。

REST

在使用任何请求数据之前,请先进行以下替换:

  • TRAINING_PIPELINE_NAME:trainingPipeline 的显示名称
  • TRAINING_TASK_INPUT:训练任务的参数。
  • PARENT_MODEL:版本将上传到的模型的资源名称。

HTTP 方法和网址:

POST http://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/trainingPipelines

请求 JSON 正文:

{
  "displayName": "TRAINING_PIPELINE_NAME",
  "trainingTaskDefinition": "gs://google-cloud-aiplatform/schema/trainingjob/definition/custom_task_1.0.0.yaml",
  "trainingTaskInputs":"TRAINING_TASK_INPUT"
  },
  "modelToUpload": {
    "displayName": "MODEL_DISPLAY_NAME",
    "containerSpec": {
       "imageUri": "IMAGE_URI"
    },
  },
  "parentModel": "PARENT_MODEL",

如需发送您的请求,请展开以下选项之一:

您应该会收到一个成功的状态代码 (2xx) 和一个空响应。

Python

Python


from google.cloud import aiplatform

def create_default_model_sample(model_id: str, project: str, location: str):
    """
    Initialize a Model resource to represent an existing model version with alias 'default'.
    Args:
        model_id: The ID of the model to initialize. Parent resource name of the model is also accepted.
        project: The project ID.
        location: The region name.
    Returns:
        Model resource.
    """
    # Initialize the client.
    aiplatform.init(project=project, location=location)

    # Initialize the Model resource with the ID 'model_id'. The parent_name of the Model resource can be also
    # 'projects/<your-project-id>/locations/<your-region>/models/<your-model-id>'
    default_model = aiplatform.Model(model_name=model_id)

    return default_model

如何查看模型所有版本的列表

从模型版本的详情页面中,您可以选择部署和测试模型、设置批量预测,以及根据模型类型进行评估。您还可以直接从版本详情页面查看用于训练模型版本的数据集。

控制台

在 Model Registry 中,您可以查看模型所有版本的列表。该列表有助于您快速了解模型并方便您对模型进行组织。

  1. 在 Google Cloud 控制台中,进入 Model Registry 页面。
    <a{: class="button button-primary" l10n-attrs-original-order="href,target,class,track-name,track-type" l10n-encrypted-href="eE471CdLRMtrJ6UgOX6O4ltigmNHgUGOXn/QVSGplOheMVJU/yDyZ/OoAbOtzTXtQQdc2CaGxVtVGC/5S+Yj6A==" target="console" track-name="consoleLink" track-type="tasks" }="">进入 Model Registry</a{:>
  2. 在“名称”列下,选择具有多个版本的模型的名称。版本页面随即会打开。
  3. 该页面会显示所有版本及其关联版本 ID 的列表。

REST

在使用任何请求数据之前,请先进行以下替换:

  • PROJECT_ID:与此模型关联的项目 ID
  • LOCATION:您在其中使用 Vertex AI 的区域。
  • MODEL_ID:与特定模型关联的 ID。

HTTP 方法和网址:

GET http://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/models/MODEL_ID

如需发送您的请求,请展开以下选项之一:

您应该会收到一个成功的状态代码 (2xx) 和一个空响应。

Python

Python


from google.cloud import aiplatform

def list_model_versions_sample(model_id: str, project: str, location: str):
    """
    List all model versions of a model.
    Args:
        model_id: The ID of the model to list. Parent resource name of the model is also accepted.
        project: The project ID.
        location: The region name.
    Returns:
        versions: List of model versions.
    """
    # Initialize the client.
    aiplatform.init(project=project, location=location)

    # Initialize the Model Registry resource with the ID 'model_id'.The parent_name of Model resource can be also
    # 'projects/<your-project-id>/locations/<your-region>/models/<your-model-id>'
    model_registry = aiplatform.models.ModelRegistry(model=model_id)

    # List all model versions of the model.
    versions = model_registry.list_versions()

    return versions

如何查看模型版本详情

在 Model Registry 中,您可以查看模型及所有模型版本。从 Model Registry 中选择模型时,模型详情页面会显示该模型的详细信息及特定模型版本的详细信息。在详情页面上,您可以评估和测试模型版本、运行批量预测或将模型部署到端点以进行在线预测。

控制台

使用以下说明查看模型详情页面。如需查看版本详细信息,请点击版本名称。

  1. 在 Google Cloud 控制台中,进入 Model Registry 页面。
    <a{: class="button button-primary" l10n-attrs-original-order="href,target,class,track-name,track-type" l10n-encrypted-href="eE471CdLRMtrJ6UgOX6O4ltigmNHgUGOXn/QVSGplOheMVJU/yDyZ/OoAbOtzTXtQQdc2CaGxVtVGC/5S+Yj6A==" target="console" track-name="consoleLink" track-type="tasks" }="">进入 Model Registry</a{:>
  2. 在 Vertex AI Model Registry 中,点击模型名称以打开模型详细信息页面。
  3. 该页面会显示所有版本及其关联版本 ID 的列表。各个模型版本会按行进行分隔;并且模型版本详细信息中会提供版本 ID、模型别名、状态、说明和标签。
  4. 如需查看其中一个模型版本的详细信息,请选择相应的版本 ID。“模型详情”页面随即会打开。
  5. 在详情页面中,您可以进行评估、部署及测试,还可以使用批量预测以及查看版本详细信息。此外,通过此页面您还可以使用 Vertex AI 模型评估来比较各个模型版本。

Python

Python


from google.cloud import aiplatform

def get_model_version_info_sample(
    model_id: str, version_id: str, project: str, location: str
):
    """
    Get model version info.
    Args:
        model_id: The ID of the model.
        version_id: The version ID of the model version.
        project: The project ID.
        location: The region name.
    Returns:
        VersionInfo resource.
    """

    # Initialize the client.
    aiplatform.init(project=project, location=location)

    # Initialize the Model Registry resource with the ID 'model_id'.The parent_name of Model resource can be also
    # 'projects/<your-project-id>/locations/<your-region>/models/<your-model-id>'
    model_registry = aiplatform.models.ModelRegistry(model=model_id)

    # Get model version info with the version 'version_id'.
    model_version_info = model_registry.get_version_info(version=version_id)

    return model_version_info