DefTruth / torchlm

💎A high level pipeline for face landmarks detection, it supports training, evaluating, exporting, inference(Python/C++) and 100+ data augmentations, can easily install via pip.
https://github.com/DefTruth/torchlm
MIT License
244 stars 25 forks source link

HorizontalFlip with Albumentations #54

Closed John1231983 closed 2 years ago

John1231983 commented 2 years ago

Great project and thanks for sharing it! For Horizontal flip, the pipnet has aug point flip such as

def random_flip(image, target, points_flip):
    if random.random() > 0.5:
        image = image.transpose(Image.FLIP_LEFT_RIGHT)
        target = np.array(target).reshape(-1, 2)
        target = target[points_flip, :]
        target[:,0] = 1-target[:,0]
        target = target.flatten()
        return image, target
    else:
        return image, target

How do you implement the above function with Albumentations? I found HorizontalFlip but it has no points_flip aug

DefTruth commented 2 years ago

HorizontalFlip with points_flip parameter is not fully support now, in my todo list.

for now, you might need to let points_flip as constant list or indices array within your custom function and use torchlm.bind to bind it togather into the transform pipeline

# First, defined your custom functions
def callable_array_noop(img: np.ndarray, landmarks: np.ndarray) -> Tuple[np.ndarray, np.ndarray]: 
    # do some transform here 
    points_flips = []  # or array ...
    return img.astype(np.uint32), landmarks.astype(np.float32)
# Then, bind your functions and put it into the transforms pipeline.
transform = torchlm.LandmarksCompose([
        torchlm.bind(callable_array_noop, bind_type=torchlm.BindEnum.Callable_Array),
])

See data-augmentation for more details.

John1231983 commented 2 years ago

Is possible to use Albumentations?

DefTruth commented 2 years ago

sure, torchlm have add a soft-support for Albumentations, data-augmentation will show you the details of how to bind transforms from Albumentations.

NOTE: Please install albumentations first if you want to bind albumentations's transforms. If you have the conflict problem between different installed version of opencv (opencv-python and opencv-python-headless, ablumentations need opencv-python-headless). Please uninstall the opencv-python and opencv-python-headless first, and then reinstall albumentations. See albumentations#1140 for more details.

# first uninstall conflict opencvs
pip uninstall opencv-python
pip uninstall opencv-python-headless
pip uninstall albumentations  # if you have installed albumentations
pip install albumentations # then reinstall albumentations, will also install deps, e.g opencv

Then, check if albumentations is available.

torchlm.albumentations_is_available()  # True or False
transform = torchlm.LandmarksCompose([
    torchlm.bind(torchvision.transforms.GaussianBlur(kernel_size=(5, 25)), prob=0.5),  
    torchlm.bind(albumentations.ColorJitter(p=0.5))
])