Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: forward() got an unexpected keyword argument 'ignore_index' #201

Closed
JinxiaoWang opened this issue Oct 20, 2020 · 4 comments · Fixed by #210
Closed

TypeError: forward() got an unexpected keyword argument 'ignore_index' #201

JinxiaoWang opened this issue Oct 20, 2020 · 4 comments · Fixed by #210

Comments

@JinxiaoWang
Copy link

I add a new loss 'my_loss' to mmseg/models/losses/ as the tutorial doc did.

my_loss.py :

`import torch
import torch.nn as nn

from ..builder import LOSSES
from .utils import weighted_loss

@weighted_loss
def my_loss(pred, target):
assert pred.size() == target.size() and target.numel() > 0
loss = torch.abs(pred - target)
return loss

@LOSSES.register_module
class MyLoss(nn.Module):

def __init__(self, reduction='mean', loss_weight=1.0):
    super(MyLoss, self).__init__()
    self.reduction = reduction
    self.loss_weight = loss_weight

def forward(self,
            pred,
            target,
            weight=None,
            avg_factor=None,
            reduction_override=None):
    assert reduction_override in (None, 'none', 'mean', 'sum')
    reduction = (
        reduction_override if reduction_override else self.reduction)
    loss = self.loss_weight * my_loss(
        pred, target, weight, reduction=reduction, avg_factor=avg_factor)
    return loss`

Then , when I use this loss to train the model, this TypeError appeared.

@xvjiarui
Copy link
Collaborator

Hi @DreamerWangDr
Try this

def my_loss(pred, target, **kwargs):

We will update tutorial to fix this.

@JinxiaoWang
Copy link
Author

Hi @DreamerWangDr
Try this

def my_loss(pred, target, **kwargs):

We will update tutorial to fix this.

Thanks, I tried this, still TypeError: forward() got an unexpected keyword argument 'ignore_index'.
Hope that Dice Loss and Focal Loss can be added to the losses module.

@xvjiarui
Copy link
Collaborator

xvjiarui commented Oct 25, 2020

Hi @DreamerWangDr
Sorry. My mistake. You may try this

def forward(self,
            pred,
            target,
            weight=None,
            avg_factor=None,
            reduction_override=None,
            **kwargs):

@JinxiaoWang
Copy link
Author

Hi @DreamerWangDr
Sorry. My mistake. You may try this

def forward(self,
            pred,
            target,
            weight=None,
            avg_factor=None,
            reduction_override=None,
            **kwargs):

Hi, Thanks. I tried this, TypeError: forward() got an unexpected keyword argument 'ignore_index' disppeard.
Then , I meet an assert error at line : assert pred.size() == target.size() and target.numel() > 0
I don't know is pred.size different from target.size or target.numel() <=0 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants