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

Ultralytics Code Refactor https://ultralytics.com/actions #13153

Merged
merged 2 commits into from
Jun 30, 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
2 changes: 2 additions & 0 deletions benchmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def run(
pt_only=False, # test PyTorch only
hard_fail=False, # throw error on benchmark failure
):
"""Run YOLOv5 benchmarks on multiple export formats and log results for model performance evaluation."""
y, t = [], time.time()
device = select_device(device)
model_type = type(attempt_load(weights, fuse=False)) # DetectionModel, SegmentationModel, etc.
Expand Down Expand Up @@ -124,6 +125,7 @@ def test(
pt_only=False, # test PyTorch only
hard_fail=False, # throw error on benchmark failure
):
"""Run YOLOv5 export tests for all supported formats and log the results, including inference speed and mAP."""
y, t = [], time.time()
device = select_device(device)
for i, (name, f, suffix, gpu) in export.export_formats().iterrows(): # index, (name, file, suffix, gpu-capable)
Expand Down
1 change: 1 addition & 0 deletions classify/predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def run(
dnn=False, # use OpenCV DNN for ONNX inference
vid_stride=1, # video frame-rate stride
):
"""Conducts YOLOv5 classification inference on diverse input sources and saves results."""
source = str(source)
save_img = not nosave and not source.endswith(".txt") # save inference images
is_file = Path(source).suffix[1:] in (IMG_FORMATS + VID_FORMATS)
Expand Down
1 change: 1 addition & 0 deletions classify/val.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def run(
criterion=None,
pbar=None,
):
"""Validates a YOLOv5 classification model on a dataset, computing metrics like top1 and top5 accuracy."""
# Initialize/load model and set device
training = model is not None
if training: # called by train.py
Expand Down
1 change: 1 addition & 0 deletions detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def run(
dnn=False, # use OpenCV DNN for ONNX inference
vid_stride=1, # video frame-rate stride
):
"""Runs YOLOv5 detection inference on various sources like images, videos, directories, streams, etc."""
source = str(source)
save_img = not nosave and not source.endswith(".txt") # save inference images
is_file = Path(source).suffix[1:] in (IMG_FORMATS + VID_FORMATS)
Expand Down
5 changes: 5 additions & 0 deletions export.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,9 @@ def export_saved_model(
keras=False,
prefix=colorstr("TensorFlow SavedModel:"),
):
"""Exports a YOLOv5 model to TensorFlow SavedModel format, supporting dynamic axes and non-maximum suppression
(NMS).
"""
# YOLOv5 TensorFlow SavedModel export
try:
import tensorflow as tf
Expand Down Expand Up @@ -477,6 +480,7 @@ def export_tflite(
keras_model, im, file, int8, per_tensor, data, nms, agnostic_nms, prefix=colorstr("TensorFlow Lite:")
):
# YOLOv5 TensorFlow Lite export
"""Exports YOLOv5 model to TensorFlow Lite format with optional FP16, INT8, and NMS support."""
import tensorflow as tf

LOGGER.info(f"\n{prefix} starting export with tensorflow {tf.__version__}...")
Expand Down Expand Up @@ -784,6 +788,7 @@ def run(
iou_thres=0.45, # TF.js NMS: IoU threshold
conf_thres=0.25, # TF.js NMS: confidence threshold
):
"""Exports YOLOv5 model to specified formats including ONNX, TensorRT, CoreML, and TensorFlow; see https://github.com/ultralytics/yolov5."""
t = time.time()
include = [x.lower() for x in include] # to lowercase
fmts = tuple(export_formats()["Argument"][1:]) # --include arguments
Expand Down
3 changes: 3 additions & 0 deletions models/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,9 @@ class Classify(nn.Module):
def __init__(
self, c1, c2, k=1, s=1, p=None, g=1, dropout_p=0.0
): # ch_in, ch_out, kernel, stride, padding, groups, dropout probability
"""Initializes YOLOv5 classification head with convolution, pooling, and dropout layers for input to output
channel transformation.
"""
super().__init__()
c_ = 1280 # efficientnet_b0 size
self.conv = Conv(c1, c_, k, s, autopad(k, p), g)
Expand Down
2 changes: 2 additions & 0 deletions models/tf.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,7 @@ def predict(
iou_thres=0.45,
conf_thres=0.25,
):
"""Runs inference on input data, with an option for TensorFlow NMS."""
y = [] # outputs
x = inputs
for m in self.model.layers:
Expand Down Expand Up @@ -730,6 +731,7 @@ def run(
dynamic=False, # dynamic batch size
):
# PyTorch model
"""Exports YOLOv5 model from PyTorch to TensorFlow and Keras formats, performing inference for validation."""
im = torch.zeros((batch_size, 3, *imgsz)) # BCHW image
model = attempt_load(weights, device=torch.device("cpu"), inplace=True, fuse=False)
_ = model(im) # inference
Expand Down
1 change: 1 addition & 0 deletions segment/predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def run(
vid_stride=1, # video frame-rate stride
retina_masks=False,
):
"""Run YOLOv5 segmentation inference on diverse sources including images, videos, directories, and streams."""
source = str(source)
save_img = not nosave and not source.endswith(".txt") # save inference images
is_file = Path(source).suffix[1:] in (IMG_FORMATS + VID_FORMATS)
Expand Down
3 changes: 3 additions & 0 deletions segment/val.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ def run(
compute_loss=None,
callbacks=Callbacks(),
):
"""Validates a YOLOv5 segmentation model on specified dataset, producing metrics, plots, and optional JSON
output.
"""
if save_json:
check_requirements("pycocotools>=2.0.6")
process = process_mask_native # more accurate
Expand Down
4 changes: 4 additions & 0 deletions utils/augmentations.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ def random_perspective(
# torchvision.transforms.RandomAffine(degrees=(-10, 10), translate=(0.1, 0.1), scale=(0.9, 1.1), shear=(-10, 10))
# targets = [cls, xyxy]

"""Applies random perspective transformation to an image, modifying the image and corresponding labels."""
height = im.shape[0] + border[0] * 2 # shape(h,w,c)
width = im.shape[1] + border[1] * 2

Expand Down Expand Up @@ -336,6 +337,9 @@ def classify_albumentations(
auto_aug=False,
):
# YOLOv5 classification Albumentations (optional, only used if package is installed)
"""Sets up and returns Albumentations transforms for YOLOv5 classification tasks depending on augmentation
settings.
"""
prefix = colorstr("albumentations: ")
try:
import albumentations as A
Expand Down
3 changes: 3 additions & 0 deletions utils/dataloaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ def create_dataloader(
shuffle=False,
seed=0,
):
"""Creates and returns a configured DataLoader instance for loading and processing image datasets."""
if rect and shuffle:
LOGGER.warning("WARNING ⚠️ --rect is incompatible with DataLoader shuffle, setting shuffle=False")
shuffle = False
Expand Down Expand Up @@ -552,6 +553,7 @@ def __init__(
rank=-1,
seed=0,
):
"""Initializes the YOLOv5 dataset loader, handling images and their labels, caching, and preprocessing."""
self.img_size = img_size
self.augment = augment
self.hyp = hyp
Expand Down Expand Up @@ -1351,6 +1353,7 @@ def create_classification_dataloader(
path, imgsz=224, batch_size=16, augment=True, cache=False, rank=-1, workers=8, shuffle=True
):
# Returns Dataloader object to be used with YOLOv5 Classifier
"""Creates a DataLoader for image classification, supporting caching, augmentation, and distributed training."""
with torch_distributed_zero_first(rank): # init dataset *.cache only once if DDP
dataset = ClassificationDataset(root=path, imgsz=imgsz, augment=augment, cache=cache)
batch_size = min(batch_size, len(dataset))
Expand Down
1 change: 1 addition & 0 deletions utils/segment/augmentations.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def random_perspective(
# torchvision.transforms.RandomAffine(degrees=(-10, 10), translate=(.1, .1), scale=(.9, 1.1), shear=(-10, 10))
# targets = [cls, xyxy]

"""Applies random perspective, rotation, scale, shear, and translation augmentations to an image and targets."""
height = im.shape[0] + border[0] * 2 # shape(h,w,c)
width = im.shape[1] + border[1] * 2

Expand Down
2 changes: 2 additions & 0 deletions utils/segment/dataloaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def create_dataloader(
overlap_mask=False,
seed=0,
):
"""Creates a dataloader for training, validating, or testing YOLO models with various dataset options."""
if rect and shuffle:
LOGGER.warning("WARNING ⚠️ --rect is incompatible with DataLoader shuffle, setting shuffle=False")
shuffle = False
Expand Down Expand Up @@ -102,6 +103,7 @@ def __init__(
rank=-1,
seed=0,
):
"""Initializes the dataset with image, label, and mask loading capabilities for training/testing."""
super().__init__(
path,
img_size,
Expand Down
1 change: 1 addition & 0 deletions val.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ def run(
callbacks=Callbacks(),
compute_loss=None,
):
"""Evaluates model on a dataset and logs performance metrics, results are saved to specific directories."""
# Initialize/load model and set device
training = model is not None
if training: # called by train.py
Expand Down
Loading