Zum Inhalt springen

Referenz f├╝r ultralytics/cfg/__init__.py

Hinweis

This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/cfg/__init__.py. If you spot a problem please help fix it by contributing a Pull Request ­čŤá´ŞĆ. Thank you ­čÖĆ!



ultralytics.cfg.cfg2dict(cfg)

Konvertiert ein Konfigurationsobjekt in ein W├Ârterbuch, egal ob es sich um einen Dateipfad, einen String oder ein SimpleNamespace-Objekt handelt.

Parameter:

Name Typ Beschreibung Standard
cfg str | Path | dict | SimpleNamespace

Konfigurationsobjekt, das in ein W├Ârterbuch konvertiert werden soll. Dies kann ein Pfad zu einer Konfigurationsdatei, einem W├Ârterbuch oder einem SimpleNamespace-Objekt.

erforderlich

Retouren:

Typ Beschreibung
dict

Konfigurationsobjekt im W├Ârterbuchformat.

Beispiel
from ultralytics.cfg import cfg2dict
from types import SimpleNamespace

# Example usage with a file path
config_dict = cfg2dict('config.yaml')

# Example usage with a SimpleNamespace
config_sn = SimpleNamespace(param1='value1', param2='value2')
config_dict = cfg2dict(config_sn)

# Example usage with a dictionary (returns the same dictionary)
config_dict = cfg2dict({'param1': 'value1', 'param2': 'value2'})
Anmerkungen
  • Wenn cfg ein Pfad oder eine Zeichenkette ist, wird sie als YAML geladen und in ein W├Ârterbuch konvertiert.
  • Wenn cfg ein SimpleNamespace-Objekt ist, wird es mit vars().
Quellcode in ultralytics/cfg/__init__.py
def cfg2dict(cfg):
    """
    Convert a configuration object to a dictionary, whether it is a file path, a string, or a SimpleNamespace object.

    Args:
        cfg (str | Path | dict | SimpleNamespace): Configuration object to be converted to a dictionary. This may be a
            path to a configuration file, a dictionary, or a SimpleNamespace object.

    Returns:
        (dict): Configuration object in dictionary format.

    Example:
        ```python
        from ultralytics.cfg import cfg2dict
        from types import SimpleNamespace

        # Example usage with a file path
        config_dict = cfg2dict('config.yaml')

        # Example usage with a SimpleNamespace
        config_sn = SimpleNamespace(param1='value1', param2='value2')
        config_dict = cfg2dict(config_sn)

        # Example usage with a dictionary (returns the same dictionary)
        config_dict = cfg2dict({'param1': 'value1', 'param2': 'value2'})
        ```

    Notes:
        - If `cfg` is a path or a string, it will be loaded as YAML and converted to a dictionary.
        - If `cfg` is a SimpleNamespace object, it will be converted to a dictionary using `vars()`.
    """
    if isinstance(cfg, (str, Path)):
        cfg = yaml_load(cfg)  # load dict
    elif isinstance(cfg, SimpleNamespace):
        cfg = vars(cfg)  # convert to dict
    return cfg



ultralytics.cfg.get_cfg(cfg=DEFAULT_CFG_DICT, overrides=None)

Laden und Zusammenf├╝hren von Konfigurationsdaten aus einer Datei oder einem W├Ârterbuch mit optionalen ├ťberschreibungen.

Parameter:

Name Typ Beschreibung Standard
cfg str | Path | dict | SimpleNamespace

Konfigurationsdatenquelle. Der Standardwert ist DEFAULT_CFG_DICT.

DEFAULT_CFG_DICT
overrides dict | None

W├Ârterbuch mit Schl├╝ssel-Wert-Paaren zum ├ťberschreiben der Basiskonfiguration. Der Standardwert ist Keine.

None

Retouren:

Typ Beschreibung
SimpleNamespace

Namespace, der die zusammengef├╝hrten Trainingsargumente enth├Ąlt.

Anmerkungen
  • Wenn sowohl cfg und overrides zur Verf├╝gung gestellt werden, werden die Werte in overrides Vorrang haben.
  • Eine spezielle Handhabung gew├Ąhrleistet die Ausrichtung und Korrektheit der Konfiguration, z. B. die Konvertierung numerischer project und name zu Zeichenfolgen und Validieren der Konfigurationsschl├╝ssel und -werte.
Beispiel
from ultralytics.cfg import get_cfg

# Load default configuration
config = get_cfg()

# Load from a custom file with overrides
config = get_cfg('path/to/config.yaml', overrides={'epochs': 50, 'batch_size': 16})

Konfigurationsw├Ârterbuch mit ├ťberschreibungen zusammengef├╝hrt:

{'epochs': 50, 'batch_size': 16, ...}

Quellcode in ultralytics/cfg/__init__.py
def get_cfg(cfg: Union[str, Path, Dict, SimpleNamespace] = DEFAULT_CFG_DICT, overrides: Dict = None):
    """
    Load and merge configuration data from a file or dictionary, with optional overrides.

    Args:
        cfg (str | Path | dict | SimpleNamespace, optional): Configuration data source. Defaults to `DEFAULT_CFG_DICT`.
        overrides (dict | None, optional): Dictionary containing key-value pairs to override the base configuration.
            Defaults to None.

    Returns:
        (SimpleNamespace): Namespace containing the merged training arguments.

    Notes:
        - If both `cfg` and `overrides` are provided, the values in `overrides` will take precedence.
        - Special handling ensures alignment and correctness of the configuration, such as converting numeric `project`
          and `name` to strings and validating the configuration keys and values.

    Example:
        ```python
        from ultralytics.cfg import get_cfg

        # Load default configuration
        config = get_cfg()

        # Load from a custom file with overrides
        config = get_cfg('path/to/config.yaml', overrides={'epochs': 50, 'batch_size': 16})
        ```

        Configuration dictionary merged with overrides:
        ```python
        {'epochs': 50, 'batch_size': 16, ...}
        ```
    """
    cfg = cfg2dict(cfg)

    # Merge overrides
    if overrides:
        overrides = cfg2dict(overrides)
        if "save_dir" not in cfg:
            overrides.pop("save_dir", None)  # special override keys to ignore
        check_dict_alignment(cfg, overrides)
        cfg = {**cfg, **overrides}  # merge cfg and overrides dicts (prefer overrides)

    # Special handling for numeric project/name
    for k in "project", "name":
        if k in cfg and isinstance(cfg[k], (int, float)):
            cfg[k] = str(cfg[k])
    if cfg.get("name") == "model":  # assign model to 'name' arg
        cfg["name"] = cfg.get("model", "").split(".")[0]
        LOGGER.warning(f"WARNING ÔÜá´ŞĆ 'name=model' automatically updated to 'name={cfg['name']}'.")

    # Type and Value checks
    check_cfg(cfg)

    # Return instance
    return IterableSimpleNamespace(**cfg)



ultralytics.cfg.check_cfg(cfg, hard=True)

Validieren Ultralytics Konfigurationsargumenttypen und -werte und konvertieren Sie sie bei Bedarf.

Quellcode in ultralytics/cfg/__init__.py
def check_cfg(cfg, hard=True):
    """Validate Ultralytics configuration argument types and values, converting them if necessary."""
    for k, v in cfg.items():
        if v is not None:  # None values may be from optional args
            if k in CFG_FLOAT_KEYS and not isinstance(v, (int, float)):
                if hard:
                    raise TypeError(
                        f"'{k}={v}' is of invalid type {type(v).__name__}. "
                        f"Valid '{k}' types are int (i.e. '{k}=0') or float (i.e. '{k}=0.5')"
                    )
                cfg[k] = float(v)
            elif k in CFG_FRACTION_KEYS:
                if not isinstance(v, (int, float)):
                    if hard:
                        raise TypeError(
                            f"'{k}={v}' is of invalid type {type(v).__name__}. "
                            f"Valid '{k}' types are int (i.e. '{k}=0') or float (i.e. '{k}=0.5')"
                        )
                    cfg[k] = v = float(v)
                if not (0.0 <= v <= 1.0):
                    raise ValueError(f"'{k}={v}' is an invalid value. " f"Valid '{k}' values are between 0.0 and 1.0.")
            elif k in CFG_INT_KEYS and not isinstance(v, int):
                if hard:
                    raise TypeError(
                        f"'{k}={v}' is of invalid type {type(v).__name__}. " f"'{k}' must be an int (i.e. '{k}=8')"
                    )
                cfg[k] = int(v)
            elif k in CFG_BOOL_KEYS and not isinstance(v, bool):
                if hard:
                    raise TypeError(
                        f"'{k}={v}' is of invalid type {type(v).__name__}. "
                        f"'{k}' must be a bool (i.e. '{k}=True' or '{k}=False')"
                    )
                cfg[k] = bool(v)



ultralytics.cfg.get_save_dir(args, name=None)

Gibt den Verzeichnispfad zum Speichern von Ausgaben zur├╝ck, der von Argumenten oder Standardeinstellungen abgeleitet wird.

Quellcode in ultralytics/cfg/__init__.py
def get_save_dir(args, name=None):
    """Returns the directory path for saving outputs, derived from arguments or default settings."""

    if getattr(args, "save_dir", None):
        save_dir = args.save_dir
    else:
        from ultralytics.utils.files import increment_path

        project = args.project or (ROOT.parent / "tests/tmp/runs" if TESTS_RUNNING else RUNS_DIR) / args.task
        name = name or args.name or f"{args.mode}"
        save_dir = increment_path(Path(project) / name, exist_ok=args.exist_ok if RANK in {-1, 0} else True)

    return Path(save_dir)



ultralytics.cfg._handle_deprecation(custom)

Behandelt veraltete Konfigurationsschl├╝ssel, indem sie aktuellen Entsprechungen mit veralteten Warnungen zugeordnet werden.

Quellcode in ultralytics/cfg/__init__.py
def _handle_deprecation(custom):
    """Handles deprecated configuration keys by mapping them to current equivalents with deprecation warnings."""

    for key in custom.copy().keys():
        if key == "boxes":
            deprecation_warn(key, "show_boxes")
            custom["show_boxes"] = custom.pop("boxes")
        if key == "hide_labels":
            deprecation_warn(key, "show_labels")
            custom["show_labels"] = custom.pop("hide_labels") == "False"
        if key == "hide_conf":
            deprecation_warn(key, "show_conf")
            custom["show_conf"] = custom.pop("hide_conf") == "False"
        if key == "line_thickness":
            deprecation_warn(key, "line_width")
            custom["line_width"] = custom.pop("line_thickness")

    return custom



ultralytics.cfg.check_dict_alignment(base, custom, e=None)

├ťberpr├╝fen Sie die Tasten├╝bereinstimmung zwischen benutzerdefinierten und Basiskonfigurationsw├Ârterb├╝chern, die veraltete Schl├╝ssel und Bereitstellen informativer Fehlermeldungen f├╝r nicht ├╝bereinstimmende Schl├╝ssel.

Parameter:

Name Typ Beschreibung Standard
base dict

Das Basiskonfigurationsw├Ârterbuch mit g├╝ltigen Schl├╝sseln.

erforderlich
custom dict

Das benutzerdefinierte Konfigurationsw├Ârterbuch, das auf Ausrichtung ├╝berpr├╝ft werden soll.

erforderlich
e Exception

Eine optionale Fehlerinstanz, die von der aufrufenden Funktion ├╝bergeben wird. Der Standardwert ist Keine.

None

Erh├Âht:

Typ Beschreibung
SystemExit

Bricht die Programmausf├╝hrung ab, wenn nicht ├╝bereinstimmende Schl├╝ssel gefunden werden.

Anmerkungen
  • Die Funktion bietet Vorschl├Ąge f├╝r nicht ├╝bereinstimmende Schl├╝ssel basierend auf ihrer ├ähnlichkeit mit g├╝ltigen Schl├╝sseln in der Basis-Konfiguration.
  • Veraltete Schl├╝ssel in der benutzerdefinierten Konfiguration werden automatisch behandelt und durch ihre aktualisierten entsprechungen.
  • F├╝r jeden nicht ├╝bereinstimmenden Schl├╝ssel wird eine detaillierte Fehlermeldung ausgegeben, die dem Benutzer hilft, ihn schnell zu identifizieren und zu korrigieren ihre benutzerdefinierten Konfigurationen.
Beispiel
base_cfg = {'epochs': 50, 'lr0': 0.01, 'batch_size': 16}
custom_cfg = {'epoch': 100, 'lr': 0.02, 'batch_size': 32}

try:
    check_dict_alignment(base_cfg, custom_cfg)
except SystemExit:
    # Handle the error or correct the configuration
Quellcode in ultralytics/cfg/__init__.py
def check_dict_alignment(base: Dict, custom: Dict, e=None):
    """
    Check for key alignment between custom and base configuration dictionaries, catering for deprecated keys and
    providing informative error messages for mismatched keys.

    Args:
        base (dict): The base configuration dictionary containing valid keys.
        custom (dict): The custom configuration dictionary to be checked for alignment.
        e (Exception, optional): An optional error instance passed by the calling function. Default is None.

    Raises:
        SystemExit: Terminates the program execution if mismatched keys are found.

    Notes:
        - The function provides suggestions for mismatched keys based on their similarity to valid keys in the
          base configuration.
        - Deprecated keys in the custom configuration are automatically handled and replaced with their updated
          equivalents.
        - A detailed error message is printed for each mismatched key, helping users to quickly identify and correct
          their custom configurations.

    Example:
        ```python
        base_cfg = {'epochs': 50, 'lr0': 0.01, 'batch_size': 16}
        custom_cfg = {'epoch': 100, 'lr': 0.02, 'batch_size': 32}

        try:
            check_dict_alignment(base_cfg, custom_cfg)
        except SystemExit:
            # Handle the error or correct the configuration
        ```
    """
    custom = _handle_deprecation(custom)
    base_keys, custom_keys = (set(x.keys()) for x in (base, custom))
    mismatched = [k for k in custom_keys if k not in base_keys]
    if mismatched:
        from difflib import get_close_matches

        string = ""
        for x in mismatched:
            matches = get_close_matches(x, base_keys)  # key list
            matches = [f"{k}={base[k]}" if base.get(k) is not None else k for k in matches]
            match_str = f"Similar arguments are i.e. {matches}." if matches else ""
            string += f"'{colorstr('red', 'bold', x)}' is not a valid YOLO argument. {match_str}\n"
        raise SyntaxError(string + CLI_HELP_MSG) from e



ultralytics.cfg.merge_equals_args(args)

F├╝gt Argumente um isolierte '='-Argumente in einer Liste von Zeichenketten zusammen. Die Funktion ber├╝cksichtigt F├Ąlle, in denen das erste Argument mit '=' endet oder das zweite mit '=' beginnt, sowie wenn das mittlere Argument ein Gleichheitszeichen ist.

Parameter:

Name Typ Beschreibung Standard
args List[str]

Eine Liste von Strings, bei der jedes Element ein Argument ist.

erforderlich

Retouren:

Typ Beschreibung
List[str]

Eine Liste von Zeichenketten, bei denen die Argumente um ein isoliertes '=' herum zusammengef├╝hrt werden.

Beispiel

Die Funktion ├Ąndert die Argumentliste wie folgt:

args = ["arg1", "=", "value"]
new_args = merge_equals_args(args)
print(new_args)  # Output: ["arg1=value"]

args = ["arg1=", "value"]
new_args = merge_equals_args(args)
print(new_args)  # Output: ["arg1=value"]

args = ["arg1", "=value"]
new_args = merge_equals_args(args)
print(new_args)  # Output: ["arg1=value"]

Quellcode in ultralytics/cfg/__init__.py
def merge_equals_args(args: List[str]) -> List[str]:
    """
    Merges arguments around isolated '=' args in a list of strings. The function considers cases where the first
    argument ends with '=' or the second starts with '=', as well as when the middle one is an equals sign.

    Args:
        args (List[str]): A list of strings where each element is an argument.

    Returns:
        (List[str]): A list of strings where the arguments around isolated '=' are merged.

    Example:
        The function modifies the argument list as follows:
        ```python
        args = ["arg1", "=", "value"]
        new_args = merge_equals_args(args)
        print(new_args)  # Output: ["arg1=value"]

        args = ["arg1=", "value"]
        new_args = merge_equals_args(args)
        print(new_args)  # Output: ["arg1=value"]

        args = ["arg1", "=value"]
        new_args = merge_equals_args(args)
        print(new_args)  # Output: ["arg1=value"]
        ```
    """
    new_args = []
    for i, arg in enumerate(args):
        if arg == "=" and 0 < i < len(args) - 1:  # merge ['arg', '=', 'val']
            new_args[-1] += f"={args[i + 1]}"
            del args[i + 1]
        elif arg.endswith("=") and i < len(args) - 1 and "=" not in args[i + 1]:  # merge ['arg=', 'val']
            new_args.append(f"{arg}{args[i + 1]}")
            del args[i + 1]
        elif arg.startswith("=") and i > 0:  # merge ['arg', '=val']
            new_args[-1] += arg
        else:
            new_args.append(arg)
    return new_args



ultralytics.cfg.handle_yolo_hub(args)

Bearbeite die Befehle der Ultralytics HUB-Befehlszeilenschnittstelle (CLI).

Diese Funktion verarbeitet Ultralytics NABE CLI Befehle wie Login und Logout. Es sollte bei der Ausf├╝hrung aufgerufen werden ein Skript mit Argumenten im Zusammenhang mit der HUB-Authentifizierung.

Parameter:

Name Typ Beschreibung Standard
args List[str]

Eine Liste von Befehlszeilenargumenten.

erforderlich

Retouren:

Typ Beschreibung
None

Keine

Beispiel
yolo hub login YOUR_API_KEY
Quellcode in ultralytics/cfg/__init__.py
def handle_yolo_hub(args: List[str]) -> None:
    """
    Handle Ultralytics HUB command-line interface (CLI) commands.

    This function processes Ultralytics HUB CLI commands such as login and logout. It should be called when executing
    a script with arguments related to HUB authentication.

    Args:
        args (List[str]): A list of command line arguments.

    Returns:
        None

    Example:
        ```bash
        yolo hub login YOUR_API_KEY
        ```
    """
    from ultralytics import hub

    if args[0] == "login":
        key = args[1] if len(args) > 1 else ""
        # Log in to Ultralytics HUB using the provided API key
        hub.login(key)
    elif args[0] == "logout":
        # Log out from Ultralytics HUB
        hub.logout()



ultralytics.cfg.handle_yolo_settings(args)

Bearbeite die Befehle der Befehlszeilenschnittstelle YOLO Einstellungen (CLI).

Diese Funktion verarbeitet YOLO Einstellungen CLI Befehle wie reset. Es sollte aufgerufen werden, wenn ein Skript mit Argumente im Zusammenhang mit YOLO Verwaltung der Einstellungen.

Parameter:

Name Typ Beschreibung Standard
args List[str]

Eine Liste von Befehlszeilenargumenten f├╝r die Verwaltung von YOLO Einstellungen.

erforderlich

Retouren:

Typ Beschreibung
None

Keine

Beispiel
yolo settings reset
Anmerkungen

Weitere Informationen zur Handhabung YOLO Einstellungen, besuchen Sie: https://docs.ultralytics.com/Schnellstart/ #ultralytics-Einstellungen

Quellcode in ultralytics/cfg/__init__.py
def handle_yolo_settings(args: List[str]) -> None:
    """
    Handle YOLO settings command-line interface (CLI) commands.

    This function processes YOLO settings CLI commands such as reset. It should be called when executing a script with
    arguments related to YOLO settings management.

    Args:
        args (List[str]): A list of command line arguments for YOLO settings management.

    Returns:
        None

    Example:
        ```bash
        yolo settings reset
        ```

    Notes:
        For more information on handling YOLO settings, visit:
        https://docs.ultralytics.com/quickstart/#ultralytics-settings
    """
    url = "https://docs.ultralytics.com/quickstart/#ultralytics-settings"  # help URL
    try:
        if any(args):
            if args[0] == "reset":
                SETTINGS_YAML.unlink()  # delete the settings file
                SETTINGS.reset()  # create new settings
                LOGGER.info("Settings reset successfully")  # inform the user that settings have been reset
            else:  # save a new setting
                new = dict(parse_key_value_pair(a) for a in args)
                check_dict_alignment(SETTINGS, new)
                SETTINGS.update(new)

        LOGGER.info(f"­čĺí Learn about settings at {url}")
        yaml_print(SETTINGS_YAML)  # print the current settings
    except Exception as e:
        LOGGER.warning(f"WARNING ÔÜá´ŞĆ settings error: '{e}'. Please see {url} for help.")



ultralytics.cfg.handle_explorer()

├ľffnen Sie die Ultralytics Explorer-GUI f├╝r die Untersuchung und Analyse von Datens├Ątzen.

Quellcode in ultralytics/cfg/__init__.py
def handle_explorer():
    """Open the Ultralytics Explorer GUI for dataset exploration and analysis."""
    checks.check_requirements("streamlit>=1.29.0")
    LOGGER.info("­čĺí Loading Explorer dashboard...")
    subprocess.run(["streamlit", "run", ROOT / "data/explorer/gui/dash.py", "--server.maxMessageSize", "2048"])



ultralytics.cfg.handle_streamlit_inference()

├ľffne die Ultralytics Live Inference streamlit App zur Objekterkennung in Echtzeit.

Quellcode in ultralytics/cfg/__init__.py
def handle_streamlit_inference():
    """Open the Ultralytics Live Inference streamlit app for real time object detection."""
    checks.check_requirements("streamlit>=1.29.0")
    LOGGER.info("­čĺí Loading Ultralytics Live Inference app...")
    subprocess.run(["streamlit", "run", ROOT / "solutions/streamlit_inference.py", "--server.headless", "true"])



ultralytics.cfg.parse_key_value_pair(pair)

Analysiert ein 'Schl├╝ssel=Wert'-Paar und gibt Schl├╝ssel und Wert zur├╝ck.

Quellcode in ultralytics/cfg/__init__.py
def parse_key_value_pair(pair):
    """Parse one 'key=value' pair and return key and value."""
    k, v = pair.split("=", 1)  # split on first '=' sign
    k, v = k.strip(), v.strip()  # remove spaces
    assert v, f"missing '{k}' value"
    return k, smart_value(v)



ultralytics.cfg.smart_value(v)

Konvertieren Sie einen String in den entsprechenden Typ (int, float, bool, None usw.).

Quellcode in ultralytics/cfg/__init__.py
def smart_value(v):
    """Convert a string to its appropriate type (int, float, bool, None, etc.)."""
    v_lower = v.lower()
    if v_lower == "none":
        return None
    elif v_lower == "true":
        return True
    elif v_lower == "false":
        return False
    else:
        with contextlib.suppress(Exception):
            return eval(v)
        return v



ultralytics.cfg.entrypoint(debug='')

Ultralytics entrypoint-Funktion zum Parsen und Ausf├╝hren von Befehlszeilenargumenten.

Diese Funktion dient als Haupteinstiegspunkt f├╝r die Ultralytics CLI, das Parsen von Befehlszeilenargumenten und Ausf├╝hren der entsprechenden Aufgaben wie Training, Validierung, Vorhersage, Exportieren von Modellen und mehr.

Parameter:

Name Typ Beschreibung Standard
debug str

Durch Leerzeichen getrennte Zeichenfolge von Befehlszeilenargumenten zu Debugzwecken. Der Standardwert ist "".

''

Retouren:

Typ Beschreibung
None

Diese Funktion gibt keinen Wert zur├╝ck.

Anmerkungen
  • Eine Liste aller verf├╝gbaren Befehle und ihrer Argumente finden Sie in den bereitgestellten Hilfemeldungen und in der Ultralytics Dokumentation unter https://docs.ultralytics.COM.
  • Wenn keine Argumente ├╝bergeben werden, zeigt die Funktion die Hilfemeldung zur Verwendung an.
Beispiel
# Train a detection model for 10 epochs with an initial learning_rate of 0.01
entrypoint("train data=coco8.yaml model=yolov8n.pt epochs=10 lr0=0.01")

# Predict a YouTube video using a pretrained segmentation model at image size 320
entrypoint("predict model=yolov8n-seg.pt source='https://youtu.be/LNwODJXcvt4' imgsz=320")

# Validate a pretrained detection model at batch-size 1 and image size 640
entrypoint("val model=yolov8n.pt data=coco8.yaml batch=1 imgsz=640")
Quellcode in ultralytics/cfg/__init__.py
def entrypoint(debug=""):
    """
    Ultralytics entrypoint function for parsing and executing command-line arguments.

    This function serves as the main entry point for the Ultralytics CLI, parsing  command-line arguments and
    executing the corresponding tasks such as training, validation, prediction, exporting models, and more.

    Args:
        debug (str, optional): Space-separated string of command-line arguments for debugging purposes. Default is "".

    Returns:
        (None): This function does not return any value.

    Notes:
        - For a list of all available commands and their arguments, see the provided help messages and the Ultralytics
          documentation at https://docs.ultralytics.com.
        - If no arguments are passed, the function will display the usage help message.

    Example:
        ```python
        # Train a detection model for 10 epochs with an initial learning_rate of 0.01
        entrypoint("train data=coco8.yaml model=yolov8n.pt epochs=10 lr0=0.01")

        # Predict a YouTube video using a pretrained segmentation model at image size 320
        entrypoint("predict model=yolov8n-seg.pt source='https://youtu.be/LNwODJXcvt4' imgsz=320")

        # Validate a pretrained detection model at batch-size 1 and image size 640
        entrypoint("val model=yolov8n.pt data=coco8.yaml batch=1 imgsz=640")
        ```
    """
    args = (debug.split(" ") if debug else ARGV)[1:]
    if not args:  # no arguments passed
        LOGGER.info(CLI_HELP_MSG)
        return

    special = {
        "help": lambda: LOGGER.info(CLI_HELP_MSG),
        "checks": checks.collect_system_info,
        "version": lambda: LOGGER.info(__version__),
        "settings": lambda: handle_yolo_settings(args[1:]),
        "cfg": lambda: yaml_print(DEFAULT_CFG_PATH),
        "hub": lambda: handle_yolo_hub(args[1:]),
        "login": lambda: handle_yolo_hub(args),
        "copy-cfg": copy_default_cfg,
        "explorer": lambda: handle_explorer(),
        "streamlit-predict": lambda: handle_streamlit_inference(),
    }
    full_args_dict = {**DEFAULT_CFG_DICT, **{k: None for k in TASKS}, **{k: None for k in MODES}, **special}

    # Define common misuses of special commands, i.e. -h, -help, --help
    special.update({k[0]: v for k, v in special.items()})  # singular
    special.update({k[:-1]: v for k, v in special.items() if len(k) > 1 and k.endswith("s")})  # singular
    special = {**special, **{f"-{k}": v for k, v in special.items()}, **{f"--{k}": v for k, v in special.items()}}

    overrides = {}  # basic overrides, i.e. imgsz=320
    for a in merge_equals_args(args):  # merge spaces around '=' sign
        if a.startswith("--"):
            LOGGER.warning(f"WARNING ÔÜá´ŞĆ argument '{a}' does not require leading dashes '--', updating to '{a[2:]}'.")
            a = a[2:]
        if a.endswith(","):
            LOGGER.warning(f"WARNING ÔÜá´ŞĆ argument '{a}' does not require trailing comma ',', updating to '{a[:-1]}'.")
            a = a[:-1]
        if "=" in a:
            try:
                k, v = parse_key_value_pair(a)
                if k == "cfg" and v is not None:  # custom.yaml passed
                    LOGGER.info(f"Overriding {DEFAULT_CFG_PATH} with {v}")
                    overrides = {k: val for k, val in yaml_load(checks.check_yaml(v)).items() if k != "cfg"}
                else:
                    overrides[k] = v
            except (NameError, SyntaxError, ValueError, AssertionError) as e:
                check_dict_alignment(full_args_dict, {a: ""}, e)

        elif a in TASKS:
            overrides["task"] = a
        elif a in MODES:
            overrides["mode"] = a
        elif a.lower() in special:
            special[a.lower()]()
            return
        elif a in DEFAULT_CFG_DICT and isinstance(DEFAULT_CFG_DICT[a], bool):
            overrides[a] = True  # auto-True for default bool args, i.e. 'yolo show' sets show=True
        elif a in DEFAULT_CFG_DICT:
            raise SyntaxError(
                f"'{colorstr('red', 'bold', a)}' is a valid YOLO argument but is missing an '=' sign "
                f"to set its value, i.e. try '{a}={DEFAULT_CFG_DICT[a]}'\n{CLI_HELP_MSG}"
            )
        else:
            check_dict_alignment(full_args_dict, {a: ""})

    # Check keys
    check_dict_alignment(full_args_dict, overrides)

    # Mode
    mode = overrides.get("mode")
    if mode is None:
        mode = DEFAULT_CFG.mode or "predict"
        LOGGER.warning(f"WARNING ÔÜá´ŞĆ 'mode' argument is missing. Valid modes are {MODES}. Using default 'mode={mode}'.")
    elif mode not in MODES:
        raise ValueError(f"Invalid 'mode={mode}'. Valid modes are {MODES}.\n{CLI_HELP_MSG}")

    # Task
    task = overrides.pop("task", None)
    if task:
        if task not in TASKS:
            raise ValueError(f"Invalid 'task={task}'. Valid tasks are {TASKS}.\n{CLI_HELP_MSG}")
        if "model" not in overrides:
            overrides["model"] = TASK2MODEL[task]

    # Model
    model = overrides.pop("model", DEFAULT_CFG.model)
    if model is None:
        model = "yolov8n.pt"
        LOGGER.warning(f"WARNING ÔÜá´ŞĆ 'model' argument is missing. Using default 'model={model}'.")
    overrides["model"] = model
    stem = Path(model).stem.lower()
    if "rtdetr" in stem:  # guess architecture
        from ultralytics import RTDETR

        model = RTDETR(model)  # no task argument
    elif "fastsam" in stem:
        from ultralytics import FastSAM

        model = FastSAM(model)
    elif "sam" in stem:
        from ultralytics import SAM

        model = SAM(model)
    else:
        from ultralytics import YOLO

        model = YOLO(model, task=task)
    if isinstance(overrides.get("pretrained"), str):
        model.load(overrides["pretrained"])

    # Task Update
    if task != model.task:
        if task:
            LOGGER.warning(
                f"WARNING ÔÜá´ŞĆ conflicting 'task={task}' passed with 'task={model.task}' model. "
                f"Ignoring 'task={task}' and updating to 'task={model.task}' to match model."
            )
        task = model.task

    # Mode
    if mode in {"predict", "track"} and "source" not in overrides:
        overrides["source"] = DEFAULT_CFG.source or ASSETS
        LOGGER.warning(f"WARNING ÔÜá´ŞĆ 'source' argument is missing. Using default 'source={overrides['source']}'.")
    elif mode in {"train", "val"}:
        if "data" not in overrides and "resume" not in overrides:
            overrides["data"] = DEFAULT_CFG.data or TASK2DATA.get(task or DEFAULT_CFG.task, DEFAULT_CFG.data)
            LOGGER.warning(f"WARNING ÔÜá´ŞĆ 'data' argument is missing. Using default 'data={overrides['data']}'.")
    elif mode == "export":
        if "format" not in overrides:
            overrides["format"] = DEFAULT_CFG.format or "torchscript"
            LOGGER.warning(f"WARNING ÔÜá´ŞĆ 'format' argument is missing. Using default 'format={overrides['format']}'.")

    # Run command in python
    getattr(model, mode)(**overrides)  # default args from model

    # Show help
    LOGGER.info(f"­čĺí Learn more at https://docs.ultralytics.com/modes/{mode}")



ultralytics.cfg.copy_default_cfg()

Kopieren und erstellen Sie eine neue Standardkonfigurationsdatei mit dem Zusatz "_copy" als Anwendungsbeispiel.

Quellcode in ultralytics/cfg/__init__.py
def copy_default_cfg():
    """Copy and create a new default configuration file with '_copy' appended to its name, providing usage example."""
    new_file = Path.cwd() / DEFAULT_CFG_PATH.name.replace(".yaml", "_copy.yaml")
    shutil.copy2(DEFAULT_CFG_PATH, new_file)
    LOGGER.info(
        f"{DEFAULT_CFG_PATH} copied to {new_file}\n"
        f"Example YOLO command with this new custom cfg:\n    yolo cfg='{new_file}' imgsz=320 batch=8"
    )





Created 2023-11-12, Updated 2024-07-19
Authors: glenn-jocher (8), Burhan-Q (1), AyushExel (1), Laughing-q (1)