如何从 kubeflow 管道 0.2.5 中的容器化组件导出指标

How to export metrics from a containerized component in kubeflow pipelines 0.2.5

我有一个由 3 个容器化组件组成的管道。在最后一个组件中,我将我想要的指标写入名为 /mlpipeline-metrics.json 的文件中,就像 here 中所解释的那样。 这是我使用的 Python 代码。

metrics = {
    'metrics': [
        {
            'name': 'accuracy',
            'numberValue':  accuracy,
            'format': 'PERCENTAGE',
        },
        {
            'name': 'average-f1-score',
            'numberValue': average_f1_score,
            'format': 'PERCENTAGE'
        },
    ]
}

with open('/mlpipeline-metrics.json', 'w') as f:
    json.dump(metrics, f)

我也试过用下面的代码写文件,就像上面链接的例子一样。

with file_io.FileIO('/mlpipeline-metrics.json', 'w') as f:
    json.dump(metrics, f)

管道运行良好,没有任何错误。但它不会在前端显示指标 UI.

我认为它与以下代码块有关。

def metric_op(accuracy, f1_scores):
    return dsl.ContainerOp(
        name='visualize_metrics',
        image='gcr.io/mgcp-1190085-asml-lpd-dev/kfp/jonas/container_tests/image_metric_comp',
        arguments=[
            '--accuracy', accuracy,
            '--f1_scores', f1_scores,
        ]
    )

这是我用来从容器化组件创建 ContainerOp 的代码。请注意,我没有指定任何 file_outputs。 在其他 ContainerOp 中,我必须指定 file_outputs 才能将变量传递到管道中的后续步骤。我应该在这里做类似的事情来将 /mlpipeline-metrics.json 映射到某个东西上以便 kubeflow 管道检测到它吗?

我正在使用托管 AI 平台管道部署 运行 Kubeflow Pipelines 0.2.5 和 Python 3.6.8.

感谢任何帮助。

经过反复试验,我终于找到了解决方案。我很高兴地说我的直觉是正确的。它确实与我没有指定的 file_outputs 有关。 为了能够导出您的指标,您必须按如下方式设置 file_outputs

def metric_op(accuracy, f1_scores):
    return dsl.ContainerOp(
        name='visualize_metrics',
        image='gcr.io/mgcp-1190085-asml-lpd-dev/kfp/jonas/container_tests/image_metric_comp',
        arguments=[
            '--accuracy', accuracy,
            '--f1_scores', f1_scores,
        ],
        file_outputs={
            'mlpipeline-metrics': '/mlpipeline-metrics.json'
        }
    )

这是编写基于 python 函数的方法时显示指标的另一种方式:

# Define your components code as standalone python functions:======================
    def add(a: float, b: float) -> NamedTuple(
        'AddOutput',
        [
            ('sum', float),
            ('mlpipeline_metrics', 'Metrics')
        ]
    ):
        '''Calculates sum of two arguments'''
        sum = a+b

        metrics = {
            'add_metrics': [
                {
                    'name': 'sum',
                    'numberValue': float(sum),
                }
            ]
        }
        print("Add Result: ", sum) # this will print it online in the 'main-logs' of each task


        from collections import namedtuple
        addOutput = namedtuple(
            'AddOutput',
        ['sum', 'mlpipeline_metrics'])
        return addOutput(sum, metrics)  # the metrics will be uploaded to the cloud

注意:我只是在这里使用基本函数。我没有使用你的功能。