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

์ฐธ์กฐ ultralytics/models/rtdetr/predict.py

์ฐธ๊ณ 

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



ultralytics.models.rtdetr.predict.RTDETRPredictor

๋ฒ ์ด์Šค: BasePredictor

RT-DETR (์‹ค์‹œ๊ฐ„ ํƒ์ง€ ํŠธ๋žœ์Šคํฌ๋จธ) ๋‹ค์Œ์„ ์‚ฌ์šฉํ•˜์—ฌ ์˜ˆ์ธก์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด BasePredictor ํด๋ž˜์Šค๋ฅผ ํ™•์žฅํ•˜๋Š” ์˜ˆ์ธก์ž ๋ฐ”์ด๋‘์˜ RT-DETR ๋ชจ๋ธ.

์ด ํด๋ž˜์Šค๋Š” ๋น„์ „ ํŠธ๋žœ์Šคํฌ๋จธ์˜ ์„ฑ๋Šฅ์„ ํ™œ์šฉํ•˜์—ฌ ์‹ค์‹œ๊ฐ„ ๋ฌผ์ฒด ๊ฐ์ง€ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๋Š” ๋™์‹œ์— ๋†’์€ ์ •ํ™•๋„๋ฅผ ์œ ์ง€ํ•ฉ๋‹ˆ๋‹ค. ํšจ์œจ์ ์ธ ํ•˜์ด๋ธŒ๋ฆฌ๋“œ ์ธ์ฝ”๋”ฉ ๋ฐ IoU ์ธ์‹ ์ฟผ๋ฆฌ ์„ ํƒ๊ณผ ๊ฐ™์€ ์ฃผ์š” ๊ธฐ๋Šฅ์„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ
from ultralytics.utils import ASSETS
from ultralytics.models.rtdetr import RTDETRPredictor

args = dict(model='rtdetr-l.pt', source=ASSETS)
predictor = RTDETRPredictor(overrides=args)
predictor.predict_cli()

์†์„ฑ:

์ด๋ฆ„ ์œ ํ˜• ์„ค๋ช…
imgsz int

์ถ”๋ก ์„ ์œ„ํ•œ ์ด๋ฏธ์ง€ ํฌ๊ธฐ(์ •์‚ฌ๊ฐํ˜• ๋ฐ ๋ˆˆ๊ธˆ์ด ์ฑ„์›Œ์ง„ ํฌ๊ธฐ์—ฌ์•ผ ํ•จ).

args dict

์˜ˆ์ธก์ž์— ๋Œ€ํ•œ ์ธ์ˆ˜ ์žฌ์ •์˜.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/models/rtdetr/predict.py
class RTDETRPredictor(BasePredictor):
    """
    RT-DETR (Real-Time Detection Transformer) Predictor extending the BasePredictor class for making predictions using
    Baidu's RT-DETR model.

    This class leverages the power of Vision Transformers to provide real-time object detection while maintaining
    high accuracy. It supports key features like efficient hybrid encoding and IoU-aware query selection.

    Example:
        ```python
        from ultralytics.utils import ASSETS
        from ultralytics.models.rtdetr import RTDETRPredictor

        args = dict(model='rtdetr-l.pt', source=ASSETS)
        predictor = RTDETRPredictor(overrides=args)
        predictor.predict_cli()
        ```

    Attributes:
        imgsz (int): Image size for inference (must be square and scale-filled).
        args (dict): Argument overrides for the predictor.
    """

    def postprocess(self, preds, img, orig_imgs):
        """
        Postprocess the raw predictions from the model to generate bounding boxes and confidence scores.

        The method filters detections based on confidence and class if specified in `self.args`.

        Args:
            preds (list): List of [predictions, extra] from the model.
            img (torch.Tensor): Processed input images.
            orig_imgs (list or torch.Tensor): Original, unprocessed images.

        Returns:
            (list[Results]): A list of Results objects containing the post-processed bounding boxes, confidence scores,
                and class labels.
        """
        if not isinstance(preds, (list, tuple)):  # list for PyTorch inference but list[0] Tensor for export inference
            preds = [preds, None]

        nd = preds[0].shape[-1]
        bboxes, scores = preds[0].split((4, nd - 4), dim=-1)

        if not isinstance(orig_imgs, list):  # input images are a torch.Tensor, not a list
            orig_imgs = ops.convert_torch2numpy_batch(orig_imgs)

        results = []
        for i, bbox in enumerate(bboxes):  # (300, 4)
            bbox = ops.xywh2xyxy(bbox)
            score, cls = scores[i].max(-1, keepdim=True)  # (300, 1)
            idx = score.squeeze(-1) > self.args.conf  # (300, )
            if self.args.classes is not None:
                idx = (cls == torch.tensor(self.args.classes, device=cls.device)).any(1) & idx
            pred = torch.cat([bbox, score, cls], dim=-1)[idx]  # filter
            orig_img = orig_imgs[i]
            oh, ow = orig_img.shape[:2]
            pred[..., [0, 2]] *= ow
            pred[..., [1, 3]] *= oh
            img_path = self.batch[0][i]
            results.append(Results(orig_img, path=img_path, names=self.model.names, boxes=pred))
        return results

    def pre_transform(self, im):
        """
        Pre-transforms the input images before feeding them into the model for inference. The input images are
        letterboxed to ensure a square aspect ratio and scale-filled. The size must be square(640) and scaleFilled.

        Args:
            im (list[np.ndarray] |torch.Tensor): Input images of shape (N,3,h,w) for tensor, [(h,w,3) x N] for list.

        Returns:
            (list): List of pre-transformed images ready for model inference.
        """
        letterbox = LetterBox(self.imgsz, auto=False, scaleFill=True)
        return [letterbox(image=x) for x in im]

postprocess(preds, img, orig_imgs)

๋ชจ๋ธ์˜ ์›์‹œ ์˜ˆ์ธก์„ ํ›„์ฒ˜๋ฆฌํ•˜์—ฌ ๊ฒฝ๊ณ„ ์ƒ์ž ๋ฐ ์‹ ๋ขฐ๋„ ์ ์ˆ˜๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

์ด ๋ฉ”์„œ๋“œ๋Š” ๋‹ค์Œ์—์„œ ์ง€์ •ํ•œ ๊ฒฝ์šฐ ์‹ ๋ขฐ๋„ ๋ฐ ํด๋ž˜์Šค๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํƒ์ง€๋ฅผ ํ•„ํ„ฐ๋งํ•ฉ๋‹ˆ๋‹ค. self.args.

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

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

๋ชจ๋ธ์˜ [์˜ˆ์ธก, ์ถ”๊ฐ€] ๋ชฉ๋ก์ž…๋‹ˆ๋‹ค.

ํ•„์ˆ˜
img Tensor

์ฒ˜๋ฆฌ๋œ ์ž…๋ ฅ ์ด๋ฏธ์ง€.

ํ•„์ˆ˜
orig_imgs list or Tensor

๊ฐ€๊ณต๋˜์ง€ ์•Š์€ ์›๋ณธ ์ด๋ฏธ์ง€.

ํ•„์ˆ˜

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

์œ ํ˜• ์„ค๋ช…
list[Results]

์‚ฌํ›„ ์ฒ˜๋ฆฌ๋œ ๊ฒฝ๊ณ„ ์ƒ์ž, ์‹ ๋ขฐ๋„ ์ ์ˆ˜๊ฐ€ ํฌํ•จ๋œ ๊ฒฐ๊ณผ ๊ฐœ์ฒด ๋ชฉ๋ก์ž…๋‹ˆ๋‹ค, ๋ฐ ํด๋ž˜์Šค ๋ ˆ์ด๋ธ”์ด ํฌํ•จ๋œ ๊ฒฐ๊ณผ ์˜ค๋ธŒ์ ํŠธ ๋ชฉ๋ก์ž…๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/models/rtdetr/predict.py
def postprocess(self, preds, img, orig_imgs):
    """
    Postprocess the raw predictions from the model to generate bounding boxes and confidence scores.

    The method filters detections based on confidence and class if specified in `self.args`.

    Args:
        preds (list): List of [predictions, extra] from the model.
        img (torch.Tensor): Processed input images.
        orig_imgs (list or torch.Tensor): Original, unprocessed images.

    Returns:
        (list[Results]): A list of Results objects containing the post-processed bounding boxes, confidence scores,
            and class labels.
    """
    if not isinstance(preds, (list, tuple)):  # list for PyTorch inference but list[0] Tensor for export inference
        preds = [preds, None]

    nd = preds[0].shape[-1]
    bboxes, scores = preds[0].split((4, nd - 4), dim=-1)

    if not isinstance(orig_imgs, list):  # input images are a torch.Tensor, not a list
        orig_imgs = ops.convert_torch2numpy_batch(orig_imgs)

    results = []
    for i, bbox in enumerate(bboxes):  # (300, 4)
        bbox = ops.xywh2xyxy(bbox)
        score, cls = scores[i].max(-1, keepdim=True)  # (300, 1)
        idx = score.squeeze(-1) > self.args.conf  # (300, )
        if self.args.classes is not None:
            idx = (cls == torch.tensor(self.args.classes, device=cls.device)).any(1) & idx
        pred = torch.cat([bbox, score, cls], dim=-1)[idx]  # filter
        orig_img = orig_imgs[i]
        oh, ow = orig_img.shape[:2]
        pred[..., [0, 2]] *= ow
        pred[..., [1, 3]] *= oh
        img_path = self.batch[0][i]
        results.append(Results(orig_img, path=img_path, names=self.model.names, boxes=pred))
    return results

pre_transform(im)

์ถ”๋ก ์„ ์œ„ํ•ด ๋ชจ๋ธ์— ์ž…๋ ฅ ์ด๋ฏธ์ง€๋ฅผ ์ œ๊ณตํ•˜๊ธฐ ์ „์— ์ž…๋ ฅ ์ด๋ฏธ์ง€๋ฅผ ์‚ฌ์ „ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ž…๋ ฅ ์ด๋ฏธ์ง€๋Š” ์ •์‚ฌ๊ฐํ˜• ์ข…ํšก๋น„๋ฅผ ๋ณด์žฅํ•˜๊ธฐ ์œ„ํ•ด ๋ ˆํ„ฐ๋ฐ•์Šค ์ฒ˜๋ฆฌ๋˜๊ณ  ์Šค์ผ€์ผ๋กœ ์ฑ„์›Œ์ง‘๋‹ˆ๋‹ค. ํฌ๊ธฐ๋Š” ์ •์‚ฌ๊ฐํ˜•(640) ๋ฐ ์Šค์ผ€์ผ ์ฑ„์›Œ์ง์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

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

์ด๋ฆ„ ์œ ํ˜• ์„ค๋ช… ๊ธฐ๋ณธ๊ฐ’
im list[ndarray] | Tensor

tensor ์˜ ๊ฒฝ์šฐ ๋„ํ˜• ์ด๋ฏธ์ง€(N,3,h,w), ๋ชฉ๋ก์˜ ๊ฒฝ์šฐ [(h,w,3) x N]์„ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค.

ํ•„์ˆ˜

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

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

๋ชจ๋ธ ์ถ”๋ก ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ฏธ๋ฆฌ ๋ณ€ํ™˜๋œ ์ด๋ฏธ์ง€ ๋ชฉ๋ก์ž…๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/models/rtdetr/predict.py
def pre_transform(self, im):
    """
    Pre-transforms the input images before feeding them into the model for inference. The input images are
    letterboxed to ensure a square aspect ratio and scale-filled. The size must be square(640) and scaleFilled.

    Args:
        im (list[np.ndarray] |torch.Tensor): Input images of shape (N,3,h,w) for tensor, [(h,w,3) x N] for list.

    Returns:
        (list): List of pre-transformed images ready for model inference.
    """
    letterbox = LetterBox(self.imgsz, auto=False, scaleFill=True)
    return [letterbox(image=x) for x in im]





Created 2023-11-12, Updated 2024-06-02
Authors: glenn-jocher (5), Burhan-Q (1)