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

μ°Έμ‘° ultralytics/data/build.py

μ°Έκ³ 

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



ultralytics.data.build.InfiniteDataLoader

기지: DataLoader

μ›Œμ»€λ₯Ό μž¬μ‚¬μš©ν•˜λŠ” 데이터 λ‘œλ”.

바닐라 λ°μ΄ν„°λ‘œλ”μ™€ λ™μΌν•œ ꡬ문을 μ‚¬μš©ν•©λ‹ˆλ‹€.

의 μ†ŒμŠ€ μ½”λ“œ ultralytics/data/build.py
class InfiniteDataLoader(dataloader.DataLoader):
    """
    Dataloader that reuses workers.

    Uses same syntax as vanilla DataLoader.
    """

    def __init__(self, *args, **kwargs):
        """Dataloader that infinitely recycles workers, inherits from DataLoader."""
        super().__init__(*args, **kwargs)
        object.__setattr__(self, "batch_sampler", _RepeatSampler(self.batch_sampler))
        self.iterator = super().__iter__()

    def __len__(self):
        """Returns the length of the batch sampler's sampler."""
        return len(self.batch_sampler.sampler)

    def __iter__(self):
        """Creates a sampler that repeats indefinitely."""
        for _ in range(len(self)):
            yield next(self.iterator)

    def reset(self):
        """
        Reset iterator.

        This is useful when we want to modify settings of dataset while training.
        """
        self.iterator = self._get_iterator()

__init__(*args, **kwargs)

μž‘μ—…μžλ₯Ό λ¬΄ν•œνžˆ μž¬ν™œμš©ν•˜λŠ” λ°μ΄ν„°λ‘œλ”λŠ” λ°μ΄ν„°λ‘œλ”λ₯Ό μƒμ†ν•©λ‹ˆλ‹€.

의 μ†ŒμŠ€ μ½”λ“œ ultralytics/data/build.py
def __init__(self, *args, **kwargs):
    """Dataloader that infinitely recycles workers, inherits from DataLoader."""
    super().__init__(*args, **kwargs)
    object.__setattr__(self, "batch_sampler", _RepeatSampler(self.batch_sampler))
    self.iterator = super().__iter__()

__iter__()

λ¬΄ν•œ λ°˜λ³΅λ˜λŠ” μƒ˜ν”ŒλŸ¬λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€.

의 μ†ŒμŠ€ μ½”λ“œ ultralytics/data/build.py
def __iter__(self):
    """Creates a sampler that repeats indefinitely."""
    for _ in range(len(self)):
        yield next(self.iterator)

__len__()

배치 μƒ˜ν”ŒλŸ¬μ˜ μƒ˜ν”ŒλŸ¬ 길이λ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.

의 μ†ŒμŠ€ μ½”λ“œ ultralytics/data/build.py
def __len__(self):
    """Returns the length of the batch sampler's sampler."""
    return len(self.batch_sampler.sampler)

reset()

반볡기λ₯Ό μž¬μ„€μ •ν•©λ‹ˆλ‹€.

이 κΈ°λŠ₯은 ν•™μŠ΅ 쀑에 데이터 μ„ΈνŠΈμ˜ 섀정을 μˆ˜μ •ν•  λ•Œ μœ μš©ν•©λ‹ˆλ‹€.

의 μ†ŒμŠ€ μ½”λ“œ ultralytics/data/build.py
def reset(self):
    """
    Reset iterator.

    This is useful when we want to modify settings of dataset while training.
    """
    self.iterator = self._get_iterator()



ultralytics.data.build._RepeatSampler

μ˜μ›νžˆ λ°˜λ³΅λ˜λŠ” μƒ˜ν”ŒλŸ¬.

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

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

λ°˜λ³΅ν•  μƒ˜ν”ŒλŸ¬μž…λ‹ˆλ‹€.

ν•„μˆ˜
의 μ†ŒμŠ€ μ½”λ“œ ultralytics/data/build.py
class _RepeatSampler:
    """
    Sampler that repeats forever.

    Args:
        sampler (Dataset.sampler): The sampler to repeat.
    """

    def __init__(self, sampler):
        """Initializes an object that repeats a given sampler indefinitely."""
        self.sampler = sampler

    def __iter__(self):
        """Iterates over the 'sampler' and yields its contents."""
        while True:
            yield from iter(self.sampler)

__init__(sampler)

주어진 μƒ˜ν”ŒλŸ¬λ₯Ό λ¬΄ν•œνžˆ λ°˜λ³΅ν•˜λŠ” 객체λ₯Ό μ΄ˆκΈ°ν™”ν•©λ‹ˆλ‹€.

의 μ†ŒμŠ€ μ½”λ“œ ultralytics/data/build.py
def __init__(self, sampler):
    """Initializes an object that repeats a given sampler indefinitely."""
    self.sampler = sampler

__iter__()

'μƒ˜ν”ŒλŸ¬'λ₯Ό λ°˜λ³΅ν•˜μ—¬ κ·Έ λ‚΄μš©μ„ μ‚°μΆœν•©λ‹ˆλ‹€.

의 μ†ŒμŠ€ μ½”λ“œ ultralytics/data/build.py
def __iter__(self):
    """Iterates over the 'sampler' and yields its contents."""
    while True:
        yield from iter(self.sampler)



ultralytics.data.build.seed_worker(worker_id)

λ°μ΄ν„°λ‘œλ” μ›Œμ»€ μ‹œλ“œ μ„€μ • https://pytorch.org/docs/stable/notes/randomness.html#dataloader.

의 μ†ŒμŠ€ μ½”λ“œ ultralytics/data/build.py
def seed_worker(worker_id):  # noqa
    """Set dataloader worker seed https://pytorch.org/docs/stable/notes/randomness.html#dataloader."""
    worker_seed = torch.initial_seed() % 2**32
    np.random.seed(worker_seed)
    random.seed(worker_seed)



ultralytics.data.build.build_yolo_dataset(cfg, img_path, batch, data, mode='train', rect=False, stride=32)

YOLO 데이터 집합을 κ΅¬μΆ•ν•©λ‹ˆλ‹€.

의 μ†ŒμŠ€ μ½”λ“œ ultralytics/data/build.py
def build_yolo_dataset(cfg, img_path, batch, data, mode="train", rect=False, stride=32):
    """Build YOLO Dataset."""
    return YOLODataset(
        img_path=img_path,
        imgsz=cfg.imgsz,
        batch_size=batch,
        augment=mode == "train",  # augmentation
        hyp=cfg,  # TODO: probably add a get_hyps_from_cfg function
        rect=cfg.rect or rect,  # rectangular batches
        cache=cfg.cache or None,
        single_cls=cfg.single_cls or False,
        stride=int(stride),
        pad=0.0 if mode == "train" else 0.5,
        prefix=colorstr(f"{mode}: "),
        task=cfg.task,
        classes=cfg.classes,
        data=data,
        fraction=cfg.fraction if mode == "train" else 1.0,
    )



ultralytics.data.build.build_dataloader(dataset, batch, workers, shuffle=True, rank=-1)

νŠΈλ ˆμ΄λ‹ λ˜λŠ” μœ νš¨μ„± 검사 집합을 μœ„ν•œ λ¬΄ν•œ 데이터 λ‘œλ” λ˜λŠ” 데이터 λ‘œλ”λ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.

의 μ†ŒμŠ€ μ½”λ“œ ultralytics/data/build.py
def build_dataloader(dataset, batch, workers, shuffle=True, rank=-1):
    """Return an InfiniteDataLoader or DataLoader for training or validation set."""
    batch = min(batch, len(dataset))
    nd = torch.cuda.device_count()  # number of CUDA devices
    nw = min([os.cpu_count() // max(nd, 1), workers])  # number of workers
    sampler = None if rank == -1 else distributed.DistributedSampler(dataset, shuffle=shuffle)
    generator = torch.Generator()
    generator.manual_seed(6148914691236517205 + RANK)
    return InfiniteDataLoader(
        dataset=dataset,
        batch_size=batch,
        shuffle=shuffle and sampler is None,
        num_workers=nw,
        sampler=sampler,
        pin_memory=PIN_MEMORY,
        collate_fn=getattr(dataset, "collate_fn", None),
        worker_init_fn=seed_worker,
        generator=generator,
    )



ultralytics.data.build.check_source(source)

μ†ŒμŠ€ μœ ν˜•μ„ ν™•μΈν•˜κ³  ν•΄λ‹Ή ν”Œλž˜κ·Έ 값을 λ°˜ν™˜ν•©λ‹ˆλ‹€.

의 μ†ŒμŠ€ μ½”λ“œ ultralytics/data/build.py
def check_source(source):
    """Check source type and return corresponding flag values."""
    webcam, screenshot, from_img, in_memory, tensor = False, False, False, False, False
    if isinstance(source, (str, int, Path)):  # int for local usb camera
        source = str(source)
        is_file = Path(source).suffix[1:] in (IMG_FORMATS + VID_FORMATS)
        is_url = source.lower().startswith(("https://", "http://", "rtsp://", "rtmp://", "tcp://"))
        webcam = source.isnumeric() or source.endswith(".streams") or (is_url and not is_file)
        screenshot = source.lower() == "screen"
        if is_url and is_file:
            source = check_file(source)  # download
    elif isinstance(source, LOADERS):
        in_memory = True
    elif isinstance(source, (list, tuple)):
        source = autocast_list(source)  # convert all list elements to PIL or np arrays
        from_img = True
    elif isinstance(source, (Image.Image, np.ndarray)):
        from_img = True
    elif isinstance(source, torch.Tensor):
        tensor = True
    else:
        raise TypeError("Unsupported image type. For supported types see https://docs.ultralytics.com/modes/predict")

    return source, webcam, screenshot, from_img, in_memory, tensor



ultralytics.data.build.load_inference_source(source=None, vid_stride=1, buffer=False)

객체 감지λ₯Ό μœ„ν•œ μΆ”λ‘  μ†ŒμŠ€λ₯Ό λ‘œλ“œν•˜κ³  ν•„μš”ν•œ λ³€ν™˜μ„ μ μš©ν•©λ‹ˆλ‹€.

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

이름 μœ ν˜• μ„€λͺ… κΈ°λ³Έκ°’
source (str, Path, Tensor, Image, ndarray)

좔둠을 μœ„ν•œ μž…λ ₯ μ†ŒμŠ€μž…λ‹ˆλ‹€.

None
vid_stride int

λΉ„λ””μ˜€ μ†ŒμŠ€μ˜ ν”„λ ˆμž„ κ°„κ²©μž…λ‹ˆλ‹€. 기본값은 1μž…λ‹ˆλ‹€.

1
buffer bool

슀트림 ν”„λ ˆμž„μ„ 버퍼링할지 μ—¬λΆ€λ₯Ό κ²°μ •ν•©λ‹ˆλ‹€. 기본값은 Falseμž…λ‹ˆλ‹€.

False

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

이름 μœ ν˜• μ„€λͺ…
dataset Dataset

μ§€μ •λœ μž…λ ₯ μ†ŒμŠ€μ— λŒ€ν•œ 데이터 μ„ΈνŠΈ κ°μ²΄μž…λ‹ˆλ‹€.

의 μ†ŒμŠ€ μ½”λ“œ ultralytics/data/build.py
def load_inference_source(source=None, vid_stride=1, buffer=False):
    """
    Loads an inference source for object detection and applies necessary transformations.

    Args:
        source (str, Path, Tensor, PIL.Image, np.ndarray): The input source for inference.
        vid_stride (int, optional): The frame interval for video sources. Default is 1.
        buffer (bool, optional): Determined whether stream frames will be buffered. Default is False.

    Returns:
        dataset (Dataset): A dataset object for the specified input source.
    """
    source, webcam, screenshot, from_img, in_memory, tensor = check_source(source)
    source_type = source.source_type if in_memory else SourceTypes(webcam, screenshot, from_img, tensor)

    # Dataloader
    if tensor:
        dataset = LoadTensor(source)
    elif in_memory:
        dataset = source
    elif webcam:
        dataset = LoadStreams(source, vid_stride=vid_stride, buffer=buffer)
    elif screenshot:
        dataset = LoadScreenshots(source)
    elif from_img:
        dataset = LoadPilAndNumpy(source)
    else:
        dataset = LoadImages(source, vid_stride=vid_stride)

    # Attach source types to the dataset
    setattr(dataset, "source_type", source_type)

    return dataset





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