Giter Club home page Giter Club logo

bbn's Introduction

BBN: Bilateral-Branch Network with Cumulative Learning for Long-Tailed Visual Recognition

Boyan Zhou, Quan Cui, Xiu-Shen Wei*, Zhao-Min Chen

This repository is the official PyTorch implementation of paper BBN: Bilateral-Branch Network with Cumulative Learning for Long-Tailed Visual Recognition. (The work has been accepted by CVPR2020, Oral Presentation)

Main requirements

  • torch == 1.0.1
  • torchvision == 0.2.2_post3
  • tensorboardX == 1.8
  • Python 3

Environmental settings

This repository is developed using python 3.5.2/3.6.7 on Ubuntu 16.04.5 LTS. The CUDA nad CUDNN version is 9.0 and 7.1.3 respectively. For Cifar experiments, we use one NVIDIA 1080ti GPU card for training and testing. (four cards for iNaturalist ones). Other platforms or GPU cards are not fully tested.

Pretrain models for iNaturalist

We provide the BBN pretrain models of both 1x scheduler and 2x scheduler for iNaturalist 2018 and iNaturalist 2017.

iNaturalist 2018: Baidu Cloud, Google Drive

iNaturalist 2017: Baidu Cloud, Google Drive

Usage

# To train long-tailed CIFAR-10 with imbalanced ratio of 50:
python main/train.py  --cfg configs/cifar10.yaml     

# To validate with the best model:
python main/valid.py  --cfg configs/cifar10.yaml

# To debug with CPU mode:
python main/train.py  --cfg configs/cifar10.yaml   CPU_MODE True

You can change the experimental setting by simply modifying the parameter in the yaml file.

Data format

The annotation of a dataset is a dict consisting of two field: annotations and num_classes. The field annotations is a list of dict with image_id, fpath, im_height, im_width and category_id.

Here is an example.

{
    'annotations': [
                    {
                        'image_id': 1,
                        'fpath': '/home/BBN/iNat18/images/train_val2018/Plantae/7477/3b60c9486db1d2ee875f11a669fbde4a.jpg',
                        'im_height': 600,
                        'im_width': 800,
                        'category_id': 7477
                    },
                    ...
                   ]
    'num_classes': 8142
}

You can use the following code to convert from the original format of iNaturalist. The images and annotations can be downloaded at iNaturalist 2018 and iNaturalist 2017

# Convert from the original format of iNaturalist
python tools/convert_from_iNat.py --file train2018.json --root /home/iNat18/images --sp /home/BBN/jsons

Citing this repository

If you find this code useful in your research, please consider citing us:

@article{zhou2020BBN,
	title={{BBN}: Bilateral-Branch Network with Cumulative Learning for Long-Tailed Visual Recognition},
	author={Boyan Zhou and Quan Cui and Xiu-Shen Wei and Zhao-Min Chen},
	booktitle={CVPR},
	pages={1--8},
	year={2020}
}

Contacts

If you have any questions about our work, please do not hesitate to contact us by emails.

Xiu-Shen Wei: [email protected]

Boyan Zhou: [email protected]

Quan Cui: [email protected]

bbn's People

Contributors

dependabot[bot] avatar zhouboyan 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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

bbn's Issues

feature compactness calculation

Hi,

thanks for the great work! I have a question about how to compute feature compactness as shown in Figure 6 in the original paper. What I did is to get resnet features (4096 dimensional tensor) before FC layer for each ground truth class, and compute the averaged distance with it's centroid. But the distances I got is generally greater than 50, even 100, as opposed to what's in the paper (<0.5).

Could you share how you computed the compactness for each class?

Thanks,
Issac

cifar10 results and imb_type

If I follow the default setting and run python main/train.py --cfg configs/cifar10.yaml, I cannot achieve similar results as reported in your paper.

I just achieve as below:
Epoch:200 Batch: 0/109 Batch_Loss:0.223 Batch_Accuracy:98.27%
Epoch:200 Batch:100/109 Batch_Loss:0.195 Batch_Accuracy:99.03%
---Epoch:200/200 Avg_Loss:0.220 Epoch_Accuracy:98.77% Epoch_Time: 0.13min---
------- Valid: Epoch:200 Valid_Loss:0.930 Valid_Acc:79.48%-------
--------------Best_Epoch:200 Best_Acc:79.48%--------------
-------------------Train Finished :BBN.CIFAR10.res32.200epoch-------------------
Would you like to help me to reproduce your results?
Furthermore,
What does mean imb_type in lib/dataset/imbalance_cifar.py? Does it be mentioned in your paper? What should I set to reproduce your results?

Looking forward to your reply!

Does the setting in this code correspond to Table 1 result on CIFAR ?

I use the default cifar10.yaml, cifar100.yaml files with imbalance ratio 100 and train from scatch with this codebase. however, I only get 76.58% on CIFAR10 and 41.56% on CIFAR100, which is far from 79.82% on CIFAR10 and 42.56% on CIFAR100 in the paper. So, if there are any differences between the training setting in this codebase and the training setting corresponding to paper results on CIFAR ?

how to get long-tail cifar

Dear Professor,
I want to run a long tail cifar10 on the resnet-32 model with backbone

  1. I want to know how to get the long-tail cifar10 dataset, can I get the long-tail data directly by run python train.py?
    or need to run imbalance_cifar.py first?

  2. How should I do if I want to run the CE experiment baseline in the article?

is it possible that I implement this method using Keras?

I cannot use pytorch to do my research for some reason., so I'm trying to "translate" the code to keras form. Now I've completed the dataloader (data generator in Keras) part, but I'm stuck with putting both balance sample data and reverse sample data in the backbone model to get 2 features.
How can I use Keras "fit" or "compile" to process both forms of data? Or is it possible to use the method using Keras?

AttributeError: 'IMBALANCECIFAR10' object has no attribute 'data'

Hi,I meet "AttributeError" when running "train.py". Could you please tell me how to fix it ?

Downloading https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz to ./datasets/imbalance_cifar10/cifar-10-python.tar.gz
212664376it [00:19, 38731722.29it/s]Traceback (most recent call last):
File "/xinfu/code/long_tail/BBN/main/train.py", line 69, in
train_set = eval(cfg.DATASET.DATASET)("train", cfg)
File "/xinfu/code/long_tail/BBN/lib/dataset/imbalance_cifar.py", line 25, in init
img_num_list = self.get_img_num_per_cls(self.cls_num, imb_type, imb_factor)
File "/xinfu/code/long_tail/BBN/lib/dataset/imbalance_cifar.py", line 44, in get_img_num_per_cls
img_max = len(self.data) / cls_num
AttributeError: 'IMBALANCECIFAR10' object has no attribute 'data'

How can I use this method in yolov5?

I want to train my dataset with yolov5, but my dataset has a long tail problem. Some categories have little data. Can I use your method to solve this problem? if can't,please give me some advice,thanks a lot

About the implementation of in the cumulative learning stage

I'm confused about the implementation of fusing features from two branches. In the combiner.py of your code, it seems that features from two branchs are mixed by concatenating. (Related codes are as follows)

        mixed_feature = 2 * torch.cat((l * feature_a, (1-l) * feature_b), dim=1)
        output = model(mixed_feature, classifier_flag=True)
        loss = l * criterion(output, label_a) + (1 - l) * criterion(output, label_b)

However, in the section 4.1 of your paper, it's said that the two features are integrated by element-wise addition.
image

Is the feature fusing conducted by concatenating or element-wise addition? Or am I misunderstanding anything?(Correct me if I'm wrong) Looking forward to your reply.

Difference between paper and code

Screen Shot 2019-12-27 at 5 14 59 PM

the corresponding code is

mixed_feature = 2 * torch.cat((l * feature_a, (1 - l) * feature_b), dim=1)
output = model(mixed_feature, classifier_flag=True)

according to the code, the target introduced in section 4.3 may can not achieve, as the feature is concatenated followed by only one classifer.

Cumulative learning strategy is proposed to shift the learning focus between the bilateral branches by controlling the weights for features produced by two branches and the classification loss L.

would you mind telling me the reason behind this change?

about train2018.json

Hi, I have downloaded iNaturalist2018, but I couldn't download the train_2018.json and val_2018.json. Would you mind to share the two .json files? Thanks in advance!

load model from pretrain model error

I encounter errors when I load bbn_res32_cifar model from pre-trained res50 model . It shows that " Unexpected keys in state_dict......". How can I solve it?

Training Time for iNaturalist'18

Hi,

Thanks for the nice work in this repo!

Could you share your GPU setup and the training time per epoch on iNaturalist'18?

Best,
Rahul

Confusions about reverse sampler

The second step of data sampler is "Randomly sample a class according to Pi;". If a random sample manner is implemented, it is seems that there is no use to calculate Pi for each category.
According to the code from https://github.com/Megvii-Nanjing/BBN/blob/7992e908842f5934f0d1ee3f430d796621e81975/lib/dataset/imbalance_cifar.py#L59, I think each category has equal probability to be select to train and it can't be describe as a "reverse sampling".
Is there any misunderstanding? Thanks for your reply

Accuracy on ImageNet-LT

Hello, thank u for the effective method and clean code!
I try to change your code to run on ImageNet-LT, but get a not so good result.
The config is as follow:

NAME: 'BBN.ImageNet.coslr.res50.90epoch'
OUTPUT_DIR: './output/bbn/ImageNet'
SHOW_STEP: 50
SAVE_STEP: 30
VALID_STEP: 1
INPUT_SIZE: (224, 224)
COLOR_SPACE: 'RGB'
CPU_MODE: False

DATASET:
  ROOT: 'Dataset/ILSVRC2012'
  DATASET: 'ImageNet'
  DATA_TYPE: 'JPEG'
  TRAIN_JSON: 'datasets/ImageNet_LT/ImageNet_LT_train.txt'
  VALID_JSON: 'datasets/ImageNet_LT/ImageNet_LT_val.txt'

BACKBONE:  
  TYPE: 'bbn_res50'

MODULE:
  TYPE: 'GAP'

LOSS:
  LOSS_TYPE: 'CrossEntropy'

CLASSIFIER:
  TYPE: 'FC'
  BIAS: True

TRAIN:
  BATCH_SIZE: 256
  MAX_EPOCH: 90
  NUM_WORKERS: 16
  COMBINER:
    TYPE: 'bbn_mix'
  TENSORBOARD:
    ENABLE: False
  SAMPLER:
    TYPE: 'default'
    DUAL_SAMPLER:
      ENABLE: True
      TYPE: 'reverse'
  OPTIMIZER:
    TYPE: 'SGD'
    BASE_LR: 0.1
    MOMENTUM: 0.9
    WEIGHT_DECAY: 5e-4
  LR_SCHEDULER:
     TYPE: 'cosine'
     COSINE_DECAY_END: 0
     WARM_EPOCH: 5
    
TRANSFORMS:
  TRAIN_TRANSFORMS: ("random_resized_crop", "random_horizontal_flip", "color_jitter")
  TEST_TRANSFORMS: ("shorter_resize_for_crop", "center_crop")

TEST:
  BATCH_SIZE: 128
  NUM_WORKERS: 16
  MODEL_FILE: '/home/BBN/models/BBN.ImageNet.res50.90epoch.best_model.pth'

But only get 46.26% on val set, the baseline is about 44%
Have you run BBN on ImageNet and get some results? Can u tell me.
Thank u very much! @ZhouBoyan

Questions about mix_feature

Thanks for sharing the codes. I find that implement of mix_feature is different from the paper which uses addition, however, using concat in code. I wonder about the difference and connection of these two methods.

question about loss curve at inaturalist2019

hi, i just use bbn to train in inaturalist2019, the config is same as config of iNaturalist2018.yaml.
thanks to the clean code, it is very easy to get good result with resnet50 baseline 2x.
But i found loss curve is very strage.
train loss at 67 epoch got highest.
Have you gotten the same curve? Could you explain the reason?

image

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.