Lightly is a computer vision framework for self-supervised learning.
We, at Lightly, are passionate engineers who want to make deep learning more efficient. We want to help popularize the use of self-supervised methods to understand and filter raw image data. Our solution can be applied before any data annotation step and the learned representations can be used to analyze and visualize datasets as well as for selecting a core set of samples.
Lightly offers features like
- modular framework
- support for multi-gpu training using PyTorch Lightning
- easy to use and written in a PyTorch like style
- supports custom backbone models for self-supervised pre-training
Want to jump to the tutorials and see lightly in action?
- Train MoCo on CIFAR-10
- Train SimCLR on clothing data
- Train SimSiam on satellite images
- Use lightly with custom augmentations
Lightly requires Python 3.6+. We recommend installing Lightly in a Linux or OSX environment.
You can install Lightly and its dependencies from PyPI with:
pip3 install lightly
We strongly recommend that you install Lightly in a dedicated virtualenv, to avoid conflicting with your system packages.
Lightly in Action
With lightly you can use latest self-supervised learning methods in a modular way using the full power of PyTorch. Experiment with different backbones, models and loss functions. The framework has been designed to be easy to use from the ground up.
import torch import torchvision import lightly.models as models import lightly.loss as loss import lightly.data as data # the collate function applies random transforms to the input images collate_fn = data.ImageCollateFunction(input_size=32, cj_prob=0.5) # create a dataset from your image folder dataset = data.LightlyDataset(input_dir='./my/cute/cats/dataset/') # build a PyTorch dataloader dataloader = torch.utils.data.DataLoader( dataset, # pass the dataset to the dataloader batch_size=128, # a large batch size helps with the learning shuffle=True, # shuffling is important! collate_fn=collate_fn) # apply transformations to the input images # use a resnet backbone resnet = torchvision.models.resnet.resnet18() resnet = nn.Sequential(*list(resnet.children())[:-1]) # build the simclr model model = models.SimCLR(resnet, num_ftrs=512) # use a criterion for self-supervised learning criterion = loss.NTXentLoss(temperature=0.5) # get a PyTorch optimizer optimizer = torch.optim.SGD(model.parameters(), lr=1e-0, weight_decay=1e-5)
You can easily use another model like SimSiam by swapping the model and the loss function.
# build the simsiam model model = models.SimSiam(resnet, num_ftrs=512) # use the SimSiam loss function criterion = loss.SymNegCosineSimilarityLoss()
Use PyTorch Lightning to train the model:
trainer = pl.Trainer(max_epochs=max_epochs, gpus=1) trainer.fit( model, dataloader )
Or train the model on 4 GPUs:
trainer = pl.Trainer( max_epochs=max_epochs, gpus=4, distributed_backend='ddp' ) trainer.fit( model, dataloader )
Lightly is accessible also through a command-line interface (CLI). To train a SimCLR model on a folder of images you can simply run the following command:
To create an embedding of a dataset you can use:
lightly-embed input_dir=/mydataset checkpoint=/mycheckpoint
The embeddings with the corresponding filename are stored in a human-readable .csv file.
Currently implemented models and their accuracy on cifar10. All models have been evaluated using kNN. We report the max test accuracy over the epochs as well as the maximum GPU memory consumption. All models in this benchmark use the same augmentations as well as the same ResNet-18 backbone. Training precision is set to FP32 and SGD is used as an optimizer with cosineLR. One epoch on cifar10 takes ~35 seconds on a V100 GPU. Learn more about the cifar10 benchmark here
|Model||Epochs||Batch Size||Test Accuracy|
Below you can see a schematic overview of the different concepts present in the lightly Python package. The terms in bold are explained in more detail in our documentation.
Head to the documentation and see the things you can achieve with Lightly!
To install dev dependencies (for example to contribute to the framework) you can use the following command:
pip3 install -e ".[dev]"
For more information about how to contribute have a look here.
Unit tests are within the
tests folder and we recommend to run them using
There are two test configurations available. By default only a subset will be run.
This is faster and should take less than a minute. You can run it using
python -m pytest -s -v
To run all tests (including the slow ones) you can use the following command.
python -m pytest -s -v --runslow
You can run the linter from your terminal either on a folder
or on a specific file
- A Simple Framework for Contrastive Learning of Visual Representations (2020)
- Momentum Contrast for Unsupervised Visual Representation Learning (2020)
- Unsupervised Learning of Visual Features by Contrasting Cluster Assignments (2020)
- What Should Not Be Contrastive in Contrastive Learning (2020)
Why should I care about self-supervised learning? Aren't pre-trained models from ImageNet much better for transfer learning?
- Self-supervised learning has become increasingly popular among scientists over the last year because the learned representations perform extraordinarily well on downstream tasks. This means that they capture the important information in an image better than other types of pre-trained models. By training a self-supervised model on your dataset, you can make sure that the representations have all the necessary information about your images.
How can I contribute?
- Create an issue if you encounter bugs or have ideas for features we should implement. You can also add your own code by forking this repository and creating a PR. More details about how to contribute with code is in our contribution guide.
Is this framework for free?
- Yes, this framework completely free to use and we provide the code. We believe that we need to make training deep learning models more data efficient to achieve widespread adoption. One step to achieve this goal is by leveraging self-supervised learning. The company behind lightly commited to keep this framework open-source.
If this framework is free, how is the company behind lightly making money?
- Training self-supervised models is only part of the solution. The company behind lightly focuses on processing and analyzing embeddings created by self-supervised models. By building, what we call a self-supervised active learning loop we help companies understand and work with their data more efficiently. This framework acts as an interface for our platform to easily upload and download datasets, embeddings and models. Whereas the platform will cost for additional features this frameworks will always remain free of charge (even for commercial use).