Skip to content

Commit

Permalink
[README] add clip compressor.
Browse files Browse the repository at this point in the history
  • Loading branch information
YannDubs committed Jul 7, 2021
1 parent fc39639 commit ad3fb3d
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 21 deletions.
29 changes: 16 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,22 +197,25 @@ bin/clip/main_small.sh

The following table comes directly from the results which are automatically saved at `results/exp_clip_bottleneck_linear_eval/**/datapred_*/**/results_predictor.csv`. It shows the result of compression from our CLIP compressor on many datasets.

| | Cars196 | STL10 | Caltech101 | Food101 | PCam | Pets37 | CIFAR10 |
|---------------|:-------:|:-----:|:----------:|:-------:|:----:|:------:|:-------:|
| Rate [bits] | 1468 | 1344 | 1341 | 1269 | 1491 | 1211 | 1408 |
| Test Acc. [%] | 79.9 | 98.7 | 93.7 | 83.6 | 81.1 | 88.3 | 94.8 |
| | Cars196 | STL10 | Caltech101 | Food101 | PCam | Pets37 | CIFAR10 | CIFAR100 |
|---------------|:-------:|:-----:|:----------:|:-------:|:----:|:------:|:-------:|:--------:|
| Rate [bits] | 1471 | 1342 | 1340 | 1266 | 1491 | 1209 | 1407 | 1413 |
| Test Acc. [%] | 80.3 | 98.5 | 93.3 | 83.8 | 81.1 | 88.8 | 94.6 | 79.0 |

Note: ImageNet is too large for training a SVM using SKlearn. You need to run MLP evaluation with `bin/clip/clip_bottleneck_mlp_eval`.
Note: ImageNet is too large for training a SVM using SKlearn. You need to run MLP evaluation with `bin/clip/clip_bottleneck_mlp_eval`. Also you have to download ImageNet manually.


## Cite
```

You can read the full paper [here](https://arxiv.org/abs/2106.10800). Please cite our paper if you use our model:

```bibtex
@inproceedings{
dubois2021lossy,
title={Lossy Compression for Lossless Prediction},
author={Yann Dubois and Benjamin Bloem-Reddy and Karen Ullrich and Chris J. Maddison},
booktitle={Neural Compression: From Information Theory to Applications -- Workshop @ ICLR 2021},
year={2021},
url={https://arxiv.org/abs/2106.10800}
dubois2021lossy,
title={Lossy Compression for Lossless Prediction},
author={Yann Dubois and Benjamin Bloem-Reddy and Karen Ullrich and Chris J. Maddison},
booktitle={Neural Compression: From Information Theory to Applications -- Workshop @ ICLR 2021},
year={2021},
url={https://arxiv.org/abs/2106.10800}
}
```
```
1 change: 1 addition & 0 deletions bin/clip/clip_bottleneck_linear_eval.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ kwargs_multi="
data@data_pred=stl10,cars196,caltech101,food101,pcam,pets37,cifar10,cifar100
"


if [ "$is_plot_only" = false ] ; then
for beta in "5e-02"
do
Expand Down
9 changes: 4 additions & 5 deletions lossyless/classical_compressors.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import logging

import PIL

import pytorch_lightning as pl
import torch
import torch.nn as nn
from compressai.utils.bench import codecs
from pytorch_lightning.core.decorators import auto_move_data
from torch.nn import functional as F
from torchvision.transforms import ToPILImage, ToTensor

import pytorch_lightning as pl
from compressai.utils.bench import codecs
from pytorch_lightning.core.decorators import auto_move_data

from .helpers import UnNormalizer, dict_mean, rename_keys_

__all__ = ["ClassicalCompressor"]
Expand Down Expand Up @@ -165,7 +165,6 @@ def __init__(self, hparams):

self.stage = self.hparams.stage # allow changing to stages

@auto_move_data # move data on correct device for inference
def forward(self, x, is_return_out=False, **kwargs):
"""Represents the data `x`.
Expand Down
4 changes: 2 additions & 2 deletions lossyless/learnable_compressors.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
from .architectures import get_Architecture
from .distortions import get_distortion_estimator
from .distributions import CondDist, Deterministic
from .helpers import BASE_LOG, Annealer, OrderedSet, Timer, append_optimizer_scheduler_
from .helpers import (BASE_LOG, Annealer, OrderedSet, Timer,
append_optimizer_scheduler_)
from .predictors import OnlineEvaluator
from .rates import get_rate_estimator

Expand Down Expand Up @@ -112,7 +113,6 @@ def predict_step(self, batch, batch_idx, dataloader_idx=None):
def predict(self, *args, **kwargs): # TODO remove in newer version of lightning
return self.predict_step(*args, **kwargs)

@auto_move_data # move data on correct device for inference
def forward(self, x, is_compress=False, is_features=None):
"""Represents the data `x`.
Expand Down
1 change: 0 additions & 1 deletion lossyless/predictors.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ def __init__(self, hparams, featurizer=None):

self.stage = self.hparams.stage

@auto_move_data # move data on correct device for inference
def forward(self, x, is_logits=True, is_return_logs=False):
"""Perform prediction for `x`.
Expand Down
5 changes: 5 additions & 0 deletions utils/Z_linear_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,10 @@ def main(cfg):
n_iter=cfg.clip_linear.n_predictors,
)

logger.info(f"Training classifier.")
clf.fit(Z, Y)

logger.info(f"Evaluating classifier.")
Y_pred = clf.predict(Z_test)

bacc = balanced_accuracy_score(Y_test, Y_pred)
Expand All @@ -91,6 +93,9 @@ def main(cfg):
analyser.results["test/pred/balanced_err"] = 1 - bacc

# save results
path = Path(analyser.cfgs[stage].paths.results)
path.mkdir(parents=True, exist_ok=True)
results_file = path / RESULTS_FILE.format(stage=stage)
test_res_rep = replace_keys(analyser.results, "test/", "")
tosave = dict(test=test_res_rep)
results = pd.DataFrame.from_dict(tosave)
Expand Down

0 comments on commit ad3fb3d

Please sign in to comment.