[Fix] Fix indentation bug in hd_loss that mishandles ignore_index#3869
[Fix] Fix indentation bug in hd_loss that mishandles ignore_index#3869Mr-Neutr0n wants to merge 2 commits into
Conversation
The `class_weight` multiplication and `total_loss` accumulation in `hd_loss()` were incorrectly placed outside the `if i != ignore_index` guard. When a class index matched `ignore_index`, the loss from the previous loop iteration would be erroneously re-added to `total_loss` (and potentially re-weighted with the wrong class weight), leading to incorrect loss values. On the first iteration, if `i == ignore_index`, this would cause a `NameError` since `hd_loss` was never assigned. Move both lines inside the `if` block so that only computed (non-ignored) class losses contribute to the total.
|
I have read the CLA Document and I hereby sign the CLA |
|
Re-ran the hausdorff loss logic locally with the fix applied (PyTorch 2.x, Python 3.12) — The
My best guess: the 1.8.1/3.7 failure was a pre-existing flake on that combo (the matrix is quite old) or an unrelated dependency-resolution issue. Worth re-running just that job with |
|
Pushed a no-op commit ( |
Motivation
There is an indentation bug in the
hd_loss()function inmmseg/models/losses/huasdorff_distance_loss.py. Theclass_weightmultiplication andtotal_lossaccumulation are placed outside theif i != ignore_indexguard block.This causes two problems:
iequalsignore_index, no loss is computed for that iteration, but the previous iteration'shd_lossvalue is re-added tototal_loss(and potentially re-weighted with the wrongclass_weight[i]). This silently produces incorrect loss values.ignore_indexhappens to equal1(the first value inrange(1, num_class)),hd_lossis never assigned, causing aNameError.Modification
Move the
class_weightcheck andtotal_loss +=lines inside theif i != ignore_indexblock so that only computed (non-ignored) class losses contribute to the total.Before (buggy)
After (fixed)
BC-breaking (No)
This is a pure bugfix. Users who were not using
HuasdorffDisstanceLossare unaffected. Users who were using it will get correct loss values.