albumentations
albumentations copied to clipboard
New YOLOv5 π + Albumentations Official Integration!!
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:
Update
To receive this YOLOv5 update:
-
Git β
git pull
from within youryolov5/
directory orgit 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
-
Docker β
sudo docker pull ultralytics/yolov5:latest
to update your image
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!
Great work @glenn-jocher !
Great work, thank you. But I'm encountering errors when I use "Normalize" function from Albumentations. Can you explain me the issue pls ?
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
@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
@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](https://user-images.githubusercontent.com/26833433/158845755-5ebf30ca-bb97-4e97-96dd-85f720041c00.png)
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 fixed that problem using
!pip uninstall opencv-python-headless
!pip install "opencv-python-headless<4.3"
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 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.
Thanks for the clarification, @glenn-jocher.
@glenn-jocher how can I apply albumentations to both training and validation dataset?
@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?
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?
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.
how to add other albumentations in yolov8 execution like motion_blur,Imagecompression.