# Evaluation

## Overview

LuxonisTrain provides tools to evaluate your trained models effectively. This means you can both test the model to gather
performance metrics and infer to visually inspect model predictions.

## Testing

To evaluate the performance of your model, you can run various tests to get metrics such as accuracy, precision, recall, and more.
These metrics help you understand how well your model is performing on your test dataset.

You can evaluate your trained model on different dataset views, such as train, validation (val), or test, to get a comprehensive
understanding of its performance.

The results of testing are also logged to the location configured in the
[tracking](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/training.md) section.

### CLI

```bash
luxonis_train test --config configs/detection_light_model.yaml \
                   --view val                                  \
                   --weights path/to/checkpoint.ckpt
```

### Python API

```python
from luxonis_train import LuxonisModel

model = LuxonisModel("configs/detection_light_model.yaml")
model.test(weights="path/to/checkpoint.ckpt")
```

> **Note:**
> The testing process can be started automatically at the end of the training by using the TestOnTrainEnd callback.

## Inference

Inferring involves using the model to make predictions on new data. This allows you to visually inspect how well the model
performs and observe the predictions it generates.

To run inference on images, datasets, or videos using LuxonisTrain, you can use either the CLI or the Python API to perform
inference with your trained models.

### CLI

#### Inference on a Dataset View

```bash
luxonis_train infer --config configs/detection_light_model.yaml \
                    --view val                                  \
                    --weights path/to/checkpoint.ckpt
```

#### Inference on a Video File

```bash
luxonis_train infer --config configs/detection_light_model.yaml \
                    --weights path/to/checkpoint.ckpt           \
                    --source-path path/to/video.mp4
```

#### Inference on an Image Directory

```bash
luxonis_train infer --config configs/detection_light_model.yaml \
                    --weights path/to/checkpoint.ckpt           \
                    --source-path path/to/images
```

#### Inference on a single Image

```bash
luxonis_train infer --config configs/detection_light_model.yaml \
                    --weights path/to/checkpoint.ckpt           \
                    --source-path path/to/image.jpg
```

### Python API

```python
from luxonis_train import LuxonisModel

model = LuxonisModel("configs/detection_light_model.yaml")

# Infer on a dataset view
model.infer(weights="path/to/checkpoint.ckpt", view="val")

# Infer on a video file
model.infer(weights="path/to/checkpoint.ckpt", source_path="path/to/video.mp4")

# Infer on an image directory and save the results
model.infer(
    weights="path/to/checkpoint.ckpt",
    source_path="path/to/images",
    save_dir="path/to/save_directory",
)
```

## Annotation

LuxonisTrain can also annotate a directory of images using the model predictions and create a new LDF dataset. This is useful when
you want to bootstrap labels from an already trained model.

### CLI

```bash
luxonis_train annotate --config configs/detection_light_model.yaml \
                       --weights path/to/checkpoint.ckpt           \
                       --source-path path/to/images
```

### Python API

```python
from luxonis_train import LuxonisModel

model = LuxonisModel("configs/detection_light_model.yaml")
dataset = model.annotate(
    dir_path="path/to/images",
    dataset_name="annotated_dataset",
    weights="path/to/checkpoint.ckpt",
)
```

The resulting dataset is returned as a LuxonisDataset, so it can be inspected, exported, or reused directly in a later training
run.

By using both testing and inference, you can gain comprehensive insights into your model's performance and make necessary
adjustments to improve its accuracy and reliability.
