lightly-ai / lightly

A python library for self-supervised learning on images.
https://docs.lightly.ai/self-supervised-learning/
MIT License
2.83k stars 246 forks source link

Ability to use pre-made augmentations #1525

Open vedranf opened 2 months ago

vedranf commented 2 months ago

Hi, is there ability to use already made augmentations? I have a use-case where the process of creating augmentations is rather complex and time consuming so I pre-created them. It would be ideal if there is a workflow where augmented files could just be read from disk and worked with as is (or with some simple additional transformations applied). Is something like this already possible, perhaps with some clever directory structure?

Thanks, Vedran

guarin commented 2 months ago

Hi! We don't have built-in support for this use-case. But you can create your own dataset to load the data in the correct format. It should return the following: tuple[list[augmentations1, augmentations2]]. Where augmentations1 and augmentations2 are tensors with a batch of images each.

Maybe store your images in a directory structure like:

root_dir/
    augmentations1/
    augmentations2/
   ...

Then you should be able to load them quite easily in your custom dataset. Let me know if something is unclear :)

vedranf commented 2 months ago

Thanks for your response! Initially I thought of somehow subclassing MultiViewCollate, but I saw it's deprecated. While I can store augmentations that way, if still using LightlyDataset, it would interpret augmentations as weak labels, no? What do you think of following (hackish) approach:

view_transform = torchvision.transforms.Compose(
    [  LoadAugmentedImage(), ... ]
)
transform = MultiViewTransform(transforms=[view_transform, view_transform])
dataset_train = LightlyDataset(input_dir=path_to_data, transform=transform)

basically instead of transforming an image, first transformer in compose would load from disk and return an already augmented image, I just need to figure out how to get the path of original image in the transform.

guarin commented 2 months ago

Not sure if this would work. LightlyDataset loads one image at a time from the input_dir. Once the image is loaded it is passed to the transform. As the transform only takes an image as input I am not sure how you would load the different augmentations based on the image.

How are your augmentations stored? Do you have one subdirectory per augmentation?