AI/TLDR

LightlySSL

A PyTorch-style library of self-supervised learning building blocks — 20+ methods from MoCo and SimCLR to DINOv2, CAPI and LeJEPA

Efficient TrainingOpen source
Language
Python
License
MIT
$pip3 install lightly

Overview

LightlySSL is a computer vision framework for self-supervised learning — pre-training an image model on unlabelled data before any labelled fine-tuning. Rather than a one-command trainer, it is a modular library that exposes the pieces each method is built from: loss functions (`lightly.loss`), projection and prediction heads (`lightly.models.modules.heads`), multi-view transforms (`lightly.transforms`) and a dataset wrapper over an image folder. You assemble them in ordinary PyTorch code around whatever backbone you like, which is what makes swapping SimCLR for SimSiam a change of two objects rather than a rewrite.

Over twenty methods are implemented with reference examples, spanning the whole arc of the field: MoCo, SimCLR, BYOL, SwAV, DenseCL, SimSiam, Barlow Twins, DetConS, DINO, NNCLR, VICReg, DCL/DCLW, MAE, iBOT, SimMIM, MSN, PMSN, DINOv2, FroSSL, AIM, CAPI, LeJEPA and Pixio. Each ships in three forms — plain PyTorch, PyTorch Lightning, and distributed PyTorch Lightning — plus Colab notebooks, so the same method can be tried on one GPU and then scaled out without re-deriving the training loop. The repository also publishes benchmark tables on ImageNet1k, ImageNet100, Imagenette and CIFAR-10.

Requirements are modest and explicit: Python 3.8+ (not yet 3.13, because PyTorch lacks support), PyTorch ≥ 1.11, Torchvision ≥ 0.12 and PyTorch Lightning ≥ 1.7.1, with PyTorch and Lightning 2.0+ supported. The library is open source; Lightly also sells a commercial version with Docker support and single-command pretraining for embedding, classification, detection and segmentation, and runs separate open-source projects around it — LightlyTrain for SSL and distillation pretraining, and LightlyStudio for data curation, visualization and annotation.

What it does

  • Modular by design — losses, model heads and multi-view transforms are exposed as low-level building blocks you compose in PyTorch
  • 20+ implemented methods including MoCo, SimCLR, BYOL, SwAV, SimSiam, Barlow Twins, DINO, VICReg, MAE, iBOT, SimMIM, DINOv2, AIM, CAPI, LeJEPA and Pixio
  • Every method provided in plain PyTorch, PyTorch Lightning and distributed PyTorch Lightning variants, each with a Colab notebook
  • Works with any custom backbone — the examples use a torchvision ResNet with its classification head replaced by Identity
  • Distributed training through PyTorch Lightning
  • Published benchmarks on ImageNet1k, ImageNet100, Imagenette and CIFAR-10

Getting started

Python 3.8+ on Linux or macOS is the recommended environment (Python 3.13 is not supported yet). Install into a dedicated virtualenv.

Install from PyPI

Pulls in the dependencies needed for the full feature set: PyTorch ≥ 1.11, Torchvision ≥ 0.12 and PyTorch Lightning ≥ 1.7.1.

bashbash
pip3 install lightly

Build a model from the blocks

A SimCLR model is a backbone plus a projection head. Dropping the backbone's classification head leaves the features.

pythonpython
import torch, torchvision
from lightly import loss, transforms
from lightly.data import LightlyDataset
from lightly.models.modules import heads

class SimCLR(torch.nn.Module):
    def __init__(self, backbone):
        super().__init__()
        self.backbone = backbone
        self.projection_head = heads.SimCLRProjectionHead(
            input_dim=512, hidden_dim=512, output_dim=128,
        )

    def forward(self, x):
        features = self.backbone(x).flatten(start_dim=1)
        return self.projection_head(features)

backbone = torchvision.models.resnet18()
backbone.fc = torch.nn.Identity()
model = SimCLR(backbone)

Point a dataset at your image folder

The transform produces several random views per image — that pairing is what the contrastive loss learns from.

pythonpython
transform = transforms.SimCLRTransform(input_size=32, cj_prob=0.5)
dataset = LightlyDataset(input_dir="./my/dataset/", transform=transform)
dataloader = torch.utils.data.DataLoader(
    dataset, batch_size=128, shuffle=True,
)

Train in a plain PyTorch loop

The loss is just another module, so the loop is the one you already write. Swapping in SimSiam means changing the model and the loss, nothing else.

pythonpython
criterion = loss.NTXentLoss(temperature=0.5)
optimizer = torch.optim.SGD(model.parameters(), lr=0.1, weight_decay=1e-6)

for epoch in range(10):
    for (view0, view1), targets, filenames in dataloader:
        z0, z1 = model(view0), model(view1)
        l = criterion(z0, z1)
        l.backward()
        optimizer.step()
        optimizer.zero_grad()

Commands and code are distilled from the project's own documentation — always check the official repo for the latest.

When to use it

  • Pre-train a vision backbone on a large pile of unlabelled images before spending a labelling budget
  • Reproduce or compare SSL methods on the same data with matched training code rather than each paper's own repo
  • Take one piece — an NT-Xent loss or a DINO head — into an existing PyTorch training script
  • Scale a method from a single GPU to distributed training by switching to the PyTorch Lightning variant

How LightlySSL compares

LightlySSL alongside other open-source efficient training tools AI/TLDR tracks, ranked by GitHub stars.

ToolStarsWhat it does
DeepSpeed★ 43.2kA deep learning optimization library whose ZeRO memory partitioning and offloading let you train very large models across many GPUs.
Megatron-LM★ 18kNVIDIA's library for training large transformer models at scale using tensor, pipeline, and sequence parallelism.
Accelerate★ 9.9kA library that runs the same PyTorch training code across CPUs, multiple GPUs, and TPUs while handling mixed precision, FSDP, and DeepSpeed.
DeepSpec★ 7.1kDeepSeek's codebase for training and evaluating draft models for speculative decoding, bundling data preparation, the DSpark, DFlash and Eagle3 drafters, training code and benchmarks.
TorchTitan★ 5.8kA PyTorch-native platform for pre-training large models that combines FSDP, tensor, pipeline, and context parallelism in one codebase.
LightlySSL★ 3.8kA PyTorch-style library of self-supervised learning building blocks — 20+ methods from MoCo and SimCLR to DINOv2, CAPI and LeJEPA
Nanotron★ 2.8kHugging Face's minimal library for pre-training LLMs with 3D parallelism, designed to be readable and easy to modify.
PyTorch/XLA★ 2.8kThe XLA compiler bridge that runs PyTorch models on Cloud TPUs, with SPMD sharding, FSDP and distributed training behind a torch_xla.step() training loop.