# 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

### ClassificationVisualizer

Visualize classification predictions and optional labels.

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)
    * Nodes: *
      [ClassificationHead](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/heads/classification_head.md)
       * [TransformerClassificationHead](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/heads/transformer_classification_head.md)

#### Methods

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

Parameters

 * `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)
   also returns a bar plot of the class probabilities as a second image. Defaults to `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)
   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.
 * `color` (`tuple[int, int, int]`): RGB color of the text. Defaults to `(255, 0, 0)`, red.
 * `thickness` (`int`): Thickness of the text strokes. With a derived font scale below `1`, the thickness becomes `1`. Defaults to
   `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`.
 * `**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),
   such as `scale` and `node`.

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

Parameters

 * `prediction_canvas` (`Tensor`): Images of shape `[B, 3, H, W]`. The method reads only its shape, dtype, and device, for the
   plots.
 * `target_canvas` (`Tensor`): Images of shape `[B, 3, H, W]` to write the text on.
 * `predictions` (`Tensor`): Logits of shape `[B, n_classes]`.
 * `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.

Returns

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

#### Attributes

##### supported_tasks
