From 50d6182da3016553cecb0d210613a3ae5671120c Mon Sep 17 00:00:00 2001 From: Jernej Sabadin Date: Wed, 4 Dec 2024 14:11:14 +0100 Subject: [PATCH 1/5] feat: new rule in cfg auto populate and new docs --- configs/README.md | 28 ++++++++++++++++++++++++++-- luxonis_train/config/config.py | 13 +++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/configs/README.md b/configs/README.md index e7c29b26..c0d4e83b 100644 --- a/configs/README.md +++ b/configs/README.md @@ -225,8 +225,7 @@ Here you can change everything related to actual training of the model. | `save_top_k` | `-1 \| NonNegativeInt` | `3` | Save top K checkpoints based on validation loss when training | | `smart_cfg_auto_populate` | `bool` | `True` | Automatically populate sensible default values for missing config fields and log warnings | | `n_validation_batches` | `PositiveInt \| None` | `None` | Limits the number of validation/test batches and makes the val/test loaders deterministic | - -**Example:** +| `smart_cfg_auto_populate` | `bool` | `True` | Automatically populate sensible default values for missing config fields and log warnings | ```yaml @@ -247,8 +246,33 @@ trainer: skip_last_batch: true log_sub_losses: true save_top_k: 3 + smart_cfg_auto_populate: true ``` +### Smart Configuration Auto-population + +When setting `trainer.smart_cfg_auto_populate = True`, the following set of rules will be applied automatically to populate missing configuration fields with sensible defaults: + +#### Auto-population Rules + +1. **Default Optimizer and Scheduler:** + + - If `training_strategy` is not defined and neither `optimizer` nor `scheduler` is set, the following defaults are applied: + - Optimizer: `Adam` + - Scheduler: `ConstantLR` + +1. **CosineAnnealingLR Adjustment:** + + - If the `CosineAnnealingLR` scheduler is used and `T_max` is not set, it is automatically set to the number of epochs. + +1. **Mosaic4 Augmentation:** + + - If `Mosaic4` augmentation is used without `out_width` and `out_height` parameters, they are set to match the training image size. + +1. **Validation/Test Views:** + + - If `train_view`, `val_view`, and `test_view` are the same, and `n_validation_batches` is not explicitly set, it defaults to `10` to prevent validation/testing on the entire training set. + ### Preprocessing We use [`Albumentations`](https://albumentations.ai/docs/) library for `augmentations`. [Here](https://albumentations.ai/docs/api_reference/full_reference/#pixel-level-transforms) you can see a list of all pixel level augmentations supported, and [here](https://albumentations.ai/docs/api_reference/full_reference/#spatial-level-transforms) you see all spatial level transformations. In the configuration you can specify any augmentation from these lists and their parameters. diff --git a/luxonis_train/config/config.py b/luxonis_train/config/config.py index 941cd649..87b6c8de 100644 --- a/luxonis_train/config/config.py +++ b/luxonis_train/config/config.py @@ -580,6 +580,19 @@ def smart_auto_populate(cls, instance: "Config") -> None: "`Mosaic4` augmentation detected. Automatically set `out_width` and `out_height` to match `train_image_size`." ) + # Rule: If train, val, and test views are the same, set n_validation_batches + if ( + instance.loader.train_view + == instance.loader.val_view + == instance.loader.test_view + and instance.trainer.n_validation_batches is None + ): + instance.trainer.n_validation_batches = 10 + logger.warning( + "Train, validation, and test views are the same. Automatically set `n_validation_batches` to 10 to prevent validation/testing on the full train set. " + "If this behavior is not desired, set `smart_cfg_auto_populate` to `False`." + ) + def is_acyclic(graph: dict[str, list[str]]) -> bool: """Tests if graph is acyclic. From c557b45c1df509c2ccf9c07114201dbc49afaedc Mon Sep 17 00:00:00 2001 From: Jernej Sabadin Date: Wed, 4 Dec 2024 14:53:02 +0100 Subject: [PATCH 2/5] fix: removed redundant lines --- configs/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/configs/README.md b/configs/README.md index c0d4e83b..24185966 100644 --- a/configs/README.md +++ b/configs/README.md @@ -251,7 +251,7 @@ trainer: ### Smart Configuration Auto-population -When setting `trainer.smart_cfg_auto_populate = True`, the following set of rules will be applied automatically to populate missing configuration fields with sensible defaults: +When setting `trainer.smart_cfg_auto_populate = True`, the following set of rules will be applied: #### Auto-population Rules From e95e94f7141fbebf82725fbc6646dedb3b8c23f7 Mon Sep 17 00:00:00 2001 From: Jernej Sabadin Date: Thu, 5 Dec 2024 11:59:40 +0100 Subject: [PATCH 3/5] fix: remove redundant line --- configs/README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/configs/README.md b/configs/README.md index 24185966..37f74c37 100644 --- a/configs/README.md +++ b/configs/README.md @@ -223,7 +223,6 @@ Here you can change everything related to actual training of the model. | `verbose` | `bool` | `True` | Print all intermediate results to console | | `pin_memory` | `bool` | `True` | Whether to pin memory in the `DataLoader` | | `save_top_k` | `-1 \| NonNegativeInt` | `3` | Save top K checkpoints based on validation loss when training | -| `smart_cfg_auto_populate` | `bool` | `True` | Automatically populate sensible default values for missing config fields and log warnings | | `n_validation_batches` | `PositiveInt \| None` | `None` | Limits the number of validation/test batches and makes the val/test loaders deterministic | | `smart_cfg_auto_populate` | `bool` | `True` | Automatically populate sensible default values for missing config fields and log warnings | From 5a6d829857b80e422861f7b91c756d8d2f817453 Mon Sep 17 00:00:00 2001 From: Jernej Sabadin Date: Thu, 5 Dec 2024 12:29:13 +0100 Subject: [PATCH 4/5] feat: add training strategy to configs, update docs --- configs/detection_heavy_model.yaml | 39 +++++++++++-------- configs/detection_light_model.yaml | 33 +++++++++------- .../config/predefined_models/README.md | 4 ++ 3 files changed, 45 insertions(+), 31 deletions(-) diff --git a/configs/detection_heavy_model.yaml b/configs/detection_heavy_model.yaml index 294034c2..b56bdba6 100644 --- a/configs/detection_heavy_model.yaml +++ b/configs/detection_heavy_model.yaml @@ -6,6 +6,11 @@ model: name: DetectionModel params: variant: heavy + loss_params: + iou_type: "siou" + n_warmup_epochs: 0 # No assigner warmup + iou_loss_weight: 20 # Should be 2.5 * accumulate_grad_batches for best results + class_loss_weight: 8 # Should be 1 * accumulate_grad_batches for best results loader: params: @@ -19,27 +24,29 @@ trainer: active: true batch_size: 8 - epochs: &epochs 200 - n_workers: 4 + epochs: &epochs 300 + accumulate_grad_batches: 8 # For best results, always accumulate gradients to effectively use 64 batch size + n_workers: 8 validation_interval: 10 n_log_images: 8 callbacks: + - name: EMACallback + params: + decay: 0.9999 + use_dynamic_decay: True + decay_tau: 2000 - name: ExportOnTrainEnd - name: TestOnTrainEnd - optimizer: - name: SGD - params: + training_strategy: # Training from scratch params + name: "TripleLRSGDStrategy" + params: + warmup_epochs: 3 + warmup_bias_lr: 0.1 + warmup_momentum: 0.8 lr: 0.01 - momentum: 0.937 - weight_decay: 0.0005 - dampening: 0.0 - nesterov: true - - scheduler: - name: CosineAnnealingLR - params: - T_max: *epochs - eta_min: 0.0001 - last_epoch: -1 + lre: 0.0001 + momentum: 0.937 + weight_decay: 0.0005 + nesterov: True \ No newline at end of file diff --git a/configs/detection_light_model.yaml b/configs/detection_light_model.yaml index 13fdc54e..6cd85a0f 100644 --- a/configs/detection_light_model.yaml +++ b/configs/detection_light_model.yaml @@ -7,6 +7,11 @@ model: name: DetectionModel params: variant: light + loss_params: + iou_type: "siou" + n_warmup_epochs: 0 # No assigner warmup + iou_loss_weight: 20 # Should be 2.5 * accumulate_grad_batches for best results + class_loss_weight: 8 # Should be 1 * accumulate_grad_batches for best results loader: params: @@ -20,7 +25,8 @@ trainer: active: true batch_size: 8 - epochs: &epochs 200 + epochs: &epochs 300 + accumulate_grad_batches: 8 # For best results, always accumulate gradients to effectively use 64 batch size n_workers: 8 validation_interval: 10 n_log_images: 8 @@ -34,17 +40,14 @@ trainer: - name: ExportOnTrainEnd - name: TestOnTrainEnd - optimizer: - name: SGD - params: - lr: 0.0032 - momentum: 0.843 - weight_decay: 0.00036 - dampening: 0.0 - nesterov: true - - scheduler: - name: CosineAnnealingLR - params: - T_max: *epochs - eta_min: 0.000384 + training_strategy: # Fine tuning params + name: "TripleLRSGDStrategy" + params: + warmup_epochs: 2 + warmup_bias_lr: 0.05 + warmup_momentum: 0.5 + lr: 0.0032 + lre: 0.000384 + momentum: 0.843 + weight_decay: 0.00036 + nesterov: True \ No newline at end of file diff --git a/luxonis_train/config/predefined_models/README.md b/luxonis_train/config/predefined_models/README.md index 0d81a0ea..98f83831 100644 --- a/luxonis_train/config/predefined_models/README.md +++ b/luxonis_train/config/predefined_models/README.md @@ -60,6 +60,10 @@ The `DetectionModel` allows for both `"light"` and `"heavy"` variants, where the See an example configuration file using this predefined model [here](../../../configs/detection_light_model.yaml) for the `"light"` variant, and [here](../../../configs/detection_heavy_model.yaml) for the `"heavy"` variant. +This detection model is based on [YOLOv6: A Single-Stage Object Detection Framework for Industrial Applications](https://arxiv.org/pdf/2209.02976.pdf). The pretrained `"light"` model on COCOtrain2017 dataset achieves **36.1% mAP** on the COCOval2017 dataset. + +For best validation accuracies, choosing a **small `conf_thresh` (e.g., 0.03)** and a **high `iou_thresh` (e.g., 0.65)** can provide an additional few percentage points in the mAP metric. + **Components:** | Name | Alias | Function | From bc3b847be8fba357c205580eb4fa2a314a6c2c13 Mon Sep 17 00:00:00 2001 From: Jernej Sabadin Date: Thu, 5 Dec 2024 13:39:00 +0100 Subject: [PATCH 5/5] fix: docs rephrase --- luxonis_train/config/predefined_models/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/luxonis_train/config/predefined_models/README.md b/luxonis_train/config/predefined_models/README.md index 98f83831..9c6727fb 100644 --- a/luxonis_train/config/predefined_models/README.md +++ b/luxonis_train/config/predefined_models/README.md @@ -62,7 +62,7 @@ See an example configuration file using this predefined model [here](../../../co This detection model is based on [YOLOv6: A Single-Stage Object Detection Framework for Industrial Applications](https://arxiv.org/pdf/2209.02976.pdf). The pretrained `"light"` model on COCOtrain2017 dataset achieves **36.1% mAP** on the COCOval2017 dataset. -For best validation accuracies, choosing a **small `conf_thresh` (e.g., 0.03)** and a **high `iou_thresh` (e.g., 0.65)** can provide an additional few percentage points in the mAP metric. +**Note**: To align with the evaluation procedure used by other state-of-the-art (SOTA) models, we adopted a small `conf_thresh` (e.g., `0.03`) and a high `iou_thresh` (e.g., `0.65`) during validation. **Components:**