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

Support for imgaug #1954

Closed
5starkarma opened this issue Jan 16, 2021 · 8 comments · Fixed by #3882
Closed

Support for imgaug #1954

5starkarma opened this issue Jan 16, 2021 · 8 comments · Fixed by #3882
Labels
enhancement New feature or request Stale

Comments

@5starkarma
Copy link

5starkarma commented Jan 16, 2021

🚀 Feature

imgaug is very popular and is easy to work with. I am wondering if I put in PR's which allowed use of imgaug, such as BoundingBox instances, to be used in parts of the program if it would be approved.

e.g. The plot_one_box function could accept then convert BoundingBox instances to the proper format (tensor) for easy plotting of bboxes.

Example:

def plot_one_box(x, img, color=None, label=None, line_thickness=None):
    # If bbox is instance of BoundingBox
    if isinstance(x, BoundingBox):
        # Convert to tensor
        x = torch.tensor([x.x1, x.y1, x.x2, x.y2])

Motivation

I use imgaug, albumentations uses imgaug. It is very easy to use.

Pitch

Add functionality which allows use of imgaug.

@5starkarma 5starkarma added the enhancement New feature or request label Jan 16, 2021
@glenn-jocher
Copy link
Member

@5starkarma yes we welcome PRs! I'm not familiar with imgaug, though we have had requests for Albumentations ops, which might go in datasets.py.

Note that you should profile any modifications to verify that performance speed is unaffected by the proposed changes as part of the PR as well.

@github-actions
Copy link
Contributor

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@lleye
Copy link

lleye commented Jun 5, 2021

@5starkarma can you give an example of how you integrate imgaug in yolov5, trying to augment as well

@glenn-jocher
Copy link
Member

the best place to insert Albumentation augmentations into the YOLOv5 dataloader is here:

yolov5/utils/datasets.py

Lines 536 to 552 in 6306091

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)

where img is the image and labels are the bounding box labels. Note that any Albumentation augmentations you add will be in addition to the existing automatic YOLOv5 augmentations defined in your hyperparameter file:

fl_gamma: 0.0 # focal loss gamma (efficientDet default gamma=1.5)
hsv_h: 0.015 # image HSV-Hue augmentation (fraction)
hsv_s: 0.7 # image HSV-Saturation augmentation (fraction)
hsv_v: 0.4 # image HSV-Value augmentation (fraction)
degrees: 0.0 # image rotation (+/- deg)
translate: 0.1 # image translation (+/- fraction)
scale: 0.5 # image scale (+/- gain)
shear: 0.0 # image shear (+/- deg)
perspective: 0.0 # image perspective (+/- fraction), range 0-0.001
flipud: 0.0 # image flip up-down (probability)
fliplr: 0.5 # image flip left-right (probability)
mosaic: 1.0 # image mosaic (probability)
mixup: 0.0 # image mixup (probability)

@lleye
Copy link

lleye commented Jul 2, 2021

@glenn-jocher quick question on your comment here "Note that any Albumentation augmentations you add will be in addition to the existing automatic YOLOv5 augmentations defined in your hyperparameter file"

Do you mean if i set mixup to 0.1, it will augment the data with an additional 10% of mixed up images making it 110%? Or is it going to be 90% normal and 10% mixed augmented?

Also i cant seem to integrate imgaug, any pointers in case you saw someone integrate it before? Thanks

@glenn-jocher
Copy link
Member

@lleye 0.10 mixup means 10% of your images will use mixup.

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

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

@glenn-jocher
Copy link
Member

@5starkarma @lleye 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
enhancement New feature or request Stale
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants