> For the complete documentation index, see [llms.txt](https://docs.unitlab.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.unitlab.ai/api-sdk-cli/zh/sdks/benchmarks.md).

# 基准测试

使用 Unitlab Python SDK 上传带隐藏答案的基准条目，导入参考标注并批准答案，用于评估标注员质量。

\*\*基准（benchmark）\*\*是带有隐藏答案的条目。标注员像处理其他条目一样标注它，其结果会与答案对比评分。可用基准衡量标注员在已知难点案例上的质量，例如被遮挡的夹爪或反光零件。

创建基准不会改变条目本身及其标注。答案初始为草稿，批准后才会生效。

## 上传新的基准条目 <a href="#upload-new-benchmark-items" id="upload-new-benchmark-items"></a>

```python
batch = project.upload("./gold", benchmark=True)
batch.wait()
print(batch.benchmarks)   # created, existing, deferred, skipped_by_reason
```

CLI：`unitlab project upload "$PROJECT_ID" --source ./gold --benchmark`。

仍在处理中的条目会在处理完成后被标记，失败的文件不会被标记。如果标记失败，上传仍然成功：`batch.benchmarks["error"]` 说明原因，可使用 `batch.benchmarks["unmarked_ids"]` 中的 ID 重试。CLI 会发出警告并以代码 1 退出。

## 标记已有条目 <a href="#mark-existing-items" id="mark-existing-items"></a>

```python
counts = project.mark_benchmarks(["DATASOURCE_ID_1", "DATASOURCE_ID_2"])
```

标记可以安全地重复执行：已有答案的条目计为 `existing`。传入 `when_ready=False` 可跳过仍在处理中的条目，而不是稍后再标记。

## 填充答案 <a href="#fill-the-answer-keys" id="fill-the-answer-keys"></a>

将参考标注导入答案：

```python
imported = project.annotation_imports.create(
    "./gold-labels.json",
    source_name="gold-v1",
    benchmark=True,
    approve_keys=True,
)
report = imported.commit()
print(report["benchmarks"])   # created, replaced, approved, not_approved, items
```

标注会写入每个匹配条目的答案，并替换已有答案。Data Group 的面板会填充该组的答案。`approve_keys=True` 会在提交时批准所有非空答案；`not_approved` 列出仍为草稿的答案及原因。也可以在 Workbench 中绘制和批准答案。

## 服务器支持 <a href="#server-support" id="server-support"></a>

| 功能                                               | 旧版服务器上的错误                                          |
| ------------------------------------------------ | -------------------------------------------------- |
| `benchmark=True` 上传、`mark_benchmarks()`          | `UnitlabError`，代码 `benchmarks_unsupported`         |
| `annotation_imports.create(..., benchmark=True)` | `AnnotationImportError`，代码 `benchmark_unsupported` |

基准功能需要 SDK 3.2.0 或更高版本。

## 相关内容 <a href="#related" id="related"></a>

[质量工作流](https://docs.unitlab.ai/documentation/zh/qa/qa-workflows) · [标注导入](https://docs.unitlab.ai/api-sdk-cli/zh/sdks/annotation-imports.md) · [项目上传](https://docs.unitlab.ai/api-sdk-cli/zh/sdks/project-uploads.md)
