Giter Club home page Giter Club logo

bufr's Introduction

Bottom-Up Feature Restoration

This repository contains the code accompanying the paper Source-Free Adaptation to Measurement Shift via Bottom-Up Feature Restoration (ICLR 2022, Spotlight).

Prerequisites:

  • python 3
  • pytorch 1.7.0+
  • WILDS (for Camelyon-17)
  • numpy, scipy, sklearn, matplotlib, PIL, torchvision

Data

By default datasets are assumed to be located in a ./datasets/ directory, this can be changed with the data-root flag in the shell scripts used to run experiments. The table below shows where the datasets can be downloaded from and the path where they should be stored. To start running the code only EMNIST-DA should be required.

Dataset Download Location
EMNIST-DA Data <data-root>/EMNIST/
MNIST-C Data <data-root>/mnist_c/
MNIST-M Set download=True in mnistm.py or here <data-root>/MNISTM/
CIFAR-10-C Data <data-root>/CIFAR-10-C/
CIFAR-100-C Data <data-root>/CIFAR-100-C/
CAMELYON-17 Downloads automatically (download=True) from WILDS <data-root>/CAMELYON17/

Simple Usage

To run Bottom-Up Feature Restoration there are three steps:

  1. Pretrain the source model
./scripts/pretrain.sh
  1. Save the source statistics
./scripts/save_train_stats.sh
  1. Adapt on the new data
./scripts/bufr.sh

Other (not bottom-up) methods, including source-only performance, AdaBN and feature restoration can be run after steps 1 & 2 by runnning

./scripts/adapt.sh

Further Information

Choosing what to run

In the configs directory are yaml files that set hyperparameters for the different methods for each dataset

  • configs/<DATASET_NAME>/dataset.yml sets arguments used for all methods across a dataset, namely the network architecture and which shifts to train on.

  • configs/<DATASET_NAME>/<method>.yml sets the specific hyperparameters for the different methods (learning rate, epochs etc.)

The shell scripts in the scripts directory set which method to run, which dataset to use and some global arguments. To choose a specific dataset the following arguments should be set where applicable

  • --alg-config to ./configs/<MDATASET_NAME>/pretrain.yml
  • --alg-configs-dir to ./configs/<DATASET_NAME>/
  • --data-config to` ./configs/<DATASET_NAME>/dataset.yml

Possible values for <DATASET_NAME> are: EMNIST-DA, MNIST-C, CIFAR-10-C, CIFAR-100-C, CAMELYON17. (The directory names inside configs.)

To choose a specific method in scripts/adapt.sh set --alg-name to the name of the method. Possible values for --alg-name are:

  • all - trains all methods for the dataset
  • fewshot - for EMNIST-DA and CAMELYON17 trains the few-shot methods
  • Any one of: adabn, bnm, bnm_im, fr, im, jg, label, pl, shot, source_only. (The names of the yaml files inside configs/<DATASET_NAME>/. Not all methods are implemented for all datasets.)

The same random seed should be used for all steps (pretraining, saving statistics and adapting), this can be set with the --seed flag.

SHOT baseline

SHOT uses different pretraining techniques, to run this baseline on EMNIST-DA or MNIST-C requires first rerunning ./scripts/pretrain.sh on with the shot_pretrain argument set to True in pretrain.yml.

Analyses

The analysis directory contains files for creating analysis plots and calculating the feature restoration score. These analyses require an adapted models to be saved in advance, this can be done by adding --save-adapted-model to the adapt.sh and bufr.sh scripts during training.

Analysis files can be run using the scripts in ./scripts/analysis and changing the PYTHONPATH appropriately. The files calibration.py, max_patches.py and tsne.py contain hardcoded paths at the start of the files that you may wish to change.

Results

The results we achieved from running this code can be found here.

Citation

@inproceedings{eastwood2022sourcefree,
     title={Source-Free Adaptation to Measurement Shift via Bottom-Up Feature Restoration}, 
     author={Cian Eastwood and Ian Mason and Christopher K. I. Williams and Bernhard Sch\"olkopf},
     booktitle={International Conference on Learning Representations},
     year={2022}
}

Readers may also be interested in our related study on Unit-level surprise in neural networks.

bufr's People

Contributors

cianeastwood avatar

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.