Concepts
Concepts
Overview
Configuration
Configuration Components
- Model: Defines the architecture of your model, including nodes, losses, visualizers and metrics.
- Loader: Configures data loading by selecting the appropriate dataset. For more details on how to create a dataset, refer to the Data Preparation section.
- Trainer: Specifies training parameters, including batch size, epochs, preprocessing (normalization, augmentations), optimizer, scheduler, callbacks, and more. For more details, refer to the Training section.
- Tracker: Monitors and logs training metrics for analysis. For more details, refer to the Tracking section.
- Exporter: Defines export, archiving, and conversion settings for trained models. For more details, refer to the Exporting section.
- Tuner: Tunes hyperparameters to optimize model performance. For more details, refer to the Hyperparameter Tuning section.
Example Configuration File
Yaml
1model:
2 name: model_name
3
4 # Use a predefined detection model instead of manually defining the architecture
5 predefined_model:
6 name: DetectionModel
7 params:
8 variant: light
9 loss_params:
10 iou_type: "siou"
11 n_warmup_epochs: 0
12
13# Dataset configuration: Download and parse the COCO dataset from Roboflow
14loader:
15 params:
16 dataset_name: coco_test
17 dataset_dir: "roboflow://team-roboflow/coco-128/2/coco"
18
19trainer:
20 batch_size: 8
21 epochs: 200
22 n_workers: 8
23 validation_interval: 10
24
25 preprocessing:
26 train_image_size: [384, 384]
27
28 # Image normalization using ImageNet standards
29 normalize:
30 active: true
31
32 # Image augmentations using Albumentations library
33 augmentations:
34 - name: Defocus
35 - name: Sharpen
36 - name: Flip
37
38 callbacks:
39 - name: ConvertOnTrainEnd
40 - name: TestOnTrainEnd
41
42 optimizer:
43 name: SGD
44 params:
45 lr: 0.02
46
47 scheduler:
48 name: ConstantLRPredefined Models
Customization
Custom Components
Example: Custom Callback
Python
1import lightning.pytorch as pl
2
3from luxonis_train import LuxonisLightningModule
4from luxonis_train.registry import CALLBACKS
5
6
7@CALLBACKS.register()
8class CustomCallback(pl.Callback):
9 def __init__(self, message: str, **kwargs):
10 super().__init__(**kwargs)
11 self.message = message
12
13 # Will be called at the end of each training epoch.
14 # Consult the PyTorch Lightning documentation for more callback methods.
15 def on_train_epoch_end(
16 self,
17 trainer: pl.Trainer,
18 pl_module: LuxonisLightningModule,
19 ) -> None:
20 print(self.message)Example: Custom Loss Function
Python
1from torch import Tensor
2
3from luxonis_train import BaseLoss, Tasks
4
5
6# Subclasses of `BaseNode`, `BaseLoss`, `BaseMetric`
7# and `BaseVisualizer` are registered automatically.
8class CustomLoss(BaseLoss):
9 supported_tasks = [Tasks.CLASSIFICATION, Tasks.SEGMENTATION]
10
11 def __init__(self, smoothing: float, **kwargs):
12 super().__init__(**kwargs)
13 self.smoothing = smoothing
14
15 def forward(self, predictions: Tensor, targets: Tensor) -> Tensor:
16 # Implement the actual loss logic here.
17 value = predictions.sum() * self.smoothing
18 return value.abs()Using Custom Components in Configuration
Yaml
1model:
2 nodes:
3 - name: SegmentationHead
4 losses:
5 - name: CustomLoss
6 params:
7 smoothing: 0.0001
8
9trainer:
10 callbacks:
11 - name: CustomCallback
12 params:
13 lr: "Hello from the custom callback!"Including Custom Components in Training
Using the CLI
Command Line
1luxonis_train --source custom_components.py train --config config.yamlUsing the Python API
Python
1# Import your custom components first
2from custom_components import *
3from luxonis_train import LuxonisModel
4
5model = LuxonisModel("config.yaml")
6model.train()