Repository: ultralytics/ultralytics
Issue category: Bug Report

Issue:
Training labels not applied properly to training data
### Search before asking

- [X] I have searched the Ultralytics YOLO [issues](https://github.com/ultralytics/ultralytics/issues) and found no similar bug report.


### Ultralytics YOLO Component

Train

### Bug

# Bug
Labels are not included in the generated train_batch**X**.jpg images during training of a segmentation model.
Code to reproduce at bottom of section including the example training data.

## Likely cause of bug
I am not familiar with how the training label images are generated, however I highly suspect the issue is that if there are no points that define the polygon (label) in the image. This is caused when Yolo performs augmentation such as crop, resize, stretch, etc as it can morph the label such that all points defining the label are outside the image. This causes the mask to encompress up to the entire image but still not be included
### I do not know if this affects anything other than segmentation!
### This may actually affect the training data itself and not just the generated image examples, but I am not sure!

## Examples
- All white parts of the images are included in the label, thus if they are unlabelled the bug has occured
![train_batch41](https://github.com/user-attachments/assets/ff8243c4-badb-4ea9-a5c0-64b9c28fbef6)
![train_batch42](https://github.com/user-attachments/assets/17895e1b-a967-4c6d-8a18-39b59962893d)

### Code to reproduce, instuctions in other section
[GitIssues.zip](https://github.com/user-attachments/files/17916419/GitIssues.zip)


### Environment

```
Ultralytics 8.3.29 🚀 Python-3.10.12 torch-2.4.1+cu121 CUDA:0 (NVIDIA GeForce RTX 4090, 24564MiB)
Setup complete ✅ (32 CPUs, 15.5 GB RAM, 23.5/251.0 GB disk)

OS                  Linux-5.10.102.1-microsoft-standard-WSL2-x86_64-with-glibc2.35
Environment         Linux
Python              3.10.12
Install             pip
RAM                 15.47 GB
Disk                23.5/251.0 GB
CPU                 13th Gen Intel Core(TM) i9-13900
CPU count           32
GPU                 NVIDIA GeForce RTX 4090, 24564MiB
GPU count           1
CUDA                12.1

numpy               ✅ 2.1.2>=1.23.0
matplotlib          ✅ 3.9.2>=3.3.0
opencv-python       ✅ 4.10.0.84>=4.6.0
pillow              ✅ 10.4.0>=7.1.2
pyyaml              ✅ 5.4.1>=5.3.1
requests            ✅ 2.32.3>=2.23.0
scipy               ✅ 1.14.1>=1.4.1
torch               ✅ 2.4.1>=1.8.0
torchvision         ✅ 0.19.1>=0.9.0
tqdm                ✅ 4.66.5>=4.64.0
psutil              ✅ 6.0.0
py-cpuinfo          ✅ 9.0.0
pandas              ✅ 2.2.3>=1.1.4
seaborn             ✅ 0.13.2>=0.11.0
ultralytics-thop    ✅ 2.0.11>=2.0.0
numpy               ✅ 2.1.2<2.0.0; sys_platform == "darwin"
torch               ✅ 2.4.1!=2.4.0,>=1.8.0; sys_platform == "win32"
```

### Minimal Reproducible Example

# How to reproduce
1. Download & Extract provided training images, config (.yaml) and test_yolo.py file 
2. Edit .yaml file such that the folder path is correct
3. Run test_yolo.py 
4. Examine the generated train_batch**X**.jpg images to see if the bug occured (You may need to train more than once)

## What to look for
- Any part that is white is labelled, so if any white pixels are unlabelled this bug has occured

### Examples
![train_batch0](https://github.com/user-attachments/assets/fe7f5b3f-1b00-4004-beb1-a50b5d5413b0)
- In this case the bottom left image is clearly white, but unlabelled

![train_batch2](https://github.com/user-attachments/assets/25cd0a90-8e46-48e8-ba99-0d15cf620719)
- Top right image does has white, but it isn't labelled


### Additional

_No response_

### Are you willing to submit a PR?

- [ ] Yes I'd like to help by submitting a PR!


Hints:
👋 Hello @TheOfficialOzone, thank you for bringing this to our attention 🚀! We understand that you're encountering an issue with labels not being applied correctly during the training of a segmentation model on the Ultralytics repository.

For us to assist you effectively, please ensure that you've provided a [minimum reproducible example](https://docs.ultralytics.com/help/minimum_reproducible_example/) if it's not already included in your report. This will help us understand and address the issue more efficiently. It seems like you've already attached a code example and some test images, which is great!

📄 In the meantime, we suggest ensuring all your dependencies are up-to-date. Upgrade to the latest `ultralytics` package, including all requirements, within a Python >=3.8 environment using PyTorch >=1.8 to see if the issue persists:

```bash
pip install -U ultralytics
```

For further tips and tricks regarding custom training, please refer to our [Tips for Best Training Results](https://docs.ultralytics.com/guides/model-training-tips/).

Join our Ultralytics community for real-time support or discussions:

- Head over to [Discord](https://ultralytics.com/discord) for chat support 🎧
- Visit [Discourse](https://community.ultralytics.com) for deeper discussions
- Share experiences or get insightful feedback on our [Subreddit](https://reddit.com/r/ultralytics)

Finally, an Ultralytics engineer will review the details of your issue soon and follow up with you for additional help. Thank you for your patience and cooperation!

## Environments

In case you wish to shift your work to a more verified environment, you might consider:

- **Notebooks** with free GPU access: <a href="https://console.paperspace.com/github/ultralytics/ultralytics"><img src="https://assets.paperspace.io/img/gradient-badge.svg" alt="Run on Gradient"/></a> <a href="https://colab.research.google.com/github/ultralytics/ultralytics/blob/main/examples/tutorial.ipynb"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"></a> <a href="https://www.kaggle.com/models/ultralytics/yolo11"><img src="https://kaggle.com/static/images/open-in-kaggle.svg" alt="Open In Kaggle"></a>

For more details on different environments, please refer to the [GCP Quickstart Guide](https://docs.ultralytics.com/yolov5/environments/google_cloud_quickstart_tutorial/), [AWS Quickstart Guide](https://docs.ultralytics.com/yolov5/environments/aws_quickstart_tutorial/), or the [Docker Quickstart Guide](https://docs.ultralytics.com/yolov5/environments/docker_image_quickstart_tutorial/).

We appreciate your engagement with the Ultralytics repository and hope to resolve your issue soon! 🌟
This issue persists after running `pip install -U ultralytics`.
The version that was upgraded to was ultralytics 8.3.37.
@TheOfficialOzone Thanks for reporting! I'm able to reproduce this with our dataset. I'll look into it!

Patch:
diff --git a/ultralytics/utils/ops.py b/ultralytics/utils/ops.py
index 25e83c61c3a..ac53546ed1b 100644
--- a/ultralytics/utils/ops.py
+++ b/ultralytics/utils/ops.py
@@ -75,9 +75,8 @@ def segment2box(segment, width=640, height=640):
         (np.ndarray): the minimum and maximum x and y values of the segment.
     """
     x, y = segment.T  # segment xy
-    inside = (x >= 0) & (y >= 0) & (x <= width) & (y <= height)
-    x = x[inside]
-    y = y[inside]
+    x = x.clip(0, width)
+    y = y.clip(0, height)
     return (
         np.array([x.min(), y.min(), x.max(), y.max()], dtype=segment.dtype)
         if any(x)


Test patch: