Code Monkey home page Code Monkey logo

contrastive_learning_in_100_lines's Introduction

Contrastive Learning in 100 Lines, in PyTorch

A simple and intuitive contrastive learning implementation It is a minimalistic toy project to understand how algorithm work containing following elements:

  1. Contrastive learning frameworks, e.g., SimCLR, BYOL and SimSiam.
  2. A toy dataset: ImageNet_5Cate to play with.
  3. Constrastive training code and supervised benchmark code.

To keep the code in minimal scale, there're no multi-gpu and FP16 support. However, I hope these acceleration can be added easily if needed.

More frameworks coming later, the progress:

  • BYOL, Bootstrap Your Own Latent
  • SimCLR
  • SimSiam (In progree)
  • Barlow Twins

Install

Only PyTorch and PIL is required, install them. Then:

git clone https://github.com/JamesQFreeman/contrastive_learning_in_100_lines.git
cd contrastive_learning_in_100_lines
python train.py

Usage

Take BYOL and ResNet50 as an example, the self-supervised training code should be like following. Complete code can be found at train.py

resnet = models.resnet50() # Chose a model you like
learner = BYOL(net=resnet) # Setup a learner for different framework
my_dataset = ImageNet_5Class(augmentation=False, annotation=False) # Self-supervision so no annotation needed

for _ in range(n_epoch):
    for data in trainloader:
        inputs = data
        # zero the parameter gradients
        optimizer.zero_grad()
        # forward + backward + optimize
        loss = learner(inputs)
        loss.backward()
        optimizer.step()
        # BYOL only, do the EMA
        learner.update_moving_average()

# save the trained encoder
torch.save(learner.online_encoder.state_dict(), "encoder.pth")

Thanks to

  1. https://github.com/lucidrains/byol-pytorch

contrastive_learning_in_100_lines's People

Contributors

jamesqfreeman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.