* Refactor code for speed and clarity

* Auto-format by https://ultralytics.com/actions

---------

Co-authored-by: UltralyticsAssistant <web@ultralytics.com>
This commit is contained in:
Glenn JocherandUltralyticsAssistant authored and GitHub committed 2024-07-16 01:20:57 +02:00
1 parent 41e26a7f83
commit 12be49963c
6 files changed
+216 -213

No files matched your search

+20 -15
View File
@@ -102,7 +102,7 @@ GIT_INFO = check_git_info()
def train(hyp, opt, device, callbacks):
"""
Trains a YOLOv5 model on a custom dataset using specified hyperparameters, options, and device, managing datasets,
Train a YOLOv5 model on a custom dataset using specified hyperparameters, options, and device, managing datasets,
model architecture, loss computation, and optimizer steps.
Args:
@@ -543,13 +543,13 @@ def train(hyp, opt, device, callbacks):
def parse_opt(known=False):
"""
Parses command-line arguments for YOLOv5 training, validation, and testing.
Parse command-line arguments for YOLOv5 training, validation, and testing.
Args:
known (bool, optional): If True, parses known arguments, ignoring the unknown. Defaults to False.
Returns:
argparse.Namespace: Parsed command-line arguments.
(argparse.Namespace): Parsed command-line arguments containing options for YOLOv5 execution.
Example:
```python
@@ -559,9 +559,9 @@ def parse_opt(known=False):
```
Links:
Models: https://github.com/ultralytics/yolov5/tree/master/models
Datasets: https://github.com/ultralytics/yolov5/tree/master/data
Tutorial: https://docs.ultralytics.com/yolov5/tutorials/train_custom_data
- Models: https://github.com/ultralytics/yolov5/tree/master/models
- Datasets: https://github.com/ultralytics/yolov5/tree/master/data
- Tutorial: https://docs.ultralytics.com/yolov5/tutorials/train_custom_data
"""
parser = argparse.ArgumentParser()
parser.add_argument("--weights", type=str, default=ROOT / "yolov5s.pt", help="initial weights path")
@@ -618,7 +618,7 @@ def parse_opt(known=False):
def main(opt, callbacks=Callbacks()):
"""
Runs training or hyperparameter evolution with specified options and optional callbacks.
Runs the main entry point for training or hyperparameter evolution with specified options and optional callbacks.
Args:
opt (argparse.Namespace): The command-line arguments parsed for YOLOv5 training and evolution.
@@ -629,7 +629,7 @@ def main(opt, callbacks=Callbacks()):
None
Note:
For detailed usage, visit:
For detailed usage, refer to:
https://github.com/ultralytics/yolov5/tree/master/models
"""
if RANK in {-1, 0}:
@@ -888,23 +888,27 @@ def main(opt, callbacks=Callbacks()):
def generate_individual(input_ranges, individual_length):
"""
Generate a random individual with gene values within specified input ranges.
Generate an individual with random hyperparameters within specified ranges.
Args:
input_ranges (list[tuple[float, float]]): List of tuples where each tuple contains the lower and upper bounds
for the corresponding gene.
individual_length (int): The number of genes in the individual.
for the corresponding gene (hyperparameter).
individual_length (int): The number of genes (hyperparameters) in the individual.
Returns:
list[float]: A list representing a generated individual with random gene values within the specified ranges.
Examples:
Example:
```python
input_ranges = [(0.01, 0.1), (0.1, 1.0), (0.9, 2.0)]
individual_length = 3
individual = generate_individual(input_ranges, individual_length)
print(individual) # Output: [0.035, 0.678, 1.456] (example output)
```
Note:
The individual returned will have a length equal to `individual_length`, with each gene value being a floating-point
number within its specified range in `input_ranges`.
"""
individual = []
for i in range(individual_length):
@@ -915,7 +919,7 @@ def generate_individual(input_ranges, individual_length):
def run(**kwargs):
"""
Executes YOLOv5 training with given options, allowing optional overrides through keyword arguments.
Execute YOLOv5 training with specified options, allowing optional overrides through keyword arguments.
Args:
weights (str, optional): Path to initial weights. Defaults to ROOT / 'yolov5s.pt'.
@@ -931,8 +935,9 @@ def run(**kwargs):
noval (bool, optional): Only validate at the final epoch. Defaults to False.
noautoanchor (bool, optional): Disable AutoAnchor. Defaults to False.
noplots (bool, optional): Do not save plot files. Defaults to False.
evolve (int, optional): Evolve hyperparameters for a specified number of generations. Use 300 if provided without a value.
evolve_population (str, optional): Directory for loading population during evolution. Defaults to ROOT / 'data/hyps'.
evolve (int, optional): Evolve hyperparameters for a specified number of generations. Use 300 if provided without a
value.
evolve_population (str, optional): Directory for loading population during evolution. Defaults to ROOT / 'data/ hyps'.
resume_evolve (str, optional): Resume hyperparameter evolution from the last generation. Defaults to None.
bucket (str, optional): gsutil bucket for saving checkpoints. Defaults to an empty string.
cache (str, optional): Cache image data in 'ram' or 'disk'. Defaults to None.