Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change bounding box label index to text #3578

Closed
hjsg1010 opened this issue Jun 10, 2021 · 8 comments · Fixed by #3882
Closed

change bounding box label index to text #3578

hjsg1010 opened this issue Jun 10, 2021 · 8 comments · Fixed by #3882
Labels
question Further information is requested

Comments

@hjsg1010
Copy link

❔Question

Hi, thanks for your great research and sharing codes.

I am applying YOLOv5 to my custom dataset.

And during learning, the mosaic section on wandb.ai shows the figure below.

image

However, I want to change the bbox label index to text label, such as '118' changes to 'cheesecake'.

What should I fix in code? or do I have to change my annotation?

Currently the data folder are consists of
data_folder/
┣ images/
┃ ┣ train/
┃ ┣ valid/
┣ labels/
┃ ┣ train/
┃ ┣ valid/
┣ data_config.yaml

Additional context

@hjsg1010 hjsg1010 added the question Further information is requested label Jun 10, 2021
@github-actions
Copy link
Contributor

github-actions bot commented Jun 10, 2021

👋 Hello @hjsg1010, thank you for your interest in 🚀 YOLOv5! Please visit our ⭐️ Tutorials to get started, where you can find quickstart guides for simple tasks like Custom Data Training all the way to advanced concepts like Hyperparameter Evolution.

If this is a 🐛 Bug Report, please provide screenshots and minimum viable code to reproduce your issue, otherwise we can not help you.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset images, training logs, screenshots, and a public link to online W&B logging if available.

For business inquiries or professional support requests please visit https://www.ultralytics.com or email Glenn Jocher at glenn.jocher@ultralytics.com.

Requirements

Python 3.8 or later with all requirements.txt dependencies installed, including torch>=1.7. To install run:

$ pip install -r requirements.txt

Environments

YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

CI CPU testing

If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training (train.py), testing (test.py), inference (detect.py) and export (export.py) on MacOS, Windows, and Ubuntu every 24 hours and on every commit.

@glenn-jocher
Copy link
Member

glenn-jocher commented Jun 10, 2021

@hjsg1010 @kalenmike yes, your results are correct. The train mosaics only display the class index rather than the class name. You can see this in the Train Custom Data tutorial. We did this because the mosaics can get quite busy for high density datasets. The test dataset labels show the full name though, i.e. test_batch0_labels.jpg, so you may want to look at those instead.
https://docs.ultralytics.com/yolov5/tutorials/train_custom_data#local-logging

We're also developing dataset introspection tools as part of the new Ultralytics HUB. This should be ready sometime later on in summer 2021 at https://ultralytics.com

duo3-1

@hjsg1010
Copy link
Author

hjsg1010 commented Jun 10, 2021

@hjsg1010 @kalenmike yes, your results are correct. The train mosaics only display the class index rather than the class name. You can see this in the Train Custom Data tutorial. We did this because the mosaics can get quite busy for high density datasets. The test dataset labels show the full name though, i.e. test_batch0_labels.jpg, so you may want to look at those instead.
https://docs.ultralytics.com/yolov5/tutorials/train_custom_data#local-logging

We're also developing dataset introspection tools as part of the new Ultralytics HUB. This should be ready sometime later on in summer 2021 at https://ultralytics.com

duo3-1

oh, Thx for quick and kind reply.
Again, thx for sharing your codes!!

@hjsg1010
Copy link
Author

@glenn-jocher
Hi, can I ask one more question?
I know that YOLO already does data augmentation.
However, I want to do custom data augmentation using Albumentations.
Then, where should I put augmentation pipeline code?
.utils/dataset.py ? or train.py?

sorry for my poor English skills, and low knowledge on object detection code..

@glenn-jocher
Copy link
Member

glenn-jocher commented Jun 10, 2021

@hjsg1010 you can inline any Albumentations augmentations you want in the dataloader here:

yolov5/utils/datasets.py

Lines 499 to 545 in 095197b

def __getitem__(self, index):
index = self.indices[index] # linear, shuffled, or image_weights
hyp = self.hyp
mosaic = self.mosaic and random.random() < hyp['mosaic']
if mosaic:
# Load mosaic
img, labels = load_mosaic(self, index)
shapes = None
# MixUp https://arxiv.org/pdf/1710.09412.pdf
if random.random() < hyp['mixup']:
img2, labels2 = load_mosaic(self, random.randint(0, self.n - 1))
r = np.random.beta(32.0, 32.0) # mixup ratio, alpha=beta=32.0
img = (img * r + img2 * (1 - r)).astype(np.uint8)
labels = np.concatenate((labels, labels2), 0)
else:
# Load image
img, (h0, w0), (h, w) = load_image(self, index)
# Letterbox
shape = self.batch_shapes[self.batch[index]] if self.rect else self.img_size # final letterboxed shape
img, ratio, pad = letterbox(img, shape, auto=False, scaleup=self.augment)
shapes = (h0, w0), ((h / h0, w / w0), pad) # for COCO mAP rescaling
labels = self.labels[index].copy()
if labels.size: # normalized xywh to pixel xyxy format
labels[:, 1:] = xywhn2xyxy(labels[:, 1:], ratio[0] * w, ratio[1] * h, padw=pad[0], padh=pad[1])
if self.augment:
# Augment imagespace
if not mosaic:
img, labels = random_perspective(img, labels,
degrees=hyp['degrees'],
translate=hyp['translate'],
scale=hyp['scale'],
shear=hyp['shear'],
perspective=hyp['perspective'])
# Augment colorspace
augment_hsv(img, hgain=hyp['hsv_h'], sgain=hyp['hsv_s'], vgain=hyp['hsv_v'])
# Apply cutouts
# if random.random() < 0.9:
# labels = cutout(img, labels)

@hjsg1010
Copy link
Author

@hjsg1010 you can inline any Albumentations augmentations you want in the dataloader here:

yolov5/utils/datasets.py

Lines 499 to 545 in 095197b

def __getitem__(self, index):
index = self.indices[index] # linear, shuffled, or image_weights
hyp = self.hyp
mosaic = self.mosaic and random.random() < hyp['mosaic']
if mosaic:
# Load mosaic
img, labels = load_mosaic(self, index)
shapes = None
# MixUp https://arxiv.org/pdf/1710.09412.pdf
if random.random() < hyp['mixup']:
img2, labels2 = load_mosaic(self, random.randint(0, self.n - 1))
r = np.random.beta(32.0, 32.0) # mixup ratio, alpha=beta=32.0
img = (img * r + img2 * (1 - r)).astype(np.uint8)
labels = np.concatenate((labels, labels2), 0)
else:
# Load image
img, (h0, w0), (h, w) = load_image(self, index)
# Letterbox
shape = self.batch_shapes[self.batch[index]] if self.rect else self.img_size # final letterboxed shape
img, ratio, pad = letterbox(img, shape, auto=False, scaleup=self.augment)
shapes = (h0, w0), ((h / h0, w / w0), pad) # for COCO mAP rescaling
labels = self.labels[index].copy()
if labels.size: # normalized xywh to pixel xyxy format
labels[:, 1:] = xywhn2xyxy(labels[:, 1:], ratio[0] * w, ratio[1] * h, padw=pad[0], padh=pad[1])
if self.augment:
# Augment imagespace
if not mosaic:
img, labels = random_perspective(img, labels,
degrees=hyp['degrees'],
translate=hyp['translate'],
scale=hyp['scale'],
shear=hyp['shear'],
perspective=hyp['perspective'])
# Augment colorspace
augment_hsv(img, hgain=hyp['hsv_h'], sgain=hyp['hsv_s'], vgain=hyp['hsv_v'])
# Apply cutouts
# if random.random() < 0.9:
# labels = cutout(img, labels)

Thx for reply. Thankyou.

@glenn-jocher glenn-jocher linked a pull request Jul 4, 2021 that will close this issue
@glenn-jocher
Copy link
Member

@hjsg1010 see PR #3882 for a proposed automatic Albumentations integration.

@glenn-jocher
Copy link
Member

@hjsg1010 good news 😃! Your original issue may now be fixed ✅ in PR #3882. This PR implements a YOLOv5 🚀 + Albumentations integration. The integration will automatically apply Albumentations transforms during YOLOv5 training if albumentations>=1.0.0 is installed in your environment.

Get Started

To use albumentations simply pip install -U albumentations and then update the augmentation pipeline as you see fit in the Albumentations class in yolov5/utils/augmentations.py. Note these Albumentations operations run in addition to the YOLOv5 hyperparameter augmentations, i.e. defined in hyp.scratch.yaml.

class Albumentations:
    # YOLOv5 Albumentations class (optional, used if package is installed)
    def __init__(self):
        self.transform = None
        try:
            import albumentations as A
            check_version(A.__version__, '1.0.0')  # version requirement

            self.transform = A.Compose([
                A.Blur(p=0.1),
                A.MedianBlur(p=0.1),
                A.ToGray(p=0.01)],
                bbox_params=A.BboxParams(format='yolo', label_fields=['class_labels']))

            logging.info(colorstr('albumentations: ') + ', '.join(f'{x}' for x in self.transform.transforms))
        except ImportError:  # package not installed, skip
            pass
        except Exception as e:
            logging.info(colorstr('albumentations: ') + f'{e}')

    def __call__(self, im, labels, p=1.0):
        if self.transform and random.random() < p:
            new = self.transform(image=im, bboxes=labels[:, 1:], class_labels=labels[:, 0])  # transformed
            im, labels = new['image'], np.array([[c, *b] for c, b in zip(new['class_labels'], new['bboxes'])])
        return im, labels

Example Result

Example train_batch0.jpg on COCO128 dataset with Blur, MedianBlur and ToGray. See the YOLOv5 Notebooks to reproduce: Open In Colab Open In Kaggle

train_batch0

Update

To receive this YOLOv5 update:

  • Gitgit pull from within your yolov5/ directory or git clone https://github.com/ultralytics/yolov5 again
  • PyTorch Hub – Force-reload with model = torch.hub.load('ultralytics/yolov5', 'yolov5s', force_reload=True)
  • Notebooks – View updated notebooks Open In Colab Open In Kaggle
  • Dockersudo docker pull ultralytics/yolov5:latest to update your image Docker Pulls

Thank you for spotting this issue and informing us of the problem. Please let us know if this update resolves the issue for you, and feel free to inform us of any other issues you discover or feature requests that come to mind. Happy trainings with YOLOv5 🚀!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants