$ show lucidrains/byol-pytorch
byol-pytorch is a Python library that implements Bootstrap Your Own Latent (BYOL), a self-supervised learning method for image representations. The core contribution is the `BYOL` module, which wraps any PyTorch image backbone (e.g., ResNet) and trains it without labels by maximizing agreement between two augmented views of the same image. The library derives its behavior from code rather than promises: it computes a cosine loss between online predictions and target projections, where the target encoder is an exponential moving average of the online encoder (or the online encoder itself for the SimSiam variant). It also supports optional Simplicial Embeddings (SEM) and custom augmentation pipelines.
The LLM identified 3 files worth highlighting for reuse:
NetWrapper is a clean, reusable abstraction that wraps an arbitrary nn.Module, locates a hidden layer by string name or integer index, registers a forward hook to flatten that layer's output, and lazily constructs a projector MLP sized to the hidden representation. It is isolated enough to be extracted for any 'intermediate feature extraction' use case in PyTorch, and it elegantly handles device placement, hook registration, and singleton projector creation.
SEM (Simplicial Embeddings Module) is a self-contained, parameter-light module that maps a latent vector into a concatenation of softmax simplices controlled by a temperature. It implements the simplicial embedding idea from Lavoie et al. and is directly reusable in any self-supervised or metric-learning pipeline that needs bounded, probability-like representations.
BYOLTrainer provides an end-to-end distributed training loop using HuggingFace Accelerate, including automatic SyncBatchNorm conversion, mixed-precision via accelerator autocast, a cyclic dataloader, periodic checkpointing of the improved backbone, and correct EMA updating synchronized across workers. It is a solid reference pattern for building distributed self-supervised training pipelines, although it is coupled to the BYOL module.