Skip to content

Replaced view with replace to prevent errors on non-contiguous tensors #1174

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

Merged
merged 1 commit into from
Jun 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions segmentation_models_pytorch/losses/dice.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,17 @@
dims = (0, 2)

if self.mode == BINARY_MODE:
y_true = y_true.view(bs, 1, -1)
y_pred = y_pred.view(bs, 1, -1)
y_true = y_true.reshape(bs, 1, -1)
y_pred = y_pred.reshape(bs, 1, -1)

if self.ignore_index is not None:
mask = y_true != self.ignore_index
y_pred = y_pred * mask
y_true = y_true * mask

if self.mode == MULTICLASS_MODE:
y_true = y_true.view(bs, -1)
y_pred = y_pred.view(bs, num_classes, -1)
y_true = y_true.reshape(bs, -1)
y_pred = y_pred.reshape(bs, num_classes, -1)

Check warning on line 86 in segmentation_models_pytorch/losses/dice.py

View check run for this annotation

Codecov / codecov/patch

segmentation_models_pytorch/losses/dice.py#L85-L86

Added lines #L85 - L86 were not covered by tests

if self.ignore_index is not None:
mask = y_true != self.ignore_index
Expand All @@ -98,8 +98,8 @@
y_true = y_true.permute(0, 2, 1) # N, C, H*W

if self.mode == MULTILABEL_MODE:
y_true = y_true.view(bs, num_classes, -1)
y_pred = y_pred.view(bs, num_classes, -1)
y_true = y_true.reshape(bs, num_classes, -1)
y_pred = y_pred.reshape(bs, num_classes, -1)

Check warning on line 102 in segmentation_models_pytorch/losses/dice.py

View check run for this annotation

Codecov / codecov/patch

segmentation_models_pytorch/losses/dice.py#L101-L102

Added lines #L101 - L102 were not covered by tests

if self.ignore_index is not None:
mask = y_true != self.ignore_index
Expand Down
4 changes: 2 additions & 2 deletions segmentation_models_pytorch/losses/focal.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@

def forward(self, y_pred: torch.Tensor, y_true: torch.Tensor) -> torch.Tensor:
if self.mode in {BINARY_MODE, MULTILABEL_MODE}:
y_true = y_true.view(-1)
y_pred = y_pred.view(-1)
y_true = y_true.reshape(-1)
y_pred = y_pred.reshape(-1)

Check warning on line 61 in segmentation_models_pytorch/losses/focal.py

View check run for this annotation

Codecov / codecov/patch

segmentation_models_pytorch/losses/focal.py#L60-L61

Added lines #L60 - L61 were not covered by tests

if self.ignore_index is not None:
# Filter predictions with ignore label from loss computation
Expand Down
12 changes: 6 additions & 6 deletions segmentation_models_pytorch/losses/jaccard.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,17 @@ def forward(self, y_pred: torch.Tensor, y_true: torch.Tensor) -> torch.Tensor:
dims = (0, 2)

if self.mode == BINARY_MODE:
y_true = y_true.view(bs, 1, -1)
y_pred = y_pred.view(bs, 1, -1)
y_true = y_true.reshape(bs, 1, -1)
y_pred = y_pred.reshape(bs, 1, -1)

if self.ignore_index is not None:
mask = y_true != self.ignore_index
y_pred = y_pred * mask
y_true = y_true * mask

if self.mode == MULTICLASS_MODE:
y_true = y_true.view(bs, -1)
y_pred = y_pred.view(bs, num_classes, -1)
y_true = y_true.reshape(bs, -1)
y_pred = y_pred.reshape(bs, num_classes, -1)

if self.ignore_index is not None:
mask = y_true != self.ignore_index
Expand All @@ -98,8 +98,8 @@ def forward(self, y_pred: torch.Tensor, y_true: torch.Tensor) -> torch.Tensor:
y_true = y_true.permute(0, 2, 1) # N, C, H*W

if self.mode == MULTILABEL_MODE:
y_true = y_true.view(bs, num_classes, -1)
y_pred = y_pred.view(bs, num_classes, -1)
y_true = y_true.reshape(bs, num_classes, -1)
y_pred = y_pred.reshape(bs, num_classes, -1)

if self.ignore_index is not None:
mask = y_true != self.ignore_index
Expand Down
10 changes: 5 additions & 5 deletions segmentation_models_pytorch/losses/lovasz.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@
"""Flattens predictions in the batch (binary case)
Remove labels equal to 'ignore'
"""
scores = scores.view(-1)
labels = labels.view(-1)
scores = scores.reshape(-1)
labels = labels.reshape(-1)

Check warning on line 81 in segmentation_models_pytorch/losses/lovasz.py

View check run for this annotation

Codecov / codecov/patch

segmentation_models_pytorch/losses/lovasz.py#L80-L81

Added lines #L80 - L81 were not covered by tests
if ignore is None:
return scores, labels
valid = labels != ignore
Expand Down Expand Up @@ -151,13 +151,13 @@
if probas.dim() == 3:
# assumes output of a sigmoid layer
B, H, W = probas.size()
probas = probas.view(B, 1, H, W)
probas = probas.reshape(B, 1, H, W)

Check warning on line 154 in segmentation_models_pytorch/losses/lovasz.py

View check run for this annotation

Codecov / codecov/patch

segmentation_models_pytorch/losses/lovasz.py#L154

Added line #L154 was not covered by tests

C = probas.size(1)
probas = torch.movedim(probas, 1, -1) # [B, C, Di, Dj, ...] -> [B, Di, Dj, ..., C]
probas = probas.contiguous().view(-1, C) # [P, C]
probas = probas.reshape(-1, C) # [P, C]

Check warning on line 158 in segmentation_models_pytorch/losses/lovasz.py

View check run for this annotation

Codecov / codecov/patch

segmentation_models_pytorch/losses/lovasz.py#L158

Added line #L158 was not covered by tests

labels = labels.view(-1)
labels = labels.reshape(-1)

Check warning on line 160 in segmentation_models_pytorch/losses/lovasz.py

View check run for this annotation

Codecov / codecov/patch

segmentation_models_pytorch/losses/lovasz.py#L160

Added line #L160 was not covered by tests
if ignore is None:
return probas, labels
valid = labels != ignore
Expand Down
4 changes: 2 additions & 2 deletions segmentation_models_pytorch/losses/mcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ def forward(self, y_pred: torch.Tensor, y_true: torch.Tensor) -> torch.Tensor:

bs = y_true.shape[0]

y_true = y_true.view(bs, 1, -1)
y_pred = y_pred.view(bs, 1, -1)
y_true = y_true.reshape(bs, 1, -1)
y_pred = y_pred.reshape(bs, 1, -1)

tp = torch.sum(torch.mul(y_pred, y_true)) + self.eps
tn = torch.sum(torch.mul((1 - y_pred), (1 - y_true))) + self.eps
Expand Down
Loading