# classification_sequence

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

## 类

### ClassificationSequenceParser

分类序列模型的后处理逻辑。模型多次预测类别并返回一个预测类别列表，其中每个项目对应于序列中的相对步骤。除了时间序列分类，此解析器还可以用于文本识别模型，其中单词可以解释为字符（类别）序列。

输出消息：

类型：dai.beta.Classifications

 * `**描述**:`：一个包含 `classes` 和 `scores` 属性的对象。`classes` 是一个包含预测类别的列表。`scores` 是一个对应的概率分数列表。

#### 方法

##### init

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

初始化解析器节点。

参数

 * `output_layer_name` (`str`)：与解析器相关的输出层名称。
 * `classes` (`list[str]`)：模型可用的类别列表。
 * `is_softmax` (`bool`)：如果为 False，则使用 softmax 函数将分数转换为概率。
 * `ignored_indexes` (`list[int]`)：在分类生成过程中要忽略的索引列表（例如，背景类、空格）。
 * `remove_duplicates` (`bool`)：如果为 True，则从序列中删除连续的重复项。
 * `concatenate_classes` (`bool`)：如果为 True，则根据预测的空格连接连续的单词。

##### build

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

配置解析器。

参数

 * `head_config` (`dict[str, Any]`)：解析器的头部配置。必需的键是 `classes`、`n_classes` 和
   `is_softmax`。除此之外，还有三个可选键，主要用于文本处理：`ignored_indexes`、`remove_duplicates` 和 `concatenate_classes`。

返回

 * `ClassificationSequenceParser`：返回具有正确配置的实例化解析器。

##### compute

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

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

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

参数

 * `scores` (`np.ndarray`)：形状为 `(steps, classes)`、`(1, steps, classes)` 或 `(steps, classes, 1)` 的分数或 logits。
 * `is_softmax` (`bool`)：分数是否已包含概率。如果为 false，则应用 softmax。

返回

 * `np.ndarray`：形状为 `(steps, classes)` 的 Float32 数组。当请求时，Softmax 会为每个步骤独立地在类别上运行。

##### emit

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

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

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

参数

 * `output` (`dai.NNData`)：携带张量和源时间戳、序列号以及可选图像转换的神经网络输出。
 * `scores` (`np.ndarray`)：形状为 `(steps, classes)` 的每步类别概率。

##### extract

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

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

参数

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

返回

 * `np.ndarray`：反量化的 float32 序列分数张量。必须已配置类名。

引发

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

##### run

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

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

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

##### setConcatenateClasses

```python
def setConcatenateClasses(concatenate_classes: bool):
```

为分类序列模型设置 concatenate_classes 标志。

参数

 * `concatenate_classes` (`bool`)：如果为 True，则将连续的类连接成一个字符串。主要用于文本处理。

##### setIgnoredIndexes

```python
def setIgnoredIndexes(ignored_indexes: list[int]):
```

为分类序列模型设置 ignored_indexes。

参数

 * `ignored_indexes` (`list[int]`)：在分类生成过程中要忽略的索引列表。

##### setRemoveDuplicates

```python
def setRemoveDuplicates(remove_duplicates: bool):
```

为分类序列模型设置 remove_duplicates 标志。

参数

 * `remove_duplicates` (`bool`)：如果为 True，则从序列中删除连续的重复项。

#### 属性

##### classes

模型可用的类别列表。

##### concatenate_classes

如果为 True，则根据预测的空格连接连续的单词。

##### ignored_indexes

在分类生成过程中要忽略的索引列表（例如，背景类、空格）。

##### is_softmax

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

##### output_layer_name

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

##### remove_duplicates

如果为 True，则从序列中删除连续的重复项。
