Pytorch implementation of the paper "Class-Balanced Loss Based on Effective Number of Samples"

Overview

Class-balanced-loss-pytorch

Pytorch implementation of the paper Class-Balanced Loss Based on Effective Number of Samples presented at CVPR'19.

Yin Cui, Menglin Jia, Tsung-Yi Lin(Google Brain), Yang Song(Google), Serge Belongie

Dependencies

  • Python (>=3.6)
  • Pytorch (>=1.2.0)

Review article of the paper

Medium Article

How it works

It works on the principle of calculating effective number of samples for all classes which is defined as:

alt-text

Thus, the loss function is defined as:

alt-text

Visualisation for effective number of samples

alt-text

References

official tensorflow implementation

Comments
  • Is this implementation correct?

    Is this implementation correct?

    Hi, I wonder if this implementation is correct?

    When I run the code with softmax, I get weights like this:

    tensor([[0.8824, 0.8824, 0.8824, 0.8824, 0.8824],
            [0.8824, 0.8824, 0.8824, 0.8824, 0.8824],
            [0.8824, 0.8824, 0.8824, 0.8824, 0.8824],
            [0.8824, 0.8824, 0.8824, 0.8824, 0.8824],
            [1.7646, 1.7646, 1.7646, 1.7646, 1.7646],
            [1.7646, 1.7646, 1.7646, 1.7646, 1.7646],
            [0.8824, 0.8824, 0.8824, 0.8824, 0.8824],
            [0.8824, 0.8824, 0.8824, 0.8824, 0.8824],
            [0.8824, 0.8824, 0.8824, 0.8824, 0.8824],
            [0.8824, 0.8824, 0.8824, 0.8824, 0.8824]])
    

    where each sample enjoin the same weights.

    This is weird, right? According to the original paper, we should have different weight for each class (column), right?

    opened by Hhhhhhao 4
  • why is no_of_classes needed for weights normialisation

    why is no_of_classes needed for weights normialisation

    Hi @vandit15,

    Thanks for sharing your code.

    In line 73, weights = weights / np.sum(weights) * no_of_classes, why is no_of_classes included here to normalise the weights? Any help would be thankful.

    Thanks

    opened by m-zheng 3
  • base information about cbloss

    base information about cbloss

    Thanks for sharing code.I have modified the code. I want to calculate the multivariate classification. The code is as follows. I can always report errors. I wonder if you have tried it?

    pred = logits.log_softmax(dim=1)
    cb_loss = F.cross_etropy(input=pred, target=labels, weights=weights)
    

    Thanks.

    opened by Adorablepet 2
  • Reduction for the Cross Entropy

    Reduction for the Cross Entropy

    thank you for sharing Pytorch implementation, I was wondering if we need to specify "reduction="none"" for the "binary_cross_entropy_with_logits" function here since we are going to do weighting and reduction on our own later? https://github.com/vandit15/Class-balanced-loss-pytorch/blob/fb634a6e42ecaa2e3d7e974cd124180c679da6c7/class_balanced_loss.py#L35

    opened by inspirit 1
  • Sigmoid Loss does not use weights

    Sigmoid Loss does not use weights

    https://github.com/vandit15/Class-balanced-loss-pytorch/blob/91a7832d86c8b66576018e5377df391424b3fd11/class_balanced_loss.py#L87

    It seems that you forgot to include weights for sigmoid loss

    opened by inspirit 0
  • typo

    typo

    https://github.com/vandit15/Class-balanced-loss-pytorch/blob/921ccb8725b1eb0903b2c22a1a752a594fcae138/class_balanced_loss.py#L28

    should be alpha: A float tensor of size [num_classes]

    opened by e-yi 1
  • Sth about samples_per_cls

    Sth about samples_per_cls

    Hi, thanks for your code sharing!

    I am now trying to understand how do you implement and I have something want to discuss.

    In your main function you have provide us with a toy example, I noticed that you have set samples_per_cls equals [2,3,1,2,2] which indicates label 0 has 2 samples , am i right? But you have random labels,so should I count samples of each class every times ?

    I would be appreciated if you can answer my question as quick as possible. Have a good day!

    opened by Liudzz 0
  • fix bug in call to BCEwithlogits

    fix bug in call to BCEwithlogits

    the keyword had a typo: 'weight' instead of 'weights'. in case weights requires grads, it needs to be detached first as BCEwithlogits is not implemented for grad tracked tensor

    opened by aalok-sathe 0
  • Is this same to apply pos_weight?

    Is this same to apply pos_weight?

    Instead calculating a weight for each batch, applying to class using pos_weight argument in torch.nn.BCELoss(pos_weights=weights)

    Simply, https://github.com/vandit15/Class-balanced-loss-pytorch/blob/master/class_balanced_loss.py#L71-L82

    Are those line of codes same with

    effective_num = 1.0 - np.power(beta, samples_per_cls)
    weights = (1.0 - beta) / np.array(effective_num)
    weights = weights / np.sum(weights) * no_of_classes
    loss = torch.nn.BCELoss(reduction='mean', pos_weight=weights)
    

    this code?

    opened by jtlee90 0
Owner
Vandit Jain
Vandit Jain
ALBERT-pytorch-implementation - ALBERT pytorch implementation

ALBERT-pytorch-implementation developing... 모델의 개념이해를 돕기 위한 구현물로 현재 변수명을 상세히 적었고

BG Kim 3 Oct 6, 2022
The LaTeX and Python code for generating the paper, experiments' results and visualizations reported in each paper is available (whenever possible) in the paper's directory

This repository contains the software implementation of most algorithms used or developed in my research. The LaTeX and Python code for generating the

João Fonseca 3 Jan 3, 2023
Official PyTorch implementation for paper Context Matters: Graph-based Self-supervised Representation Learning for Medical Images

Context Matters: Graph-based Self-supervised Representation Learning for Medical Images Official PyTorch implementation for paper Context Matters: Gra

null 49 Nov 23, 2022
A PyTorch re-implementation of the paper 'Exploring Simple Siamese Representation Learning'. Reproduced the 67.8% Top1 Acc on ImageNet.

Exploring simple siamese representation learning This is a PyTorch re-implementation of the SimSiam paper on ImageNet dataset. The results match that

Taojiannan Yang 72 Nov 9, 2022
This is a Pytorch implementation of the paper: Self-Supervised Graph Transformer on Large-Scale Molecular Data.

This is a Pytorch implementation of the paper: Self-Supervised Graph Transformer on Large-Scale Molecular Data.

null 212 Dec 25, 2022
Official pytorch implementation of paper "Image-to-image Translation via Hierarchical Style Disentanglement".

HiSD: Image-to-image Translation via Hierarchical Style Disentanglement Official pytorch implementation of paper "Image-to-image Translation

null 364 Dec 14, 2022
PyTorch implementation of paper "Neural Scene Flow Fields for Space-Time View Synthesis of Dynamic Scenes", CVPR 2021

Neural Scene Flow Fields PyTorch implementation of paper "Neural Scene Flow Fields for Space-Time View Synthesis of Dynamic Scenes", CVPR 20

Zhengqi Li 585 Jan 4, 2023
Official pytorch implementation of paper "Inception Convolution with Efficient Dilation Search" (CVPR 2021 Oral).

IC-Conv This repository is an official implementation of the paper Inception Convolution with Efficient Dilation Search. Getting Started Download Imag

Jie Liu 111 Dec 31, 2022
Official implementation of our paper "LLA: Loss-aware Label Assignment for Dense Pedestrian Detection" in Pytorch.

LLA: Loss-aware Label Assignment for Dense Pedestrian Detection This project provides an implementation for "LLA: Loss-aware Label Assignment for Dens

null 35 Dec 6, 2022
An implementation of Geoffrey Hinton's paper "How to represent part-whole hierarchies in a neural network" in Pytorch.

GLOM An implementation of Geoffrey Hinton's paper "How to represent part-whole hierarchies in a neural network" for MNIST Dataset. To understand this

null 50 Oct 19, 2022
Official implementation of our CVPR2021 paper "OTA: Optimal Transport Assignment for Object Detection" in Pytorch.

OTA: Optimal Transport Assignment for Object Detection This project provides an implementation for our CVPR2021 paper "OTA: Optimal Transport Assignme

null 217 Jan 3, 2023
This is the official PyTorch implementation of the paper "TransFG: A Transformer Architecture for Fine-grained Recognition" (Ju He, Jie-Neng Chen, Shuai Liu, Adam Kortylewski, Cheng Yang, Yutong Bai, Changhu Wang, Alan Yuille).

TransFG: A Transformer Architecture for Fine-grained Recognition Official PyTorch code for the paper: TransFG: A Transformer Architecture for Fine-gra

Ju He 307 Jan 3, 2023
[PyTorch] Official implementation of CVPR2021 paper "PointDSC: Robust Point Cloud Registration using Deep Spatial Consistency". https://arxiv.org/abs/2103.05465

PointDSC repository PyTorch implementation of PointDSC for CVPR'2021 paper "PointDSC: Robust Point Cloud Registration using Deep Spatial Consistency",

null 153 Dec 14, 2022
PyTorch implementation of the Deep SLDA method from our CVPRW-2020 paper "Lifelong Machine Learning with Deep Streaming Linear Discriminant Analysis"

Lifelong Machine Learning with Deep Streaming Linear Discriminant Analysis This is a PyTorch implementation of the Deep Streaming Linear Discriminant

Tyler Hayes 41 Dec 25, 2022
The official pytorch implementation of our paper "Is Space-Time Attention All You Need for Video Understanding?"

TimeSformer This is an official pytorch implementation of Is Space-Time Attention All You Need for Video Understanding?. In this repository, we provid

Facebook Research 1k Dec 31, 2022
PyTorch 1.5 implementation for paper DECOR-GAN: 3D Shape Detailization by Conditional Refinement.

DECOR-GAN PyTorch 1.5 implementation for paper DECOR-GAN: 3D Shape Detailization by Conditional Refinement, Zhiqin Chen, Vladimir G. Kim, Matthew Fish

Zhiqin Chen 72 Dec 31, 2022
Pytorch implementation of our paper under review — Lottery Jackpots Exist in Pre-trained Models

Lottery Jackpots Exist in Pre-trained Models (Paper Link) Requirements Python >= 3.7.4 Pytorch >= 1.6.1 Torchvision >= 0.4.1 Reproduce the Experiment

Yuxin Zhang 27 Jun 28, 2022
Official Pytorch Implementation of: "ImageNet-21K Pretraining for the Masses"(2021) paper

ImageNet-21K Pretraining for the Masses Paper | Pretrained models Official PyTorch Implementation Tal Ridnik, Emanuel Ben-Baruch, Asaf Noy, Lihi Zelni

null 574 Jan 2, 2023
The official PyTorch implementation of the paper: *Xili Dai, Xiaojun Yuan, Haigang Gong, Yi Ma. "Fully Convolutional Line Parsing." *.

F-Clip — Fully Convolutional Line Parsing This repository contains the official PyTorch implementation of the paper: *Xili Dai, Xiaojun Yuan, Haigang

Xili Dai 115 Dec 28, 2022