2023-02-23 11:17:16 +08:00
# SwAV
> [Unsupervised Learning of Visual Features by Contrasting Cluster Assignments](https://arxiv.org/abs/2006.09882)
<!-- [ALGORITHM] -->
## Abstract
Unsupervised image representations have significantly reduced the gap with supervised pretraining, notably with the recent achievements of contrastive learning methods. These contrastive methods typically work online and rely on a large number of explicit pairwise feature comparisons, which is computationally challenging. In this paper, we propose an online algorithm, SwAV, that takes advantage of contrastive methods without requiring to compute pairwise comparisons. Specifically, our method simultaneously clusters the data while enforcing consistency between cluster assignments produced for different augmentations (or “views”) of the same image, instead of comparing features directly as in contrastive learning. Simply put, we use a “swapped” prediction mechanism where we predict the code of a view from the representation of another view. Our method can be trained with large and small batches and can scale to unlimited amounts of data. Compared to previous contrastive methods, our method is more memory efficient since it does not require a large memory bank or a special momentum network. In addition, we also propose a new data augmentation strategy, multi-crop, that uses a mix of views with different resolutions in place of two full-resolution views, without increasing the memory or compute requirements.
2023-03-02 13:29:07 +08:00
< div align = center >
2023-02-23 11:17:16 +08:00
< img src = "https://user-images.githubusercontent.com/36138628/149724517-9f1e7bdf-04c7-43e3-92f4-2b8fc1399123.png" width = "500" / >
< / div >
2023-03-02 13:29:07 +08:00
## How to use it?
2023-02-23 11:17:16 +08:00
2023-03-02 13:29:07 +08:00
<!-- [TABS - BEGIN] -->
2023-02-23 11:17:16 +08:00
2023-03-02 13:29:07 +08:00
**Predict image**
2023-02-23 11:17:16 +08:00
2023-03-02 13:29:07 +08:00
```python
from mmpretrain import inference_model
2023-02-23 11:17:16 +08:00
2023-03-02 13:29:07 +08:00
predict = inference_model('resnet50_swav-pre_8xb32-linear-coslr-100e_in1k', 'demo/bird.JPEG')
print(predict['pred_class'])
print(predict['pred_score'])
```
2023-02-23 11:17:16 +08:00
2023-03-02 13:29:07 +08:00
**Use the model**
2023-02-23 11:17:16 +08:00
2023-03-02 13:29:07 +08:00
```python
import torch
from mmpretrain import get_model
2023-02-23 11:17:16 +08:00
2023-03-02 13:29:07 +08:00
model = get_model('swav_resnet50_8xb32-mcrop-coslr-200e_in1k-224px-96px', pretrained=True)
inputs = torch.rand(1, 3, 224, 224)
out = model(inputs)
print(type(out))
# To extract features.
feats = model.extract_feat(inputs)
print(type(feats))
```
2023-02-23 11:17:16 +08:00
2023-03-02 13:29:07 +08:00
**Train/Test Command**
2023-02-23 11:17:16 +08:00
2023-04-06 20:58:52 +08:00
Prepare your dataset according to the [docs ](https://mmpretrain.readthedocs.io/en/latest/user_guides/dataset_prepare.html#prepare-dataset ).
2023-02-23 11:17:16 +08:00
2023-03-02 13:29:07 +08:00
Train:
2023-02-23 11:17:16 +08:00
2023-03-02 13:29:07 +08:00
```shell
python tools/train.py configs/swav/swav_resnet50_8xb32-mcrop-coslr-200e_in1k-224px-96px.py
```
2023-02-23 11:17:16 +08:00
2023-03-02 13:29:07 +08:00
Test:
2023-02-23 11:17:16 +08:00
2023-03-02 13:29:07 +08:00
```shell
python tools/test.py configs/swav/benchmarks/resnet50_8xb512-linear-coslr-90e_in1k.py https://download.openmmlab.com/mmselfsup/1.x/swav/swav_resnet50_8xb32-mcrop-2-6-coslr-200e_in1k-224-96/resnet50_linear-8xb32-coslr-100e_in1k/resnet50_linear-8xb32-coslr-100e_in1k_20220825-80341e08.pth
```
2023-02-23 11:17:16 +08:00
2023-03-02 13:29:07 +08:00
<!-- [TABS - END] -->
2023-02-23 11:17:16 +08:00
2023-03-02 13:29:07 +08:00
## Models and results
2023-02-23 11:17:16 +08:00
2023-03-02 13:29:07 +08:00
### Pretrained models
2023-02-23 11:17:16 +08:00
2023-03-02 13:29:07 +08:00
| Model | Params (M) | Flops (G) | Config | Download |
| :----------------------------------------------------- | :--------: | :-------: | :------------------------------------------------------------: | :---------------------------------------------------------------: |
2023-03-20 14:30:57 +08:00
| `swav_resnet50_8xb32-mcrop-coslr-200e_in1k-224px-96px` | 28.35 | 4.11 | [config ](swav_resnet50_8xb32-mcrop-coslr-200e_in1k-224px-96px.py ) | [model ](https://download.openmmlab.com/mmselfsup/1.x/swav/swav_resnet50_8xb32-mcrop-2-6-coslr-200e_in1k-224-96/swav_resnet50_8xb32-mcrop-2-6-coslr-200e_in1k-224-96_20220825-5b3fc7fc.pth ) \| [log ](https://download.openmmlab.com/mmselfsup/1.x/swav/swav_resnet50_8xb32-mcrop-2-6-coslr-200e_in1k-224-96/swav_resnet50_8xb32-mcrop-2-6-coslr-200e_in1k-224-96_20220825-5b3fc7fc.json ) |
2023-02-23 11:17:16 +08:00
2023-03-02 13:29:07 +08:00
### Image Classification on ImageNet-1k
2023-02-23 11:17:16 +08:00
2023-03-02 13:29:07 +08:00
| Model | Pretrain | Params (M) | Flops (G) | Top-1 (%) | Config | Download |
| :---------------------------------------- | :------------------------------------------: | :--------: | :-------: | :-------: | :----------------------------------------: | :-------------------------------------------: |
2023-03-20 14:30:57 +08:00
| `resnet50_swav-pre_8xb32-linear-coslr-100e_in1k` | [SWAV ](https://download.openmmlab.com/mmselfsup/1.x/swav/swav_resnet50_8xb32-mcrop-2-6-coslr-200e_in1k-224-96/swav_resnet50_8xb32-mcrop-2-6-coslr-200e_in1k-224-96_20220825-5b3fc7fc.pth ) | 25.56 | 4.11 | 70.50 | [config ](benchmarks/resnet50_8xb512-linear-coslr-90e_in1k.py ) | [model ](https://download.openmmlab.com/mmselfsup/1.x/swav/swav_resnet50_8xb32-mcrop-2-6-coslr-200e_in1k-224-96/resnet50_linear-8xb32-coslr-100e_in1k/resnet50_linear-8xb32-coslr-100e_in1k_20220825-80341e08.pth ) \| [log ](https://download.openmmlab.com/mmselfsup/1.x/swav/swav_resnet50_8xb32-mcrop-2-6-coslr-200e_in1k-224-96/resnet50_linear-8xb32-coslr-100e_in1k/resnet50_linear-8xb32-coslr-100e_in1k_20220825-80341e08.json ) |
2023-02-23 11:17:16 +08:00
## Citation
```bibtex
@article {caron2020unsupervised,
title={Unsupervised Learning of Visual Features by Contrasting Cluster Assignments},
author={Caron, Mathilde and Misra, Ishan and Mairal, Julien and Goyal, Priya and Bojanowski, Piotr and Joulin, Armand},
booktitle={NeurIPS},
year={2020}
}
```