# classification_sequence

Python API: `depthai_nodes.node.parsers.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).

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.

#### Methods

##### 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.

Parameters

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

##### build

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

Configures the parser.

Parameters

 * `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`.

Returns

 * `ClassificationSequenceParser`: Returns the instantiated parser with the correct configuration.

##### 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); see that helper for tensor layout and validation details.

Parameters

 * `scores` (`np.ndarray`): Scores or logits of shape `(steps, classes)`, `(1, steps, classes)`, or `(steps, classes, 1)`.
 * `is_softmax` (`bool`): Whether scores already contain probabilities. If false, apply softmax.

Returns

 * `np.ndarray`: Float32 array of shape `(steps, classes)`. Softmax, when requested, runs over classes independently for each
   step.

##### 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.

Parameters

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

##### extract

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

Select and dequantize the model tensors needed for parsing.

Parameters

 * `output` (`dai.NNData`): Neural network output carrying tensors and source timestamps, sequence number, and optional image
   transformation.

Returns

 * `np.ndarray`: Dequantized float32 sequence score tensor. Class names must have been configured.

Raises

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

##### run

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

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.

##### setConcatenateClasses

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

Sets the concatenate_classes flag for the classification sequence model.

Parameters

 * `concatenate_classes` (`bool`): If True, concatenates consecutive classes into a single string. Used mostly for text
   processing.

##### setIgnoredIndexes

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

Sets the ignored_indexes for the classification sequence model.

Parameters

 * `ignored_indexes` (`list[int]`): A list of indexes to ignore during classification generation.

##### setRemoveDuplicates

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

Sets the remove_duplicates flag for the classification sequence model.

Parameters

 * `remove_duplicates` (`bool`): If True, removes consecutive duplicates from the sequence.

#### Attributes

##### 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.
