QingyangZhang / QMF

Quality-aware multimodal fusion on ICML 2023
MIT License
71 stars 3 forks source link

RuntimeError: margin_ranking_loss : All input tensors should have same dimension but got sizes #4

Closed Comfident closed 3 months ago

Comfident commented 4 months ago

作者您好🤝,抱歉打扰,我在运行RGBD-scene-recognition文件夹下的train_QMF_nyud2.py时出现以下报错:

RuntimeError: margin_ranking_loss : All input tensors should have same dimension but got sizes: input1: torch.Size([32, 1]), input2: torch.Size([32, 32]), target: torch.Size([32]) 

下面是完整的报错信息

C:\Users\ZZX\anaconda3\envs\QMF\python.exe C:/Users/ZZX/Desktop/QMF-main/QMF-main/RGBD-scene-recognition/train_QMF_nyud2.py
INFO - 07/07/24 21:13:53 - 0:00:00 - CONTENT_MODEL_PATH: resnet18-f37072fd.pth
                                     FINE_SIZE: 224
                                     LOAD_SIZE: 256
                                     annealing_epoch: 10
                                     batch_sz: 32
                                     data_path: datasets/nyud2_trainvaltest
                                     dropout: 0.1
                                     gradient_accumulation_steps: 3
                                     hidden: []
                                     hidden_sz: 768
                                     img_embed_pool_type: avg
                                     img_hidden_sz: 512
                                     include_bn: True
                                     lamb: 0.1
                                     lr: 0.0001
                                     lr_factor: 0.3
                                     lr_patience: 10
                                     max_epochs: 500
                                     n_classes: 10
                                     n_workers: 4
                                     name: s
                                     num_image_embeds: 1
                                     patience: 20
                                     savedir: ./savepath/QMF/nyud/pretrained_resnet18/s\1
                                     seed: 1
  0%|          | 0/25 [00:10<?, ?it/s]
Traceback (most recent call last):
  File "C:\Users\ZZX\Desktop\QMF-main\QMF-main\RGBD-scene-recognition\train_QMF_nyud2.py", line 291, in <module>
    cli_main()
  File "C:\Users\ZZX\Desktop\QMF-main\QMF-main\RGBD-scene-recognition\train_QMF_nyud2.py", line 285, in cli_main
    train(args)
  File "C:\Users\ZZX\Desktop\QMF-main\QMF-main\RGBD-scene-recognition\train_QMF_nyud2.py", line 217, in train
    loss, depth_out, rgb_out, depthrgb, tgt = model_forward_train(i_epoch, model, args, batch, depth_history, rgb_history)
  File "C:\Users\ZZX\Desktop\QMF-main\QMF-main\RGBD-scene-recognition\train_QMF_nyud2.py", line 99, in model_forward_train
    depth_rank_loss = rank_loss(depth_conf, idx, depth_history)
  File "C:\Users\ZZX\Desktop\QMF-main\QMF-main\RGBD-scene-recognition\train_QMF_nyud2.py", line 70, in rank_loss
    ranking_loss = nn.MarginRankingLoss(margin=0.0)(rank_input1,
  File "C:\Users\ZZX\anaconda3\envs\QMF\lib\site-packages\torch\nn\modules\module.py", line 1532, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "C:\Users\ZZX\anaconda3\envs\QMF\lib\site-packages\torch\nn\modules\module.py", line 1541, in _call_impl
    return forward_call(*args, **kwargs)
  File "C:\Users\ZZX\anaconda3\envs\QMF\lib\site-packages\torch\nn\modules\loss.py", line 1353, in forward
    return F.margin_ranking_loss(input1, input2, target, margin=self.margin, reduction=self.reduction)
  File "C:\Users\ZZX\anaconda3\envs\QMF\lib\site-packages\torch\nn\functional.py", line 3399, in margin_ranking_loss
    raise RuntimeError(
RuntimeError: margin_ranking_loss : All input tensors should have same dimension but got sizes: input1: torch.Size([32, 1]), input2: torch.Size([32, 32]), target: torch.Size([32]) 

Process finished with exit code 1

由于我是windows端, 下图中红色框是我对该文件中涉及参数路径的修改:

image

针对--CONTENT_MODEL_PATH这个参数,您项目中的默认路径为:

/media/zhangqingyang/DF/checkpoint/resnet18_pretrained.pth

因为我在项目中没有找到这个预训练模型,同时在README.md中也没有,于是使用官方下载的resnet18-f37072fd.pth,我不确定这是不是错误原因,感到有些困惑

如果这个resnet18_pretrained.pth文件是您自己准备的,能麻烦您上传到项目里吗,谢谢🙂

期待您的回复,🌹

QingyangZhang commented 3 months ago

作者您好🤝,抱歉打扰,我在运行RGBD-scene-recognition文件夹下的train_QMF_nyud2.py时出现以下报错:

RuntimeError: margin_ranking_loss : All input tensors should have same dimension but got sizes: input1: torch.Size([32, 1]), input2: torch.Size([32, 32]), target: torch.Size([32]) 

下面是完整的报错信息

C:\Users\ZZX\anaconda3\envs\QMF\python.exe C:/Users/ZZX/Desktop/QMF-main/QMF-main/RGBD-scene-recognition/train_QMF_nyud2.py
INFO - 07/07/24 21:13:53 - 0:00:00 - CONTENT_MODEL_PATH: resnet18-f37072fd.pth
                                     FINE_SIZE: 224
                                     LOAD_SIZE: 256
                                     annealing_epoch: 10
                                     batch_sz: 32
                                     data_path: datasets/nyud2_trainvaltest
                                     dropout: 0.1
                                     gradient_accumulation_steps: 3
                                     hidden: []
                                     hidden_sz: 768
                                     img_embed_pool_type: avg
                                     img_hidden_sz: 512
                                     include_bn: True
                                     lamb: 0.1
                                     lr: 0.0001
                                     lr_factor: 0.3
                                     lr_patience: 10
                                     max_epochs: 500
                                     n_classes: 10
                                     n_workers: 4
                                     name: s
                                     num_image_embeds: 1
                                     patience: 20
                                     savedir: ./savepath/QMF/nyud/pretrained_resnet18/s\1
                                     seed: 1
  0%|          | 0/25 [00:10<?, ?it/s]
Traceback (most recent call last):
  File "C:\Users\ZZX\Desktop\QMF-main\QMF-main\RGBD-scene-recognition\train_QMF_nyud2.py", line 291, in <module>
    cli_main()
  File "C:\Users\ZZX\Desktop\QMF-main\QMF-main\RGBD-scene-recognition\train_QMF_nyud2.py", line 285, in cli_main
    train(args)
  File "C:\Users\ZZX\Desktop\QMF-main\QMF-main\RGBD-scene-recognition\train_QMF_nyud2.py", line 217, in train
    loss, depth_out, rgb_out, depthrgb, tgt = model_forward_train(i_epoch, model, args, batch, depth_history, rgb_history)
  File "C:\Users\ZZX\Desktop\QMF-main\QMF-main\RGBD-scene-recognition\train_QMF_nyud2.py", line 99, in model_forward_train
    depth_rank_loss = rank_loss(depth_conf, idx, depth_history)
  File "C:\Users\ZZX\Desktop\QMF-main\QMF-main\RGBD-scene-recognition\train_QMF_nyud2.py", line 70, in rank_loss
    ranking_loss = nn.MarginRankingLoss(margin=0.0)(rank_input1,
  File "C:\Users\ZZX\anaconda3\envs\QMF\lib\site-packages\torch\nn\modules\module.py", line 1532, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "C:\Users\ZZX\anaconda3\envs\QMF\lib\site-packages\torch\nn\modules\module.py", line 1541, in _call_impl
    return forward_call(*args, **kwargs)
  File "C:\Users\ZZX\anaconda3\envs\QMF\lib\site-packages\torch\nn\modules\loss.py", line 1353, in forward
    return F.margin_ranking_loss(input1, input2, target, margin=self.margin, reduction=self.reduction)
  File "C:\Users\ZZX\anaconda3\envs\QMF\lib\site-packages\torch\nn\functional.py", line 3399, in margin_ranking_loss
    raise RuntimeError(
RuntimeError: margin_ranking_loss : All input tensors should have same dimension but got sizes: input1: torch.Size([32, 1]), input2: torch.Size([32, 32]), target: torch.Size([32]) 

Process finished with exit code 1

由于我是windows端, 下图中红色框是我对该文件中涉及参数路径的修改:

image

针对--CONTENT_MODEL_PATH这个参数,您项目中的默认路径为:

/media/zhangqingyang/DF/checkpoint/resnet18_pretrained.pth

因为我在项目中没有找到这个预训练模型,同时在README.md中也没有,于是使用官方下载的resnet18-f37072fd.pth,我不确定这是不是错误原因,感到有些困惑

如果这个resnet18_pretrained.pth文件是您自己准备的,能麻烦您上传到项目里吗,谢谢🙂

期待您的回复,🌹

Hi,这个error可以这样解决: 5201720669161_ pic

预训练模型使用torch官方提供的即可。

Comfident commented 3 months ago

非常感谢您的回复!