# classification

Python API: `depthai_nodes.node.parsers.classification`

## 类

### ClassificationParser

分类模型的后处理逻辑。

输出消息：

类型：dai.beta.Classifications

描述：一个包含 `classes` 和 `scores` 属性的对象。`classes` 是按分数降序排列的类别列表。`scores` 是对应的分数列表。

#### 方法

##### init

```python
def __init__(output_layer_name: str = '', classes: list[str] = None, is_softmax: bool = True):
```

初始化解析器节点。

参数

 * `output_layer_name` (`str`)：与解析器相关的输出层的名称。
 * `classes` (`list[str]`)：用于与各自得分关联的类名列表。预期与神经网络的输出顺序相同。如果未提供，消息将只返回排序后的得分。
 * `is_softmax` (`bool`)：如果为 False，则使用 softmax 函数将得分转换为概率。

##### build

```python
def build(head_config: dict[str, Any]) -> ClassificationParser:
```

配置解析器。

参数

 * `head_config` (`dict[str, Any]`)：解析器的头部配置。

返回

 * `ClassificationParser`：设置了头部配置的解析器对象。

##### compute

```python
def compute(scores: np.ndarray, *, is_softmax: bool = True) -> np.ndarray:
```

从提取的张量计算解析器结果，而不发送消息。

> **注意**
> 使用 [depthai_nodes.node.parsers.utils.classification.compute_classification_scores](/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/utils/classification.md)；有关张量布局和验证详细信息，请参阅该帮助程序。

参数

 * `scores` (`np.ndarray`)：模型得分张量。
 * `is_softmax` (`bool`)：得分是否已包含概率。如果为 false，则应用 softmax。

返回

 * `np.ndarray`：一维数组，每个类别一个得分。

##### emit

```python
def emit(output: dai.NNData, scores: np.ndarray):
```

创建 `dai.beta.Classifications` 消息并将其发送到 `out`。

复制源时间戳和序列号，并在存在时携带源图像转换。

参数

 * `output` (`dai.NNData`)：携带张量和源时间戳、序列号以及可选图像转换的神经网络输出。
 * `scores` (`np.ndarray`)：与计算出的有效载荷对应的置信度得分。

##### extract

```python
def extract(output: dai.NNData) -> np.ndarray:
```

选择并反量化解析所需的模型张量。

参数

 * `output` (`dai.NNData`)：携带张量和源时间戳、序列号以及可选图像转换的神经网络输出。

返回

 * `np.ndarray`：展平的反量化类得分。当设置了非零类计数时，它们的计数必须与配置的类匹配。

引发

 * `ValueError`：如果未配置输出名称且消息不包含恰好一个层，或者未满足配置的类要求。

##### run

```python
def run(self):
```

读取排队的网络输出，解析它们，并在运行时发出结果。

管道调用此处理循环。当输入队列关闭或节点停止时，它会退出。

##### setClasses

```python
def setClasses(classes: list[str]):
```

设置分类模型的类名。

参数

 * `classes` (`list[str]`)：用于与各自得分关联的类名列表。

##### setOutputLayerName

```python
def setOutputLayerName(output_layer_name: str):
```

设置输出层的名称。

参数

 * `output_layer_name` (`str`)：输出层的名称。

##### setSoftmax

```python
def setSoftmax(is_softmax: bool):
```

设置分类模型的 softmax 标志。

参数

 * `is_softmax` (`bool`)：如果为 False，解析器将使用 softmax 函数将得分转换为概率。

#### 属性

##### classes

用于与各自得分关联的类名列表。预期与神经网络的输出顺序相同。如果未提供，消息将只返回排序后的得分。

##### is_softmax

如果为 False，则使用 softmax 函数将得分转换为概率。

##### n_classes

##### output_layer_name

与解析器相关的输出层的名称。
