# classification_visualizer

Python API: `luxonis_train.attached_modules.visualizers.classification_visualizer`

Draws the predicted class name, the label class name, and a bar plot of the class probabilities.

## Classes

| Name | Description |
| --- | --- |
| [`ClassificationVisualizer`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/visualizers/classification_visualizer.md#classification_visualizer-Classes-ClassificationVisualizer) | Visualize classification predictions and optional labels. |

### class `ClassificationVisualizer`

```python
class ClassificationVisualizer(BaseVisualizer)
```

Visualize classification predictions and optional labels.

![https://raw.githubusercontent.com/luxonis/luxonis-train/e542cf0efa20a0fc5c781ff505d699031cb0d228/media/example_viz/class.png](https://raw.githubusercontent.com/luxonis/luxonis-train/e542cf0efa20a0fc5c781ff505d699031cb0d228/media/example_viz/class.png)

The image shows the target class and the predicted class. The plot shows the predicted probability of each class.

 * `Inputs:`: * `prediction_canvas`, `target_canvas` (`Tensor`): [B, 3, H, W]
    * `predictions` (`Tensor`): [B, nclasses] logits
    * `target` (`Tensor | None`): [B, nclasses] one-hot, or multi-hot with `multilabel`
 * `Outputs:`: * `Tensor | tuple[Tensor, Tensor]`: [B, 3, H, W], with a probability plot when `include_plot` is set

> **References**
> * Source: This project.
 * License: Apache-2.0 (this project)

> **Notes**
> Renders predicted and target class names with OpenCV and can add a Matplotlib probability bar plot.

> **Example**
> Attached to a `ClassificationHead` in `model.nodes`:

```yaml
- name: ClassificationHead
  inputs: [ResNet]
  visualizers:
    - name: ClassificationVisualizer
```

 * `Compatible with:`: * Used by:
   [ClassificationModel](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/config/predefined_models/classification/v1/model.md#model-Classes-ClassificationModel)
    * Nodes: *
      [ClassificationHead](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/heads/classification_head.md#classification_head-Classes-ClassificationHead)
       * [TransformerClassificationHead](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/heads/transformer_classification_head.md#transformer_classification_head-Classes-TransformerClassificationHead)

Attributes

| Name | Description |
| --- | --- |
| `supported_tasks` |  |

Methods

| Name | Description |
| --- | --- |
| [`__init__`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/visualizers/classification_visualizer.md#classification_visualizer-Classes-ClassificationVisualizer-__init__) | Initialize the visualizer and store the text and plot options. |
| [`forward`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/visualizers/classification_visualizer.md#classification_visualizer-Classes-ClassificationVisualizer-forward) | Write the class names on the images and plot the probabilities. |

#### method `__init__`

```python
def __init__(
    include_plot: bool = True,
    font_scale: float | None = None,
    color: tuple[int, int, int] = (255, 0, 0),
    thickness: int = 2,
    multilabel: bool = False,
    **kwargs,
)
```

Initialize the visualizer and store the text and plot options.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `include_plot` | `bool` | Whether [forward](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/visualizers/classification_visualizer.md#classification_visualizer-Classes-ClassificationVisualizer-forward) also returns a bar plot of the class probabilities as a second image. Defaults to `True`. | `True` |
| `font_scale` | `float | None` | Font scale of the OpenCV text. When `None`, [dynamically_determine_font_scale](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/visualizers/utils.md#utils-Functions-dynamically_determine_font_scale) derives the scale from the image size. The text lines then sit at 15 and 25 percent of the image height. A value of `0` selects the same line positions, but keeps the font scale `0` and sets the thickness to `1`. Any other value applies as given, together with `thickness`, and puts the text lines at `y = 50` and `y = 75` pixels. | `None` |
| `color` | `tuple[int, int, int]` | RGB color of the text. Defaults to `(255, 0, 0)`, red. | `(255, 0, 0)` |
| `thickness` | `int` | Thickness of the text strokes. With a derived font scale below `1`, the thickness becomes `1`. Defaults to `2`. | `2` |
| `multilabel` | `bool` | Whether the task is multi-label. When `True`, the text lists every class whose raw value is greater than `0.5`, and the plot shows sigmoid probabilities. When `False`, the text shows the class with the highest value, and the plot shows softmax probabilities. Defaults to `False`. | `False` |
| `**kwargs` |  | Keyword arguments forwarded to [BaseVisualizer](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/visualizers/base_visualizer.md#base_visualizer-Classes-BaseVisualizer), such as `scale` and `node`. |  |

#### method `forward`

```python
def forward(
    prediction_canvas: Tensor,
    target_canvas: Tensor,
    predictions: Tensor,
    target: Tensor | None,
) -> Tensor | tuple[Tensor, Tensor]
```

Write the class names on the images and plot the probabilities.

For each image, the method writes `Pred: <name>` on a copy of the target canvas image. When `target` is given, it writes `GT:
<name>` above it. Both lines use `color` and the font scale and thickness that `font_scale` selects. With `include_plot`, it also
renders a bar plot of the class probabilities at the size of the prediction canvas.

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `prediction_canvas` | `Tensor` | Images of shape `[B, 3, H, W]`. The method reads only its shape, dtype, and device, for the plots. | required |
| `target_canvas` | `Tensor` | Images of shape `[B, 3, H, W]` to write the text on. | required |
| `predictions` | `Tensor` | Logits of shape `[B, n_classes]`. | required |
| `target` | `Tensor | None` | One-hot labels of shape `[B, n_classes]`, or multi-hot with `multilabel`. `None` when the batch has no `classification` labels; then the method writes no `GT` line. | required |

Returns

| Type | Description |
| --- | --- |
| `Tensor | tuple[Tensor, Tensor]` | With `include_plot`, the pair `(text_images, plots)`, both of shape `[B, 3, H, W]`. Otherwise only the text images. |
