์ฝ˜ํ…์ธ ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ

์ฐธ์กฐ ultralytics/hub/__init__.py

์ฐธ๊ณ 

์ด ํŒŒ์ผ์€ https://github.com/ultralytics/ ultralytics/blob/main/ ultralytics/ hub/init.py์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์ œ๋ฅผ ๋ฐœ๊ฒฌํ•˜๋ฉด ํ’€ ๋ฆฌํ€˜์ŠคํŠธ ๐Ÿ› ๏ธ ์— ๊ธฐ์—ฌํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋„๋ก ๋„์™€์ฃผ์„ธ์š”. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค ๐Ÿ™!



ultralytics.hub.login(api_key=None, save=True)

์ œ๊ณต๋œ API ํ‚ค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Ultralytics HUB API์— ๋กœ๊ทธ์ธํ•ฉ๋‹ˆ๋‹ค.

์„ธ์…˜์€ ์ €์žฅ๋˜์ง€ ์•Š์œผ๋ฉฐ, ์ธ์ฆ์— ์„ฑ๊ณตํ•˜๋ฉด ์ €์žฅ๋œ ์„ค์ • ๋˜๋Š” HUB_API_KEY ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒˆ ์„ธ์…˜์ด ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.

๋งค๊ฐœ๋ณ€์ˆ˜:

์ด๋ฆ„ ์œ ํ˜• ์„ค๋ช… ๊ธฐ๋ณธ๊ฐ’
api_key str

์ธ์ฆ์— ์‚ฌ์šฉํ•  API ํ‚ค์ž…๋‹ˆ๋‹ค. ์ œ๊ณตํ•˜์ง€ ์•Š์œผ๋ฉด ์„ค์ • ๋˜๋Š” HUB_API_KEY ํ™˜๊ฒฝ ๋ณ€์ˆ˜์—์„œ ๊ฒ€์ƒ‰๋ฉ๋‹ˆ๋‹ค.

None
save bool

์ธ์ฆ์— ์„ฑ๊ณตํ•œ ๊ฒฝ์šฐ API ํ‚ค๋ฅผ ์„ค์ •์— ์ €์žฅํ• ์ง€ ์—ฌ๋ถ€์ž…๋‹ˆ๋‹ค.

True

๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค:

์œ ํ˜• ์„ค๋ช…
bool

์ธ์ฆ์— ์„ฑ๊ณตํ•˜๋ฉด ์ฐธ์ด๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ๊ฑฐ์ง“์ž…๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/hub/__init__.py
def login(api_key: str = None, save=True) -> bool:
    """
    Log in to the Ultralytics HUB API using the provided API key.

    The session is not stored; a new session is created when needed using the saved SETTINGS or the HUB_API_KEY
    environment variable if successfully authenticated.

    Args:
        api_key (str, optional): API key to use for authentication.
            If not provided, it will be retrieved from SETTINGS or HUB_API_KEY environment variable.
        save (bool, optional): Whether to save the API key to SETTINGS if authentication is successful.

    Returns:
        (bool): True if authentication is successful, False otherwise.
    """
    checks.check_requirements("hub-sdk>=0.0.2")
    from hub_sdk import HUBClient

    api_key_url = f"{HUB_WEB_ROOT}/settings?tab=api+keys"  # set the redirect URL
    saved_key = SETTINGS.get("api_key")
    active_key = api_key or saved_key
    credentials = {"api_key": active_key} if active_key and active_key != "" else None  # set credentials

    client = HUBClient(credentials)  # initialize HUBClient

    if client.authenticated:
        # Successfully authenticated with HUB

        if save and client.api_key != saved_key:
            SETTINGS.update({"api_key": client.api_key})  # update settings with valid API key

        # Set message based on whether key was provided or retrieved from settings
        log_message = (
            "New authentication successful โœ…" if client.api_key == api_key or not credentials else "Authenticated โœ…"
        )
        LOGGER.info(f"{PREFIX}{log_message}")

        return True
    else:
        # Failed to authenticate with HUB
        LOGGER.info(f"{PREFIX}Retrieve API key from {api_key_url}")
        return False



ultralytics.hub.logout()

์„ค์ • ํŒŒ์ผ์—์„œ API ํ‚ค๋ฅผ ์ œ๊ฑฐํ•˜์—ฌ Ultralytics HUB ์—์„œ ๋กœ๊ทธ์•„์›ƒํ•ฉ๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋กœ๊ทธ์ธํ•˜๋ ค๋ฉด 'yolo hub ๋กœ๊ทธ์ธ'์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ์ œ
from ultralytics import hub

hub.logout()
์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/hub/__init__.py
def logout():
    """
    Log out of Ultralytics HUB by removing the API key from the settings file. To log in again, use 'yolo hub login'.

    Example:
        ```python
        from ultralytics import hub

        hub.logout()
        ```
    """
    SETTINGS["api_key"] = ""
    SETTINGS.save()
    LOGGER.info(f"{PREFIX}logged out โœ…. To log in again, use 'yolo hub login'.")



ultralytics.hub.reset_model(model_id='')

ํ•™์Šต๋œ ๋ชจ๋ธ์„ ํ•™์Šต๋˜์ง€ ์•Š์€ ์ƒํƒœ๋กœ ์ดˆ๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/hub/__init__.py
def reset_model(model_id=""):
    """Reset a trained model to an untrained state."""
    r = requests.post(f"{HUB_API_ROOT}/model-reset", json={"modelId": model_id}, headers={"x-api-key": Auth().api_key})
    if r.status_code == 200:
        LOGGER.info(f"{PREFIX}Model reset successfully")
        return
    LOGGER.warning(f"{PREFIX}Model reset failure {r.status_code} {r.reason}")



ultralytics.hub.export_fmts_hub()

HUB-์ง€์›๋˜๋Š” ๋‚ด๋ณด๋‚ด๊ธฐ ํ˜•์‹์˜ ๋ชฉ๋ก์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/hub/__init__.py
def export_fmts_hub():
    """Returns a list of HUB-supported export formats."""
    from ultralytics.engine.exporter import export_formats

    return list(export_formats()["Argument"][1:]) + ["ultralytics_tflite", "ultralytics_coreml"]



ultralytics.hub.export_model(model_id='', format='torchscript')

๋ชจ๋“  ํ˜•์‹์œผ๋กœ ๋ชจ๋ธ์„ ๋‚ด๋ณด๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/hub/__init__.py
def export_model(model_id="", format="torchscript"):
    """Export a model to all formats."""
    assert format in export_fmts_hub(), f"Unsupported export format '{format}', valid formats are {export_fmts_hub()}"
    r = requests.post(
        f"{HUB_API_ROOT}/v1/models/{model_id}/export", json={"format": format}, headers={"x-api-key": Auth().api_key}
    )
    assert r.status_code == 200, f"{PREFIX}{format} export failure {r.status_code} {r.reason}"
    LOGGER.info(f"{PREFIX}{format} export started โœ…")



ultralytics.hub.get_export(model_id='', format='torchscript')

๋‹ค์šด๋กœ๋“œ URL๊ณผ ํ•จ๊ป˜ ๋‚ด๋ณด๋‚ธ ๋ชจ๋ธ ์‚ฌ์ „์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/hub/__init__.py
def get_export(model_id="", format="torchscript"):
    """Get an exported model dictionary with download URL."""
    assert format in export_fmts_hub(), f"Unsupported export format '{format}', valid formats are {export_fmts_hub()}"
    r = requests.post(
        f"{HUB_API_ROOT}/get-export",
        json={"apiKey": Auth().api_key, "modelId": model_id, "format": format},
        headers={"x-api-key": Auth().api_key},
    )
    assert r.status_code == 200, f"{PREFIX}{format} get_export failure {r.status_code} {r.reason}"
    return r.json()



ultralytics.hub.check_dataset(path='', task='detect')

์—…๋กœ๋“œํ•˜๊ธฐ ์ „์— HUB ๋ฐ์ดํ„ฐ์„ธํŠธ Zip ํŒŒ์ผ์˜ ์˜ค๋ฅ˜๋ฅผ ๊ฒ€์‚ฌํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ๋ฐ์ดํ„ฐ ์ง‘ํ•ฉ์„ ์—…๋กœ๋“œํ•˜๊ธฐ ์ „์— ์˜ค๋ฅ˜๋ฅผ ๊ฒ€์‚ฌํ•ฉ๋‹ˆ๋‹ค. HUB์— ์—…๋กœ๋“œํ•˜๊ธฐ ์ „์— ์˜ค๋ฅ˜๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉ ์˜ˆ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

๋งค๊ฐœ๋ณ€์ˆ˜:

์ด๋ฆ„ ์œ ํ˜• ์„ค๋ช… ๊ธฐ๋ณธ๊ฐ’
path str

data.zip์˜ ๊ฒฝ๋กœ(data.zip ์•ˆ์— data.yaml ํฌํ•จ). ๊ธฐ๋ณธ๊ฐ’์€ ''์ž…๋‹ˆ๋‹ค.

''
task str

๋ฐ์ดํ„ฐ ์„ธํŠธ ์ž‘์—…. ์˜ต์…˜์€ '๊ฐ์ง€', '์„ธ๊ทธ๋จผํŠธ', 'ํฌ์ฆˆ', '๋ถ„๋ฅ˜'์ž…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ '๊ฐ์ง€'์ž…๋‹ˆ๋‹ค.

'detect'
์˜ˆ์ œ
from ultralytics.hub import check_dataset

check_dataset('path/to/coco8.zip', task='detect')  # detect dataset
check_dataset('path/to/coco8-seg.zip', task='segment')  # segment dataset
check_dataset('path/to/coco8-pose.zip', task='pose')  # pose dataset
์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/hub/__init__.py
def check_dataset(path="", task="detect"):
    """
    Function for error-checking HUB dataset Zip file before upload. It checks a dataset for errors before it is uploaded
    to the HUB. Usage examples are given below.

    Args:
        path (str, optional): Path to data.zip (with data.yaml inside data.zip). Defaults to ''.
        task (str, optional): Dataset task. Options are 'detect', 'segment', 'pose', 'classify'. Defaults to 'detect'.

    Example:
        ```python
        from ultralytics.hub import check_dataset

        check_dataset('path/to/coco8.zip', task='detect')  # detect dataset
        check_dataset('path/to/coco8-seg.zip', task='segment')  # segment dataset
        check_dataset('path/to/coco8-pose.zip', task='pose')  # pose dataset
        ```
    """
    HUBDatasetStats(path=path, task=task).get_json()
    LOGGER.info(f"Checks completed correctly โœ…. Upload this dataset to {HUB_WEB_ROOT}/datasets/.")





์ƒ์„ฑ๋จ 2023-11-12, ์—…๋ฐ์ดํŠธ๋จ 2023-11-25
์ž‘์„ฑ์ž: glenn-jocher (3)