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

Remove albumentations #1136

Open
Ezward opened this issue Jul 2, 2023 · 4 comments
Open

Remove albumentations #1136

Ezward opened this issue Jul 2, 2023 · 4 comments
Assignees

Comments

@Ezward
Copy link
Contributor

Ezward commented Jul 2, 2023

Albumentations still has the problem on jetson nano that it causes a crash because underneath it still uses a dependency that installs opencv-headless.

The reality is that we only use two augmentations from it; a brightness augmentation and a guassian blur. These are both easily implemented with opencv.

Replace the brightness augmentation and the guassian blur augmentation in

class ImageAugmentation:
. with ones based on opencv so we can remove albumentations.

@Ezward Ezward self-assigned this Jul 2, 2023
@DocGarbanzo
Copy link
Contributor

Yes, in the current setup that would work. But there are other augmentations, like CoarseDropout for example, which might be quite useful (one can play with them here: https://demo.albumentations.ai/). However, they might be easy to generate with cv too.

@mdlopezme
Copy link

mdlopezme commented Feb 23, 2024

Hello I am trying to train in a Nvidia Jetson AGX Xavier,
I am having problems with albumentations.
When I pip install albumentations, it seems to uninstall my current version of opencv-python. ( The problem is that I have to use a custom version of this library to be able to do gpu accelation in my platform).
I get this error after installing albumentations: pip install albumentations

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.8/dist-packages/cv2/__init__.py", line 181, in <module>
    bootstrap()
  File "/usr/local/lib/python3.8/dist-packages/cv2/__init__.py", line 175, in bootstrap
    if __load_extra_py_code_for_module("cv2", submodule, DEBUG):
  File "/usr/local/lib/python3.8/dist-packages/cv2/__init__.py", line 28, in __load_extra_py_code_for_module
    py_module = importlib.import_module(module_name)
  File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/usr/local/lib/python3.8/dist-packages/cv2/mat_wrapper/__init__.py", line 39, in <module>
    cv._registerMatType(Mat)
AttributeError: partially initialized module 'cv2' has no attribute '_registerMatType' (most likely due to a circular import)

Is there a reason why albumentations is needed? I need guidance to know if I can to try to use opencv version.
Would you like me to send a pull request if it does work with opencv implementation?

import cv2
import logging
from cv2 import GaussianBlur
from cv2 import addWeighted
import numpy as np

from donkeycar.config import Config

logger = logging.getLogger(__name__)

class ImageAugmentation:
    def __init__(self, cfg, key, prob=0.5, always_apply=False):
        aug_list = getattr(cfg, key, [])
        self.augmentations = [ImageAugmentation.create(a, cfg, prob, always_apply) for a in aug_list]

    @classmethod
    def create(cls, aug_type: str, config: Config, prob, always) -> callable:
        """ Augmentation factory."""

        if aug_type == 'BRIGHTNESS':
                b_limit = getattr(config, 'AUG_BRIGHTNESS_RANGE', 0.2)
                logger.info(f'Creating augmentation {aug_type} {b_limit}')
                bightness = np.random.uniform(-b_limit, b_limit)
                return lambda img_arr: addWeighted(img_arr, 1.0 + bightness, img_arr, 0, 0)
        
        elif aug_type == 'BLUR':
            b_range = getattr(config, 'AUG_BLUR_RANGE', (0,3))
            logger.info(f'Creating augmentation {aug_type} {b_range}')
            bur_intensity = np.random.uniform(b_range[0], b_range[1])
            return lambda img_arr: GaussianBlur(img_arr, (13,13), bur_intensity)
        
    # Parts interface
    def run(self, img_arr):
        for augmentation in self.augmentations:
            img_arr = augmentation(img_arr)
        return img_arr

@mdlopezme
Copy link

Little update, I was able to train. After committing the above update on my Jetson.
Will test on real data soon and send a little clip within the next few days.

@mdlopezme
Copy link

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

No branches or pull requests

3 participants