Code Monkey home page Code Monkey logo

tailcalibx's Introduction

TailCalibX : Feature Generation for Long-tail Classification

by Rahul Vigneswaran, Marc T. Law, Vineeth N. Balasubramanian, Makarand Tapaswi

[arXiv] [Code] [pip Package] [Video] TailCalibX methodology

Table of contents

🐣 Easy Usage (Recommended way to use our method)

⚠ Caution: TailCalibX is just TailCalib employed multiple times. Specifically, we generate a set of features once every epoch and use them to train the classifier. In order to mimic that, three things must be done at every epoch in the following order:

  1. Collect all the features from your dataloader.
  2. Use the tailcalib package to make the features balanced by generating samples.
  3. Train the classifier.
  4. Repeat.

πŸ’» Installation

Use the package manager pip to install tailcalib.

pip install tailcalib

πŸ‘¨β€πŸ’» Example Code

Check the instruction here for a much more detailed python package information.

# Import
from tailcalib import tailcalib

# Initialize
a = tailcalib(base_engine="numpy")   # Options: "numpy", "pytorch"

# Imbalanced random fake data
import numpy as np
X = np.random.rand(200,100)
y = np.random.randint(0,10, (200,))

# Balancing the data using "tailcalib"
feat, lab, gen = a.generate(X=X, y=y)

# Output comparison
print(f"Before: {np.unique(y, return_counts=True)}")
print(f"After: {np.unique(lab, return_counts=True)}")

πŸ§ͺ Advanced Usage

βœ” Things to do before you run the code from this repo

  • Change the data_root for your dataset in main.py.
  • If you are using wandb logging (Weights & Biases), make sure to change the wandb.init in main.py accordingly.

πŸ“€ How to use?

  • For just the methods proposed in this paper :
    • For CIFAR100-LT: run_TailCalibX_CIFAR100-LT.sh
    • For mini-ImageNet-LT : run_TailCalibX_mini-ImageNet-LT.sh
  • For all the results show in the paper :
    • For CIFAR100-LT: run_all_CIFAR100-LT.sh
    • For mini-ImageNet-LT : run_all_mini-ImageNet-LT.sh

πŸ“š How to create the mini-ImageNet-LT dataset?

Check Notebooks/Create_mini-ImageNet-LT.ipynb for the script that generates the mini-ImageNet-LT dataset with varying imbalance ratios and train-test-val splits.

βš™ Arguments

  • --seed : Select seed for fixing it.

    • Default : 1
  • --gpu : Select the GPUs to be used.

    • Default : "0,1,2,3"
  • --experiment: Experiment number (Check 'libs/utils/experiment_maker.py').

    • Default : 0.1
  • --dataset : Dataset number.

    • Choices : 0 - CIFAR100, 1 - mini-imagenet
    • Default : 0
  • --imbalance : Select Imbalance factor.

    • Choices : 0: 1, 1: 100, 2: 50, 3: 10
    • Default : 1
  • --type_of_val : Choose which dataset split to use.

    • Choices: "vt": val_from_test, "vtr": val_from_train, "vit": val_is_test
    • Default : "vit"
  • --cv1 to --cv9 : Custom variable to use in experiments - purpose changes according to the experiment.

    • Default : "1"
  • --train : Run training sequence

    • Default : False
  • --generate : Run generation sequence

    • Default : False
  • --retraining : Run retraining sequence

    • Default : False
  • --resume : Will resume from the 'latest_model_checkpoint.pth' and wandb if applicable.

    • Default : False
  • --save_features : Collect feature representations.

    • Default : False
  • --save_features_phase : Dataset split of representations to collect.

    • Choices : "train", "val", "test"
    • Default : "train"
  • --config : If you have a yaml file with appropriate config, provide the path here. Will override the 'experiment_maker'.

    • Default : None

πŸ‹οΈβ€β™‚οΈ Trained weights

Experiment CIFAR100-LT (ResNet32, seed 1, Imb 100) mini-ImageNet-LT (ResNeXt50)
TailCalib Git-LFS Git-LFS
TailCalibX Git-LFS Git-LFS
CBD + TailCalibX Git-LFS Git-LFS

πŸͺ€ Results on a Toy Dataset

Open In Colab

The higher the Imb ratio, the more imbalanced the dataset is. Imb ratio = maximum_sample_count / minimum_sample_count.

Check this notebook to play with the toy example from which the plot below was generated.

🌴 Directory Tree

TailCalibX
β”œβ”€β”€ libs
β”‚   β”œβ”€β”€ core
β”‚   β”‚   β”œβ”€β”€ ce.py
β”‚   β”‚   β”œβ”€β”€ core_base.py
β”‚   β”‚   β”œβ”€β”€ ecbd.py
β”‚   β”‚   β”œβ”€β”€ modals.py
β”‚   β”‚   β”œβ”€β”€ TailCalib.py
β”‚   β”‚   └── TailCalibX.py
β”‚   β”œβ”€β”€ data
β”‚   β”‚   β”œβ”€β”€ dataloader.py
β”‚   β”‚   β”œβ”€β”€ ImbalanceCIFAR.py
β”‚   β”‚   └── mini-imagenet
β”‚   β”‚       β”œβ”€β”€ 0.01_test.txt
β”‚   β”‚       β”œβ”€β”€ 0.01_train.txt
β”‚   β”‚       └── 0.01_val.txt
β”‚   β”œβ”€β”€ loss
β”‚   β”‚   β”œβ”€β”€ CosineDistill.py
β”‚   β”‚   └── SoftmaxLoss.py
β”‚   β”œβ”€β”€ models
β”‚   β”‚   β”œβ”€β”€ CosineDotProductClassifier.py
β”‚   β”‚   β”œβ”€β”€ DotProductClassifier.py
β”‚   β”‚   β”œβ”€β”€ ecbd_converter.py
β”‚   β”‚   β”œβ”€β”€ ResNet32Feature.py
β”‚   β”‚   β”œβ”€β”€ ResNext50Feature.py
β”‚   β”‚   └── ResNextFeature.py
β”‚   β”œβ”€β”€ samplers
β”‚   β”‚   └── ClassAwareSampler.py
β”‚   └── utils
β”‚       β”œβ”€β”€ Default_config.yaml
β”‚       β”œβ”€β”€ experiments_maker.py
β”‚       β”œβ”€β”€ globals.py
β”‚       β”œβ”€β”€ logger.py
β”‚       └── utils.py
β”œβ”€β”€ LICENSE
β”œβ”€β”€ main.py
β”œβ”€β”€ Notebooks
β”‚   β”œβ”€β”€ Create_mini-ImageNet-LT.ipynb
β”‚   └── toy_example.ipynb
β”œβ”€β”€ readme_assets
β”‚   β”œβ”€β”€ method.svg
β”‚   └── toy_example_output.svg
β”œβ”€β”€ README.md
β”œβ”€β”€ run_all_CIFAR100-LT.sh
β”œβ”€β”€ run_all_mini-ImageNet-LT.sh
β”œβ”€β”€ run_TailCalibX_CIFAR100-LT.sh
└── run_TailCalibX_mini-imagenet-LT.sh

Ignored tailcalib_pip as it is for the tailcalib pip package.

πŸ“ƒ Citation

@inproceedings{rahul2021tailcalibX,
    title   = {{Feature Generation for Long-tail Classification}},
    author  = {Rahul Vigneswaran and Marc T. Law and Vineeth N. Balasubramanian and Makarand Tapaswi},
    booktitle = {ICVGIP},
    year = {2021}
}

πŸ‘ Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

❀ About me

Rahul Vigneswaran

✨ Extras

🐝 Long-tail buzz : If you are interested in deep learning research which involves long-tailed / imbalanced dataset, take a look at Long-tail buzz to learn about the recent trending papers in this field.

πŸ“ License

MIT

tailcalibx's People

Contributors

rahulvigneswaran 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

Watchers

 avatar  avatar  avatar  avatar

tailcalibx's Issues

Creatin mini ImageNet_LT

Hi Rahul,
Just wondering if you could help with the following error in creating a mini imageNet_LT dataset. I am using imageNet2012 which has 2 directories,
imageNet
train and val.
ImageNet Directory pic is also provided below the error message.
Error
(base) [Angelina@rob-gpu Notebooks]$ python create_mini_ImageNet_LT.py
Traceback (most recent call last):
File "create_mini_ImageNet_LT.py", line 69, in
train_x, test_x, train_y, test_y = train_test_split(final_1, labels_1, train_size=train_split_ratio, test_size=test_split_ratio, stratify=labels_1)
File "/home/Angelina/anaconda3/lib/python3.8/site-packages/sklearn/model_selection/_split.py", line 2130, in train_test_split
n_train, n_test = _validate_shuffle_split(n_samples, test_size, train_size,
File "/home/Angelina/anaconda3/lib/python3.8/site-packages/sklearn/model_selection/_split.py", line 1810, in _validate_shuffle_split
raise ValueError( ValueError: With n_samples=0, test_size=0.19999999999999996 and train_size=0.8, the resulting train set will be empty. Adjust any of the aforementioned parameters.

maybe a bug

self.batch_forward(inputs, labels, phase="train", retrain=retrain)
image

Memory issues?

Hello, after training, is TailClaib implemented on the whole dataset at once? Or is it implemented batch-wise?

If it implemented at once, then it takes up a ridiculous amount of memory and is unable to run.
While I am implementing it batch-wise, there is a separate issue. There are some batches which only have a single instance in some classes. This gives a zero division error while calculating the co-variance. How to deal with this?

The experiments of CE+TailCalib

Thanks for your excellent work!
But I have a question about experiments of CE+TailCalib. I want to find this experiment, but it seems that not including in below.
So how can I find the information about this experiment? Thank you!
image

BUG in TailCalibX.py

in TailCalibX.py, it inherit the generate_points(self) function, but in that, it cant generate featrue and store for multi-time:
image

question about cosineCE

I have run the CIFAR-100-Imb100 experiment_no=0.2, i got this results which is different from that in the paper.

CIFAR100 Imb100 val-toatal val-many val-med val-few
Seed1 CosineCE 0.403 0.676 0.379 0.112
Seed2 CosineCE 0.403 0.670 0.396 0.101
Seed3 CosineCE 0.405 0.680 0.380 0.113
Seed10 CosineCE 0.4051 0.6737 0.3923 0.1067
Seed20 CosineCE 0.3968 0.6754 0.3909 0.7867
Seed30 CosineCE 0.4045 0.6811 0.378 0.1127

Missing torch import in shuffle_all

The current pip release of tailcalib raises NameError: name 'torch' is not defined when using base_engine="pytorch".

Fix:

Add missing import here:

if self.base_engine == "numpy":
    import numpy as np
    index = np.random.permutation(x.shape[0])
else:
    import torch
    index = torch.randperm(x.size(0))

Add missing shuffle condition at lines here and here:

if self.shuffle:
    feat_all, labs_all = self.shuffle_all(feat_all, labs_all)

File Not found error

Hi, while executing the code, it generates the following error. The file is also present in the current directory. I was just wondering if anyone could help with this.

image

2 errors while reproducing the results

Hi Rahul,
Thanks for such excellent work, could you please resolve the following 2 errors:

File "main.py", line 191, in
training_model.accumulate(phase="train", save=args.save_features)
TypeError: accumulate() got an unexpected keyword argument 'save'
wandb: Waiting for W&B process to finish... (failed 1).

The second error is, it cant find the Pretrain path :

===> Saving cfg parameters to: /data/angelina/Experimentaltesults/TailCalibration/logs/vit/CIFAR100_LT/100/CBD+TailCalibX_CIFAR100_LT_imb_100_resnet32/cfg.yaml
Using steps for training.
Loading Softmax Loss.
Using 1 GPUs.
Loading Cosine Dot Product Classifier.
Traceback (most recent call last):
File "main.py", line 171, in
training_model = source_import(config["core"]).get_core(config, data)
File "./libs/core/TailCalibX.py", line 311, in get_core
return model(*args)
File "/home/angelina/ TailCalibration/TailCalib_Feature_Generation/libs/core/core_base.py", line 54, in init
self.init_models()
File "/home/angelina/ TailCalibration /TailCalib_Feature_Generation/libs/core/core_base.py", line 76, in init_models
self.networks[key] = source_import(def_file).create_model(**model_args)
File "./libs/models/CosineDotProductClassifier.py", line 48, in create_model
raise Exception(f"Pretrain path doesn't exist!!-{pretrain_dir}")
Exception: Pretrain path doesn't exist!!-/data/angelina/TailCalibration/logs/vit/CIFAR100_LT/100/CBD_CIFAR100_LT_imb_100_resnet32/alpha_0.8,beta_100.0_normal_k_1_aug_k_0/final_model_checkpoint.pth
wandb: Waiting for W&B process to finish... (failed 1).

backbone training

thx for your great work!i have a question:how do you train a backbone? just class aware sampler + cosineCE?

Good job

I really like this paper, though it does what I want to do. \facepalm

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.