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

์ฐธ์กฐ ultralytics/utils/patches.py

์ฐธ๊ณ 

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



ultralytics.utils.patches.imread(filename, flags=cv2.IMREAD_COLOR)

ํŒŒ์ผ์—์„œ ์ด๋ฏธ์ง€๋ฅผ ์ฝ์Šต๋‹ˆ๋‹ค.

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

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

์ฝ์„ ํŒŒ์ผ์˜ ๊ฒฝ๋กœ์ž…๋‹ˆ๋‹ค.

ํ•„์ˆ˜
flags int

cv2.IMREAD_* ๊ฐ’์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ํ”Œ๋ž˜๊ทธ์ž…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ cv2.IMREAD_COLOR์ž…๋‹ˆ๋‹ค.

IMREAD_COLOR

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

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

์ฝ์€ ์ด๋ฏธ์ง€์ž…๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/patches.py
def imread(filename: str, flags: int = cv2.IMREAD_COLOR):
    """
    Read an image from a file.

    Args:
        filename (str): Path to the file to read.
        flags (int, optional): Flag that can take values of cv2.IMREAD_*. Defaults to cv2.IMREAD_COLOR.

    Returns:
        (np.ndarray): The read image.
    """
    return cv2.imdecode(np.fromfile(filename, np.uint8), flags)



ultralytics.utils.patches.imwrite(filename, img, params=None)

ํŒŒ์ผ์— ์ด๋ฏธ์ง€๋ฅผ ์”๋‹ˆ๋‹ค.

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

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

์ž‘์„ฑํ•  ํŒŒ์ผ์˜ ๊ฒฝ๋กœ์ž…๋‹ˆ๋‹ค.

ํ•„์ˆ˜
img ndarray

์ž‘์„ฑํ•  ์ด๋ฏธ์ง€.

ํ•„์ˆ˜
params list of ints

์ถ”๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜. OpenCV ์„ค๋ช…์„œ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

None

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

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

ํŒŒ์ผ์ด ์ž‘์„ฑ๋œ ๊ฒฝ์šฐ ์ฐธ, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ๊ฑฐ์ง“์ž…๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/patches.py
def imwrite(filename: str, img: np.ndarray, params=None):
    """
    Write an image to a file.

    Args:
        filename (str): Path to the file to write.
        img (np.ndarray): Image to write.
        params (list of ints, optional): Additional parameters. See OpenCV documentation.

    Returns:
        (bool): True if the file was written, False otherwise.
    """
    try:
        cv2.imencode(Path(filename).suffix, img, params)[1].tofile(filename)
        return True
    except Exception:
        return False



ultralytics.utils.patches.imshow(winname, mat)

์ง€์ •๋œ ์ฐฝ์— ์ด๋ฏธ์ง€๋ฅผ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.

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

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

์ฐฝ์˜ ์ด๋ฆ„์ž…๋‹ˆ๋‹ค.

ํ•„์ˆ˜
mat ndarray

ํ‘œ์‹œํ•  ์ด๋ฏธ์ง€.

ํ•„์ˆ˜
์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/patches.py
def imshow(winname: str, mat: np.ndarray):
    """
    Displays an image in the specified window.

    Args:
        winname (str): Name of the window.
        mat (np.ndarray): Image to be shown.
    """
    _imshow(winname.encode("unicode_escape").decode(), mat)



ultralytics.utils.patches.torch_save(*args, **kwargs)

ํ”ผํด์ด ์ด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ ๋”œ(์žˆ๋Š” ๊ฒฝ์šฐ)์„ ์‚ฌ์šฉํ•˜์—ฌ ๋žŒ๋‹ค ํ•จ์ˆ˜๋ฅผ ์ง๋ ฌํ™”ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ์ €์žฅ ์‹คํŒจ ์‹œ 3๋ฒˆ์˜ ์žฌ์‹œ๋„๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ์ง€์ˆ˜ ์Šคํƒ ๋“œ์˜คํ”„๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ 3๋ฒˆ์˜ ์žฌ์‹œ๋„๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ์ผ์‹œ์ ์ธ ๋ฌธ์ œ์— ๋Œ€ํ•œ ๊ฒฌ๊ณ ์„ฑ์„ ํ–ฅ์ƒ์‹œํ‚ต๋‹ˆ๋‹ค.

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

์ด๋ฆ„ ์œ ํ˜• ์„ค๋ช… ๊ธฐ๋ณธ๊ฐ’
*args tuple

torch.save์— ์ „๋‹ฌํ•  ์œ„์น˜ ์ธ์ˆ˜์ž…๋‹ˆ๋‹ค.

()
**kwargs dict

torch.save์— ์ „๋‹ฌํ•  ํ‚ค์›Œ๋“œ ์ธ์ˆ˜์ž…๋‹ˆ๋‹ค.

{}
์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/utils/patches.py
def torch_save(*args, **kwargs):
    """
    Use dill (if exists) to serialize the lambda functions where pickle does not do this. Also adds 3 retries with
    exponential standoff in case of save failure to improve robustness to transient issues.

    Args:
        *args (tuple): Positional arguments to pass to torch.save.
        **kwargs (dict): Keyword arguments to pass to torch.save.
    """
    try:
        import dill as pickle  # noqa
    except ImportError:
        import pickle

    if "pickle_module" not in kwargs:
        kwargs["pickle_module"] = pickle  # noqa

    for i in range(4):  # 3 retries
        try:
            return _torch_save(*args, **kwargs)
        except RuntimeError:  # unable to save, possibly waiting for device to flush or anti-virus to finish scanning
            if i == 3:
                raise
            time.sleep((2**i) / 2)  # exponential standoff 0.5s, 1.0s, 2.0s





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