概念
概念
概述
配置
配置组件
- Model: 定义模型的架构,包括节点、损失函数、可视化器和指标。
- Loader: 通过选择适当的数据集来配置数据加载。有关如何创建数据集的更多详细信息,请参阅 数据准备 部分。
- Trainer: 指定训练参数,包括批大小、周期数、预处理(归一化、增强)、优化器、调度器、回调函数等。有关更多详细信息,请参阅 训练 部分。
- Tracker: 监视和记录训练指标以供分析。有关更多详细信息,请参阅 跟踪 部分。
- Exporter: 定义训练模型的导出、存档和转换设置。有关更多详细信息,请参阅 导出 部分。
- Tuner: 调整超参数以优化模型性能。有关更多详细信息,请参阅 超参数调整 部分。
示例配置文件
Yaml
1model:
2 name: model_name
3
4 # 使用预定义的检测模型而不是手动定义架构
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# 数据集配置:从 Roboflow 下载并解析 COCO 数据集
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 # 使用 ImageNet 标准进行图像归一化
29 normalize:
30 active: true
31
32 # 使用 Albumentations 库进行图像增强
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: ConstantLR预定义模型
自定义
自定义组件
示例:自定义回调函数
Python
1import lightning.pytorch as pl
2
3from luxonis_train import LuxonisLightningModule
4from luxonis_train.registry import CALLBACKS
5
6@CALLBACKS.register()
7class CustomCallback(pl.Callback):
8 def __init__(self, message: str, **kwargs):
9 super().__init__(**kwargs)
10 self.message = message
11
12 # 将在每个训练周期结束时调用。
13 # 有关更多回调方法,请参阅 PyTorch Lightning 文档。
14 def on_train_epoch_end(
15 self,
16 trainer: pl.Trainer,
17 pl_module: LuxonisLightningModule,
18 ) -> None:
19 print(self.message)示例:自定义损失函数
Python
1from torch import Tensor
2
3from luxonis_train import BaseLoss, Tasks
4
5# `BaseNode`、`BaseLoss`、`BaseMetric`
6# 和 `BaseVisualizer` 的子类会自动注册。
7class CustomLoss(BaseLoss):
8 supported_tasks = [Tasks.CLASSIFICATION, Tasks.SEGMENTATION]
9
10 def __init__(self, smoothing: float, **kwargs):
11 super().__init__(**kwargs)
12 self.smoothing = smoothing
13
14 def forward(self, predictions: Tensor, targets: Tensor) -> Tensor:
15 # 在此处实现实际的损失逻辑。
16 value = predictions.sum() * self.smoothing
17 return value.abs()在配置中使用自定义组件
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!"将自定义组件包含在训练中
使用 CLI
Command Line
1luxonis_train --source custom_components.py train --config config.yaml使用 Python API
Python
1# 首先导入您的自定义组件
2from custom_components import *
3from luxonis_train import LuxonisModel
4
5model = LuxonisModel("config.yaml")
6model.train()