Code Monkey home page Code Monkey logo

robustness's Introduction

PWC PWC PWC

Robustness evaluation and Adaptation of ImageNet models

This repo contains a growing collection of helper functions, tools and methods for robustness evaluation and adaptation of ImageNet scale models. The focus is on simple methods that work at scale.

We currently have the following features available:

  • examples/batchnorm: A reference implementation of batch norm adaptation used by Schneider, Rusak et al. (NeurIPS 2020)
  • examples/selflearning: A reference implementation of self learning with robust pseudo labeling used by Rusak, Schneider et al. (arxiv 2021)
  • examples/imagenet_d: Example runs on the ImageNet-D dataset used by Rusak, Schneider et al. (arxiv 2021)

Planned features for future releases are (please open an issue if you can think of additional interesting parts to add):

  • Helper functions for robustness datasets like ImageNet-A, ImageNet-R and ImageNet-C
  • examples/clip: Robustness evaluation for CLIP, Radford et al. (2021)
  • examples/dino: Robustness evaluation for DINO, Caron et al. (2021)

News

  • May '21: We will present our work on self-learing as a contributed talk at the WeaSuL 2021 workshop at ICLR.
  • April '21: The pre-print for "Adapting ImageNet-scale models to complex distribution shifts with self-learning" is now available on arXiv: arxiv.org/abs/2104.12928
  • September 2020: The BatchNorm adaptation paper was accepted for poster presentation at NeurIPS 2020.
  • July '20: A shorter workshop version of the paper was accepted for oral presentation at the Uncertainty & Robustness in Deep Learning Workshop at ICML 2020.
  • June '20: The pre-print for "Improving robustness against common corruptions by covariate shift adaptation" is available on arXiv: arxiv.org/abs/2006.16971.pdf

☕ The robusta toolbox for Robustness and Adaptation

Motivation

Besides reference implementations, this repo is mainly intended to provide a quick and easy way to adapt your own code. In particular, when developing new methods for improving robustness on deep learning models, we find it interesting to report results after adapting your model to the test datasets. This paints a more holistic image of model robustness: Some people might be interested in ad-hoc model performance, other might be interested in the performance obtained in a transductive inference setting.

Note that the package is not intended for general purpose domain adaptation. Instead, we focus on providing simple methods that prove to be effective for ImageNet scale model adaptation at test time. The package provides helper functions that are "minimally invasive" and can easily be added to existing source code for model evaluation.

Quick Start

You can install the package by running

pip install robusta

Depending on your system setup, it can make sense to first manually install the correct torch and torchvision versions as described on the PyTorch website.

Here is an example for how to use robusta for batchnorm adaptation & robust pseudo-labeling.

    model = torchvision.models.resnet50(pretrained=True)

    # We provide implementations for ImageNet-val, ImageNetC, ImageNetR,
    # ImageNetA and ImageNetD:
    val_dataset = robusta.datasets.imagenetc.ImageNetC(
        root=dataset_folder, corruption="gaussian_blur", severity=1,
        transform=transforms.Compose([transforms.ToTensor()])
        )
    val_loader = torch.utils.data.DataLoader(
        val_dataset, batch_size=batch_size, shuffle=True)

    # We offer different options for batch norm adaptation;
    # alternatives are "ema", "batch_wise_prior", ...
    robusta.batchnorm.adapt(model, adapt_type="batch_wise")

    # The accuracy metric can be specific to the dataset:
    # For example, ImageNet-R requires remapping into 200 classes.
    accuracy_metric = val_dataset.accuracy

    # You can also easily use self-learning in your model.
    # Self-learning adaptation can be combined with batch norm adaptation, example:
    parameters = robusta.selflearning.adapt(model, adapt_type="affine")
    optimizer = torch.optim.SGD(parameters, lr=1e-3)

    # You can choose from a set of adaptation losses (GCE, Entropy, ...)
    rpl_loss = robusta.selflearning.GeneralizedCrossEntropy(q=0.8)

    acc1_sum, acc5_sum, num_samples = 0., 0., 0.
    for epoch in range(num_epochs):
        predictions = []
        for images, targets in val_loader:

            logits = model(images)
            predictions = logits.argmax(dim=1)

            # Predictions are optional. If you do not specify them,
            # they will be computed within the loss function.
            loss = rpl_loss(logits, predictions)

            # When using self-learning, you need to add an additional optimizer
            # step in your evaluation loop.
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

            acc1_sum, acc5_sum += accuracy_metric(predictions, targets, topk=(1,5))
            num_samples += len(targets)
            print(f"Top-1: {acc1_sum/num_samples}, Top-5: {acc5_sum/num_samples}")

Example Implementations

Batch Norm Adaptation

PWC PWC

[Paper] [Web] [README] [Implementation]

We propose to go beyond the assumption of a single sample from the target domain when evaluating robustness. Re-computing BatchNorm statistics is a simple baseline algorithm for improving the corruption error up to 14% points over a wide range of models, when access to more than a single sample is possible.

Self-Learning

PWC PWC PWC

[Paper] [Web] [README] [Implementation]

Test-time adaptation with self-learning improves robustness of large-scale computer vision models on ImageNet-C, -R, and -A.

Robustness evaluation of DINO models

[Blog Post] [Implementation coming soon]

License

Unless noted otherwise, code in this repo is released under an Apache 2.0 license. Some parts of the implementation use third party code. We typically indicate this in the file header or in the methods directly, and include the original license in the NOTICE file.

This repo does not contain the full code-base used in Rusak, Schneider et al. (2021) and is instead currently limited to a reference re-implementation for robust-pseudo labeling and entropy minimization. A full version of the codebase might be independently released in the future.

If you want to use part of this code commercially, please carefully check the involved parts. Part of the third-party implementations might be released under licences with a non-commercial use clause such as CC-NC. If in doubt, please reach out.

Contact

Please reach out for feature requests. Contributions welcome!

Note: The current version of this code base is a work in progress. We still decided to do this pre-release since the core methods are conceptually easy to use in your own code (batch norm adaptation, self-learning, ... and the current state might already be a useful place to start.

robustness's People

Contributors

evgeniaar avatar stes avatar

Stargazers

LinGray avatar Yi Li avatar tian avatar Derek Everett avatar Jeff Carpenter avatar MasterNL avatar Wenyu Shu avatar Yanwei Liu avatar  avatar Xinyao Wu avatar Ivan Ruchkin avatar kiwanPark avatar Hyesu Lim avatar Jordan Kanter avatar Hong Jia avatar ChG avatar  avatar Haneol Jang avatar Daehoon Gwak avatar Bubble@Learning avatar Thomas Friedel avatar Ruiming Guo (郭睿明) avatar  avatar Fin Amin avatar Shuai Zhao avatar  avatar Sameer Ambekar avatar  avatar Zheyuan Zhang avatar Yinqi Li avatar Sai Srinivas Kancheti avatar Lee Minyoung avatar  avatar Rodrigo Kobashikawa avatar Pengkun Jiao avatar  avatar Mohammad Reza Taesiri avatar  avatar  avatar  avatar Max avatar Nav avatar Zhiheng Li avatar Theodoros Ntakouris avatar Zahra'a Hamwi avatar Naman Gupta avatar George Pachitariu avatar Yi Chenyu Ian avatar Mingjie Sun avatar Kaushal Jadhav avatar Calvin-Khang Ta avatar Swarnashree avatar  avatar Yuning Lu avatar Aashiq Muhamed avatar Xi Wang avatar Andrea Panizza avatar Honglin Mu avatar Khushdeep-singh avatar  avatar Yongxing Dai avatar omar shabab avatar slyviacassell avatar Jialong Wu avatar Andrei Bursuc avatar  avatar  avatar justin avatar Sanket Sharma avatar Akshay Kulkarni avatar  avatar Daehan Kim avatar 杨志毅 avatar zero alpha avatar Gianluca Scarpellini avatar tim avatar Jacob A Rose avatar  avatar seominseok avatar Yusuke Iwasawa avatar  avatar  avatar 曾逸夫(Zeng Yifu) avatar David Marx avatar Victor Turrisi avatar  avatar LiuZhuang avatar  avatar wuxiaolian avatar  avatar Roger GOU avatar Enze Xie avatar Xa9aX ツ avatar  avatar Tao BAI avatar 爱可可-爱生活 avatar Minghui Chen avatar Ivan Ustyuzhaninov avatar  avatar Kantharaju C N avatar

Watchers

Saif avatar  avatar James Cloos avatar Behar Veliqi avatar  avatar Rindra Ramamonjison avatar Jonas Rauber avatar Wieland Brendel avatar  avatar Fatemeh Azimi avatar Kantharaju C N avatar Auguste Schulz avatar  avatar  avatar Puneet Mangla avatar Cagri Demir avatar

robustness's Issues

Train all parameters or only BN layers

As I understand from the paper, you intend to only train the params of the BN layers. Is this what you are trying to do at this line? If so please be aware that module.eval() does not freeze the module’s params. If this is not what you are trying to do please kindly let me know what is the purpose of the mentioned line.

Thanks

performing self-learning and accuracy measuring on Imagenet-A

Does it make sense to perform self-learning on Imagenet-A? I tried implementing it but somehow the accuracy never goes above 0.2%

my code looks like this after combining Imagenet-C code from here
https://github.com/bethgelab/robustness/blob/main/examples/selflearning/main.py
with the eval-code from
https://github.com/hendrycks/natural-adv-examples/blob/master/eval.py

now looking like this:

    imga = ImageNetA() # as defined here https://github.com/bethgelab/robustness/blob/main/robusta/datasets/imageneta.py
    # but fixed every attribute to self.attribute
    for epoch in range(num_epochs):
        predictions = []
        for images, targets in val_loader:

            logits = model(images.cuda())[:,imga.indices_in_1k] # fixed for correct accuracy metric?
            predictions = logits.data.max(1)[1]
            loss = gce(logits, predictions, q = gce_q)

            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

            num_correct += predictions.eq(targets.data).sum().item()
            confidence.extend(to_np(F.softmax(logits, dim=1).max(1)[0]).squeeze().tolist())
            correct.extend(predictions.eq(targets).to('cpu').numpy().squeeze().tolist())
            num_samples += len(targets)
            print(f"Correct: {num_correct:#5.0f}/{num_samples:#5.0f} ({100 * num_correct / num_samples:.2f} %)")

Is my approach wrong? should I use an adapted accuracy metric like in imagenet-R?
I believe I had fixed for that with the line of code only retaining the relevant logits (see above)...

error in installation

Dear Authors,
Thanks for the wonderful repo. Could you take a look at this installation issue I encountered when running 'pip install --verbose git+git://github.com/bethgelab/robustness.git'?
'''
Fetching project page and analyzing links: https://pypi.ngc.nvidia.com/pip/
Getting page https://pypi.ngc.nvidia.com/pip/
Found index url https://pypi.ngc.nvidia.com
Starting new HTTPS connection (1): pypi.ngc.nvidia.com:443
https://pypi.ngc.nvidia.com:443 "GET /pip/ HTTP/1.1" 301 0
Starting new HTTPS connection (1): developer.download.nvidia.com:443
https://developer.download.nvidia.com:443 "GET /compute/redist/pip/ HTTP/1.1" 404 445
Could not fetch URL https://pypi.ngc.nvidia.com/pip/: 404 Client Error: Not Found for url: https://developer.download.nvidia.com/compute/redist/pip/ - skipping
Given no hashes to check 209 links for project 'pip': discarding no candidates
Removed build tracker: '/tmp/pip-req-tracker-b4ndlbqj'
'''
Thank you

Code release.

Hello! I'm interested in seeing your implementation of the paper. When do you plan on releasing the code? Thanks!

issues in test_datasets.py

Hello, Schneider

I came across some questions during reproducing this work and need your help:

  • When I set cwd as "/robustness", and run the following command python examples/batchnorm/src/evaluate.py --imagenet-path imagenet/, I came across a ModuleNotFoundError: No module named robusta. I add /robusta into sys.path to solve this problem. I guess it is maybe because forgetting to change the relative path after the movement of evaluate.py to src. Of course, maybe you are right and I don't know some specific implementation under this situation.

  • I want to ask the difference between EMABatchNorm and PartlyAdaptiveBN when setting adapt_mean and adapt_var as True. I think they both use testing set statistics, but the momentum of EMABatchNorm is not zero, while the momentum of PartlyAdaptiveBN is zero.

Thanks in advance!

Code for reproducing Mixed IN-C dataset

Hello,
Where can I find the code for creating the Mixed IN-C dataset as mentioned in Section 6 of the paper "Improving robustness against common corruptions by covariate shift adaptation"?

Thanks in advance.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.