size mismatch for roi_heads.box_predictor.cls_score.weight: copying a param with shape torch.Size([9

1. 报错 RuntimeError: Error(s) in loading state_dict for FasterRCNN: size mismatch for roi_heads.box_predictor.cls_score.weight: copying a param with shape torch.Size([91, 1024]) from checkpoint, the shape in current model is torch.Size([80, 1024]).

pytorch 1.11.0
torchvision 0.12.0

Traceback (most recent call last):
  File "C:\Users\Administrator\.IntelliJIdea2017.2\config\plugins\python\helpers\pydev\pydevd.py", line 1596, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)
  File "C:\Users\Administrator\.IntelliJIdea2017.2\config\plugins\python\helpers\pydev\pydevd.py", line 1023, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Users\Administrator\.IntelliJIdea2017.2\config\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "E:/mat/py-demo-22/p220625/detect_image.py", line 60, in <module>
    num_classes=len(CLASSES), pretrained_backbone=True).to(DEVICE)
  File "D:\python374\lib\site-packages\torchvision\models\detection\faster_rcnn.py", line 399, in fasterrcnn_resnet50_fpn
    model.load_state_dict(state_dict)
  File "D:\python374\lib\site-packages\torch\nn\modules\module.py", line 1498, in load_state_dict
    self.__class__.__name__, "\n\t".join(error_msgs)))
RuntimeError: Error(s) in loading state_dict for FasterRCNN:
	size mismatch for roi_heads.box_predictor.cls_score.weight: copying a param with shape torch.Size([91, 1024]) from checkpoint, the shape in current model is torch.Size([80, 1024]).
	size mismatch for roi_heads.box_predictor.cls_score.bias: copying a param with shape torch.Size([91]) from checkpoint, the shape in current model is torch.Size([80]).
	size mismatch for roi_heads.box_predictor.bbox_pred.weight: copying a param with shape torch.Size([364, 1024]) from checkpoint, the shape in current model is torch.Size([320, 1024]).
	size mismatch for roi_heads.box_predictor.bbox_pred.bias: copying a param with shape torch.Size([364]) from checkpoint, the shape in current model is torch.Size([320]).

解决

  • model.load_state_dict(torch.load(“./model.pth”), False) 未解决
  • 调整pytorch版本:pip install pytorch==1.7.1 未解决
    报错
Traceback (most recent call last):
  File "C:\Users\Administrator\.IntelliJIdea2017.2\config\plugins\python\helpers\pydev\pydevd.py", line 1596, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)
  File "C:\Users\Administrator\.IntelliJIdea2017.2\config\plugins\python\helpers\pydev\pydevd.py", line 1023, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Users\Administrator\.IntelliJIdea2017.2\config\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "E:/mat/py-demo-22/p220625/detect_image.py", line 50, in <module>
    "frcnn-mobilenet": detection.fasterrcnn_mobilenet_v3_large_320_fpn,
AttributeError: module 'torchvision.models.detection' has no attribute 'fasterrcnn_mobilenet_v3_large_320_fpn'
  • 加载重新保存

解决办法1: https://github.com/facebookresearch/maskrcnn-benchmark/issues/430size mismatch for roi_heads.box_predictor.cls_score.weight: copying a param with shape torch.Size([9_第1张图片

解决办法2:

size mismatch for roi_heads.box_predictor.cls_score.weight: copying a param with shape torch.Size([9_第2张图片

你可能感兴趣的:(深度学习,pytorch,深度学习,pytorch,python)