Machine learning 如何为KubeFlow管道构建映像?

Machine learning 如何为KubeFlow管道构建映像?,machine-learning,kubernetes,kubeflow,kubeflow-pipelines,Machine Learning,Kubernetes,Kubeflow,Kubeflow Pipelines,我最近发现了kubeflow和kubeflow管道,但我不清楚如何从python程序构建映像 假设我有一个简单的python函数来裁剪图像: class Image_Proc: def crop_image(self, image, start_pixel, end_pixel): # crop return cropped_image 我该如何将其装箱并在KubeFlow管道中使用?我是否需要将其包装在API中(例如使用Flask),或者需要连接到某个

我最近发现了kubeflow和kubeflow管道,但我不清楚如何从python程序构建映像

假设我有一个简单的python函数来裁剪图像:

class Image_Proc:
    def crop_image(self, image, start_pixel, end_pixel):
        # crop
        return cropped_image
我该如何将其装箱并在KubeFlow管道中使用?我是否需要将其包装在API中(例如使用Flask),或者需要连接到某个媒体/数据代理


KubeFlow管道如何将输入发送到此代码并将此代码的输出传输到下一步?

基本上,您可以按照Docker提供的步骤创建Docker映像并发布到Docker Hub(或者您可以构建自己的私有Docker注册表,但我认为这对初学者来说可能太多了)。大致列出以下步骤:

  • 创建Dockerfile。在Dockerfile中,只需指定几项内容:基本映像(对于您的情况,只需使用Docker提供的python映像)、工作目录以及运行此映像时要执行的命令
  • 在本地运行映像以确保其按预期工作(如果尚未安装docker,请先安装docker),然后推送到docker Hub
  • 一旦发布,您将在发布到Docker Hub后拥有图像URL,然后在Kubeflow中创建管道时使用该URL
  • 此外,您还可以阅读本文以了解如何创建管道(Kubeflow管道只是argo工作流)。对于您的情况,只需在管道YAML文件中填写所需步骤的
    输入
    和/或
    输出
    部分

  • 您不需要构建图像。对于中小型组件,您可以在现有图像的基础上工作。检查一下。 有关python,请参见 有关非python的信息,请参见

  • KFPSDK对构建容器映像有一定的支持。请参见示例

  • 阅读官方的组件编写

  • 假设我有一个简单的python函数来裁剪图像:

    class Image_Proc:
        def crop_image(self, image, start_pixel, end_pixel):
            # crop
            return cropped_image
    
    您可以通过python函数创建一个组件,如下所示:

    from kfp.components import InputPath, OutputPath, create_component_from_func
    
    # Declare function (with annotations)
    def crop_image(
        image_path: InputPath(),
        start_pixel: int,
        end_pixel: int,
        cropped_image_path: OutputPath(),
    ):
        import some_image_lib
        some_image_lib.crop(image_path, start_pixel, end_pixel, cropped_image_path)
    
    # Create component
    crop_image_op = create_component_from_func(
      crop_image,
      # base_image=..., # Optional. Base image that has most of the packages that you need. E.g. tensorflow/tensorflow:2.2.0
      packages_to_install=['some_image_lib==1.2.3'],
      output_component_file='component.yaml', # Optional. Use this to share the component between pipelines, teams or people in the world
    )
    
    # Create pipeline
    def my_pipeline():
        download_image_task = download_image_op(...)
    
        crop_image_task = crop_image_op(
            image=download_image_task.output,
            start_pixel=10,
            end_pixel=200,
        )
    
    # Submit pipeline
    kfp.Client(host=...).create_run_from_pipeline_func(my_pipeline, arguments={})