• Deformable DETR进行目标检测,解决size mismatch问题


    问题描述:

    strict=False 但还是size mismatch for []: copying a param with shape [] from checkpoint,the shape in cur []

    接着(6条消息) Deformable DETR环境配置和应用_Alaso_soso的博客-CSDN博客_deformable detr

     上面的链接进行继续写,发现很多人同样也遇到了,我遇到的这个问题,找到了解决方案,记录一下,或许也可以解决在训练自己的模型的时候出现的size问题不匹配的问题.

    背景:

    前期只是用官方给的完整的deformable detr 模型进行预测的,因此没有出现size不匹配的问题,后面报了一大堆类似一下的错误,网上的有的人用pop解决了问题,然而我却不ok,这次使用的预训练模型是r50_deformable_detr_single_scale-checkpoint.pth:

    size mismatch for transformer.level_embed: copying a param with shape torch.Size([1, 256]) from checkpoint torch.Size([64, 256])
    

    解决方案:

    首先定位到detect.py文件:detect.py参考链接,修改model_path,以为可以直接运行,结果gg了:

    model_path = './r50_deformable_detr_single_scale-checkpoint.pth'

    Deformable-DETR部署和体验 - 简书 (jianshu.com)

     在定位到load_model方法:

    1. def load_model(model_path, args):
    2. model, _, _ = build_model(args)
    3. model.cuda()
    4. model.eval()
    5. ckpt=state_dict = torch.load(model_path) # <-----------修改加载模型的路径
    6. msg=model.load_state_dict(state_dict["model"],strict=False)
    7. model.to(device)
    8. print("load model sucess")
    9. return model

     在定位到 model, _, _ = build_model(args),这一句创建模型代码上:

    1. def build_model(args):
    2. return build(args)

    在定位到build(args)这里

    1. def build(args):
    2. # 类别个数
    3. # num_class = 20
    4. # num_classes = 20 if args.dataset_file != 'coco' else (num_class + 1)
    5. num_classes = 20 if args.dataset_file != 'coco' else 91
    6. if args.dataset_file == "coco_panoptic":
    7. num_classes = 250
    8. device = torch.device(args.device)
    9. backbone = build_backbone(args)
    10. transformer = build_deforamble_transformer(args)
    11. model = DeformableDETR(
    12. backbone,
    13. transformer,
    14. num_classes=num_classes,
    15. num_queries=args.num_queries,
    16. num_feature_levels=args.num_feature_levels,
    17. aux_loss=args.aux_loss,
    18. with_box_refine=args.with_box_refine,
    19. two_stage=args.two_stage,
    20. )
    21. if args.masks:
    22. model = DETRsegm(model, freeze_detr=(args.frozen_weights is not None))
    23. matcher = build_matcher(args)
    24. weight_dict = {'loss_ce': args.cls_loss_coef, 'loss_bbox': args.bbox_loss_coef}
    25. weight_dict['loss_giou'] = args.giou_loss_coef
    26. if args.masks:
    27. weight_dict["loss_mask"] = args.mask_loss_coef
    28. weight_dict["loss_dice"] = args.dice_loss_coef
    29. # TODO this is a hack
    30. if args.aux_loss:
    31. aux_weight_dict = {}
    32. for i in range(args.dec_layers - 1):
    33. aux_weight_dict.update({k + f'_{i}': v for k, v in weight_dict.items()})
    34. aux_weight_dict.update({k + f'_enc': v for k, v in weight_dict.items()})
    35. weight_dict.update(aux_weight_dict)
    36. losses = ['labels', 'boxes', 'cardinality']
    37. if args.masks:
    38. losses += ["masks"]
    39. # num_classes, matcher, weight_dict, losses, focal_alpha=0.25
    40. criterion = SetCriterion(num_classes, matcher, weight_dict, losses, focal_alpha=args.focal_alpha)
    41. criterion.to(device)
    42. postprocessors = {'bbox': PostProcess()}
    43. if args.masks:
    44. postprocessors['segm'] = PostProcessSegm()
    45. if args.dataset_file == "coco_panoptic":
    46. is_thing_map = {i: i <= 90 for i in range(201)}
    47. postprocessors["panoptic"] = PostProcessPanoptic(is_thing_map, threshold=0.85)
    48. return model, criterion, postprocessors

    根据自己的类别修改num_classes,args是参数配置,此时就定位到configs文件夹下对应的.sh文件了。

     注意到这两个配置文件的区别在于--num_feature_levels 1,问题就出在这里,需要在运行detect.py文件的时候添加上这个配置参数。

    python detect.py --num_feature_levels 1

    这里就结束了,可以正常运行,进行图片视频的预测了。

     感谢某人的帮助捏。*★,°*:.☆( ̄▽ ̄)/$:*.°★* 。

  • 相关阅读:
    C++左右值及引用
    Leetcode 410. Split Array Largest Sum (Binary Search经典)
    节省时间的分层测试,到底怎么做?
    最近项目上线太忙了-后期会发布大厂物流进厂文章
    分布(五)利用python绘制蜂群图
    大模型分布式训练并行技术(一)-概述
    亚马逊鲲鹏系统可全自动化批量操作亚马逊买家号
    【服务器数据恢复】农科院某研究所DELL服务器raid5两块硬盘掉线的数据恢复
    sql实例-2
    从人工测绘到无人机航测:探索测绘技术的巨大变革
  • 原文地址:https://blog.csdn.net/qq_44808827/article/details/126794548