如何为KubeFlow管道构建映像?

时间:2020-02-03 14:59:38

标签: 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管道如何将输入发送到此代码并将此代码的输出传输到下一步?

2 个答案:

答案 0 :(得分:1)

  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={})

答案 1 :(得分:0)

基本上,您可以按照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部分即可。