SimCLR/README.md

90 lines
4.2 KiB
Markdown
Raw Normal View History

# PyTorch implementation of SimCLR: A Simple Framework for Contrastive Learning of Visual Representations
2020-03-07 12:01:58 -03:00
### Blog post with full documentation: [Exploring SimCLR: A Simple Framework for Contrastive Learning of Visual Representations](https://sthalles.github.io/simple-self-supervised-learning/)
2020-03-09 10:25:20 -03:00
#### For a Tensorflow 2.0 Implementation: [Tensorflow SimCLR](https://github.com/sthalles/SimCLR-tensorflow)
2020-03-07 11:56:34 -03:00
![Image of SimCLR Arch](https://sthalles.github.io/assets/contrastive-self-supervised/cover.png)
2020-03-09 10:24:56 -03:00
2020-03-13 23:12:08 -03:00
## Installation
2020-03-07 12:01:58 -03:00
2020-03-13 23:11:07 -03:00
```
$ conda create --name simclr python=3.7 --file requirements.txt
$ conda activate simclr
$ python run.py
```
2020-02-25 08:49:51 -03:00
## Config file
2020-03-09 07:41:47 -03:00
Before running SimCLR, make sure you choose the correct running configurations on the ```config.yaml``` file.
2020-02-25 08:49:51 -03:00
2020-02-25 08:51:49 -03:00
```yaml
2020-03-13 22:54:00 -03:00
# A batch size of N, produces 2 * (N-1) negative samples. Original implementation uses a batch size of 8192
batch_size: 512
# Number of epochs to train
epochs: 40
2020-03-14 07:27:27 -03:00
# Frequency to eval the similarity score using the validation set
2020-03-13 22:54:00 -03:00
eval_every_n_epochs: 1
2020-03-15 07:02:30 -03:00
# Specify a folder containing a pre-trained model to fine-tune. If training from scratch, pass None.
2020-03-13 22:54:00 -03:00
fine_tune_from: 'Mar13_20-12-09_thallessilva'
# Frequency to which tensorboard is updated
log_every_n_steps: 50
# Model related parameters
model:
# Output dimensionality of the embedding vector z. Original implementation uses 2048
out_dim: 256
# The ConvNet base model. Choose one of: "resnet18" or "resnet50". Original implementation uses resnet50
base_model: "resnet18"
# Dataset related parameters
dataset:
s: 1
# dataset input shape. For datasets containing images of different size, this defines the final
input_shape: (96,96,3)
# Number of workers for the data loader
num_workers: 0
# Size of the validation set in percentage
valid_size: 0.05
# NTXent loss related parameters
loss:
# Temperature parameter for the contrastive objective
temperature: 0.5
# Distance metric for contrastive loss. If False, uses dot product. Original implementation uses cosine similarity.
use_cosine_similarity: True
2020-02-25 08:49:51 -03:00
```
2020-02-25 08:53:40 -03:00
## Feature Evaluation
2020-03-13 20:33:50 -03:00
Feature evaluation is done using a linear model protocol.
Features are learned using the ```STL10 train+unsupervised``` set and evaluated in the ```test``` set;
2020-03-07 11:52:02 -03:00
2020-03-14 11:35:26 -03:00
Check the [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://github.com/sthalles/SimCLR/blob/master/feature_eval/linear_feature_eval.ipynb) notebook for reproducibility.
2020-03-13 20:33:50 -03:00
2020-03-14 11:31:37 -03:00
| Linear Classifier | Feature Extractor | Architecture | Feature dimensionality | Projection Head dimensionality | Epochs | STL10 Top 1 |
|:---------------------------:|:-----------------:|:------------:|:----------------------:|:-------------------------------:|:------:|:-----------:|
| Logistic Regression | PCA Features | - | 256 | - | | 36.0% |
| KNN | PCA Features | - | 256 | - | | 31.8% |
2020-03-15 10:43:09 -03:00
| Logistic Regression (LBFGS) | SimCLR | [ResNet-18](https://drive.google.com/open?id=1c4eVon0sUd-ChVhH6XMpF6nCngNJsAPk) | 512 | 256 | 40 | 70.3% |
2020-03-14 11:31:37 -03:00
| KNN | SimCLR | ResNet-18 | 512 | 256 | 40 | 66.2% |
2020-03-15 10:43:09 -03:00
| Logistic Regression (LBFGS) | SimCLR | [ResNet-18](https://drive.google.com/open?id=1L0yoeY9i2mzDcj69P4slTWb-cfr3PyoT) | 512 | 256 | 80 | 72.9% |
2020-03-14 11:31:37 -03:00
| KNN | SimCLR | ResNet-18 | 512 | 256 | 80 | 69.8% |
2020-03-15 10:43:09 -03:00
| Logistic Regression (Adam) | SimCLR | [ResNet-50](https://drive.google.com/open?id=1TZqBNTFCsO-mxAiR-zJeyupY-J2gA27Q) | 2048 | 128 | 40 | 73.6% |
| Logistic Regression (Adam) | SimCLR | [ResNet-50](https://drive.google.com/open?id=1is1wkBRccHdhSKQnPUTQoaFkVNSaCb35) | 2048 | 128 | 80 | 76.3% |
2020-03-14 11:31:37 -03:00
2020-03-07 12:01:58 -03:00