ModelEMA
Updated Exponential Moving Average (EMA) from https://github.com/rwightman/pytorch-image-models
Keeps a moving average of everything in the model state_dict (parameters and buffers)
For EMA details see https://www.tensorflow.org/api_docs/python/tf/train/ExponentialMovingAverage
To disable EMA set the enabled
attribute to False
.
Source code in ultralytics/yolo/utils/torch_utils.py
__init__(model, decay=0.9999, tau=2000, updates=0)
Create EMA.
Source code in ultralytics/yolo/utils/torch_utils.py
update(model)
Update EMA parameters.
Source code in ultralytics/yolo/utils/torch_utils.py
update_attr(model, include=(), exclude=('process_group', 'reducer'))
Updates attributes and saves stripped model with optimizer removed.
EarlyStopping
Early stopping class that stops training when a specified number of epochs have passed without improvement.
Source code in ultralytics/yolo/utils/torch_utils.py
__call__(epoch, fitness)
Check whether to stop training
Parameters:
Name | Type | Description | Default |
---|---|---|---|
epoch |
int
|
Current epoch of training |
required |
fitness |
float
|
Fitness value of current epoch |
required |
Returns:
Type | Description |
---|---|
bool
|
True if training should stop, False otherwise |
Source code in ultralytics/yolo/utils/torch_utils.py
__init__(patience=50)
Initialize early stopping object
Parameters:
Name | Type | Description | Default |
---|---|---|---|
patience |
int
|
Number of epochs to wait after fitness stops improving before stopping. |
50
|
Source code in ultralytics/yolo/utils/torch_utils.py
torch_distributed_zero_first
Decorator to make all processes in distributed training wait for each local_master to do something.
Source code in ultralytics/yolo/utils/torch_utils.py
smart_inference_mode
Applies torch.inference_mode() decorator if torch>=1.9.0 else torch.no_grad() decorator.
Source code in ultralytics/yolo/utils/torch_utils.py
select_device
Selects PyTorch Device. Options are device = None or 'cpu' or 0 or '0' or '0,1,2,3'.
Source code in ultralytics/yolo/utils/torch_utils.py
time_sync
fuse_conv_and_bn
Fuse Conv2d() and BatchNorm2d() layers https://tehnokv.com/posts/fusing-batchnorm-and-conv/.
Source code in ultralytics/yolo/utils/torch_utils.py
fuse_deconv_and_bn
Fuse ConvTranspose2d() and BatchNorm2d() layers.
Source code in ultralytics/yolo/utils/torch_utils.py
model_info
Model information. imgsz may be int or list, i.e. imgsz=640 or imgsz=[640, 320].
Source code in ultralytics/yolo/utils/torch_utils.py
get_num_params
get_num_gradients
model_info_for_loggers
Return model info dict with useful model information.
Example for YOLOv8n
{'model/parameters': 3151904, 'model/GFLOPs': 8.746, 'model/speed_ONNX(ms)': 41.244, 'model/speed_TensorRT(ms)': 3.211, 'model/speed_PyTorch(ms)': 18.755}
Source code in ultralytics/yolo/utils/torch_utils.py
get_flops
Return a YOLO model's FLOPs.
Source code in ultralytics/yolo/utils/torch_utils.py
get_flops_with_torch_profiler
Source code in ultralytics/yolo/utils/torch_utils.py
initialize_weights
Initialize model weights to random values.
Source code in ultralytics/yolo/utils/torch_utils.py
scale_img
Source code in ultralytics/yolo/utils/torch_utils.py
make_divisible
copy_attr
Copies attributes from object 'b' to object 'a', with options to include/exclude certain attributes.
Source code in ultralytics/yolo/utils/torch_utils.py
get_latest_opset
intersect_dicts
Returns a dictionary of intersecting keys with matching shapes, excluding 'exclude' keys, using da values.
Source code in ultralytics/yolo/utils/torch_utils.py
is_parallel
de_parallel
one_cycle
init_seeds
Initialize random number generator (RNG) seeds https://pytorch.org/docs/stable/notes/randomness.html.
Source code in ultralytics/yolo/utils/torch_utils.py
strip_optimizer
Strip optimizer from 'f' to finalize training, optionally save as 's'.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
f |
str
|
file path to model to strip the optimizer from. Default is 'best.pt'. |
'best.pt'
|
s |
str
|
file path to save the model with stripped optimizer to. If not provided, 'f' will be overwritten. |
''
|
Returns:
Type | Description |
---|---|
None
|
None |
Usage
from pathlib import Path from ultralytics.yolo.utils.torch_utils import strip_optimizer for f in Path('/Users/glennjocher/Downloads/weights').rglob('*.pt'): strip_optimizer(f)
Source code in ultralytics/yolo/utils/torch_utils.py
profile
YOLOv8 speed/memory/FLOPs profiler
Usage
input = torch.randn(16, 3, 640, 640) m1 = lambda x: x * torch.sigmoid(x) m2 = nn.SiLU() profile(input, [m1, m2], n=100) # profile over 100 iterations
Source code in ultralytics/yolo/utils/torch_utils.py
Created 2023-04-16, Updated 2023-05-28
Authors: Glenn Jocher (4)