lightly-ai / lightly-ai/lightly
Use torchvision transforms instead of PIL operations for solarization and gaussian blurring
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 3.8k
- Forks
- 367
- Avg merge
- 3d 22h
- Merged PRs (30d)
- 5
Description
**Problem**
When using a transform that uses `lightly.transforms.gaussian_blur.GaussianBlur` or `lightly.transforms.solarization.RandomSolarization`, e.g. `DINOTransform`, the input must be transformed to a `PIL.Image` first, because the Gaussian blur and the solarization use `PIL.ImageFilter.GaussianBlur` and `PIL.ImageOps.solarize` respectively. For training, the `PIL.Image` has to be transformed back to a tensor.
**Background**
It might be beneficial to get rid of this conversion for performance purposes. Sometimes, using a different reader like pyvips/openslide/cucim is necessary to extract only a patch of an image because images are too big to fit in memory (e.g. in computational pathology or remote sensing). Here, often patches are extracted and immediately transformed to torch tensors for torchvision to transform the patches. Doing all the transforms on tensors overcomes converting to other formats, like PIL, increasing performance.
**Alternative**
Torchvision provides seemingly fast (jitted) implementations for solarization [1] and gaussian blurring [2].
I'm not aware of any other transforms in the lightly package that rely on PIL.
**References**
[1] [implementation](https://github.com/pytorch/vision/blob/4249b610811b290ea9ac9e445260be195ce52ae1/torchvision/transforms/v2/functional/_color.py#L483-L486) [docs](https://pytorch.org/vision/main/generated/torchvision.transforms.v2.RandomSolarize.html#torchvision.transforms.v2.RandomSolarize)
[2] [implementation](https://github.com/pytorch/vision/blob/4249b610811b290ea9ac9e445260be195ce52ae1/torchvision/transforms/v2/functional/_misc.py#L75-L78) [docs](https://pytorch.org/vision/main/generated/torchvision.transforms.v2.GaussianBlur.html?highlight=gaussianblur#torchvision.transforms.v2.GaussianBlur)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with lightly.transforms.gaussian_blur.GaussianBlur and lightly.transforms.solarization.RandomSolarization, then trace how DINOTransform uses them. Compare their PIL-based operations with the referenced torchvision implementations. Done means solarization and Gaussian blurring work on tensors without the described PIL conversions while preserving the existing transform behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python, pytorch
- Domain
- computer-vision, machine-learning
- Issue type
- Refactor
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100