1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
- import os
- import requests
- import torch
- from PIL import Image
- from super_gradients.training import Trainer, dataloaders, models
- from super_gradients.training.dataloaders.dataloaders import (
- coco_detection_yolo_format_train, coco_detection_yolo_format_val
- )
- from super_gradients.training.losses import PPYoloELoss
- from super_gradients.training.metrics import DetectionMetrics_050
- from super_gradients.training.models.detection_models.pp_yolo_e import (
- PPYoloEPostPredictionCallback
- )
- class config:
- #trainer params
- CHECKPOINT_DIR = '../models' #specify the path you want to save checkpoints to
- EXPERIMENT_NAME = 'airplane_det_yolonas' #specify the experiment name
- #dataset params
- DATA_DIR = '../data/' #parent directory to where data lives
- TRAIN_IMAGES_DIR = 'images/train' #child dir of DATA_DIR where train images are
- TRAIN_LABELS_DIR = 'labels/train' #child dir of DATA_DIR where train labels are
- VAL_IMAGES_DIR = 'images/val' #child dir of DATA_DIR where validation images are
- VAL_LABELS_DIR = 'labels/val' #child dir of DATA_DIR where validation labels are
- # if you have a test set
- # TEST_IMAGES_DIR = 'test/images' #child dir of DATA_DIR where test images are
- # TEST_LABELS_DIR = 'test/labels' #child dir of DATA_DIR where test labels are
- CLASSES = ['airplane'] #what class names do you have
- NUM_CLASSES = len(CLASSES)
- #dataloader params - you can add whatever PyTorch dataloader params you have
- #could be different across train, val, and test
- DATALOADER_PARAMS={
- 'batch_size':16,
- 'num_workers':2
- }
- # model params
- MODEL_NAME = 'yolo_nas_m' # choose from yolo_nas_s, yolo_nas_m, yolo_nas_l
- PRETRAINED_WEIGHTS = 'coco' #only one option here: coco
- train_data = coco_detection_yolo_format_train(
- dataset_params={
- 'data_dir': config.DATA_DIR,
- 'images_dir': config.TRAIN_IMAGES_DIR,
- 'labels_dir': config.TRAIN_LABELS_DIR,
- 'classes': config.CLASSES
- },
- dataloader_params=config.DATALOADER_PARAMS
- )
- val_data = coco_detection_yolo_format_val(
- dataset_params={
- 'data_dir': config.DATA_DIR,
- 'images_dir': config.VAL_IMAGES_DIR,
- 'labels_dir': config.VAL_LABELS_DIR,
- 'classes': config.CLASSES
- },
- dataloader_params=config.DATALOADER_PARAMS
- )
- model = models.get(config.MODEL_NAME,
- num_classes=config.NUM_CLASSES,
- pretrained_weights=config.PRETRAINED_WEIGHTS
- )
- train_params = {
- "sg_logger": "dagshub_sg_logger",
- "sg_logger_params": # Params that will be passes to __init__ of the logger super_gradients.common.sg_loggers.dagshub_sg_logger.DagsHubSGLogger
- {
- "dagshub_repository": "DagsHub/PlaneDetector", # Optional: Your DagsHub project name, consisting of the owner name, followed by '/', and the repo name. If this is left empty, you'll be prompted in your run to fill it in manually.
- "log_mlflow_only": False, # Optional: Change to true to bypass logging to DVC, and log all artifacts only to MLflow
- "save_checkpoints_remote": True,
- "save_tensorboard_remote": True,
- "save_logs_remote": True,
- },
- # ENABLING SILENT MODE
- "average_best_models":True,
- "warmup_mode": "linear_epoch_step",
- # "warmup_initial_lr": 1e-2,#1e-6,
- "lr_warmup_epochs": 3,
- "initial_lr": 1e-2,
- "lr_mode": "cosine",
- "cosine_final_lr_ratio": 0.1,
- "optimizer": "SGD",
- # "optimizer_params": {"weight_decay": 0.0001},
- "zero_weight_decay_on_bias_and_bn": True,
- "ema": True,
- "ema_params": {"decay": 0.9, "decay_type": "threshold"},
- # ONLY TRAINING FOR 10 EPOCHS FOR THIS EXAMPLE NOTEBOOK
- "max_epochs": 100,
- "mixed_precision": True,
- "loss": PPYoloELoss(
- use_static_assigner=False,
- # NOTE: num_classes needs to be defined here
- num_classes=config.NUM_CLASSES,
- reg_max=16
- ),
- "valid_metrics_list": [
- DetectionMetrics_050(
- score_thres=0.1,
- top_k_predictions=300,
- # NOTE: num_classes needs to be defined here
- num_cls=config.NUM_CLASSES,
- normalize_targets=True,
- post_prediction_callback=PPYoloEPostPredictionCallback(
- score_threshold=0.01,
- nms_top_k=1000,
- max_predictions=300,
- nms_threshold=0.7
- )
- )
- ],
- "metric_to_watch": 'mAP@0.50'
- }
- trainer = Trainer(experiment_name=config.EXPERIMENT_NAME, ckpt_root_dir=config.CHECKPOINT_DIR)
- trainer.train(model=model,
- training_params=train_params,
- train_loader=train_data,
- valid_loader=val_data)
|