albumentations icon indicating copy to clipboard operation
albumentations copied to clipboard

New YOLOv5 πŸš€ + Albumentations Official Integration!!

Open glenn-jocher opened this issue 3 years ago β€’ 15 comments

I'm super excited to announce our new YOLOv5 πŸš€ + Albumentations integration!! Now you can train the world's best Vision AI models even better with custom Albumentations automatically applied πŸ˜ƒ!

PR https://github.com/ultralytics/yolov5/pull/3882 implements this integration, which will automatically apply Albumentations transforms during YOLOv5 training if albumentations>=1.0.3 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 new 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.

Here's an example that applies Blur, MedianBlur and ToGray albumentations in addition to the YOLOv5 hyperparameter augmentations normally applied to your training mosaics :)

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.3')  # version requirement

            self.transform = A.Compose([
                A.Blur(blur_limit=50, p=0.1),
                A.MedianBlur(blur_limit=51, p=0.1),
                A.ToGray(p=0.3)],
                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:

  • Git – git 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
  • Docker – sudo docker pull ultralytics/yolov5:latest to update your image Docker Pulls

Thank you for your feedback and let us know if this update works for you, and of course feel free to inform us of any other issues you discover or feature requests that come to mind. Happy trainings with YOLOv5 πŸš€ + Albumentations!

glenn-jocher avatar Jul 05 '21 16:07 glenn-jocher

Great work @glenn-jocher !

BloodAxe avatar Jul 05 '21 17:07 BloodAxe

Great work, thank you. But I'm encountering errors when I use "Normalize" function from Albumentations. Can you explain me the issue pls ?

tcotte avatar Oct 15 '21 12:10 tcotte

on google colab :

after installing "pip install -U albumentations" and trying to run the train.py an error is thrown up related to cv2

Traceback (most recent call last): File "train.py", line 41, in import val # for end-of-epoch mAP File "/content/yolov5/val.py", line 38, in from models.common import DetectMultiBackend File "/content/yolov5/models/common.py", line 14, in import cv2 File "/usr/local/lib/python3.7/dist-packages/cv2/init.py", line 9, in from .cv2 import _registerMatType ImportError: cannot import name '_registerMatType' from 'cv2.cv2' (/usr/local/lib/python3.7/dist-packages/cv2/cv2.cpython-37m-x86_64-linux-gnu.so)

testproducts20 avatar Mar 13 '22 09:03 testproducts20

@testproducts20 this seems unrelated to YOLOv5. If I run this Colab I see the same error:

!pip install -U albumentations
import cv2

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
[<ipython-input-1-b305364478bf>](https://localhost:8080/#) in <module>()
      1 get_ipython().system('pip install -U albumentations')
      2 
----> 3 import cv2

3 frames
[/usr/local/lib/python3.7/dist-packages/cv2/__init__.py](https://localhost:8080/#) in <module>()
      7 
      8 from .cv2 import *
----> 9 from .cv2 import _registerMatType
     10 from . import mat_wrapper
     11 from . import gapi

ImportError: cannot import name '_registerMatType' from 'cv2.cv2' (/usr/local/lib/python3.7/dist-packages/cv2/cv2.cpython-37m-x86_64-linux-gnu.so)

@BloodAxe could you take a look at Colab compatibility for Albumentations? The opencv-headless install seems to be causing conflicts. To reproduce just run this in colab:

!pip install -U albumentations
import cv2

glenn-jocher avatar Mar 14 '22 13:03 glenn-jocher

@BloodAxe following up on this. Albumentation seems to have a serious Colab problem that users are encountering i.e. https://github.com/ultralytics/yolov5/issues/7014, https://github.com/albumentations-team/albumentations/issues/1140. You can reproduce in Colab with:

!pip install -U albumentations
import cv2
Screenshot 2022-03-17 at 17 18 29

glenn-jocher avatar Mar 17 '22 16:03 glenn-jocher

I checked what happened.

Default colab has:

opencv-contrib-python         4.1.2.30
opencv-python                 4.1.2.30

The albumentation requirements are given from the result of pkginfo -f requires_dist albumentations-1.1.0-py3-none-any.whl:

opencv-python-headless>=4.1.1

After the pip -u albumentations

opencv-contrib-python         4.1.2.30
opencv-python                 4.1.2.30
opencv-python-headless        4.5.5.64

So, multiple versions may cause conflicts.

I think there is no simple solution to this problem. The opencv team provides multiple package options and says, "SELECT ONLY ONE OF THEM."

  • opencv-python
  • opencv-contrib-python
  • opencv-python-headless
  • opencv-contrib-python-headless (See also, https://pypi.org/project/opencv-python/ )

The package owner can not know which opencv package the user's environment has installed or none of them. Unfortunately, the situation would be worse because there are environments where multiple openvcv packages have been installed, like Colab.

Installing all of them with the same version seems to work, but it introduces unnecessary and redundant dependencies. I think such behavior would not be wanted for users who use packages in production.

i-aki-y avatar Mar 18 '22 12:03 i-aki-y

I fixed that problem using !pip uninstall opencv-python-headless !pip install "opencv-python-headless<4.3"

image

ismaArce avatar Apr 04 '22 17:04 ismaArce

This is more of a clarification than an issue but... Assuming you have albumentations installed, then Yolo will apply both the hyperparameters in the hyp.scratch-low.yaml (default) AND apply the albumentation pipeline defined in utils/augmentations.py. Since there are overlaps in the two capabilities, you could possibly end up canceling out an effect or, at the very least, end up with unexpected results. For example, if I have fliplr set in the hyperparameters but then use A.HorzontalFlip in the albumentation side, there is a chance that I could flip the same image twice (effectively canceling out the flip). Is this correct or am I over thinking this? It is definitely something to consider when using albumentations.

brownrc avatar May 23 '22 20:05 brownrc

@brownrc that's correct, albumentations are additive to existing augmentations. Though I think your conclusion is inconsistent with statistical additive noise, which always results in more noise and never cancels itself out statistically speaking.

glenn-jocher avatar May 23 '22 20:05 glenn-jocher

Thanks for the clarification, @glenn-jocher.

brownrc avatar May 23 '22 21:05 brownrc

@glenn-jocher how can I apply albumentations to both training and validation dataset?

myasser63 avatar Aug 16 '22 09:08 myasser63

@glenn-jocher Hi, I have been unable to use albumentaion in a GCP compute engine environment. It keep s saying package not found, but the requirement is already satisfied. Also, why is it commented in the latest requirements.txt? image image

guptasaumya avatar Nov 17 '22 22:11 guptasaumya

Is there a way to add variables to the class Albumentations? I want to use A.CenterCrop(width=h,height=h, p=1) , where h is the height of the image (so I would always get a square image), but I donΒ΄t find how to pass this parameter to the function as the class never opens the image. I could hardcode it to a fix number, but I'm not sure if all my images are the same size. Do you know if there is a solution?

BelenEspino avatar Mar 07 '23 12:03 BelenEspino

I would like to add more randomness to the augmentation process.

In this regard, Is there a way to apply different albumentation pipeline to each training image for instance? Currently all the parameters are fixed before training begins.

nopps07 avatar Apr 17 '23 23:04 nopps07

how to add other albumentations in yolov8 execution like motion_blur,Imagecompression.

BhaveshWadibhasme avatar Aug 11 '23 13:08 BhaveshWadibhasme