Skip to content

RuntimeError with SegFormer and multilabel FocalLoss #1163

@simonreise

Description

@simonreise

When I train SMP SegFormer with multilabel FocalLoss this RuntimeError appears:

     58 if self.mode in {BINARY_MODE, MULTILABEL_MODE}:
     59     y_true = y_true.view(-1)
---> 60     y_pred = y_pred.view(-1)
     62     if self.ignore_index is not None:
     63         # Filter predictions with ignore label from loss computation
     64         not_ignored = y_true != self.ignore_index

RuntimeError: view size is not compatible with input tensor's size and stride (at least one dimension spans across two contiguous subspaces). Use .reshape(...) instead.

It does not appear in multiclass mode.

I checked my y_pred and y_true shapes and they are

torch.Size([16, 14, 256, 256])
torch.Size([16, 14, 256, 256])

After changing

 loss = self.loss(inputs, labels)

to

 loss = self.loss(inputs.contiguous(), labels.contiguous())

the error disappeared.

Maybe it is related to #998 where .contiguous() is removed from SegFormer decoder.

As mode logic is usually the same in every loss, this error may appear in other losses too, and in binary mode too.

Maybe replacing view with reshape can be a good fix? Or it will slow computations down like contiguous() did?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions