# 自定义Metrics

Sextant支持用户自定义Metrics计算方式，并输出计算结果。您只需要将自定义Metrics算法上传至Github Repo并把Url填入Sextant即可。

![](/files/-MjDCkRhtJ1dLmLwKNkO)

{% hint style="info" %}
需要将您的Metrics算法使用python进行编写，并放到Github中
{% endhint %}

## 支持类型

Sextant支持自定义Float和Curve两种类型的指标，并指定他们的生效范围

### Float

输出结果为一个浮点值，在前端显示为“名称=数值“的样式，如mAP=0.75

### Curve

输出结果为两个一维数组，分别命名为x和y，在前端渲染为曲线样式，x数组和y数组中的值会对应的渲染为曲线上点的横纵坐标

### 生效范围

可以定义指标的生效范围为Data级别或者Dataset级别

Data级别：针对参与评估的每个数据会返回一个对应值，例如单张数据的标注IoU均值

Dataset级别：每一次评估只会返回一个对应值，例如本次评估的mAP

## 定义规则

您的Metrics算法需要满足以下规则：

1. ‌如果您的代码中需要额外的依赖包，则需要在根目录下创建requirements.txt，并在其中写入依赖（不支持torch、tensorflow等深度学习框架的GPU运算）。
2. 您需要在github根路径下创建一个`__init__.py`，以确保repo被clone到本地后，其路径能够作为python package导出一个名为 Evaluator 的 class。
3. python库中有且只有一个名为 Evaluator 的 class
4. Evaluator 中需要有获取单个图片标注的评分方法 evaluate\_one\_data(input\_source: dict， input\_target: dict) -> dict 方法，input\_source 和 input\_target 值请参考[graviti标准化文档](https://tensorbay-python-sdk.graviti.com/en/stable/)
5. Evaluator 中需要有获取整体标注评分的方法 get\_result() -> dict
6. 以上两个方法的返回值需满足以下结构:（当前仅支持float和curve两种类型的数据）

## 代码示例

### 目录结构示例

```python
(yourgithubrepo_root) 
    -- __init.py 
    -- Evaluator.py 
    -- requirements.txt
```

### **init**.py示例

```python
from .Evaluator import Evaluator
all = ["Evaluator"]
```

### requirements.txt示例：

```python
numpy=1.21.0
```

### Evaluator.py示例

````python
import numpy as np

class Evaluator:
   def __init__(self):
       """
       You can initialize your model here
       """
       ...


   evaluate_one_data(self, input_source: dict， input_target: dict) -> dict:
       """
       Do the evaluation job
       :param input_source: Ground truth boxes in one image
       :param input_target：Target boxes in the same image
       :return: A dict containing evaluation on one image and each category within it.
       """
       ...
   def get_result(self) -> dict:
       """Overall evaluation.


       Returns:
           A dict containing overall evaluation on all images and all categories.
       """
```
{
   'scope':1,//1-data级别；2-dataset级别
   'overall': {
       'mAP':0.123, // Float类型返回
       'pr':{  // Curve类型返回
             'x':[1,0.5],
             'y':[1,0.5]//x数据将在前端渲染为横轴数据，y数据将在前端渲染为纵轴数据，x和y均为list，且长度一致。
            }
   },
   'categories': {
       '{your_category}': {
            'mAP': np.mean([1, 2, 3]).tolist(),
       }
   }
}

````


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.graviti.cn/apps/sextant/metrics.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
