# SupContrast: Supervised Contrastive Learning
<p align="center">
<img src="figures/teaser.png" width="700">
</p>
This repo covers an reference implementation for the following papers in PyTorch, using CIFAR as an illustrative example:
(1) Supervised Contrastive Learning. [Paper](https://arxiv.org/abs/2004.11362)
(2) A Simple Framework for Contrastive Learning of Visual Representations. [Paper](https://arxiv.org/abs/2002.05709)
## Update
ImageNet model (small batch size with the trick of the momentum encoder) is released [here](https://www.dropbox.com/s/l4a69ececk4spdt/supcon.pth?dl=0).
## Loss Function
The loss function [`SupConLoss`](https://github.com/HobbitLong/SupContrast/blob/master/losses.py#L11) in `losses.py` takes `features` (L2 normalized) and `labels` as input, and return the loss. If `labels` is `None` or not passed to the it, it degenerates to SimCLR.
Usage:
```python
from losses import SupConLoss
# define loss with a temperature `temp`
criterion = SupConLoss(temperature=temp)
# features: [bsz, n_views, f_dim]
# `n_views` is the number of crops from each image
# better be L2 normalized in f_dim dimension
features = ...
# labels: [bsz]
labels = ...
# SupContrast
loss = criterion(features, labels)
# or SimCLR
loss = criterion(features)
...
```
## Comparison
Results on CIFAR-10:
| |Arch | Setting | Loss | Accuracy(%) |
|----------|:----:|:---:|:---:|:---:|
| SupCrossEntropy | ResNet50 | Supervised | Cross Entropy | 95.0 |
| SupContrast | ResNet50 | Supervised | Contrastive | 96.0 |
| SimCLR | ResNet50 | Unsupervised | Contrastive | 93.6 |
Results on CIFAR-100:
| |Arch | Setting | Loss | Accuracy(%) |
|----------|:----:|:---:|:---:|:---:|
| SupCrossEntropy | ResNet50 | Supervised | Cross Entropy | 75.3 |
| SupContrast | ResNet50 | Supervised | Contrastive | 76.5 |
| SimCLR | ResNet50 | Unsupervised | Contrastive | 70.7 |
Results on ImageNet (Stay tuned):
| |Arch | Setting | Loss | Accuracy(%) |
|----------|:----:|:---:|:---:|:---:|
| SupCrossEntropy | ResNet50 | Supervised | Cross Entropy | - |
| SupContrast | ResNet50 | Supervised | Contrastive | 79.1 (MoCo trick) |
| SimCLR | ResNet50 | Unsupervised | Contrastive | - |
## Running
You might use `CUDA_VISIBLE_DEVICES` to set proper number of GPUs, and/or switch to CIFAR100 by `--dataset cifar100`.
**(1) Standard Cross-Entropy**
```
python main_ce.py --batch_size 1024 \
--learning_rate 0.8 \
--cosine --syncBN \
```
**(2) Supervised Contrastive Learning**
Pretraining stage:
```
python main_supcon.py --batch_size 1024 \
--learning_rate 0.5 \
--temp 0.1 \
--cosine
```
<s>You can also specify `--syncBN` but I found it not crucial for SupContrast (`syncBN` 95.9% v.s. `BN` 96.0%). </s>
WARN: Currently, `--syncBN` has no effect since the code is using `DataParallel` instead of `DistributedDataParaleel`
Linear evaluation stage:
```
python main_linear.py --batch_size 512 \
--learning_rate 5 \
--ckpt /path/to/model.pth
```
**(3) SimCLR**
Pretraining stage:
```
python main_supcon.py --batch_size 1024 \
--learning_rate 0.5 \
--temp 0.5 \
--cosine --syncBN \
--method SimCLR
```
The `--method SimCLR` flag simply stops `labels` from being passed to `SupConLoss` criterion.
Linear evaluation stage:
```
python main_linear.py --batch_size 512 \
--learning_rate 1 \
--ckpt /path/to/model.pth
```
On custom dataset:
```
python main_supcon.py --batch_size 1024 \
--learning_rate 0.5 \
--temp 0.1 --cosine \
--dataset path \
--data_folder ./path \
--mean "(0.4914, 0.4822, 0.4465)" \
--std "(0.2675, 0.2565, 0.2761)" \
--method SimCLR
```
The `--data_folder` must be of form ./path/label/xxx.png folowing https://pytorch.org/docs/stable/torchvision/datasets.html#torchvision.datasets.ImageFolder convension.
and
## t-SNE Visualization
**(1) Standard Cross-Entropy**
<p align="center">
<img src="figures/SupCE.jpg" width="400">
</p>
**(2) Supervised Contrastive Learning**
<p align="center">
<img src="figures/SupContrast.jpg" width="800">
</p>
**(3) SimCLR**
<p align="center">
<img src="figures/SimCLR.jpg" width="800">
</p>
## Reference
```
@Article{khosla2020supervised,
title = {Supervised Contrastive Learning},
author = {Prannay Khosla and Piotr Teterwak and Chen Wang and Aaron Sarna and Yonglong Tian and Phillip Isola and Aaron Maschinot and Ce Liu and Dilip Krishnan},
journal = {arXiv preprint arXiv:2004.11362},
year = {2020},
}
```
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
基于深度学习的积灰检测识别-图像分类源码+数据集,纯手打高分项目。 介绍 解决灰尘识别问题 采用自制灰尘数据集 方法介绍 (1)普通数据增广 (2)AutoAugment数据增强 (3)resnet (4)监督对比学习损失 (5)各种常用深度学习算法 基于深度学习的积灰检测识别-图像分类源码+数据集,纯手打高分项目。 介绍 解决灰尘识别问题 采用自制灰尘数据集 方法介绍 (1)普通数据增广 (2)AutoAugment数据增强 (3)resnet (4)监督对比学习损失 (5)各种常用深度学习算法 基于深度学习的积灰识别-图像分类+数据集,纯手打高分项目。 介绍 解决灰尘识别问题 采用自制灰尘数据集 方法介绍 (1)普通数据增广 (2)AutoAugment数据增强 (3)resnet (4)监督对比学习损失 (5)各种常用深度学习算法
资源推荐
资源详情
资源评论
收起资源包目录
基于深度学习的积灰检测识别-图像分类源码+数据集.zip (240个子文件)
3.7z 3.87MB
3.7z 3.87MB
DensNet121-torch 4KB
train183.jpg 7.86MB
train173.jpg 5.73MB
train162.jpg 3.75MB
172.jpg 2.73MB
4.jpg 2.36MB
train178.jpg 2.34MB
25.jpg 2.14MB
117.jpg 2.02MB
train182.jpg 1.88MB
23.jpg 1.85MB
24.jpg 1.84MB
102.jpg 1.82MB
37.jpg 1.62MB
train175.jpg 1.58MB
train11.jpg 1.52MB
train184.jpg 1.51MB
161.jpg 1.47MB
34.jpg 1.45MB
111.jpg 1.4MB
118.jpg 1.38MB
train174.jpg 1.37MB
40.jpg 1.34MB
32.jpg 1.34MB
train31.jpg 1.32MB
33.jpg 1.31MB
36.jpg 1.31MB
30.jpg 1.31MB
train176.jpg 1.16MB
21.jpg 1.08MB
120.jpg 1.05MB
35.jpg 1.02MB
27.jpg 1MB
116.jpg 1012KB
177.jpg 1004KB
121.jpg 983KB
38.jpg 980KB
112.jpg 969KB
105.jpg 928KB
106.jpg 918KB
train180.jpg 915KB
115.jpg 878KB
3.jpg 832KB
119.jpg 792KB
train177.jpg 730KB
174.jpg 722KB
train179.jpg 700KB
train181.jpg 597KB
173.jpg 595KB
train87.jpg 533KB
175.jpg 515KB
176.jpg 497KB
55.jpg 492KB
178.jpg 487KB
train5.jpg 484KB
39.jpg 437KB
179.jpg 429KB
180.jpg 403KB
train44.jpg 397KB
train169.jpg 386KB
train77.jpg 363KB
train48.jpg 343KB
train46.jpg 343KB
30.jpg 342KB
train3.jpg 326KB
24.jpg 308KB
train8.jpg 298KB
train45.jpg 275KB
train47.jpg 275KB
train49.jpg 268KB
train163.jpg 247KB
train1.jpg 244KB
168.jpg 227KB
train43.jpg 225KB
train10.jpg 222KB
21.jpg 219KB
76.jpg 216KB
train171.jpg 214KB
49.jpg 203KB
51.jpg 200KB
45.jpg 199KB
108.jpg 199KB
43.jpg 192KB
107.jpg 191KB
54.jpg 189KB
27.jpg 180KB
train170.jpg 175KB
28.jpg 164KB
104.jpg 162KB
50.jpg 159KB
67.jpg 157KB
46.jpg 150KB
train18.jpg 149KB
167.jpg 146KB
train71.jpg 143KB
65.jpg 139KB
44.jpg 136KB
48.jpg 135KB
共 240 条
- 1
- 2
- 3
程序员张小妍
- 粉丝: 1w+
- 资源: 3252
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页