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

Albumentations-Pipeline is applied to BGR not to RGB #8641

Closed
2 tasks done
UnglvKitDe opened this issue Jul 20, 2022 · 8 comments · Fixed by #8695
Closed
2 tasks done

Albumentations-Pipeline is applied to BGR not to RGB #8641

UnglvKitDe opened this issue Jul 20, 2022 · 8 comments · Fixed by #8695
Labels
bug Something isn't working TODO

Comments

@UnglvKitDe
Copy link
Contributor

Search before asking

  • I have searched the YOLOv5 issues and found no similar bug report.

YOLOv5 Component

No response

Bug

As written here in step 3, Albumentations internally uses the RGB format and not the BGR format of opencv. However, the data is currently passed internally as BGR:

yolov5/utils/dataloaders.py

Lines 626 to 628 in 92e47b8

# Albumentations
img, labels = self.albumentations(img, labels)
nl = len(labels) # update after albumentations

img = img.transpose((2, 0, 1))[::-1] # HWC to CHW, BGR to RGB

Or am I missing something?

Environment

YOLOv5 torch 1.11 (cuda 11.3) and 1.12 (cuda 11.6)

Minimal Reproducible Example

No response

Additional

No response

Are you willing to submit a PR?

  • Yes I'd like to help by submitting a PR!
@UnglvKitDe UnglvKitDe added the bug Something isn't working label Jul 20, 2022
@seonho
Copy link

seonho commented Jul 20, 2022

YOLOv5 uses the following augmentation transforms

A.CLAHE(p=0.01),

AFAIK, all other transforms except CLAHE are channel order independent.
But, CLAHE considers channel order if image has 3 dimensions.
https://github.com/albumentations-team/albumentations/blob/a8dc46ee29f9573c8569213f0d243254980b02f1/albumentations/augmentations/functional.py#L673

@UnglvKitDe
Copy link
Contributor Author

@seonho According to the current state YOLOv5 also uses Gray. There it depends on the implementation of cv2.cvtCOLOR() how the RGB image is transformed to a Gray image (I couldn't find the OpenCV implementation). But either way, it is confusing if you want to adjust e.g. the default pipline for your dataset.

@seonho
Copy link

seonho commented Jul 21, 2022

@UnglvKitDe Yes, I missed ToGray. According to OpenCV's Color conversion document, RGB2GRAY also affected by channel order.

So, at least two (ToGray, CLAHE) transforms are channel order dependent.

@glenn-jocher
Copy link
Member

glenn-jocher commented Jul 23, 2022

@UnglvKitDe yes you are correct, there is an inconsistency there is Albumentations is used. The best solution I can think of is to immediately convert to RGB after cv2 imread (and update where required like augment_hsv()). Then we can safely assume RGB-order images throughout all dataloaders.

I think this might be slightly slower than the current implementation which reverses BGR-RGB in the first dimension (CHW) rather than the last (HWC), resulting in a faster BGR-RGB conversion. I'll add a TODO here so I don't forget.

TODO: Fix bug that Albumentations inputs BGR images but expects RGB images.

@UnglvKitDe
Copy link
Contributor Author

@glenn-jocher I have pushed a first idea.

@glenn-jocher
Copy link
Member

@UnglvKitDe thanks! I will take a look at the PR. For reference some BGR to RGB speeds:
Screen Shot 2022-07-23 at 11 20 14 PM

import numpy as np
import cv2
import time

im = np.random.rand(640,640,3).astype(np.uint8)

t = time.time()
for _ in range(10000):
  x = im[...,::-1]
print(time.time() - t)

t = time.time()
for _ in range(10000):
  np.flip(im,2)
print(time.time() - t)

t = time.time()
for _ in range(10000):
  cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
print(time.time() - t)

Screen Shot 2022-07-23 at 11 21 59 PM

glenn-jocher added a commit that referenced this issue Jul 23, 2022
glenn-jocher added a commit that referenced this issue Jul 23, 2022
@UnglvKitDe
Copy link
Contributor Author

UnglvKitDe commented Jul 23, 2022

@glenn-jocher Oh thats a good point. I thought opencv do something like that. But it looks very slow. Thx!

UnglvKitDe added a commit to UnglvKitDe/yolov5-1 that referenced this issue Jul 23, 2022
@glenn-jocher glenn-jocher linked a pull request Jul 26, 2022 that will close this issue
glenn-jocher added a commit that referenced this issue Jul 26, 2022
* Fix BGR->RGB Bug in albumentations #8641

* Change transform methode from cv2 to numpy

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Simplify

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update augmentations.py

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
@arun-gautham arun-gautham mentioned this issue Jul 26, 2022
2 tasks
glenn-jocher added a commit that referenced this issue Jul 26, 2022
glenn-jocher added a commit that referenced this issue Jul 26, 2022
glenn-jocher added a commit that referenced this issue Jul 26, 2022
Revert "Fix BGR->RGB Bug in albumentations #8641 (#8695)"

This reverts commit 2e1291f.
ctjanuhowski pushed a commit to ctjanuhowski/yolov5 that referenced this issue Sep 8, 2022
ctjanuhowski pushed a commit to ctjanuhowski/yolov5 that referenced this issue Sep 8, 2022
* Fix BGR->RGB Bug in albumentations ultralytics#8641

* Change transform methode from cv2 to numpy

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Simplify

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update augmentations.py

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
ctjanuhowski pushed a commit to ctjanuhowski/yolov5 that referenced this issue Sep 8, 2022
@glenn-jocher
Copy link
Member

@UnglvKitDe you're welcome! Thank you for your understanding. Let me know if you have any questions or need further assistance with the PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working TODO
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants