μ½˜ν…μΈ λ‘œ κ±΄λ„ˆλ›°κΈ°

μ°Έμ‘° ultralytics/hub/utils.py

μ°Έκ³ 

이 νŒŒμΌμ€ https://github.com/ultralytics/ ultralytics/blob/main/ ultralytics/ hub/utils .pyμ—μ„œ 확인할 수 μžˆμŠ΅λ‹ˆλ‹€. 문제λ₯Ό λ°œκ²¬ν•˜λ©΄ ν’€ λ¦¬ν€˜μŠ€νŠΈ (πŸ› οΈ) λ₯Ό 톡해 문제λ₯Ό ν•΄κ²°ν•˜λ„λ‘ λ„μ™€μ£Όμ„Έμš”. κ°μ‚¬ν•©λ‹ˆλ‹€ πŸ™!



ultralytics.hub.utils.Events

읡λͺ… 이벀트 뢄석을 μˆ˜μ§‘ν•˜κΈ° μœ„ν•œ ν΄λž˜μŠ€μž…λ‹ˆλ‹€. μ„€μ •μ—μ„œ 동기화가 참이면 이벀트 뢄석이 ν™œμ„±ν™”λ˜κ³  동기화=False이면 λΉ„ν™œμ„±ν™”λ©λ‹ˆλ‹€. 'yolo settings'λ₯Ό μ‹€ν–‰ν•˜μ—¬ μ„€μ • YAML νŒŒμΌμ„ ν™•μΈν•˜κ³  μ—…λ°μ΄νŠΈν•©λ‹ˆλ‹€.

속성:

이름 μœ ν˜• μ„€λͺ…
url str

읡λͺ… 이벀트λ₯Ό 전솑할 URLμž…λ‹ˆλ‹€.

rate_limit float

이벀트 전솑에 λŒ€ν•œ 초 λ‹¨μœ„ 전솑 속도 μ œν•œμž…λ‹ˆλ‹€.

metadata dict

ν™˜κ²½μ— λŒ€ν•œ 메타데이터가 ν¬ν•¨λœ μ‚¬μ „μž…λ‹ˆλ‹€.

enabled bool

νŠΉμ • 쑰건에 따라 이벀트λ₯Ό ν™œμ„±ν™” λ˜λŠ” λΉ„ν™œμ„±ν™”ν•˜λŠ” ν”Œλž˜κ·Έμž…λ‹ˆλ‹€.

의 μ†ŒμŠ€ μ½”λ“œ ultralytics/hub/utils.py
class Events:
    """
    A class for collecting anonymous event analytics. Event analytics are enabled when sync=True in settings and
    disabled when sync=False. Run 'yolo settings' to see and update settings YAML file.

    Attributes:
        url (str): The URL to send anonymous events.
        rate_limit (float): The rate limit in seconds for sending events.
        metadata (dict): A dictionary containing metadata about the environment.
        enabled (bool): A flag to enable or disable Events based on certain conditions.
    """

    url = "https://www.google-analytics.com/mp/collect?measurement_id=G-X8NCJYTQXM&api_secret=QLQrATrNSwGRFRLE-cbHJw"

    def __init__(self):
        """Initializes the Events object with default values for events, rate_limit, and metadata."""
        self.events = []  # events list
        self.rate_limit = 60.0  # rate limit (seconds)
        self.t = 0.0  # rate limit timer (seconds)
        self.metadata = {
            "cli": Path(sys.argv[0]).name == "yolo",
            "install": "git" if is_git_dir() else "pip" if is_pip_package() else "other",
            "python": ".".join(platform.python_version_tuple()[:2]),  # i.e. 3.10
            "version": __version__,
            "env": ENVIRONMENT,
            "session_id": round(random.random() * 1e15),
            "engagement_time_msec": 1000,
        }
        self.enabled = (
            SETTINGS["sync"]
            and RANK in (-1, 0)
            and not TESTS_RUNNING
            and ONLINE
            and (is_pip_package() or get_git_origin_url() == "https://github.com/ultralytics/ultralytics.git")
        )

    def __call__(self, cfg):
        """
        Attempts to add a new event to the events list and send events if the rate limit is reached.

        Args:
            cfg (IterableSimpleNamespace): The configuration object containing mode and task information.
        """
        if not self.enabled:
            # Events disabled, do nothing
            return

        # Attempt to add to events
        if len(self.events) < 25:  # Events list limited to 25 events (drop any events past this)
            params = {
                **self.metadata,
                "task": cfg.task,
                "model": cfg.model if cfg.model in GITHUB_ASSETS_NAMES else "custom",
            }
            if cfg.mode == "export":
                params["format"] = cfg.format
            self.events.append({"name": cfg.mode, "params": params})

        # Check rate limit
        t = time.time()
        if (t - self.t) < self.rate_limit:
            # Time is under rate limiter, wait to send
            return

        # Time is over rate limiter, send now
        data = {"client_id": SETTINGS["uuid"], "events": self.events}  # SHA-256 anonymized UUID hash and events list

        # POST equivalent to requests.post(self.url, json=data)
        smart_request("post", self.url, json=data, retry=0, verbose=False)

        # Reset events and rate limit timer
        self.events = []
        self.t = t

__call__(cfg)

이벀트 λͺ©λ‘μ— μƒˆ 이벀트λ₯Ό μΆ”κ°€ν•˜κ³  전솑λ₯  μ œν•œμ— λ„λ‹¬ν•˜λ©΄ 이벀트λ₯Ό 보내렀고 μ‹œλ„ν•©λ‹ˆλ‹€.

λ§€κ°œλ³€μˆ˜:

이름 μœ ν˜• μ„€λͺ… κΈ°λ³Έκ°’
cfg IterableSimpleNamespace

λͺ¨λ“œ 및 μž‘μ—… 정보가 ν¬ν•¨λœ ꡬ성 κ°œμ²΄μž…λ‹ˆλ‹€.

ν•„μˆ˜
의 μ†ŒμŠ€ μ½”λ“œ ultralytics/hub/utils.py
def __call__(self, cfg):
    """
    Attempts to add a new event to the events list and send events if the rate limit is reached.

    Args:
        cfg (IterableSimpleNamespace): The configuration object containing mode and task information.
    """
    if not self.enabled:
        # Events disabled, do nothing
        return

    # Attempt to add to events
    if len(self.events) < 25:  # Events list limited to 25 events (drop any events past this)
        params = {
            **self.metadata,
            "task": cfg.task,
            "model": cfg.model if cfg.model in GITHUB_ASSETS_NAMES else "custom",
        }
        if cfg.mode == "export":
            params["format"] = cfg.format
        self.events.append({"name": cfg.mode, "params": params})

    # Check rate limit
    t = time.time()
    if (t - self.t) < self.rate_limit:
        # Time is under rate limiter, wait to send
        return

    # Time is over rate limiter, send now
    data = {"client_id": SETTINGS["uuid"], "events": self.events}  # SHA-256 anonymized UUID hash and events list

    # POST equivalent to requests.post(self.url, json=data)
    smart_request("post", self.url, json=data, retry=0, verbose=False)

    # Reset events and rate limit timer
    self.events = []
    self.t = t

__init__()

이벀트, rate_limit 및 메타데이터에 λŒ€ν•œ κΈ°λ³Έκ°’μœΌλ‘œ 이벀트 객체λ₯Ό μ΄ˆκΈ°ν™”ν•©λ‹ˆλ‹€.

의 μ†ŒμŠ€ μ½”λ“œ ultralytics/hub/utils.py
def __init__(self):
    """Initializes the Events object with default values for events, rate_limit, and metadata."""
    self.events = []  # events list
    self.rate_limit = 60.0  # rate limit (seconds)
    self.t = 0.0  # rate limit timer (seconds)
    self.metadata = {
        "cli": Path(sys.argv[0]).name == "yolo",
        "install": "git" if is_git_dir() else "pip" if is_pip_package() else "other",
        "python": ".".join(platform.python_version_tuple()[:2]),  # i.e. 3.10
        "version": __version__,
        "env": ENVIRONMENT,
        "session_id": round(random.random() * 1e15),
        "engagement_time_msec": 1000,
    }
    self.enabled = (
        SETTINGS["sync"]
        and RANK in (-1, 0)
        and not TESTS_RUNNING
        and ONLINE
        and (is_pip_package() or get_git_origin_url() == "https://github.com/ultralytics/ultralytics.git")
    )



ultralytics.hub.utils.request_with_credentials(url)

Google Colab ν™˜κ²½μ—μ„œ μΏ ν‚€κ°€ μ²¨λΆ€λœ AJAX μš”μ²­μ„ μˆ˜ν–‰ν•©λ‹ˆλ‹€.

λ§€κ°œλ³€μˆ˜:

이름 μœ ν˜• μ„€λͺ… κΈ°λ³Έκ°’
url str

μš”μ²­ν•  URLμž…λ‹ˆλ‹€.

ν•„μˆ˜

λ°˜ν™˜ν•©λ‹ˆλ‹€:

μœ ν˜• μ„€λͺ…
any

AJAX μš”μ²­μ˜ 응닡 λ°μ΄ν„°μž…λ‹ˆλ‹€.

올리기:

μœ ν˜• μ„€λͺ…
OSError

이 ν•¨μˆ˜κ°€ Google Colab ν™˜κ²½μ—μ„œ μ‹€ν–‰λ˜μ§€ μ•ŠλŠ” 경우.

의 μ†ŒμŠ€ μ½”λ“œ ultralytics/hub/utils.py
def request_with_credentials(url: str) -> any:
    """
    Make an AJAX request with cookies attached in a Google Colab environment.

    Args:
        url (str): The URL to make the request to.

    Returns:
        (any): The response data from the AJAX request.

    Raises:
        OSError: If the function is not run in a Google Colab environment.
    """
    if not is_colab():
        raise OSError("request_with_credentials() must run in a Colab environment")
    from google.colab import output  # noqa
    from IPython import display  # noqa

    display.display(
        display.Javascript(
            """
            window._hub_tmp = new Promise((resolve, reject) => {
                const timeout = setTimeout(() => reject("Failed authenticating existing browser session"), 5000)
                fetch("%s", {
                    method: 'POST',
                    credentials: 'include'
                })
                    .then((response) => resolve(response.json()))
                    .then((json) => {
                    clearTimeout(timeout);
                    }).catch((err) => {
                    clearTimeout(timeout);
                    reject(err);
                });
            });
            """
            % url
        )
    )
    return output.eval_js("_hub_tmp")



ultralytics.hub.utils.requests_with_progress(method, url, **kwargs)

선택 사항인 진행λ₯  ν‘œμ‹œμ€„κ³Ό ν•¨κ»˜ μ§€μ •λœ 방법과 URL을 μ‚¬μš©ν•˜μ—¬ HTTP μš”μ²­μ„ ν•©λ‹ˆλ‹€.

λ§€κ°œλ³€μˆ˜:

이름 μœ ν˜• μ„€λͺ… κΈ°λ³Έκ°’
method str

μ‚¬μš©ν•  HTTP λ©”μ„œλ“œ(예: 'GET', 'POST').

ν•„μˆ˜
url str

μš”μ²­μ„ 보낼 URLμž…λ‹ˆλ‹€.

ν•„μˆ˜
**kwargs dict

κΈ°λ³Έ ν‚€μ›Œλ“œμ— 전달할 μΆ”κ°€ ν‚€μ›Œλ“œ 인자 requests.request ν•¨μˆ˜μž…λ‹ˆλ‹€.

{}

λ°˜ν™˜ν•©λ‹ˆλ‹€:

μœ ν˜• μ„€λͺ…
Response

HTTP μš”μ²­μ˜ 응닡 κ°μ²΄μž…λ‹ˆλ‹€.

μ°Έκ³ 
  • '진행 쀑'이 True둜 μ„€μ •λ˜μ–΄ 있으면 진행λ₯  ν‘œμ‹œμ€„μ— μ•Œλ €μ§„ μ½˜ν…μΈ  길이λ₯Ό 가진 μ‘λ‹΅μ˜ λ‹€μš΄λ‘œλ“œ 진행λ₯ μ΄ μ½˜ν…μΈ  길이λ₯Ό ν‘œμ‹œν•©λ‹ˆλ‹€.
  • '진행λ₯ '이 숫자인 경우 μ½˜ν…μΈ  길이 = 진행λ₯ λ‘œ κ°€μ •ν•˜μ—¬ 진행λ₯  ν‘œμ‹œμ€„μ΄ ν‘œμ‹œλ©λ‹ˆλ‹€.
의 μ†ŒμŠ€ μ½”λ“œ ultralytics/hub/utils.py
def requests_with_progress(method, url, **kwargs):
    """
    Make an HTTP request using the specified method and URL, with an optional progress bar.

    Args:
        method (str): The HTTP method to use (e.g. 'GET', 'POST').
        url (str): The URL to send the request to.
        **kwargs (dict): Additional keyword arguments to pass to the underlying `requests.request` function.

    Returns:
        (requests.Response): The response object from the HTTP request.

    Note:
        - If 'progress' is set to True, the progress bar will display the download progress for responses with a known
        content length.
        - If 'progress' is a number then progress bar will display assuming content length = progress.
    """
    progress = kwargs.pop("progress", False)
    if not progress:
        return requests.request(method, url, **kwargs)
    response = requests.request(method, url, stream=True, **kwargs)
    total = int(response.headers.get("content-length", 0) if isinstance(progress, bool) else progress)  # total size
    try:
        pbar = TQDM(total=total, unit="B", unit_scale=True, unit_divisor=1024)
        for data in response.iter_content(chunk_size=1024):
            pbar.update(len(data))
        pbar.close()
    except requests.exceptions.ChunkedEncodingError:  # avoid 'Connection broken: IncompleteRead' warnings
        response.close()
    return response



ultralytics.hub.utils.smart_request(method, url, retry=3, timeout=30, thread=True, code=-1, verbose=True, progress=False, **kwargs)

'requests' 라이브러리λ₯Ό μ‚¬μš©ν•˜μ—¬ μ§€μ •λœ μ‹œκ°„ μ œν•œκΉŒμ§€ κΈ°ν•˜κΈ‰μˆ˜μ μœΌλ‘œ λ°±μ˜€ν”„λ₯Ό μž¬μ‹œλ„ν•˜λŠ” HTTP μš”μ²­μ„ ν•©λ‹ˆλ‹€.

λ§€κ°œλ³€μˆ˜:

이름 μœ ν˜• μ„€λͺ… κΈ°λ³Έκ°’
method str

μš”μ²­μ— μ‚¬μš©ν•  HTTP λ©”μ„œλ“œμž…λ‹ˆλ‹€. 선택 κ°€λŠ₯ν•œ λ©”μ„œλ“œλŠ” 'post'와 'get'μž…λ‹ˆλ‹€.

ν•„μˆ˜
url str

μš”μ²­ν•  URLμž…λ‹ˆλ‹€.

ν•„μˆ˜
retry int

ν¬κΈ°ν•˜κΈ° 전에 μ‹œλ„ν•  수 μžˆλŠ” μž¬μ‹œλ„ νšŸμˆ˜μž…λ‹ˆλ‹€. 기본값은 3μž…λ‹ˆλ‹€.

3
timeout int

κΈ°λŠ₯이 μž¬μ‹œλ„λ₯Ό ν¬κΈ°ν•˜λŠ” μ‹œκ°„(초)μž…λ‹ˆλ‹€. 기본값은 30μž…λ‹ˆλ‹€.

30
thread bool

λ³„λ„μ˜ 데λͺ¬ μŠ€λ ˆλ“œμ—μ„œ μš”μ²­μ„ 싀행할지 μ—¬λΆ€μž…λ‹ˆλ‹€. 기본값은 Trueμž…λ‹ˆλ‹€.

True
code int

λ‘œκΉ… λͺ©μ μœΌλ‘œ μ‚¬μš©λ˜λŠ” μš”μ²­μ˜ μ‹λ³„μžμž…λ‹ˆλ‹€. 기본값은 -1μž…λ‹ˆλ‹€.

-1
verbose bool

μ½˜μ†”μ— 좜λ ₯할지 μ—¬λΆ€λ₯Ό κ²°μ •ν•˜λŠ” ν”Œλž˜κ·Έμž…λ‹ˆλ‹€. 기본값은 Trueμž…λ‹ˆλ‹€.

True
progress bool

μš”μ²­ 쀑 진행λ₯  ν‘œμ‹œμ€„μ„ ν‘œμ‹œν• μ§€ μ—¬λΆ€μž…λ‹ˆλ‹€. 기본값은 Falseμž…λ‹ˆλ‹€.

False
**kwargs dict

λ©”μ„œλ“œμ— μ§€μ •λœ μš”μ²­ ν•¨μˆ˜μ— 전달할 ν‚€μ›Œλ“œ μΈμˆ˜μž…λ‹ˆλ‹€.

{}

λ°˜ν™˜ν•©λ‹ˆλ‹€:

μœ ν˜• μ„€λͺ…
Response

HTTP 응닡 κ°μ²΄μž…λ‹ˆλ‹€. μš”μ²­μ΄ λ³„λ„μ˜ μŠ€λ ˆλ“œμ—μ„œ μ‹€ν–‰λ˜λŠ” 경우 None을 λ°˜ν™˜ν•©λ‹ˆλ‹€.

의 μ†ŒμŠ€ μ½”λ“œ ultralytics/hub/utils.py
def smart_request(method, url, retry=3, timeout=30, thread=True, code=-1, verbose=True, progress=False, **kwargs):
    """
    Makes an HTTP request using the 'requests' library, with exponential backoff retries up to a specified timeout.

    Args:
        method (str): The HTTP method to use for the request. Choices are 'post' and 'get'.
        url (str): The URL to make the request to.
        retry (int, optional): Number of retries to attempt before giving up. Default is 3.
        timeout (int, optional): Timeout in seconds after which the function will give up retrying. Default is 30.
        thread (bool, optional): Whether to execute the request in a separate daemon thread. Default is True.
        code (int, optional): An identifier for the request, used for logging purposes. Default is -1.
        verbose (bool, optional): A flag to determine whether to print out to console or not. Default is True.
        progress (bool, optional): Whether to show a progress bar during the request. Default is False.
        **kwargs (dict): Keyword arguments to be passed to the requests function specified in method.

    Returns:
        (requests.Response): The HTTP response object. If the request is executed in a separate thread, returns None.
    """
    retry_codes = (408, 500)  # retry only these codes

    @TryExcept(verbose=verbose)
    def func(func_method, func_url, **func_kwargs):
        """Make HTTP requests with retries and timeouts, with optional progress tracking."""
        r = None  # response
        t0 = time.time()  # initial time for timer
        for i in range(retry + 1):
            if (time.time() - t0) > timeout:
                break
            r = requests_with_progress(func_method, func_url, **func_kwargs)  # i.e. get(url, data, json, files)
            if r.status_code < 300:  # return codes in the 2xx range are generally considered "good" or "successful"
                break
            try:
                m = r.json().get("message", "No JSON message.")
            except AttributeError:
                m = "Unable to read JSON."
            if i == 0:
                if r.status_code in retry_codes:
                    m += f" Retrying {retry}x for {timeout}s." if retry else ""
                elif r.status_code == 429:  # rate limit
                    h = r.headers  # response headers
                    m = (
                        f"Rate limit reached ({h['X-RateLimit-Remaining']}/{h['X-RateLimit-Limit']}). "
                        f"Please retry after {h['Retry-After']}s."
                    )
                if verbose:
                    LOGGER.warning(f"{PREFIX}{m} {HELP_MSG} ({r.status_code} #{code})")
                if r.status_code not in retry_codes:
                    return r
            time.sleep(2**i)  # exponential standoff
        return r

    args = method, url
    kwargs["progress"] = progress
    if thread:
        threading.Thread(target=func, args=args, kwargs=kwargs, daemon=True).start()
    else:
        return func(*args, **kwargs)





생성됨 2023-11-12, μ—…λ°μ΄νŠΈλ¨ 2023-11-25
μž‘μ„±μž: glenn-jocher (3)