SimCLR/README.md

87 lines
2.8 KiB
Markdown
Raw Normal View History

# PyTorch implementation of SimCLR: A Simple Framework for Contrastive Learning of Visual Representations
2020-03-07 23:01:58 +08: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 21:25:20 +08:00
#### For a Tensorflow 2.0 Implementation: [Tensorflow SimCLR](https://github.com/sthalles/SimCLR-tensorflow)
2020-03-07 22:56:34 +08:00
![Image of SimCLR Arch](https://sthalles.github.io/assets/contrastive-self-supervised/cover.png)
2020-03-09 21:24:56 +08:00
2020-03-14 10:11:25 +08:00
## Instalation
2020-03-07 23:01:58 +08:00
2020-03-14 10:11:07 +08:00
```
$ conda create --name simclr python=3.7 --file requirements.txt
$ conda activate simclr
$ python run.py
```
2020-02-25 19:49:51 +08:00
## Config file
2020-03-09 18:41:47 +08:00
Before running SimCLR, make sure you choose the correct running configurations on the ```config.yaml``` file.
2020-02-25 19:49:51 +08:00
2020-02-25 19:51:49 +08:00
```yaml
2020-03-14 09:54:00 +08: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
# Frequency to eval the similary score using the validation set
eval_every_n_epochs: 1
# Specify a folder containing a pre-trained model to fine-tune
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 19:49:51 +08:00
```
2020-02-25 19:53:40 +08:00
## Feature Evaluation
2020-03-14 07:33:50 +08: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 22:52:02 +08:00
2020-03-10 19:52:57 +08:00
Check the ```feature_eval/linear_feature_eval.ipynb``` notebook for reproducebility.
2020-03-14 07:33:50 +08:00
2020-03-07 22:56:34 +08:00
| Feature Extractor | Method | Architecture | Top 1 |
|:-------------------:|:------------:|:------------:|:-----:|
| Logistic Regression | PCA Features | - | 36.0% |
2020-03-11 08:41:58 +08:00
| KNN | PCA Features | - | 31.8% |
2020-03-11 18:54:24 +08:00
| Logistic Regression | SimCLR | ResNet-18 | 75.0% |
| KNN | SimCLR | ResNet-18 | 70.0% |
2020-03-07 23:01:58 +08:00
## Download pre-trained model
- [ResNet-18](https://drive.google.com/open?id=1zDHvk1oE1k3b4ApsK1AdMREIAcpkG-We) Trained using ```STl10 unsupervised``` set.