mmpose icon indicating copy to clipboard operation
mmpose copied to clipboard

Using heatmap loss error

Open LjIA26 opened this issue 2 years ago • 2 comments

Hello Thanks for the work you have done. Could you help me with the following error?

Thanks for your error report and we appreciate it a lot. If you feel we have helped you, give us a STAR! :satisfied:

Checklist

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

Describe the bug

A clear and concise description of what the bug is.

Reproduction

  • What command or script did you run?
python tools/train.py configs/plants4.py --work-dir work_dir/

  • What config did you run?
td-hm_hrnet-w32_8xb64-210e_animalpose-256x256
  • Did you make any modifications on the code or config? Did you understand what you have modified?

Tried the Adaptive Wing Loss.

head=dict( type='HeatmapHead', in_channels=32, out_channels=100, deconv_out_channels=None, loss=dict(type='AdaptiveWingLoss', use_target_weight=True), decoder=codec)

  • What dataset did you use?

Custom dataset

Environment

  1. Please run PYTHONPATH=${PWD}:$PYTHONPATH python mmpose/utils/collect_env.py to collect necessary environment information and paste it here.
  2. You may add addition that may be helpful for locating the problem, such as
  • How you installed PyTorch [e.g., pip, conda, source]
  • Other environment variables that may be related (such as $PATH, $LD_LIBRARY_PATH, $PYTHONPATH, etc.)

Error traceback

If applicable, paste the error traceback here.

  File "C:\Users\LJ\anaconda3\envs\toolkit\lib\site-packages\torch\nn\modules\module.py", line 1130, in _call_impl
    return forward_call(*input, **kwargs)
  File "d:\toolkit\mmpose\mmpose\models\pose_estimators\base.py", line 74, in forward
    return self.loss(inputs, data_samples)
  File "d:\toolkit\mmpose\mmpose\models\pose_estimators\topdown.py", line 109, in loss
    self.head.loss(feats, data_samples, train_cfg=self.train_cfg))
  File "d:\toolkit\mmpose\mmpose\models\heads\heatmap_heads\heatmap_head.py", line 330, in loss
    loss = self.loss_module(pred_fields, gt_heatmaps, keypoint_weights)
  File "C:\Users\LJ\anaconda3\envs\toolkit\lib\site-packages\torch\nn\modules\module.py", line 1130, in _call_impl
    return forward_call(*input, **kwargs)
  File "d:\toolkit\mmpose\mmpose\models\losses\heatmap_loss.py", line 84, in forward
    loss = self.criterion(output * target_weights,

RuntimeError: The size of tensor a (64) must match the size of tensor b (100) at non-singleton dimension 3

Bug fix

If you have already identified the reason, you can provide the information here. If you are willing to create a PR to fix it, please also leave a comment here and that would be much appreciated!

LjIA26 avatar Feb 01 '23 23:02 LjIA26

Deleted the " if self.use_target_weight: loss = self.criterion(output * target_weights, target * target_weights) else: " and left the line: loss = self.criterion(output, target)

and now it worked. I don't get what the problem was though.

LjIA26 avatar Feb 02 '23 01:02 LjIA26

Sorry, there is a bug in the AdaptiveWingLoss class. The output and target_weights have different numbers of dimensions, so they cannot be multiplied directly. You can fix this bug by expanding the dimension of target_weights

if self.use_target_weight:
    target_weights = target_weights.unsqueeze(2).unsqueeze(3)
    loss = self.criterion(output * target_weights, target * target_weights)

Ben-Louis avatar Feb 02 '23 10:02 Ben-Louis

Thank you!

LjIA26 avatar Feb 02 '23 21:02 LjIA26