# Yolov11 -obb custom dataloader

**URL:** <https://community.ultralytics.com/t/yolov11-obb-custom-dataloader/1184>\
**Category:** YOLO\
**Tags:** yolo\
**Created:** [June 26, 2025, 1:53pm UTC](https://community.ultralytics.com/t/yolov11-obb-custom-dataloader/1184 "2025-06-26T13:53:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![William\_C](https://sea1.discourse-cdn.com/flex001/user_avatar/community.ultralytics.com/william_c/32/821_2.png) [@William\_C](https://community.ultralytics.com/u/William_C)\
**Post date:** [June 26, 2025, 1:53pm UTC](https://community.ultralytics.com/t/yolov11-obb-custom-dataloader/1184/1 "2025-06-26T13:53:29Z")

</div>

Im using a custom dataloader to train yolo in order to ovveride how yolo reads files as i need to load them from a server rather than the os.

im using a yolo collate function

```auto
def yolo_collate_fn(batch):
    imgs = torch.stack([sample['img'] for sample in batch], dim=0)
    cls_list = [sample['cls'] for sample in batch]
    bboxes_list = [sample['bboxes'] for sample in batch]
    img_paths = [sample['img_path'] for sample in batch]
    ori_shapes = [sample['ori_shape'] for sample in batch]

    batch_idx = []
    for i, cls in enumerate(cls_list):
        batch_idx.append(torch.full((cls.shape[0],), i, dtype=torch.long))
    batch_idx = torch.cat(batch_idx, dim=0) if batch_idx else torch.tensor([], dtype=torch.long)

    return {
        'img': imgs,
        'cls': torch.cat(cls_list, dim=0) if cls_list else torch.tensor([], dtype=torch.float32),
        'bboxes': torch.cat(bboxes_list, dim=0) if bboxes_list else torch.tensor([], dtype=torch.float32),
        'batch_idx': batch_idx,
        'img_path': img_paths,
        'im_file': img_paths,
        'ori_shape': ori_shapes,
    }

```

my bboxes are length 8 to coorespond to xyxyxyxy format

tensor([0.7008, 0.9064, 0.7146, 0.9037, 0.7170, 0.9157, 0.7032, 0.9185])

im training via this manner

```auto
overrides = {
    'model': "yolo11n-obb.pt",
    'task': 'detect-obb',    
    'imgsz': 768,
    'epochs': 1,
    'batch': BATCH_SIZE,
    'device': 0,
    'workers': 0,
    'verbose': True,
    'data': 'data.yaml',
}

trainer = Custom_OBBTrainer(overrides=overrides)

trainer.train()

```

yet, when i train it seems like the loss function is expecting xyhwa format as i get this error

```auto
RuntimeError Traceback (most recent call last)
~/.conda/envs/py-cv-client-ipython/lib/python3.9/site-packages/ultralytics/utils/loss.py in __call__ (self, preds, batch)
    701 batch_idx = batch["batch_idx"].view(-1, 1)
--> 702 targets = torch.cat((batch_idx, batch["cls"].view(-1, 1), batch["bboxes"].view(-1, 5)), 1)
    703 rw, rh = targets[:, 4] * imgsz[0].item(), targets[:, 5] * imgsz[1].item()

RuntimeError: shape '[-1, 5]' is invalid for input of size 64

```

is it possible to train with a custom dataloader via xyxyxyxy format or should i swap all my data to 5 dim format?

thanks

---

<div class="post-metadata">

**Author:** ![BurhanQ](https://sea1.discourse-cdn.com/flex001/user_avatar/community.ultralytics.com/burhanq/32/7_2.png) [@BurhanQ](https://community.ultralytics.com/u/BurhanQ)\
**Post date:** [June 26, 2025, 2:32pm UTC](https://community.ultralytics.com/t/yolov11-obb-custom-dataloader/1184/2 "2025-06-26T14:32:54Z")

</div>

I haven’t tried running a custom dataloader, but there are a few things to point you to that should help. First, the `xyxyxyxy` annotation format is correct, but it’s treated as a `segment` not a `bbox`.

> <https://github.com/ultralytics/ultralytics/blob/320d75860a5d4f912dc74887a48162351c813486/ultralytics/data/utils.py#L205>

You’ll want to follow the operations for the `segments` variable in the `verify_image_label` function (or the function directly). That way you’ll end up with the same input.

For the `collate_fn` you should check out this line:

> <https://github.com/ultralytics/ultralytics/blob/320d75860a5d4f912dc74887a48162351c813486/ultralytics/data/dataset.py#L307>

and additionally follow the processing steps there. I _think_ that should help you get closer to where you need to be (if not all the way there).

---

<div class="post-metadata">

**Author:** ![William\_C](https://sea1.discourse-cdn.com/flex001/user_avatar/community.ultralytics.com/william_c/32/821_2.png) [@William\_C](https://community.ultralytics.com/u/William_C)\
**Post date:** [June 26, 2025, 3:15pm UTC](https://community.ultralytics.com/t/yolov11-obb-custom-dataloader/1184/3 "2025-06-26T15:15:54Z")

</div>

still getting the same errors, is there something i need to set in the yaml or elsewhere to tell yolo to use segments vs bboxes, it is still expecting a 5 length tensor in the loss function

ity seems like line 702 of ~ultralytics/utils/loss.py suggests its hard coded to length 5. i dont see a length 8 alternative

---

<div class="post-metadata">

**Author:** ![BurhanQ](https://sea1.discourse-cdn.com/flex001/user_avatar/community.ultralytics.com/burhanq/32/7_2.png) [@BurhanQ](https://community.ultralytics.com/u/BurhanQ)\
**Post date:** [June 27, 2025, 12:36pm UTC](https://community.ultralytics.com/t/yolov11-obb-custom-dataloader/1184/4 "2025-06-27T12:36:19Z")

</div>

I pointed out that segments are used, but boxes are _also_ used and you can see the conversion in the `verify_image_label()` function

> <https://github.com/ultralytics/ultralytics/blob/320d75860a5d4f912dc74887a48162351c813486/ultralytics/data/utils.py#L208>

---

<div class="post-metadata">

**Author:** ![BurhanQ](https://sea1.discourse-cdn.com/flex001/user_avatar/community.ultralytics.com/burhanq/32/7_2.png) [@BurhanQ](https://community.ultralytics.com/u/BurhanQ)\
**Post date:** [June 27, 2025, 12:37pm UTC](https://community.ultralytics.com/t/yolov11-obb-custom-dataloader/1184/5 "2025-06-27T12:37:53Z")

</div>

I think you’ll have to follow the same logic in or reuse the `verify_image_label` function

---

<div class="post-metadata">

**Author:** ![BurhanQ](https://sea1.discourse-cdn.com/flex001/user_avatar/community.ultralytics.com/burhanq/32/7_2.png) [@BurhanQ](https://community.ultralytics.com/u/BurhanQ)\
**Post date:** [June 27, 2025, 12:42pm UTC](https://community.ultralytics.com/t/yolov11-obb-custom-dataloader/1184/6 "2025-06-27T12:42:01Z")

</div>

Oh, also there’s the `Format` class that includes this operation for OBB conversion

> <https://github.com/ultralytics/ultralytics/blob/4e962d7bcbdbe3b1179d97836fd721fc376738b6/ultralytics/data/augment.py#L2181>

It’s used in the `build_transforms` method of the `YOLODataset` class

> <https://github.com/ultralytics/ultralytics/blob/4e962d7bcbdbe3b1179d97836fd721fc376738b6/ultralytics/data/dataset.py#L226>
