Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Added new rule in config auto-population and updated documentation. #140

Merged
merged 5 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions configs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,8 @@ 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 |

**Example:**
| `smart_cfg_auto_populate` | `bool` | `True` | Automatically populate sensible default values for missing config fields and log warnings |
JSabadin marked this conversation as resolved.
Show resolved Hide resolved

```yaml

Expand All @@ -247,8 +245,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:

#### 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.
Expand Down
39 changes: 23 additions & 16 deletions configs/detection_heavy_model.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
33 changes: 18 additions & 15 deletions configs/detection_light_model.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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
13 changes: 13 additions & 0 deletions luxonis_train/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions luxonis_train/config/predefined_models/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

**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:**

| Name | Alias | Function |
Expand Down
Loading