Vertex AI 模型批量预测、引用云存储上的现有模型和输入文件的问题

2024-03-07

我正在努力正确设置 Vertex AI 管道,该管道执行以下操作:

  1. 从 API 读取数据并存储到 GCS 并作为批量预测的输入。
  2. 获取现有模型(Vertex AI 上的视频分类)
  3. 使用点 1 的输入创建批量预测作业。
    正如您将看到的,我对 Vertex Pipelines/Kubeflow 没有太多经验,因此我寻求帮助/建议,希望这只是一些初学者的错误。 这是我用作管道的代码的要点
from google_cloud_pipeline_components import aiplatform as gcc_aip
from kfp.v2 import dsl

from kfp.v2.dsl import component
from kfp.v2.dsl import (
    Output,
    Artifact,
    Model,
)

PROJECT_ID = 'my-gcp-project'
BUCKET_NAME = "mybucket"
PIPELINE_ROOT = "{}/pipeline_root".format(BUCKET_NAME)


@component
def get_input_data() -> str:
    # getting data from API, save to Cloud Storage
    # return GS URI
    gcs_batch_input_path = 'gs://somebucket/file'
    return gcs_batch_input_path


@component(
    base_image="python:3.9",
    packages_to_install=['google-cloud-aiplatform==1.8.0']
)
def load_ml_model(project_id: str, model: Output[Artifact]):
    """Load existing Vertex model"""
    import google.cloud.aiplatform as aip

    model_id = '1234'
    model = aip.Model(model_name=model_id, project=project_id, location='us-central1')



@dsl.pipeline(
    name="batch-pipeline", pipeline_root=PIPELINE_ROOT,
)
def pipeline(gcp_project: str):
    input_data = get_input_data()
    ml_model = load_ml_model(gcp_project)

    gcc_aip.ModelBatchPredictOp(
        project=PROJECT_ID,
        job_display_name=f'test-prediction',
        model=ml_model.output,
        gcs_source_uris=[input_data.output],  # this doesn't work
        # gcs_source_uris=['gs://mybucket/output/'],  # hardcoded gs uri works
        gcs_destination_output_uri_prefix=f'gs://{PIPELINE_ROOT}/prediction_output/'
    )


if __name__ == '__main__':
    from kfp.v2 import compiler
    import google.cloud.aiplatform as aip
    pipeline_export_filepath = 'test-pipeline.json'
    compiler.Compiler().compile(pipeline_func=pipeline,
                                package_path=pipeline_export_filepath)
    # pipeline_params = {
    #     'gcp_project': PROJECT_ID,
    # }
    # job = aip.PipelineJob(
    #     display_name='test-pipeline',
    #     template_path=pipeline_export_filepath,
    #     pipeline_root=f'gs://{PIPELINE_ROOT}',
    #     project=PROJECT_ID,
    #     parameter_values=pipeline_params,
    # )

    # job.run()

运行管道时,它会在运行批量预测时抛出此异常:
details = "List of found errors: 1.Field: batch_prediction_job.model; Message: Invalid Model resource name. 所以我不确定可能出了什么问题。我尝试在笔记本中加载模型(在组件之外)并且它正确返回。

我遇到的第二个问题是引用 GCS URI 作为从组件到批处理作业输入的输出。

   input_data = get_input_data2()
   gcc_aip.ModelBatchPredictOp(
        project=PROJECT_ID,
        job_display_name=f'test-prediction',
        model=ml_model.output,
        gcs_source_uris=[input_data.output],  # this doesn't work
        # gcs_source_uris=['gs://mybucket/output/'],  # hardcoded gs uri works
        gcs_destination_output_uri_prefix=f'gs://{PIPELINE_ROOT}/prediction_output/'
    )

在编译过程中,我得到以下异常TypeError: Object of type PipelineParam is not JSON serializable,尽管我认为这可能是 ModelBatchPredictOp 组件的问题。

再次感谢任何帮助/建议,我从昨天开始处理这个问题,所以也许我错过了一些明显的东西。

我正在使用的库:

google-cloud-aiplatform==1.8.0  
google-cloud-pipeline-components==0.2.0  
kfp==1.8.10  
kfp-pipeline-spec==0.1.13  
kfp-server-api==1.7.1

UPDATE经过评论、一些研究和调整,用于参考模型:

@component
def load_ml_model(project_id: str, model: Output[Artifact]):
    region = 'us-central1'
    model_id = '1234'
    model_uid = f'projects/{project_id}/locations/{region}/models/{model_id}'
    model.uri = model_uid
    model.metadata['resourceName'] = model_uid

然后我可以按预期使用它:

batch_predict_op = gcc_aip.ModelBatchPredictOp(
        project=gcp_project,
        job_display_name=f'batch-prediction-test',
        model=ml_model.outputs['model'],
        gcs_source_uris=[input_batch_gcs_path],
gcs_destination_output_uri_prefix=f'gs://{BUCKET_NAME}/prediction_output/test'
    )

UPDATE 2对于 GCS 路径,解决方法是在组件外部定义路径并将其作为输入参数传递,例如(缩写):

@dsl.pipeline(
    name="my-pipeline",
    pipeline_root=PIPELINE_ROOT,
)
def pipeline(
        gcp_project: str,
        region: str,
        bucket: str
):
    ts = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
    
    gcs_prediction_input_path = f'gs://{BUCKET_NAME}/prediction_input/video_batch_prediction_input_{ts}.jsonl'
    batch_input_data_op = get_input_data(gcs_prediction_input_path)  # this loads input data to GCS path

    batch_predict_op = gcc_aip.ModelBatchPredictOp(
        project=gcp_project,
        model=training_job_run_op.outputs["model"],
        job_display_name='batch-prediction',
        # gcs_source_uris=[batch_input_data_op.output],
        gcs_source_uris=[gcs_prediction_input_path],
        gcs_destination_output_uri_prefix=f'gs://{BUCKET_NAME}/prediction_output/',
    ).after(batch_input_data_op)  # we need to add 'after' so it runs after input data is prepared since get_input_data doesn't returns anything

仍然不确定,为什么当我返回 GCS 路径时它不起作用/编译get_input_data成分


我很高兴您解决了大部分主要问题并找到了模型声明的解决方法。

为您input.output观察gcs_source_uris,其背后的原因是因为函数/类返回值的方式。如果你深入研究以下类/方法google_cloud_pipeline_components你会发现它实现了一个允许你使用的结构.outputs来自被调用函数的返回值。

如果你去实现管道的一个组件,你会发现它返回一个输出数组convert_method_to_component功能。因此,为了在您的自定义类/函数中实现该功能,您的函数应该返回一个可以作为属性调用的值。下面是它的基本实现。

class CustomClass():
     def __init__(self):
       self.return_val = {'path':'custompath','desc':'a desc'}
      
     @property
     def output(self):
       return self.return_val 

hello = CustomClass()
print(hello.output['path'])

如果您想深入了解它,可以访问以下页面:

  • 将方法转换为组件 https://github.com/bharathdsce/kubeflow/blob/fcd627714664956b2c280b0109b64633bc99fa05/components/google-cloud/google_cloud_pipeline_components/aiplatform/utils.py#L383,这是执行convert_method_to_component

  • 特性 https://www.programiz.com/python-programming/property,Python 中属性的基础知识。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Vertex AI 模型批量预测、引用云存储上的现有模型和输入文件的问题 的相关文章

  • SSR 如何与 PWA 结合

    如何使用 PWA 渐进式 Web 应用程序 进行 SSR 服务器端渲染 据我了解 SSR SSR 运行时将加载页面并运行必要的脚本以将数据加载到页面上 然后返回渲染后的html 这对于不会运行 javascript 的网络爬虫和无脚本的浏览
  • 使用字符串访问属性

    给定一个与对象字段同名的字符串 如何获取对象字段的引用 例如 假设我向 GetFieldByStr 方法传入一个名为 field1 的字符串 并且该对象具有字段名称 field1 那么如何获取对 field1 对象的引用 我假设以某种方式使
  • 使用 Ratchet\Push.js 加载页面后执行自定义脚本

    所以在 GitHub 文档上棘轮2 0 2 https github com twbs ratchet我发现了以下说法 包含 JavaScript 的脚本标签将不会在以下页面上执行 加载了push js 如果您想将事件处理程序附加到 其他页
  • 负整数除法令人惊讶的结果

    在我的应用程序中 我遇到了以下情况并对结果感到惊讶 8 7 2 均为整数 这是什么意思 对于实际值 即8 0 7 0 结果大致为 1 143 使用整数除法的结果将向下舍入到更负的值 2 这也称为 楼层划分 这就是为什么你会得到一些令人困惑的
  • 如何获取字符串中单词的所有组合

    我想获得字符串中所有相邻单词的组合 例如 细绳get all combinations我想要得到 get all combinations all combinations get all all get combinations 我写下一
  • SQL 多个 where 子句

    我找不到像这样使用多个 where 子句是否有效 我使用 JPA MySQL 我需要多个 where 子句 其中一个在这里是 not 还是我遗漏了一些东西 select d from T DEBIT d where d status PEN

随机推荐