Update README.md
Browse files
README.md
CHANGED
@@ -1,3 +1,178 @@
|
|
1 |
---
|
2 |
license: mit
|
3 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
license: mit
|
3 |
---
|
4 |
+
# Mixed Barlow Twins
|
5 |
+
[**Guarding Barlow Twins Against Overfitting with Mixed Samples**](https://arxiv.org/abs/2312.02151)<br>
|
6 |
+
|
7 |
+
[Wele Gedara Chaminda Bandara](https://www.wgcban.com) (Johns Hopkins University), [Celso M. De Melo](https://celsodemelo.net) (U.S. Army Research Laboratory), and [Vishal M. Patel](https://engineering.jhu.edu/vpatel36/) (Johns Hopkins University) <br>
|
8 |
+
|
9 |
+
## 1 Overview of Mixed Barlow Twins
|
10 |
+
|
11 |
+
TL;DR
|
12 |
+
- Mixed Barlow Twins aims to improve sample interaction during Barlow Twins training via linearly interpolated samples.
|
13 |
+
- We introduce an additional regularization term to the original Barlow Twins objective, assuming linear interpolation in the input space translates to linearly interpolated features in the feature space.
|
14 |
+
- Pre-training with this regularization effectively mitigates feature overfitting and further enhances the downstream performance on `CIFAR-10`, `CIFAR-100`, `TinyImageNet`, `STL-10`, and `ImageNet` datasets.
|
15 |
+
|
16 |
+
<img src="figs/mix-bt.svg" width="1024">
|
17 |
+
|
18 |
+
$C^{MA} = (Z^M)^TZ^A$
|
19 |
+
|
20 |
+
$C^{MB} = (Z^M)^TZ^B$
|
21 |
+
|
22 |
+
$C^{MA}_{gt} = \lambda (Z^A)^TZ^A + (1-\lambda)\mathtt{Shuffle}^*(Z^B)^TZ^A$
|
23 |
+
|
24 |
+
$C^{MB}_{gt} = \lambda (Z^A)^TZ^B + (1-\lambda)\mathtt{Shuffle}^*(Z^B)^TZ^B$
|
25 |
+
|
26 |
+
## 2 Usage
|
27 |
+
### 2.1 Requirements
|
28 |
+
|
29 |
+
Before using this repository, make sure you have the following prerequisites installed:
|
30 |
+
|
31 |
+
- [Anaconda](https://www.anaconda.com/download/)
|
32 |
+
- [PyTorch](https://pytorch.org)
|
33 |
+
|
34 |
+
You can install PyTorch with the following [command](https://pytorch.org/get-started/locally/) (in Linux OS):
|
35 |
+
```bash
|
36 |
+
conda install pytorch torchvision pytorch-cuda=11.8 -c pytorch -c nvidia
|
37 |
+
```
|
38 |
+
|
39 |
+
### 2.2 Installation
|
40 |
+
|
41 |
+
To get started, clone this repository:
|
42 |
+
```bash
|
43 |
+
git clone https://github.com/wgcban/mix-bt.git
|
44 |
+
```
|
45 |
+
|
46 |
+
Next, create the [conda](https://docs.conda.io/projects/conda/en/stable/) environment named `ssl-aug` by executing the following command:
|
47 |
+
```bash
|
48 |
+
conda env create -f environment.yml
|
49 |
+
```
|
50 |
+
|
51 |
+
All the train-val-test statistics will be automatically upload to [`wandb`](https://wandb.ai/home), and please refer [`wandb-quick-start`](https://wandb.ai/quickstart?utm_source=app-resource-center&utm_medium=app&utm_term=quickstart) documentation if you are not familiar with using `wandb`.
|
52 |
+
|
53 |
+
### 2.3 Supported Pre-training Datasets
|
54 |
+
|
55 |
+
This repository supports the following pre-training datasets:
|
56 |
+
- `CIFAR-10`: https://www.cs.toronto.edu/~kriz/cifar.html
|
57 |
+
- `CIFAR-100`: https://www.cs.toronto.edu/~kriz/cifar.html
|
58 |
+
- `Tiny-ImageNet`: https://github.com/rmccorm4/Tiny-Imagenet-200
|
59 |
+
- `STL-10`: https://cs.stanford.edu/~acoates/stl10/
|
60 |
+
- `ImageNet`: https://www.image-net.org
|
61 |
+
|
62 |
+
`CIFAR-10`, `CIFAR-100`, and `STL-10` datasets are directly available in PyTorch.
|
63 |
+
|
64 |
+
To use `TinyImageNet`, please follow the preprocessing instructions provided in the [TinyImageNet-Script](https://gist.github.com/moskomule/2e6a9a463f50447beca4e64ab4699ac4). Download these datasets and place them in the `data` directory.
|
65 |
+
|
66 |
+
### 2.4 Supported Transfer Learning Datasets
|
67 |
+
You can download and place transfer learning datasets under their respective paths, such as 'data/DTD'. The supported transfer learning datasets include:
|
68 |
+
- `DTD`: https://www.robots.ox.ac.uk/~vgg/data/dtd/
|
69 |
+
- `MNIST`: http://yann.lecun.com/exdb/mnist/
|
70 |
+
- `FashionMNIST`: https://github.com/zalandoresearch/fashion-mnist
|
71 |
+
- `CUBirds`: http://www.vision.caltech.edu/visipedia/CUB-200-2011.html
|
72 |
+
- `VGGFlower`: https://www.robots.ox.ac.uk/~vgg/data/flowers/102/
|
73 |
+
- `Traffic Signs`: https://benchmark.ini.rub.de/gtsdb_dataset.html
|
74 |
+
- `Aircraft`: https://www.robots.ox.ac.uk/~vgg/data/fgvc-aircraft/
|
75 |
+
|
76 |
+
### 2.5 Supported SSL Methods
|
77 |
+
|
78 |
+
This repository supports the following Self-Supervised Learning (SSL) methods:
|
79 |
+
|
80 |
+
- [`SimCLR`](https://arxiv.org/abs/2002.05709): contrastive learning for SSL
|
81 |
+
- [`BYOL`](https://arxiv.org/abs/2006.07733): distilation for SSL
|
82 |
+
- [`Witening MSE`](http://proceedings.mlr.press/v139/ermolov21a/ermolov21a.pdf): infomax for SSL
|
83 |
+
- [`Barlow Twins`](https://arxiv.org/abs/2103.03230): infomax for SSL
|
84 |
+
- **`Mixed Barlow Twins (ours)`**: infomax + mixed samples for SSL
|
85 |
+
|
86 |
+
### 2.6 Pre-Training with Mixed Barlow Twins
|
87 |
+
To start pre-training and obtain k-NN evaluation results for Mixed Barlow Twins on `CIFAR-10`, `CIFAR-100`, `TinyImageNet`, and `STL-10` with `ResNet-18/50` backbones, please run:
|
88 |
+
```bash
|
89 |
+
sh scripts-pretrain-resnet18/[dataset].sh
|
90 |
+
```
|
91 |
+
```bash
|
92 |
+
sh scripts-pretrain-resnet50/[dataset].sh
|
93 |
+
```
|
94 |
+
|
95 |
+
To start the pre-training on `ImageNet` with `ResNet-50` backbone, please run:
|
96 |
+
```bash
|
97 |
+
sh scripts-pretrain-resnet18/imagenet.sh
|
98 |
+
```
|
99 |
+
|
100 |
+
### 2.7 Linear Evaluation of Pre-trained Models
|
101 |
+
Before running linear evaluation, *ensure that you specify the `model_path` argument correctly in the corresponding .sh file*.
|
102 |
+
|
103 |
+
To obtain linear evaluation results on `CIFAR-10`, `CIFAR-100`, `TinyImageNet`, `STL-10` with `ResNet-18/50` backbones, please run:
|
104 |
+
```bash
|
105 |
+
sh scripts-linear-resnet18/[dataset].sh
|
106 |
+
```
|
107 |
+
```bash
|
108 |
+
sh scripts-linear-resnet50/[dataset].sh
|
109 |
+
```
|
110 |
+
|
111 |
+
To obtain linear evaluation results on `ImageNet` with `ResNet-50` backbone, please run:
|
112 |
+
```bash
|
113 |
+
sh scripts-linear-resnet50/imagenet_sup.sh
|
114 |
+
```
|
115 |
+
|
116 |
+
|
117 |
+
### 2.8 Transfer Learning of Pre-trained Models
|
118 |
+
To perform transfer learning from pre-trained models on `CIFAR-10`, `CIFAR-100`, and `STL-10` to fine-grained classification datasets, execute the following command, making sure to specify the `model_path` argument correctly:
|
119 |
+
```bash
|
120 |
+
sh scripts-transfer-resnet18/[dataset]-to-x.sh
|
121 |
+
```
|
122 |
+
|
123 |
+
## 3 Pre-Trained Checkpoints
|
124 |
+
Download the pre-trained models from [GitHub (Releases v1.0.0)](https://github.com/wgcban/mix-bt/releases/tag/v1.0.0) and store them in `checkpoints/`. This repository provides pre-trained checkpoints for both [`ResNet-18`](https://arxiv.org/abs/1512.03385) and [`ResNet-50`](https://arxiv.org/abs/1512.03385) architectures.
|
125 |
+
|
126 |
+
#### 3.1 ResNet-18
|
127 |
+
| Dataset | $d$ | $\lambda_{BT}$ | $\lambda_{reg}$ | Download Link to Pretrained Model | KNN Acc. | Linear Acc. |
|
128 |
+
| ---------- | --- | ---------- | ---------- | ------------------------ | -------- | ----------- |
|
129 |
+
| `CIFAR-10` | 1024 | 0.0078125 | 4.0 | [4wdhbpcf_0.0078125_1024_256_cifar10_model.pth](https://github.com/wgcban/mix-bt/releases/download/v1.0.0/4wdhbpcf_0.0078125_1024_256_cifar10_model.pth) | 90.52 | 92.58 |
|
130 |
+
| `CIFAR-100` | 1024 | 0.0078125 | 4.0 | [76kk7scz_0.0078125_1024_256_cifar100_model.pth](https://github.com/wgcban/mix-bt/releases/download/v1.0.0/76kk7scz_0.0078125_1024_256_cifar100_model.pth) | 61.25 | 69.31 |
|
131 |
+
| `TinyImageNet` | 1024 | 0.0009765 | 4.0 | [02azq6fs_0.0009765_1024_256_tiny_imagenet_model.pth](https://github.com/wgcban/mix-bt/releases/download/v1.0.0/02azq6fs_0.0009765_1024_256_tiny_imagenet_model.pth) | 38.11 | 51.67 |
|
132 |
+
| `STL-10` | 1024 | 0.0078125 | 2.0 | [i7det4xq_0.0078125_1024_256_stl10_model.pth](https://github.com/wgcban/mix-bt/releases/download/v1.0.0/i7det4xq_0.0078125_1024_256_stl10_model.pth) | 88.94 | 91.02 |
|
133 |
+
|
134 |
+
#### 3.2 ResNet-50
|
135 |
+
| Dataset | $d$ | $\lambda_{BT}$ | $\lambda_{reg}$ | Download Link to Pretrained Model | KNN Acc. | Linear Acc. |
|
136 |
+
| ---------- | --- | ---------- | ---------- | ------------------------ | -------- | ----------- |
|
137 |
+
| `CIFAR-10` | 1024 | 0.0078125 | 4.0 | [v3gwgusq_0.0078125_1024_256_cifar10_model.pth](https://github.com/wgcban/mix-bt/releases/download/v1.0.0/v3gwgusq_0.0078125_1024_256_cifar10_model.pth) | 91.39 | 93.89 |
|
138 |
+
| `CIFAR-100` | 1024 | 0.0078125 | 4.0 | [z6ngefw7_0.0078125_1024_256_cifar100_model.pth](https://github.com/wgcban/mix-bt/releases/download/v1.0.0/z6ngefw7_0.0078125_1024_256_cifar100_model.pth) | 64.32 | 72.51 |
|
139 |
+
| `TinyImageNet` | 1024 | 0.0009765 | 4.0 | [kxlkigsv_0.0009765_1024_256_tiny_imagenet_model.pth](https://github.com/wgcban/mix-bt/releases/download/v1.0.0/kxlkigsv_0.0009765_1024_256_tiny_imagenet_model.pth) | 42.21 | 51.84 |
|
140 |
+
| `STL-10` | 1024 | 0.0078125 | 2.0 | [pbknx38b_0.0078125_1024_256_stl10_model.pth](https://github.com/wgcban/mix-bt/releases/download/v1.0.0/pbknx38b_0.0078125_1024_256_stl10_model.pth) | 87.79 | 91.70 |
|
141 |
+
|
142 |
+
**On `ImageNet`**
|
143 |
+
| # Epochs | $d$ | $\lambda_{BT}$ | $\lambda_{reg}$ | Download Link to Pretrained Model | Linear Acc. |
|
144 |
+
| ---------- | --- | ---------- | ---------- | ------------------------ | ----------- |
|
145 |
+
| 300 | 8192 | 0.0051 | 0.0 (BT) | [3on0l4wl_0.0000_8192_1024_imagenet_resnet50.pth](https://github.com/wgcban/mix-bt/releases/download/v1.0.0/3on0l4wl_0.0000_8192_1024_imagenet_resnet50.pth) | 71.3 |
|
146 |
+
| 300 | 8192 | 0.0051 | 0.0025 | [l418b9zw_0.0025_8192_1024_imagenet_resnet50.pth](https://github.com/wgcban/mix-bt/releases/download/v1.0.0/l418b9zw_0.0025_8192_1024_imagenet_resnet50.pth) | 70.9 |
|
147 |
+
| 300 | 8192 | 0.0051 | 0.1 | [13awtq23_0.1000_8192_1024_imagenet_resnet50](https://github.com/wgcban/mix-bt/releases/download/v1.0.0/13awtq23_0.1000_8192_1024_imagenet_resnet50.pth) | 71.6 |
|
148 |
+
| 300 | 8192 | 0.0051 | 1.0 | [3fb1op86_1.0000_8192_1024_imagenet_resnet50.pth](https://github.com/wgcban/mix-bt/releases/download/v1.0.0/3fb1op86_1.0000_8192_1024_imagenet_resnet50.pth) | **72.2** |
|
149 |
+
| 300 | 8192 | 0.0051 | 3.0 | [TBU]() | TBU |
|
150 |
+
| 300 | 8192 | 0.0051 | 5.0 | [TBU]() | TBU |
|
151 |
+
|
152 |
+
## 4 Training Statistics
|
153 |
+
Here we provide some training and validation (linear probing) statistics for Barlow Twins *vs.* Mixed Barlow Twins with `ResNet-50` backbone on `ImageNet`:
|
154 |
+
|
155 |
+
<img src="figs/in-loss-bt.png" width="256"/> <img src="figs/in-loss-reg.png" width="256"/> <img src="figs/in-linear.png" width="256"/>
|
156 |
+
|
157 |
+
## 5 Disclaimer
|
158 |
+
A large portion of the code is from [Barlow Twins HSIC](https://github.com/yaohungt/Barlow-Twins-HSIC) (for experiments on small datasets: `CIFAR-10`, `CIFAR-100`, `TinyImageNet`, and `STL-10`) and official implementation of Barlow Twins [here](https://github.com/facebookresearch/barlowtwins) (for experiments on `ImageNet`), which is a great resource for academic development.
|
159 |
+
|
160 |
+
Also, note that the implementation of SOTA methods ([SimCLR](https://arxiv.org/abs/2002.05709), [BYOL](https://arxiv.org/abs/2006.07733), and [Witening-MSE](https://arxiv.org/abs/2007.06346)) in `ssl-sota` are copied from [Witening-MSE](https://github.com/htdt/self-supervised).
|
161 |
+
|
162 |
+
We would like to thank all of them for making their repositories publicly available for the research community. 🙏
|
163 |
+
|
164 |
+
## 6 Reference
|
165 |
+
If you feel our work is useful, please consider citing our work. Thanks!
|
166 |
+
```bibtex
|
167 |
+
@misc{bandara2023guarding,
|
168 |
+
title={Guarding Barlow Twins Against Overfitting with Mixed Samples},
|
169 |
+
author={Wele Gedara Chaminda Bandara and Celso M. De Melo and Vishal M. Patel},
|
170 |
+
year={2023},
|
171 |
+
eprint={2312.02151},
|
172 |
+
archivePrefix={arXiv},
|
173 |
+
primaryClass={cs.CV}
|
174 |
+
}
|
175 |
+
```
|
176 |
+
|
177 |
+
## 7 License
|
178 |
+
This code is under MIT licence, you can find the complete file [here](https://github.com/wgcban/mix-bt/blob/main/LICENSE).
|