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

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

์ฐธ๊ณ 

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



ultralytics.models.nas.predict.NASPredictor

๋ฒ ์ด์Šค: BasePredictor

Ultralytics YOLO ๋ฌผ์ฒด ๊ฐ์ง€๋ฅผ ์œ„ํ•œ NAS Predictor.

์ด ํด๋ž˜์Šค๋Š” BasePredictor ์˜ Ultralytics ์—”์ง„์—์„œ ์ƒ์„ฑ๋œ ์›์‹œ ์˜ˆ์ธก์˜ ์‚ฌํ›„ ์ฒ˜๋ฆฌ๋ฅผ ๋‹ด๋‹นํ•˜๋ฉฐ YOLO ์›์‹œ ์˜ˆ์ธก์„ ์‚ฌํ›„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ๋น„์ตœ๋Œ€ ์–ต์ œ์™€ ๊ฐ™์€ ์ž‘์—…์„ ์ ์šฉํ•˜๊ณ  ์›๋ณธ ์ด๋ฏธ์ง€ ํฌ๊ธฐ์— ๋งž๊ฒŒ ์›๋ณธ ์ด๋ฏธ์ง€ ํฌ๊ธฐ์— ๋งž๊ฒŒ ๊ฒฝ๊ณ„ ์ƒ์ž ํฌ๊ธฐ ์กฐ์ • ๋“ฑ์˜ ์ž‘์—…์„ ์ ์šฉํ•ฉ๋‹ˆ๋‹ค.

์†์„ฑ:

์ด๋ฆ„ ์œ ํ˜• ์„ค๋ช…
args Namespace

ํ›„์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ๋‹ค์–‘ํ•œ ๊ตฌ์„ฑ์ด ํฌํ•จ๋œ ๋„ค์ž„์ŠคํŽ˜์ด์Šค์ž…๋‹ˆ๋‹ค.

์˜ˆ
from ultralytics import NAS

model = NAS('yolo_nas_s')
predictor = model.predictor
# Assumes that raw_preds, img, orig_imgs are available
results = predictor.postprocess(raw_preds, img, orig_imgs)
์ฐธ๊ณ 

์ผ๋ฐ˜์ ์œผ๋กœ ์ด ํด๋ž˜์Šค๋Š” ์ง์ ‘ ์ธ์Šคํ„ด์Šคํ™”๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ด ํด๋ž˜์Šค๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ NAS ํด๋ž˜์Šค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/models/nas/predict.py
class NASPredictor(BasePredictor):
    """
    Ultralytics YOLO NAS Predictor for object detection.

    This class extends the `BasePredictor` from Ultralytics engine and is responsible for post-processing the
    raw predictions generated by the YOLO NAS models. It applies operations like non-maximum suppression and
    scaling the bounding boxes to fit the original image dimensions.

    Attributes:
        args (Namespace): Namespace containing various configurations for post-processing.

    Example:
        ```python
        from ultralytics import NAS

        model = NAS('yolo_nas_s')
        predictor = model.predictor
        # Assumes that raw_preds, img, orig_imgs are available
        results = predictor.postprocess(raw_preds, img, orig_imgs)
        ```

    Note:
        Typically, this class is not instantiated directly. It is used internally within the `NAS` class.
    """

    def postprocess(self, preds_in, img, orig_imgs):
        """Postprocess predictions and returns a list of Results objects."""

        # Cat boxes and class scores
        boxes = ops.xyxy2xywh(preds_in[0][0])
        preds = torch.cat((boxes, preds_in[0][1]), -1).permute(0, 2, 1)

        preds = ops.non_max_suppression(
            preds,
            self.args.conf,
            self.args.iou,
            agnostic=self.args.agnostic_nms,
            max_det=self.args.max_det,
            classes=self.args.classes,
        )

        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, pred in enumerate(preds):
            orig_img = orig_imgs[i]
            pred[:, :4] = ops.scale_boxes(img.shape[2:], pred[:, :4], orig_img.shape)
            img_path = self.batch[0][i]
            results.append(Results(orig_img, path=img_path, names=self.model.names, boxes=pred))
        return results

postprocess(preds_in, img, orig_imgs)

์˜ˆ์ธก์„ ์‚ฌํ›„ ์ฒ˜๋ฆฌํ•˜๊ณ  ๊ฒฐ๊ณผ ๊ฐœ์ฒด ๋ชฉ๋ก์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ ultralytics/models/nas/predict.py
def postprocess(self, preds_in, img, orig_imgs):
    """Postprocess predictions and returns a list of Results objects."""

    # Cat boxes and class scores
    boxes = ops.xyxy2xywh(preds_in[0][0])
    preds = torch.cat((boxes, preds_in[0][1]), -1).permute(0, 2, 1)

    preds = ops.non_max_suppression(
        preds,
        self.args.conf,
        self.args.iou,
        agnostic=self.args.agnostic_nms,
        max_det=self.args.max_det,
        classes=self.args.classes,
    )

    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, pred in enumerate(preds):
        orig_img = orig_imgs[i]
        pred[:, :4] = ops.scale_boxes(img.shape[2:], pred[:, :4], orig_img.shape)
        img_path = self.batch[0][i]
        results.append(Results(orig_img, path=img_path, names=self.model.names, boxes=pred))
    return results





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