Git Product home page Git Product logo

jiawei-ren / balancedmse Goto Github PK

View Code? Open in Web Editor NEW
358.0 7.0 33.0 11.53 MB

[CVPR 2022 Oral] Balanced MSE for Imbalanced Visual Regression https://arxiv.org/abs/2203.16427

Home Page: https://sites.google.com/view/balanced-mse/home

License: MIT License

Python 79.92% Jupyter Notebook 20.08%
computer-vision imbalanced-learning imbalanced-regression age-estimation depth-estimation human-mesh-recovery

balancedmse's Introduction

Balanced MSE

Code for the paper:

Balanced MSE for Imbalanced Visual Regression
Jiawei Ren, Mingyuan Zhang, Cunjun Yu, Ziwei Liu

CVPR 2022 (Oral)

News

Live Demo

Check out our live demo in the Hugging Face 🤗 space!

Tutorial

We provide a minimal working example of Balanced MSE using the BMC implementation on a small-scale dataset, Boston Housing dataset.

Open In Colab

The notebook is developed on top of Deep Imbalanced Regression (DIR) Tutorial, we thank the authors for their amazing tutorial!

Quick Preview

A code snippet of the Balanced MSE loss is shown below. We use the BMC implementation for demonstration, BMC does not require any label prior beforehand.

One-dimensional Balanced MSE

def bmc_loss(pred, target, noise_var):
    """Compute the Balanced MSE Loss (BMC) between `pred` and the ground truth `targets`.
    Args:
      pred: A float tensor of size [batch, 1].
      target: A float tensor of size [batch, 1].
      noise_var: A float number or tensor.
    Returns:
      loss: A float tensor. Balanced MSE Loss.
    """
    logits = - (pred - target.T).pow(2) / (2 * noise_var)   # logit size: [batch, batch]
    loss = F.cross_entropy(logits, torch.arange(pred.shape[0]))     # contrastive-like loss
    loss = loss * (2 * noise_var).detach()  # optional: restore the loss scale, 'detach' when noise is learnable 

    return loss

noise_var is a one-dimensional hyper-parameter. noise_var can be optionally optimized in training:

class BMCLoss(_Loss):
    def __init__(self, init_noise_sigma):
        super(BMCLoss, self).__init__()
        self.noise_sigma = torch.nn.Parameter(torch.tensor(init_noise_sigma))

    def forward(self, pred, target):
        noise_var = self.noise_sigma ** 2
        return bmc_loss(pred, target, noise_var)

criterion = BMCLoss(init_noise_sigma)
optimizer.add_param_group({'params': criterion.noise_sigma, 'lr': sigma_lr, 'name': 'noise_sigma'})

Multi-dimensional Balanced MSE

The multi-dimensional implementation is compatible with the 1-D version.

from torch.distributions import MultivariateNormal as MVN

def bmc_loss_md(pred, target, noise_var):
    """Compute the Multidimensional Balanced MSE Loss (BMC) between `pred` and the ground truth `targets`.
    Args:
      pred: A float tensor of size [batch, d].
      target: A float tensor of size [batch, d].
      noise_var: A float number or tensor.
    Returns:
      loss: A float tensor. Balanced MSE Loss.
    """
    I = torch.eye(pred.shape[-1])
    logits = MVN(pred.unsqueeze(1), noise_var*I).log_prob(target.unsqueeze(0))  # logit size: [batch, batch]
    loss = F.cross_entropy(logits, torch.arange(pred.shape[0]))     # contrastive-like loss
    loss = loss * (2 * noise_var).detach()  # optional: restore the loss scale, 'detach' when noise is learnable 
    
    return loss

noise_var is still a one-dimensional hyper-parameter and can be optionally learned in training.

Run Experiments

Please go into the sub-folder to run experiments.

As for IHMR, we have released our code and pretrained models in MMHuman3d

Citation

@inproceedings{ren2021bmse,
  title={Balanced MSE for Imbalanced Visual Regression},
  author={Ren, Jiawei and Zhang, Mingyuan and Yu, Cunjun and Liu, Ziwei},
  booktitle={Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition},
  year={2022}
}

Acknowledgment

This work is supported by NTU NAP, MOE AcRF Tier 2 (T2EP20221-0033), the National Research Foundation, Singapore under its AI Singapore Programme, and under the RIE2020 Industry Alignment Fund – Industry Collabo- ration Projects (IAF-ICP) Funding Initiative, as well as cash and in-kind contribution from the industry partner(s).

The code is developed on top of Delving into Deep Imbalanced Regression.

balancedmse's People

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

balancedmse's Issues

reconstruction loss

Hi!
Can it be applied to image reconstruction loss?
because image reconstruction loss is often used in MSE

The shape of logits

The shape of logits in bmc loss is [batch, batch] which means both pred and target have the size [batch, 1]. For dense prediction, do we have to flat the 2d-image to a 1d vector with an operation of unsqueeze(-1)?

About the "bmc_loss_md"

Thanks for your work.

def bmc_loss_md(pred, target, noise_var):
I = torch.eye(pred.shape[-1])
logits = MVN(pred.unsqueeze(1), noise_var*I).log_prob(target.unsqueeze(0))
loss = F.cross_entropy(logits, torch.arange(pred.shape[0]))
loss = loss * (2 * noise_var).detach()
return loss

My size of pred and target are [30,3,256,256]
when running this code "loss = F.cross_entropy(logits, torch.arange(pred.shape[0]))",i got a error because "torch.arange(pred.shape[0])"is 1d,and logits is 4d.

How can i solve this error

about GAILoss

Thank you for your work.

gmm = {k: gmm[k].reshape(1, -1).expand(pred.shape[0], -1) for k in gmm}
mse_term = F.mse_loss(pred, target, reduction='none') / 2 / noise_var + 0.5 * noise_var.log()
sum_var = gmm['variances'] + noise_var
balancing_term = - 0.5 * sum_var.log() - 0.5 * (pred - gmm['means']).pow(2) / sum_var + gmm['weights'].log()
balancing_term = torch.logsumexp(balancing_term, dim=-1, keepdim=True)

when I use gai loss, my pred size is [128], but the data of gmm is [128, 8] after expand.
How I solve this, looking forward to your recovery

Don't understand the code.

What do you mean by this line?
Cross entropy between logits and [0,1,2,3,4....,batchsize]

F.cross_entropy(logits, torch.arange(pred.shape[0]))

How to apply BalancedMSE for the d-dimensional regression task?

@jiawei-ren
I wondered how should I apply Balanced-MSE for the D-dimensional regression task? I checked the tutorial code, the example code is formatted for one-dimensional regression.

def bmc_loss(pred, target, noise_var):
    logits = - (pred - target.T).pow(2) / (2 * noise_var)
    loss = F.cross_entropy(logits, torch.arange(pred.shape[0]))
    loss = loss * (2 * noise_var).detach()  # optional: restore the loss scale

    return loss

For example, the pred shape is (N, D), and the target is also (N, D). N is the batch size, and D is the dimension for regressing tasks.
If following the above example, there is an error in the F.cross_entropy function.

Dose BMCloss only work for minibatch > 1?

The bmcloss equals 0.0 when I added BMCloss to my project with minibatch==1.
I noticed that bmcloss is calculated by treating the regression predictions in a batch as multiclass classification. But since the size of each of my samples is different, my minibatch can only be set to 1.
Is it not feasible to apply bmcloss in this case?

BMCLossMD loss Multi-dimensional

Thank you for sharing, I refer BalancedMSE/synthetic_benchmark/loss.py;
Modified loss function in distributed run regression, after training the result is very low is my code modified by mistake?
Below is the code for my change, which can explain why
image

err

def bmc_loss_md(pred, target, noise_var):
I = torch.eye(pred.shape[-1]).cuda()
logits = MVN(pred.unsqueeze(1), noise_var*I)
logits = logits.log_prob(target.unsqueeze(0))
loss = F.cross_entropy(logits, torch.arange(pred.shape[0]))
loss = loss * (2 * noise_var).detach()
return loss
用于ssrnet年龄回归任务。
在这一段代码中,loss = F.cross_entropy(logits, torch.arange(pred.shape[0])) 报错:
IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1) 

回归任务不就是一维的吗。

Negative loss while using BNI implementation option

Hi @jiawei-ren
I am trying to implement the Balanced MSE to other long tail regression task. Firstly, I tried the BNI, and then the negative traning loss are often computed and they are used to update the model. I would like to ask the appearance of negative loss is usual while using the BNI?

Should one scale multi-dimensional targets when training with BMCLossMD?

Hello, thank you for this great work.
I am currently trying to train a network for multi-task regression using BMCLossMD. However, the distributions of the target variables are different. For example, one of them looks exponential, another one resembles gaussian and has 4x larger std then the exponential one. Should I then scale the variables to at least one std? The noise_var in BMCLossMD is 1D, so I assume yes but have no proof...

Bad performance on train set

Hi. I apply bMSE on my task which is a single-dimensional regression task. I found that the performance on train set is weird. The prediction of relatively low value data point is very high. In all, the performance on train set is bad. So it is normal? Looking forward to your reply!

Restore the loss scale?

Deat authors,

Thanks for the great job. I have a question:
why loss = loss * (2 * noise_var).detach(). can restore the loss scale?

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.