GithubHelp home page GithubHelp logo

Classification losses error about theseus HOT 1 CLOSED

kaylode avatar kaylode commented on August 11, 2024
Classification losses error

from theseus.

Comments (1)

kaylode avatar kaylode commented on August 11, 2024

ok, I've analyzed this error. It is because MixupCutmix occasionally transforms the targets tensor into soft one-hot encoding , whereas the current FocalLoss and SmoothCE requires argmax labels.

For FocalLoss it can be easily fixed by this:

class FocalLoss(nn.Module):
    def forward(self, outputs: Dict[str, Any], batch: Dict[str, Any], device: torch.device):
        outputs = outputs['outputs']
        targets = move_to(batch['targets'], device)
        num_classes = outputs.shape[-1]

        # Need to be one hot encoding
        if outputs.shape != targets.shape:
            targets = nn.functional.one_hot(targets, num_classes=num_classes)
            targets = targets.float().squeeze()

        loss = sigmoid_focal_loss(outputs, targets, self.alpha, self.gamma, self.reduction)
        loss_dict = {"L": loss.item()}
        return loss, loss_dict

For SmoothCE, timm has SoftTargetCrossEntropy class which is suitable for soft one-hot encoding:

from timm.loss import LabelSmoothingCrossEntropy, SoftTargetCrossEntropy

class SmoothCELoss(nn.Module):
    def __init__(self, smoothing: float=0.1, **kwargs):
        super(SmoothCELoss, self).__init__(**kwargs)
        self.smooth_criterion = LabelSmoothingCrossEntropy()
        self.soft_criterion = SoftTargetCrossEntropy()

    def forward(self, outputs: Dict[str, Any], batch: Dict[str, Any], device: torch.device):
        pred = outputs['outputs']
        target = move_to(batch["targets"], device)

        if pred.shape == target.shape:
            loss = self.soft_criterion(pred, target)
        else:
            loss = self.smooth_criterion(pred, target.view(-1).contiguous())
        loss_dict = {"CE": loss.item()}
        return loss, loss_dict

I will make a PR and you can test it out

from theseus.

Related Issues (14)

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.