# classification_sequence

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

## Classes

| Name | Description |
| --- | --- |
| [`ClassificationSequenceParser`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/classification_sequence.md#classification_sequence-Classes-ClassificationSequenceParser) | Postprocessing logic for a classification sequence model. The model predicts the classes multiple times and returns a list of predicted classes, where each item corresponds to the relative step in the sequence. In addition to time series classification, this parser can also be used for text recognition models where words can be interpreted as a sequence of characters (classes). |

### class `ClassificationSequenceParser`

```python
class ClassificationSequenceParser(ClassificationParser)
```

Postprocessing logic for a classification sequence model. The model predicts the classes multiple times and returns a list of
predicted classes, where each item corresponds to the relative step in the sequence. In addition to time series classification,
this parser can also be used for text recognition models where words can be interpreted as a sequence of characters (classes).

Output messages:

Type: dai.beta.Classifications

 * `**Description**:`: An object with attributes `classes` and `scores`. `classes` is a list containing the predicted classes.
   `scores` is a list of corresponding probability scores.

Attributes

| Name | Description |
| --- | --- |
| `classes` | List of available classes for the model. |
| `concatenate_classes` | If True, concatenates consecutive words based on the predicted spaces. |
| `ignored_indexes` | List of indexes to ignore during classification generation (e.g., background class, blank space). |
| `is_softmax` | If False, the scores are converted to probabilities using softmax function. |
| `output_layer_name` | Name of the output layer relevant to the parser. |
| `remove_duplicates` | If True, removes consecutive duplicates from the sequence. |

Methods

| Name | Description |
| --- | --- |
| [`__init__`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/classification_sequence.md#classification_sequence-Classes-ClassificationSequenceParser-__init__) | Initializes the parser node. |
| [`build`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/classification_sequence.md#classification_sequence-Classes-ClassificationSequenceParser-build) | Configures the parser. |
| [`compute`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/classification_sequence.md#classification_sequence-Classes-ClassificationSequenceParser-compute) | Compute parser results from extracted tensors without sending messages. |
| [`emit`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/classification_sequence.md#classification_sequence-Classes-ClassificationSequenceParser-emit) | Create a `dai.beta.Classifications` message and send it on `out`. |
| [`extract`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/classification_sequence.md#classification_sequence-Classes-ClassificationSequenceParser-extract) | Select and dequantize the model tensors needed for parsing. |
| [`run`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/classification_sequence.md#classification_sequence-Classes-ClassificationSequenceParser-run) | Read queued network outputs, parse them, and emit results while running. |
| [`setConcatenateClasses`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/classification_sequence.md#classification_sequence-Classes-ClassificationSequenceParser-setConcatenateClasses) | Sets the concatenate_classes flag for the classification sequence model. |
| [`setIgnoredIndexes`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/classification_sequence.md#classification_sequence-Classes-ClassificationSequenceParser-setIgnoredIndexes) | Sets the ignored_indexes for the classification sequence model. |
| [`setRemoveDuplicates`](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/classification_sequence.md#classification_sequence-Classes-ClassificationSequenceParser-setRemoveDuplicates) | Sets the remove_duplicates flag for the classification sequence model. |

#### method `__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,
)
```

Initializes the parser node.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `output_layer_name` | `str` | Name of the output layer relevant to the parser. | `''` |
| `classes` | `list[str]` | List of available classes for the model. | `None` |
| `is_softmax` | `bool` | If False, the scores are converted to probabilities using softmax function. | `True` |
| `ignored_indexes` | `list[int]` | List of indexes to ignore during classification generation (e.g., background class, blank space). | `None` |
| `remove_duplicates` | `bool` | If True, removes consecutive duplicates from the sequence. | `False` |
| `concatenate_classes` | `bool` | If True, concatenates consecutive words based on the predicted spaces. | `False` |

#### method `build`

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

Configures the parser.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `head_config` | `dict[str, Any]` | The head configuration for the parser. The required keys are `classes`, `n_classes`, and `is_softmax`. In addition to these, there are three optional keys that are mostly used for text processing: `ignored_indexes`, `remove_duplicates` and `concatenate_classes`. | required |

Returns

| Type | Description |
| --- | --- |
| `ClassificationSequenceParser` | Returns the instantiated parser with the correct configuration. |

#### static method `compute`

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

Compute parser results from extracted tensors without sending messages.

> **Note**
> Uses [depthai_nodes.node.parsers.utils.classification_sequence.compute_classification_sequence_scores](https://docs.luxonis.com/software-v3/ai-inference/inference/depthai-nodes/depthai-nodes-api-reference/node/parsers/utils/classification_sequence.md#classification_sequence-Functions-compute_classification_sequence_scores); see that helper for tensor layout and validation details.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `scores` | `np.ndarray` | Scores or logits of shape `(steps, classes)`, `(1, steps, classes)`, or `(steps, classes, 1)`. | required |
| `is_softmax` | `bool` | Whether scores already contain probabilities. If false, apply softmax. | `True` |

Returns

| Type | Description |
| --- | --- |
| `np.ndarray` | Float32 array of shape `(steps, classes)`. Softmax, when requested, runs over classes independently for each step. |

#### method `emit`

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

Create a `dai.beta.Classifications` message and send it on `out`.

Copies source timestamps and sequence number, and carries the source image transformation when present.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `output` | `dai.NNData` | Neural network output carrying tensors and source timestamps, sequence number, and optional image transformation. | required |
| `scores` | `np.ndarray` | Per-step class probabilities of shape `(steps, classes)`. | required |

#### method `extract`

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

Select and dequantize the model tensors needed for parsing.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `output` | `dai.NNData` | Neural network output carrying tensors and source timestamps, sequence number, and optional image transformation. | required |

Returns

| Type | Description |
| --- | --- |
| `np.ndarray` | Dequantized float32 sequence score tensor. Class names must have been configured. |

Raises

| Type | Description |
| --- | --- |
| `ValueError` | If no output name is configured and the message does not contain exactly one layer, or configured class requirements are not met. |

#### method `run`

```python
def run()
```

Read queued network outputs, parse them, and emit results while running.

The pipeline invokes this processing loop. It exits when the input queue closes or the node stops.

#### method `setConcatenateClasses`

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

Sets the concatenate_classes flag for the classification sequence model.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `concatenate_classes` | `bool` | If True, concatenates consecutive classes into a single string. Used mostly for text processing. | required |

#### method `setIgnoredIndexes`

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

Sets the ignored_indexes for the classification sequence model.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `ignored_indexes` | `list[int]` | A list of indexes to ignore during classification generation. | required |

#### method `setRemoveDuplicates`

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

Sets the remove_duplicates flag for the classification sequence model.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `remove_duplicates` | `bool` | If True, removes consecutive duplicates from the sequence. | required |
