─░├žeri─če ge├ž

Referans i├žin ultralytics/models/rtdetr/train.py

Not

Bu dosya https://github.com/ultralytics/ultralytics/blob/main/ ultralytics/models/rtdetr/train .py adresinde mevcuttur. Bir sorun tespit ederseniz l├╝tfen bir ├çekme ─░ste─či ­čŤá´ŞĆ ile katk─▒da bulunarak d├╝zeltilmesine yard─▒mc─▒ olun. Te┼čekk├╝rler ­čÖĆ!



ultralytics.models.rtdetr.train.RTDETRTrainer

├ťsler: DetectionTrainer

Baidu taraf─▒ndan ger├žek zamanl─▒ nesne alg─▒lama i├žin geli┼čtirilen RT-DETR modeli i├žin e─čitmen s─▒n─▒f─▒. DetectionTrainer'─▒ geni┼čletir s─▒n─▒f─▒n─▒ YOLO 'un belirli ├Âzelliklerine ve mimarisine uyarlamak i├žin kullan─▒r. RT-DETR. Bu model Vision D├Ân├╝┼čt├╝r├╝c├╝ler ve IoU fark─▒ndal─▒ sorgu se├žimi ve uyarlanabilir ├ž─▒kar─▒m h─▒z─▒ gibi yeteneklere sahiptir.

Notlar
  • RT-DETR adresinde kullan─▒lan F.grid_sample desteklemiyor deterministic=True Tart─▒┼čma.
  • AMP e─čitimi NaN ├ž─▒kt─▒lar─▒na yol a├žabilir ve iki par├žal─▒ grafik e┼čle┼čtirme s─▒ras─▒nda hatalar ├╝retebilir.
├ľrnek
from ultralytics.models.rtdetr.train import RTDETRTrainer

args = dict(model='rtdetr-l.yaml', data='coco8.yaml', imgsz=640, epochs=3)
trainer = RTDETRTrainer(overrides=args)
trainer.train()
Kaynak kodu ultralytics/models/rtdetr/train.py
class RTDETRTrainer(DetectionTrainer):
    """
    Trainer class for the RT-DETR model developed by Baidu for real-time object detection. Extends the DetectionTrainer
    class for YOLO to adapt to the specific features and architecture of RT-DETR. This model leverages Vision
    Transformers and has capabilities like IoU-aware query selection and adaptable inference speed.

    Notes:
        - F.grid_sample used in RT-DETR does not support the `deterministic=True` argument.
        - AMP training can lead to NaN outputs and may produce errors during bipartite graph matching.

    Example:
        ```python
        from ultralytics.models.rtdetr.train import RTDETRTrainer

        args = dict(model='rtdetr-l.yaml', data='coco8.yaml', imgsz=640, epochs=3)
        trainer = RTDETRTrainer(overrides=args)
        trainer.train()
        ```
    """

    def get_model(self, cfg=None, weights=None, verbose=True):
        """
        Initialize and return an RT-DETR model for object detection tasks.

        Args:
            cfg (dict, optional): Model configuration. Defaults to None.
            weights (str, optional): Path to pre-trained model weights. Defaults to None.
            verbose (bool): Verbose logging if True. Defaults to True.

        Returns:
            (RTDETRDetectionModel): Initialized model.
        """
        model = RTDETRDetectionModel(cfg, nc=self.data["nc"], verbose=verbose and RANK == -1)
        if weights:
            model.load(weights)
        return model

    def build_dataset(self, img_path, mode="val", batch=None):
        """
        Build and return an RT-DETR dataset for training or validation.

        Args:
            img_path (str): Path to the folder containing images.
            mode (str): Dataset mode, either 'train' or 'val'.
            batch (int, optional): Batch size for rectangle training. Defaults to None.

        Returns:
            (RTDETRDataset): Dataset object for the specific mode.
        """
        return RTDETRDataset(
            img_path=img_path,
            imgsz=self.args.imgsz,
            batch_size=batch,
            augment=mode == "train",
            hyp=self.args,
            rect=False,
            cache=self.args.cache or None,
            prefix=colorstr(f"{mode}: "),
            data=self.data,
        )

    def get_validator(self):
        """
        Returns a DetectionValidator suitable for RT-DETR model validation.

        Returns:
            (RTDETRValidator): Validator object for model validation.
        """
        self.loss_names = "giou_loss", "cls_loss", "l1_loss"
        return RTDETRValidator(self.test_loader, save_dir=self.save_dir, args=copy(self.args))

    def preprocess_batch(self, batch):
        """
        Preprocess a batch of images. Scales and converts the images to float format.

        Args:
            batch (dict): Dictionary containing a batch of images, bboxes, and labels.

        Returns:
            (dict): Preprocessed batch.
        """
        batch = super().preprocess_batch(batch)
        bs = len(batch["img"])
        batch_idx = batch["batch_idx"]
        gt_bbox, gt_class = [], []
        for i in range(bs):
            gt_bbox.append(batch["bboxes"][batch_idx == i].to(batch_idx.device))
            gt_class.append(batch["cls"][batch_idx == i].to(device=batch_idx.device, dtype=torch.long))
        return batch

build_dataset(img_path, mode='val', batch=None)

E─čitim veya do─črulama i├žin bir RT-DETR veri k├╝mesi olu┼čturun ve d├Ând├╝r├╝n.

Parametreler:

─░sim Tip A├ž─▒klama Varsay─▒lan
img_path str

G├Âr├╝nt├╝leri i├žeren klas├Âr├╝n yolu.

gerekli
mode str

Veri k├╝mesi modu, 'train' veya 'val'.

'val'
batch int

Dikd├Ârtgen e─čitimi i├žin toplu i┼č boyutu. Varsay─▒lan de─čer Yok'tur.

None

─░ade:

Tip A├ž─▒klama
RTDETRDataset

Belirli mod i├žin veri k├╝mesi nesnesi.

Kaynak kodu ultralytics/models/rtdetr/train.py
def build_dataset(self, img_path, mode="val", batch=None):
    """
    Build and return an RT-DETR dataset for training or validation.

    Args:
        img_path (str): Path to the folder containing images.
        mode (str): Dataset mode, either 'train' or 'val'.
        batch (int, optional): Batch size for rectangle training. Defaults to None.

    Returns:
        (RTDETRDataset): Dataset object for the specific mode.
    """
    return RTDETRDataset(
        img_path=img_path,
        imgsz=self.args.imgsz,
        batch_size=batch,
        augment=mode == "train",
        hyp=self.args,
        rect=False,
        cache=self.args.cache or None,
        prefix=colorstr(f"{mode}: "),
        data=self.data,
    )

get_model(cfg=None, weights=None, verbose=True)

Nesne alg─▒lama g├Ârevleri i├žin bir RT-DETR modelini ba┼člat─▒r ve d├Ând├╝r├╝r.

Parametreler:

─░sim Tip A├ž─▒klama Varsay─▒lan
cfg dict

Model yap─▒land─▒rmas─▒. Varsay─▒lan de─čer Yok'tur.

None
weights str

├ľnceden e─čitilmi┼č model a─č─▒rl─▒klar─▒na giden yol. Varsay─▒lan de─čer Yok'tur.

None
verbose bool

True ise ayr─▒nt─▒l─▒ g├╝nl├╝k kayd─▒. Varsay─▒lan de─čer True'dur.

True

─░ade:

Tip A├ž─▒klama
RTDETRDetectionModel

Model ba┼člat─▒ld─▒.

Kaynak kodu ultralytics/models/rtdetr/train.py
def get_model(self, cfg=None, weights=None, verbose=True):
    """
    Initialize and return an RT-DETR model for object detection tasks.

    Args:
        cfg (dict, optional): Model configuration. Defaults to None.
        weights (str, optional): Path to pre-trained model weights. Defaults to None.
        verbose (bool): Verbose logging if True. Defaults to True.

    Returns:
        (RTDETRDetectionModel): Initialized model.
    """
    model = RTDETRDetectionModel(cfg, nc=self.data["nc"], verbose=verbose and RANK == -1)
    if weights:
        model.load(weights)
    return model

get_validator()

RT-DETR model do─črulamas─▒ i├žin uygun bir DetectionValidator d├Ând├╝r├╝r.

─░ade:

Tip A├ž─▒klama
RTDETRValidator

Model do─črulamas─▒ i├žin do─črulay─▒c─▒ nesnesi.

Kaynak kodu ultralytics/models/rtdetr/train.py
def get_validator(self):
    """
    Returns a DetectionValidator suitable for RT-DETR model validation.

    Returns:
        (RTDETRValidator): Validator object for model validation.
    """
    self.loss_names = "giou_loss", "cls_loss", "l1_loss"
    return RTDETRValidator(self.test_loader, save_dir=self.save_dir, args=copy(self.args))

preprocess_batch(batch)

Bir grup g├Âr├╝nt├╝y├╝ ├Ân i┼čleme tabi tutar. G├Âr├╝nt├╝leri ├Âl├žeklendirir ve kayan bi├žime d├Ân├╝┼čt├╝r├╝r.

Parametreler:

─░sim Tip A├ž─▒klama Varsay─▒lan
batch dict

Bir grup g├Âr├╝nt├╝, bbox ve etiket i├žeren s├Âzl├╝k.

gerekli

─░ade:

Tip A├ž─▒klama
dict

├ľnceden i┼členmi┼č parti.

Kaynak kodu ultralytics/models/rtdetr/train.py
def preprocess_batch(self, batch):
    """
    Preprocess a batch of images. Scales and converts the images to float format.

    Args:
        batch (dict): Dictionary containing a batch of images, bboxes, and labels.

    Returns:
        (dict): Preprocessed batch.
    """
    batch = super().preprocess_batch(batch)
    bs = len(batch["img"])
    batch_idx = batch["batch_idx"]
    gt_bbox, gt_class = [], []
    for i in range(bs):
        gt_bbox.append(batch["bboxes"][batch_idx == i].to(batch_idx.device))
        gt_class.append(batch["cls"][batch_idx == i].to(device=batch_idx.device, dtype=torch.long))
    return batch





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