Code Monkey home page Code Monkey logo

lift's Introduction

Long-Tail Learning with Foundation Model: Heavy Fine-Tuning Hurts

This is the source code for the paper: Long-Tail Learning with Foundation Model: Heavy Fine-Tuning Hurts (ICML 2024).

Requirements

  • Python 3.8
  • PyTorch 2.0
  • Torchvision 0.15
  • Tensorboard

To install requirements, run:

conda create -n lift python=3.8 -y
conda activate lift
conda install pytorch==2.0.0 torchvision==0.15.0 pytorch-cuda=11.7 -c pytorch -c nvidia
conda install tensorboard
pip install -r requirements.txt

We encourage installing the latest dependencies. If there are any incompatibilities, please install the dependencies with the following versions.

numpy==1.24.3
scipy==1.10.1
scikit-learn==1.2.1
yacs==0.1.8
tqdm==4.64.1
ftfy==6.1.1
regex==2022.7.9
timm==0.6.12

Hardware

Most experiments can be reproduced using a single GPU with 20GB of memory (larger models such as ViT-L require more memory).

  • To further reduce the GPU memory cost, gradient accumulation is recommended. Please refer to Usage for detailed instructions.

Quick Start on the CIFAR-100-LT dataset

# run LIFT on CIFAR-100-LT (with imbalanced ratio=100)
python main.py -d cifar100_ir100 -m clip_vit_b16 adaptformer True

By running the above command, you can automatically download the CIFAR-100 dataset and run the method (LIFT).

Running on Large-scale Long-tailed Datasets

Prepare the Dataset

Download the dataset Places, ImageNet, and iNaturalist 2018.

Put files in the following locations and change the path in the data configure files in configs/data:

  • Places
Path/To/Dataset
├─ train
│  ├─ airfield
|  |  ├─ 00000001.jpg
|  |  └─ ......
│  └─ ......
└─ val
   ├─ airfield
   |  ├─ Places365_val_00000435.jpg
   |  └─ ......
   └─ ......
  • ImageNet
Path/To/Dataset
├─ train
│  ├─ n01440764
|  |  ├─ n01440764_18.JPEG
|  |  └─ ......
│  └─ ......
└─ val
   ├─ n01440764
   |  ├─ ILSVRC2012_val_00000293.JPEG
   |  └─ ......
   └─ ......
  • iNaturalist 2018
Path/To/Dataset
└─ train_val2018
   ├─ Actinopterygii
   |  ├─ 2229
   |  |  ├─ 2c5596da5091695e44b5604c2a53c477.jpg
   |  |  └─ ......
   |  └─ ......
   └─ ......

Reproduction

To reproduce the main result in the paper, please run

# run LIFT on ImageNet-LT
python main.py -d imagenet_lt -m clip_vit_b16 adaptformer True

# run LIFT on Places-LT
python main.py -d places_lt -m clip_vit_b16 adaptformer True

# run LIFT on iNaturalist 2018
python main.py -d inat2018 -m clip_vit_b16_peft adaptformer True num_epochs 20

For other experiments, please refer to scripts for reproduction commands.

Detailed Usage

To train and test the proposed method on more settings, run

python main.py -d [data] -m [model] [options]

The [data] can be the name of a .yaml file in configs/data, including imagenet_lt, places_lt, inat2018, cifar100_ir100, cifar100_ir50, cifar100_ir10, etc.

The [model] can be the name of a .yaml file in configs/model, including clip_rn50, clip_vit_b16, in21k_vit_b16, etc.

Note that using only -d and -m options denotes only fine-tuning the classifier. Please use additional [options] for more settings.

  • To apply lightweight fine-tuning methods, add options like lora True, adaptformer True, etc.

  • To apply test-time ensembling, add tte True.

Moreover, [options] can facilitate modifying the configure options in utils/config.py. Following are some examples.

  • To specify the root path of datasets, add root Path/To/Datasets.

  • To change the output directory, add an option like output_dir NewExpDir. Then the results will be saved in output/NewExpDir.

  • To assign a single GPU (for example, GPU 0), add an option like gpu 0.

  • To apply gradient accumulation, add micro_batch_size XX. This can further reduce GPU memory costs. Note that XX should be a divisor of batch_size.

  • To test an existing model, add test_only True. This option will test the model trained by your configure file. To test another model, add an additional option like model_dir output/AnotherExpDir.

  • To test an existing model on the training set, add test_train True.

You can also refer to scripts for example commands.

Acknowledgment

We thank the authors for the following repositories for code reference: [OLTR], [Classifier-Balancing], [Dassl], [CoOp].

Citation

If you find this repo useful for your work, please cite as:

@inproceedings{shi2024longtail,
  title={Long-Tail Learning with Foundation Model: Heavy Fine-Tuning Hurts},
  author={Jiang-Xin Shi and Tong Wei and Zhi Zhou and Jie-Jing Shao and Xin-Yan Han and Yu-Feng Li},
  booktitle={Proceedings of the 41st International Conference on Machine Learning},
  year={2024}
}

lift's People

Contributors

shijxcs avatar stomach-ache avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

lift's Issues

syntax error of start command

when executing "python main.py ..." in terminal on Windows, the terminal would not recognize clip_vit_b16 as the filename to be join with ".yaml", we should add a flag "-m" before it.

测试中训练集和测试集选择的问题

For the test at the end of training, why would you use the one loaded from the training set instead of the train_test_loader, instead of the test_loader loaded from the test set, and the accuracy of the test from the training set is not accurate, right?

Typo line 41 `peft_rn.py`

Just a note that there is a typo in peft_rn.py line 41. bias_list is declared instead of bn_list.

Thank you for your work. This repo has been very helpful.

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.