最后更新
LuxonisModel 中。通过 YAML 配置这些组件,并从 CLI 或 Python 运行相同的工作流。预定义模型
Command Line
1luxonis_train list-models
2luxonis_train info --model detection --variant light--model 选择打包配置。--variant 选择其模型变体,并且需要 --model:Command Line
1luxonis_train train \
2 --model detection --variant light \
3 loader.params.dataset_name "my_dataset" \
4 trainer.epochs 50Python
1from luxonis_train import LuxonisModel
2
3model = LuxonisModel(
4 model="detection",
5 variant="light",
6 opts={
7 "loader.params.dataset_name": "my_dataset",
8 "trainer.epochs": 50,
9 },
10)
11model.train()--model 和 --config 互斥。在 Python 中,使用 model= 或 cfg 参数。打包配置从已安装的包中读取;它不需要本地 configs/ 目录。配置
| 部分 | 用途 |
|---|---|
model | 预定义架构或节点图,带有附加的损失、指标和可视化工具。 |
loader | 加载器实现、数据集参数以及用于每个视图的拆分。 |
trainer | 图像预处理、批处理、epochs、优化、硬件和回调。 |
tracker | 运行名称、输出目录以及 TensorBoard、MLflow 或 Weights & Biases 日志记录。 |
exporter | ONNX 导出、预处理元数据、平台转换和 AIMET 设置。 |
archiver | NN Archive 命名和上传目标。 |
tuner | Optuna 搜索空间、研究存储和优化目标。 |
name 选择一个已注册的实现,params 提供其构造函数参数。配置 schema 定义了字段和默认值;打包配置 提供了特定任务的示例。训练页面包含一个完整的 config.yaml 示例。运行它:Command Line
1luxonis_train train --config config.yaml trainer.epochs 50opts={"trainer.epochs": 50} 传递。Python 构造函数接受配置路径、字典或 Config 实例;当您提供已构造的 Config 实例时,opts 不适用。LuxonisTrain 检查点存储其配置和数据集元数据。当未提供 --config 或 --model 时,带有 --weights 的命令可以使用该配置。基于数据集的操作仍然需要访问数据集。有关示例,请参阅 评估 和 导出。模型图
model.predefined_model 将注册的模型族扩展为节点和附加模块。要自己定义图,请使用 model.nodes。节点的 inputs 命名前面的节点;input_sources 命名来自加载器的输入。使用 alias 来区分同一节点类的多个实例。此分类模型将骨干网络连接到头部,并附加损失、指标和可视化工具:Yaml
1model:
2 name: custom_classifier
3 nodes:
4 - name: ResNet
5 alias: backbone
6 variant: "18"
7 input_sources: [image]
8 - name: ClassificationHead
9 inputs: [backbone]
10 losses:
11 - name: CrossEntropyLoss
12 metrics:
13 - name: Accuracy
14 is_main_metric: true
15 visualizers:
16 - name: ClassificationVisualizermodel 部分与分类数据集的加载器和训练器设置一起使用。在具有多个任务的数据集上,将每个头部的 task_name 设置为其应学习的任务。在用于选择最佳指标检查点的指标上设置 is_main_metric: true。节点的 freezing 和 finetuning 设置控制哪些参数进行训练以及它们的优化器或调度器设置。自定义
CALLBACKS 显式注册。该框架还公开了优化器、调度器和训练策略的注册表。自定义回调
custom_components.py 中: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):
10 super().__init__()
11 self.message = message
12
13 def on_train_epoch_end(
14 self,
15 trainer: pl.Trainer,
16 pl_module: LuxonisLightningModule,
17 ) -> None:
18 print(self.message)Yaml
1trainer:
2 callbacks:
3 - name: CustomCallback
4 params:
5 message: "Training epoch completed"自定义损失
forward 并声明其支持的任务。输入参数名称决定它接收哪些预测和标签:predictions 选择任务的主要输出,target 选择其单个必需标签。对于具有多个标签的任务,请使用 target_boundingbox 等名称。将此单标签分类损失添加到 custom_components.py。它消耗分类 logits 和加载器的 one-hot 类目标:Python
1import torch.nn.functional as F
2from torch import Tensor
3
4from luxonis_train import BaseLoss, Tasks
5
6
7class SmoothedClassificationLoss(BaseLoss):
8 supported_tasks = [Tasks.CLASSIFICATION]
9
10 def __init__(self, smoothing: float = 0.1, **kwargs):
11 super().__init__(**kwargs)
12 self.smoothing = smoothing
13
14 def forward(self, predictions: Tensor, target: Tensor) -> Tensor:
15 return F.cross_entropy(
16 predictions,
17 target.argmax(dim=1),
18 label_smoothing=self.smoothing,
19 )Yaml
1model:
2 predefined_model:
3 name: ClassificationModel
4 variant: light
5 params:
6 loss: SmoothedClassificationLoss
7 loss_params:
8 smoothing: 0.1加载自定义组件
--source 选项;对于多个文件,重复使用:Command Line
1luxonis_train --source custom_components.py train --config config.yamlPython
1import custom_components
2from luxonis_train import LuxonisModel
3
4model = LuxonisModel("config.yaml")
5model.train()