【问题标题】:How to build an image for KubeFlow pipeline?如何为 KubeFlow 管道构建映像?
【发布时间】:2020-05-19 08:39:17
【问题描述】:

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

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

class Image_Proc:
    def crop_image(self, image, start_pixel, end_pixel):
        # crop
        return cropped_image

我应该如何将它容器化并在 KubeFlow 管道中使用它?我需要将其封装在 API 中(例如使用 Flask)还是需要连接到某些媒体/数据代理?

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

【问题讨论】:

    标签: machine-learning kubernetes kubeflow kubeflow-pipelines


    【解决方案1】:

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

    1. 创建 Dockerfile。在您的 Dockerfile 中,只需指定几件事:基本映像(对于您的情况,只需使用 Docker 中的 python 映像)、工作目录以及运行此映像时要执行的命令
    2. 在本地运行您的镜像以确保它按预期工作(如果您还没有安装 docker,请先安装),然后推送到 Docker Hub
    3. 发布后,您将在发布到 Docker Hub 后获得映像 URL,然后在 Kubeflow 中创建管道时使用该 URL。

    此外,您可以阅读此doc 以了解如何创建管道(Kubeflow 管道只是 argo 工作流程)。对于您的情况,只需在管道 YAML 文件中填写您想要的步骤的 inputs 和/或 outputs 部分。

    【讨论】:

      【解决方案2】:
      1. 您不需要构建映像。对于中小型组件,您可以在现有图像之上工作。检查lightweight components sample。 对于 python,请参阅Data passing in python components 对于非python,请参阅 Creating components from command-line programs

      2. KFP SDK 支持构建容器镜像。请参阅container_build 示例。

      3. 阅读官方组件创作documentation

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

      您可以像这样从 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={})
      

      【讨论】:

        猜你喜欢
        • 2019-11-15
        • 1970-01-01
        • 2021-07-03
        • 1970-01-01
        • 2020-10-27
        • 2019-06-30
        • 1970-01-01
        • 1970-01-01
        • 2020-12-25
        相关资源
        最近更新 更多