Utilities¶
Logging¶
yolo.utils.logging_utils
¶
Module for initializing logging tools used in machine learning and data processing. Supports integration with Weights & Biases (wandb), Loguru, TensorBoard, and other logging frameworks as needed.
This setup ensures consistent logging across various platforms, facilitating effective monitoring and debugging.
Example
from tools.logger import custom_logger custom_logger()
logger = logging.getLogger('yolo')
module-attribute
¶
Config
dataclass
¶
Source code in yolo/config/config.py
YOLOLayer
dataclass
¶
YOLO
¶
Bases: Module
A preliminary YOLO (You Only Look Once) model class still under development.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_cfg
|
ModelConfig
|
Configuration for the YOLO model. Expected to define the layers, parameters, and any other relevant configuration details. |
required |
Source code in yolo/model/builder.py
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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | |
save_load_weights(weights)
¶
Update the model's weights with the provided weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weights
|
Union[Path, OrderedDict]
|
A OrderedDict containing the new weights. |
required |
Source code in yolo/model/builder.py
EMA
¶
Bases: Callback
Exponential Moving Average of model weights as a Lightning Callback.
Keeps a shadow copy of model parameters smoothed over training steps
beta = decay * (1 - exp(-step / tau)) shadow = beta * shadow + (1 - beta) * model
The tau warmup ramps beta up from ~0 at step 0, so early noisy updates don't dominate the shadow. Validation always runs on shadow weights; training weights are swapped back immediately after.
Source code in yolo/training/callbacks.py
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 | |
setup(trainer, pl_module, stage)
¶
Initialise shadow from the model before training begins.
Source code in yolo/training/callbacks.py
update(pl_module)
¶
Blend model parameters into shadow; copy buffers directly.
Source code in yolo/training/callbacks.py
apply_shadow(pl_module)
¶
Snapshot training weights then load shadow weights into the model.
Source code in yolo/training/callbacks.py
restore(pl_module)
¶
Reload the training-weight snapshot, discarding the shadow swap.
Source code in yolo/training/callbacks.py
GradientAccumulation
¶
Bases: Callback
Source code in yolo/training/callbacks.py
YOLOCustomProgress
¶
YOLORichProgressBar
¶
Bases: RichProgressBar
Source code in yolo/utils/logging_utils.py
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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | |
YOLORichModelSummary
¶
Bases: RichModelSummary
Source code in yolo/utils/logging_utils.py
ImageLogger
¶
Bases: Callback
Source code in yolo/utils/logging_utils.py
make_ap_table(score, past_result=[], max_result=None, epoch=-1)
¶
Source code in yolo/utils/solver_utils.py
set_seed(seed)
¶
Source code in yolo/utils/logging_utils.py
setup_logger(logger_name, quiet=False)
¶
Source code in yolo/utils/logging_utils.py
setup(cfg)
¶
Source code in yolo/utils/logging_utils.py
log_model_structure(model)
¶
Source code in yolo/utils/logging_utils.py
validate_log_directory(cfg, exp_name)
¶
Source code in yolo/utils/logging_utils.py
log_bbox(bboxes, class_list=None, image_size=(640, 640))
¶
Convert bounding boxes tensor to a list of dictionaries for logging, normalized by the image size.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bboxes
|
Tensor
|
Bounding boxes with shape (N, 5) or (N, 6), where each box is [class_id, x_min, y_min, x_max, y_max, (confidence)]. |
required |
class_list
|
Optional[List[str]]
|
List of class names. Defaults to None. |
None
|
image_size
|
Tuple[int, int]
|
The size of the image, used for normalization. Defaults to (640, 640). |
(640, 640)
|
Returns:
| Type | Description |
|---|---|
List[dict]
|
List[dict]: List of dictionaries containing normalized bounding box information. |
Source code in yolo/utils/logging_utils.py
Deployment¶
yolo.utils.deploy_utils
¶
logger = logging.getLogger('yolo')
module-attribute
¶
Config
dataclass
¶
Source code in yolo/config/config.py
FastModelLoader
¶
Source code in yolo/utils/deploy_utils.py
create_model(model_cfg, weight_path=True, class_num=80)
¶
Constructs and returns a YOLO model from a model config.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_cfg
|
ModelConfig
|
The model configuration (architecture definition). |
required |
weight_path
|
Union[bool, Path]
|
Path to pretrained weights. |
True
|
class_num
|
int
|
Number of output classes. |
80
|
Returns:
| Name | Type | Description |
|---|---|---|
YOLO |
YOLO
|
An instance of the model defined by the given configuration. |
Source code in yolo/model/builder.py
Model Utilities¶
yolo.utils.model_utils
¶
IDX_TO_ID = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 67, 70, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 84, 85, 86, 87, 88, 89, 90]
module-attribute
¶
logger = logging.getLogger('yolo')
module-attribute
¶
NMSConfig
dataclass
¶
Anc2Box
¶
Source code in yolo/tasks/detection/postprocess.py
Vec2Box
¶
Source code in yolo/tasks/detection/postprocess.py
update(image_size)
¶
image_size: W, H
Source code in yolo/tasks/detection/postprocess.py
PostProcess
¶
TODO: function document scale back the prediction and do nms for pred_bbox
Source code in yolo/utils/model_utils.py
bbox_nms(cls_dist, bbox, nms_cfg, confidence=None)
¶
Source code in yolo/tasks/detection/postprocess.py
transform_bbox(bbox, indicator='xywh -> xyxy')
¶
Source code in yolo/tasks/detection/postprocess.py
initialize_distributed()
¶
Source code in yolo/utils/model_utils.py
get_device(device_spec)
¶
Source code in yolo/utils/model_utils.py
collect_prediction(predict_json, local_rank)
¶
Collects predictions from all distributed processes and gathers them on the main process (rank 0).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predict_json
|
List
|
The prediction data (can be of any type) generated by the current process. |
required |
local_rank
|
int
|
The rank of the current process. Typically, rank 0 is the main process. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
List |
List
|
The combined list of predictions from all processes if on rank 0, otherwise predict_json. |
Source code in yolo/utils/model_utils.py
predicts_to_json(img_paths, predicts, rev_tensor)
¶
TODO: function document turn a batch of imagepath and predicts(n x 6 for each image) to a List of diction(Detection output)
Source code in yolo/utils/model_utils.py
Format Converters¶
yolo.utils.format_converters
¶
convert_dict = {'19.cv1': '19.conv', '16.cv1': '16.conv', '.7.cv1': '.7.conv', '.5.cv1': '.5.conv', '.3.cv1': '.3.conv', '.28.': '.29.', '.25.': '.26.', '.22.': '.23.', 'cv': 'conv', '.m.': '.bottleneck.'}
module-attribute
¶
HEAD_NUM = '29'
module-attribute
¶
head_converter = {'head_conv': 'm', 'implicit_a': 'ia', 'implicit_m': 'im'}
module-attribute
¶
SPP_converter = {'pre_conv.0': 'cv1', 'pre_conv.1': 'cv3', 'pre_conv.2': 'cv4', 'post_conv.0': 'cv5', 'post_conv.1': 'cv6', 'short_conv': 'cv2', 'merge_conv': 'cv7'}
module-attribute
¶
REP_converter = {'conv1': 'rbr_dense', 'conv2': 'rbr_1x1', 'conv': '0', 'bn': '1'}
module-attribute
¶
replace_dict = {'cv': 'conv', '.m.': '.bottleneck.'}
module-attribute
¶
convert_weight(old_state_dict, new_state_dict, model_size=38)
¶
Source code in yolo/utils/format_converters.py
convert_weight_v7(old_state_dict, new_state_dict)
¶
Source code in yolo/utils/format_converters.py
convert_weight_seg(old_state_dict, new_state_dict)
¶
Source code in yolo/utils/format_converters.py
discretize_categories(categories)
¶
Maps each category id to a sequential integer index.