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

Dice loss module causes TypeError: forward() got an unexpected keyword argument 'ignore_index' in v1.1.0 #3172

Closed
ff98li opened this issue Jul 6, 2023 · 14 comments

Comments

@ff98li
Copy link

ff98li commented Jul 6, 2023

Checklist

  1. I have searched related issues but cannot get the expected help. ☑️
  1. The bug has not been fixed in the latest version. ☑️

Describe the bug
Attempted to train SegFormer using Dice loss + CE loss; the following error occurred after updating to mmsegmentation v1.1.0:

Traceback (most recent call last):
  File "/home/fei/pkgs/mmsegmentation/tools/train.py", line 104, in <module>
    main()
  File "/home/fei/pkgs/mmsegmentation/tools/train.py", line 100, in main
    runner.train()
  File "/home/fei/software/mambaforge/envs/openmmlab/lib/python3.8/site-packages/mmengine/runner/runner.py", line 1701, in train
    model = self.train_loop.run()  # type: ignore
  File "/home/fei/software/mambaforge/envs/openmmlab/lib/python3.8/site-packages/mmengine/runner/loops.py", line 278, in run
    self.run_iter(data_batch)
  File "/home/fei/software/mambaforge/envs/openmmlab/lib/python3.8/site-packages/mmengine/runner/loops.py", line 301, in run_iter
    outputs = self.runner.model.train_step(
  File "/home/fei/software/mambaforge/envs/openmmlab/lib/python3.8/site-packages/mmengine/model/base_model/base_model.py", line 114, in train_step
    losses = self._run_forward(data, mode='loss')  # type: ignore
  File "/home/fei/software/mambaforge/envs/openmmlab/lib/python3.8/site-packages/mmengine/model/base_model/base_model.py", line 326, in _run_forward
    results = self(**data, mode=mode)
  File "/home/fei/software/mambaforge/envs/openmmlab/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
  File "/home/fei/pkgs/mmsegmentation/mmseg/models/segmentors/base.py", line 94, in forward
    return self.loss(inputs, data_samples)
  File "/home/fei/pkgs/mmsegmentation/mmseg/models/segmentors/encoder_decoder.py", line 176, in loss
    loss_decode = self._decode_head_forward_train(x, data_samples)
  File "/home/fei/pkgs/mmsegmentation/mmseg/models/segmentors/encoder_decoder.py", line 137, in _decode_head_forward_train
    loss_decode = self.decode_head.loss(inputs, data_samples,
  File "/home/fei/pkgs/mmsegmentation/mmseg/models/decode_heads/decode_head.py", line 262, in loss
    losses = self.loss_by_feat(seg_logits, batch_data_samples)
  File "/home/fei/pkgs/mmsegmentation/mmseg/models/decode_heads/decode_head.py", line 324, in loss_by_feat
    loss[loss_decode.loss_name] = loss_decode(
  File "/home/fei/software/mambaforge/envs/openmmlab/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1501, in _call_impl
    return forward_call(*args, **kwargs)
TypeError: forward() got an unexpected keyword argument 'ignore_index'

Reproduction

  1. What command or script did you run?

Added Dice loss to my config for SegFormer as below:

  model = dict(
   ## Don't use data_preprocessor in predefined config
   data_preprocessor = data_preprocessor,
   pretrained = None,
   decode_head = dict(
       num_classes = 13, 
       loss_decode = [
           dict(
               type = "CrossEntropyLoss",
               loss_name = "loss_ce",
               loss_weight = 2.0
           ),
           dict(
               type = 'DiceLoss',
               loss_name = 'loss_dice',
               loss_weight = 1.0
           )
       ]
   )
)

I believe this issue is directly related to the Dice loss as after removing the Dice loss the error no longer occurs.

  1. Did you make any modifications on the code or config? Did you understand what you have modified?

Yes, as described above. It used to work with dev-1.* before I updated mmseg to the main branch version.

  1. What dataset did you use?
    🤐 Custom datasets, but used to work with dev-1.*
    Environment
sys.platform: linux
Python: 3.8.16 | packaged by conda-forge | (default, Feb  1 2023, 16:01:55) [GCC 11.3.0]
CUDA available: True
numpy_random_seed: 2147483648
GPU 0,1: NVIDIA RTX A6000
CUDA_HOME: /usr/local/cuda
NVCC: Cuda compilation tools, release 11.6, V11.6.124
GCC: gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
PyTorch: 2.0.0
PyTorch compiling details: PyTorch built with:
  - GCC 9.3
  - C++ Version: 201703
  - Intel(R) oneAPI Math Kernel Library Version 2022.1-Product Build 20220311 for Intel(R) 64 architecture applications
  - Intel(R) MKL-DNN v2.7.3 (Git Hash 6dbeffbae1f23cbbeae17adb7b5b13f1f37c080e)
  - OpenMP 201511 (a.k.a. OpenMP 4.5)
  - LAPACK is enabled (usually provided by MKL)
  - NNPACK is enabled
  - CPU capability usage: AVX2
  - CUDA Runtime 11.7
  - NVCC architecture flags: -gencode;arch=compute_37,code=sm_37;-gencode;arch=compute_50,code=sm_50;-gencode;arch=compute_60,code=sm_60;-gencode;arch=compute_61,code=sm_61;-gencode;arch=compute_70,code=sm_70;-gencode;arch=compute_75,code=sm_75;-gencode;arch=compute_80,code=sm_80;-gencode;arch=compute_86,code=sm_86;-gencode;arch=compute_37,code=compute_37
  - CuDNN 8.5
  - Magma 2.6.1
  - Build settings: BLAS_INFO=mkl, BUILD_TYPE=Release, CUDA_VERSION=11.7, CUDNN_VERSION=8.5.0, CXX_COMPILER=/opt/rh/devtoolset-9/root/usr/bin/c++, CXX_FLAGS= -D_GLIBCXX_USE_CXX11_ABI=0 -fabi-version=11 -Wno-deprecated -fvisibility-inlines-hidden -DUSE_PTHREADPOOL -DNDEBUG -DUSE_KINETO -DLIBKINETO_NOROCTRACER -DUSE_FBGEMM -DUSE_QNNPACK -DUSE_PYTORCH_QNNPACK -DUSE_XNNPACK -DSYMBOLICATE_MOBILE_DEBUG_HANDLE -O2 -fPIC -Wall -Wextra -Werror=return-type -Werror=non-virtual-dtor -Werror=bool-operation -Wnarrowing -Wno-missing-field-initializers -Wno-type-limits -Wno-array-bounds -Wno-unknown-pragmas -Wunused-local-typedefs -Wno-unused-parameter -Wno-unused-function -Wno-unused-result -Wno-strict-overflow -Wno-strict-aliasing -Wno-error=deprecated-declarations -Wno-stringop-overflow -Wno-psabi -Wno-error=pedantic -Wno-error=redundant-decls -Wno-error=old-style-cast -fdiagnostics-color=always -faligned-new -Wno-unused-but-set-variable -Wno-maybe-uninitialized -fno-math-errno -fno-trapping-math -Werror=format -Werror=cast-function-type -Wno-stringop-overflow, LAPACK_INFO=mkl, PERF_WITH_AVX=1, PERF_WITH_AVX2=1, PERF_WITH_AVX512=1, TORCH_DISABLE_GPU_ASSERTS=ON, TORCH_VERSION=2.0.0, USE_CUDA=ON, USE_CUDNN=ON, USE_EXCEPTION_PTR=1, USE_GFLAGS=OFF, USE_GLOG=OFF, USE_MKL=ON, USE_MKLDNN=ON, USE_MPI=OFF, USE_NCCL=ON, USE_NNPACK=ON, USE_OPENMP=ON, USE_ROCM=OFF, 

TorchVision: 0.15.0
OpenCV: 4.7.0
MMEngine: 0.7.0
MMSegmentation: 1.1.0+0079076
@callmewenhao
Copy link

i meet the same error🤣

@yeedrag
Copy link
Contributor

yeedrag commented Jul 7, 2023

I had similar problems when trying to use multiple losses too

@longphamkhac
Copy link

longphamkhac commented Jul 11, 2023

Beside the ignore_index, I think the dice loss implementation has it own logical problem as well. The output_preds has shape [bs, num_classes, H, W] but the target's shape is [bs, H, W], which is cannot be the same shape when flattenning. I think the target has to pass through the one hot encoding first.

@okdha1234
Copy link

facing similar issue. Any solution to this ,?
@yeedrag @callmewenhao @longphamkhac @kahkeng

@stone-cloud
Copy link

I'm also having a similar issue when using STDC-v2

@yeedrag
Copy link
Contributor

yeedrag commented Jul 19, 2023

facing similar issue. Any solution to this ,? @yeedrag @callmewenhao @longphamkhac @kahkeng

We solved it by just writing a new loss that calculates and combines both losses together, ugly but it works.

@DXY121
Copy link

DXY121 commented Jul 21, 2023

me too.....

@xiexinch
Copy link
Collaborator

facing similar issue. Any solution to this ,? @yeedrag @callmewenhao @longphamkhac @kahkeng

We solved it by just writing a new loss that calculates and combines both losses together, ugly but it works.

Hi @yeedrag,
Could you create a PR to fix this problem? The dice loss was modified by this PR #3002, the implementation is the same as mmdet.

@yeedrag
Copy link
Contributor

yeedrag commented Jul 26, 2023

facing similar issue. Any solution to this ,? @yeedrag @callmewenhao @longphamkhac @kahkeng

We solved it by just writing a new loss that calculates and combines both losses together, ugly but it works.

Hi @yeedrag, Could you create a PR to fix this problem? The dice loss was modified by this PR #3002, the implementation is the same as mmdet.

Hi @xiexinch, our solution was to just make a new loss called focal_dice_loss, where we calculate the two losses inside. We didn't modify it in any way.
I was interested in fixing the problem though, so I did a few changes and now the dice loss can run properly:
I added ignore_index inside the forward function (although not used), and I added one hot encoding for pred to ensure it has the same dim as target ( which is [N, num_classes, H, W as @longphamkhac mentioned).
The problem is I'm not familiar with contributing to open sources, and this is basically my first time. How can I check my code is absolutely correct (I ran pytest on test_dice_loss.py and it passed) and what procedures do I need to do to make the pr?
I have also noticed a few weird things. The first one is ignore_index in dice loss isn't actually used although it should. also, the default value of ignore_index across losses is inconsistent with a few as -100 and some as 255.
Thanks in advance!

@xiexinch
Copy link
Collaborator

Hi @yeedrag,
Thanks for your interest, just follow this workflow, create a pull request, then we can review and merge your code.

@sasniper007
Copy link

sasniper007 commented Jul 26, 2023

I modified the dice_loss.py like below
微信截图_20230726222722
def forward(self, pred, target, weight=None, avg_factor=None, reduction_override=None, ignore_index=255, **kwargs):
it works well

@yeedrag
Copy link
Contributor

yeedrag commented Jul 26, 2023

Hi @yeedrag, Thanks for your interest, just follow this workflow, create a pull request, then we can review and merge your code.

Hey @xiexinch, quick question: Im trying to commit my changes, and the pre-commit update-model-index hook modifies all the metafile.yaml files so I couldn't pass the pre-commit check if I only committed the dice_loss.py. Should I also commit the yaml files where the only change is the file paths are modified to be local, or is there a workaround? Sorry I'm not familiar with this and Thanks in advance.
(I'm not sure whether this should be opened as a new issue, as I think this is a rather insignificant problem)

xiexinch pushed a commit that referenced this issue Aug 9, 2023
Added ignore_index param to forward(),
also implemented one hot encoding to ensure the dims of target matches
pred.

Thanks for your contribution and we appreciate it a lot. The following
instructions would make your pull request more healthy and more easily
get feedback. If you do not understand some items, don't worry, just
make the pull request and seek help from maintainers.

## Motivation

Please describe the motivation of this PR and the goal you want to
achieve through this PR.

Attempted to solve the problems mentioned by #3172 

## Modification

Please briefly describe what modification is made in this PR.

Added ignore_index into forward function (although the dice loss itself
does not actually take account for it for some reason).
Added _expand_onehot_labels_dice, which takes the target with shape [N,
H, W] into [N, num_classes, H, W].

## BC-breaking (Optional)

Does the modification introduce changes that break the
backward-compatibility of the downstream repos?
If so, please describe how it breaks the compatibility and how the
downstream projects should modify their code to keep compatibility with
this PR.

## Use cases (Optional)

If this PR introduces a new feature, it is better to list some use cases
here, and update the documentation.

## Checklist

1. Pre-commit or other linting tools are used to fix the potential lint
issues.
2. The modification is covered by complete unit tests. If not, please
add more unit test to ensure the correctness.
3. If the modification has potential influence on downstream projects,
this PR should be tested with downstream projects, like MMDet or
MMDet3D.
4. The documentation has been modified accordingly, like docstring or
example tutorials.

This is my first time contributing to open-source code, so I might have
made some stupid mistakes. Please don't hesitate to point it out.
@ff98li
Copy link
Author

ff98li commented Aug 19, 2023

@yeedrag Thank you so much for the fix! I'm closing this issue now as the fix has been merged.

@ff98li ff98li closed this as completed Aug 19, 2023
@TommyZihao
Copy link

same question

angiecao pushed a commit to angiecao/mmsegmentation that referenced this issue Aug 31, 2023
…ab#3237)

Added ignore_index param to forward(),
also implemented one hot encoding to ensure the dims of target matches
pred.

Thanks for your contribution and we appreciate it a lot. The following
instructions would make your pull request more healthy and more easily
get feedback. If you do not understand some items, don't worry, just
make the pull request and seek help from maintainers.

## Motivation

Please describe the motivation of this PR and the goal you want to
achieve through this PR.

Attempted to solve the problems mentioned by open-mmlab#3172 

## Modification

Please briefly describe what modification is made in this PR.

Added ignore_index into forward function (although the dice loss itself
does not actually take account for it for some reason).
Added _expand_onehot_labels_dice, which takes the target with shape [N,
H, W] into [N, num_classes, H, W].

## BC-breaking (Optional)

Does the modification introduce changes that break the
backward-compatibility of the downstream repos?
If so, please describe how it breaks the compatibility and how the
downstream projects should modify their code to keep compatibility with
this PR.

## Use cases (Optional)

If this PR introduces a new feature, it is better to list some use cases
here, and update the documentation.

## Checklist

1. Pre-commit or other linting tools are used to fix the potential lint
issues.
2. The modification is covered by complete unit tests. If not, please
add more unit test to ensure the correctness.
3. If the modification has potential influence on downstream projects,
this PR should be tested with downstream projects, like MMDet or
MMDet3D.
4. The documentation has been modified accordingly, like docstring or
example tutorials.

This is my first time contributing to open-source code, so I might have
made some stupid mistakes. Please don't hesitate to point it out.
emily-lin pushed a commit to emily-lin/mmsegmentation that referenced this issue Nov 18, 2023
…ab#3237)

Added ignore_index param to forward(),
also implemented one hot encoding to ensure the dims of target matches
pred.

Thanks for your contribution and we appreciate it a lot. The following
instructions would make your pull request more healthy and more easily
get feedback. If you do not understand some items, don't worry, just
make the pull request and seek help from maintainers.

## Motivation

Please describe the motivation of this PR and the goal you want to
achieve through this PR.

Attempted to solve the problems mentioned by open-mmlab#3172 

## Modification

Please briefly describe what modification is made in this PR.

Added ignore_index into forward function (although the dice loss itself
does not actually take account for it for some reason).
Added _expand_onehot_labels_dice, which takes the target with shape [N,
H, W] into [N, num_classes, H, W].

## BC-breaking (Optional)

Does the modification introduce changes that break the
backward-compatibility of the downstream repos?
If so, please describe how it breaks the compatibility and how the
downstream projects should modify their code to keep compatibility with
this PR.

## Use cases (Optional)

If this PR introduces a new feature, it is better to list some use cases
here, and update the documentation.

## Checklist

1. Pre-commit or other linting tools are used to fix the potential lint
issues.
2. The modification is covered by complete unit tests. If not, please
add more unit test to ensure the correctness.
3. If the modification has potential influence on downstream projects,
this PR should be tested with downstream projects, like MMDet or
MMDet3D.
4. The documentation has been modified accordingly, like docstring or
example tutorials.

This is my first time contributing to open-source code, so I might have
made some stupid mistakes. Please don't hesitate to point it out.
nahidnazifi87 pushed a commit to nahidnazifi87/mmsegmentation_playground that referenced this issue Apr 5, 2024
…ab#3237)

Added ignore_index param to forward(),
also implemented one hot encoding to ensure the dims of target matches
pred.

Thanks for your contribution and we appreciate it a lot. The following
instructions would make your pull request more healthy and more easily
get feedback. If you do not understand some items, don't worry, just
make the pull request and seek help from maintainers.

## Motivation

Please describe the motivation of this PR and the goal you want to
achieve through this PR.

Attempted to solve the problems mentioned by open-mmlab#3172 

## Modification

Please briefly describe what modification is made in this PR.

Added ignore_index into forward function (although the dice loss itself
does not actually take account for it for some reason).
Added _expand_onehot_labels_dice, which takes the target with shape [N,
H, W] into [N, num_classes, H, W].

## BC-breaking (Optional)

Does the modification introduce changes that break the
backward-compatibility of the downstream repos?
If so, please describe how it breaks the compatibility and how the
downstream projects should modify their code to keep compatibility with
this PR.

## Use cases (Optional)

If this PR introduces a new feature, it is better to list some use cases
here, and update the documentation.

## Checklist

1. Pre-commit or other linting tools are used to fix the potential lint
issues.
2. The modification is covered by complete unit tests. If not, please
add more unit test to ensure the correctness.
3. If the modification has potential influence on downstream projects,
this PR should be tested with downstream projects, like MMDet or
MMDet3D.
4. The documentation has been modified accordingly, like docstring or
example tutorials.

This is my first time contributing to open-source code, so I might have
made some stupid mistakes. Please don't hesitate to point it out.
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

No branches or pull requests

10 participants