智晨爱代码 2023-11-22 09:21 采纳率: 71.4%
浏览 49
已结题

训练Mask Rcnn时报错:gt_mask = instance[‘mask‘] KeyError: ‘mask‘

我用自己的数据集尝试训练Mask Rcnn我已经配置好了环境,且跑通了Voc的官方代码,但无法跑自己的数据集。我本来是txt格式在Roboflow中转换成了类似VOC的xml格式。

对比了一下VOC官方数据集,我多出了0 不知道是否是这个的问题。
这是我的数据集中xml格式里的样例

<annotation>
    <folder></folder>
    <filename>China_Drone_000000_jpg.rf.07a740aaf2b5fb932dcf2001c49eaa6e.jpg</filename>
    <path>China_Drone_000000_jpg.rf.07a740aaf2b5fb932dcf2001c49eaa6e.jpg</path>
    <source>
        <database>roboflow.com</database>
    </source>
    <size>
        <width>640</width>
        <height>640</height>
        <depth>3</depth>
    </size>
    <segmented>0</segmented>
    <object>
        <name>D10</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <occluded>0</occluded>
        <bndbox>
            <xmin>155</xmin>
            <xmax>239</xmax>
            <ymin>336</ymin>
            <ymax>376</ymax>
        </bndbox>
    </object>
</annotation>

这是我的报错情况

/home/asus/miniconda3/envs/rcnn/bin/python /home/asus/Desktop/wjl-project/Mask_RCNN/mask_rcnn/train.py 
Namespace(amp=False, aspect_ratio_group_factor=3, batch_size=2, data_path='', device='cuda:1', epochs=26, lr=0.004, lr_gamma=0.1, lr_steps=[16, 22], momentum=0.9, num_classes=6, output_dir='./save_weights', pretrain=True, resume='', start_epoch=0, weight_decay=0.0001)
Using cuda device training.
INFO: no objects in VOCdevkit/VOC2012/Annotations/China_Drone_000256_jpg.rf.ab91e2b57ecad4b64d9a84b0b5700b48.xml, skip this annotation file.
INFO: no objects in VOCdevkit/VOC2012/Annotations/China_Drone_001391_jpg.rf.96d5321ab049c025d7fa8f65d7d30684.xml, skip this annotation file.
INFO: no objects in VOCdevkit/VOC2012/Annotations/China_Drone_000170_jpg.rf.190d0cf1c18f88b7cc0e37f616fb25e2.xml, skip this annotation file.
INFO: no objects in VOCdevkit/VOC2012/Annotations/China_Drone_000256_jpg.rf.e173a96af0d22482bbba8089cca73a4f.xml, skip this annotation file.
Using [0, 0.5, 0.6299605249474366, 0.7937005259840997, 1.0, 1.2599210498948732, 1.5874010519681994, 2.0, inf] as bins for aspect ratio quantization
Count of instances per bin: [4150]
Using 2 dataloader workers
INFO: no objects in VOCdevkit/VOC2012/Annotations/China_Drone_001134_jpg.rf.51e0a6c595b78bce2d912dfd6ceaf882.xml, skip this annotation file.
INFO: no objects in VOCdevkit/VOC2012/Annotations/China_Drone_000256_jpg.rf.4911be1b87dec91644f29aa503d5d777.xml, skip this annotation file.
INFO: no objects in VOCdevkit/VOC2012/Annotations/China_Drone_001134_jpg.rf.404195a7aa8cf62f9742f7e4c0da359c.xml, skip this annotation file.
INFO: no objects in VOCdevkit/VOC2012/Annotations/China_Drone_000218_jpg.rf.112dd0036ba94a1cc2d52ff0d6e22ed5.xml, skip this annotation file.
_IncompatibleKeys(missing_keys=[], unexpected_keys=['fc.weight', 'fc.bias'])
_IncompatibleKeys(missing_keys=['roi_heads.box_predictor.cls_score.weight', 'roi_heads.box_predictor.cls_score.bias', 'roi_heads.box_predictor.bbox_pred.weight', 'roi_heads.box_predictor.bbox_pred.bias', 'roi_heads.mask_predictor.mask_fcn_logits.weight', 'roi_heads.mask_predictor.mask_fcn_logits.bias'], unexpected_keys=[])
/home/asus/miniconda3/envs/rcnn/lib/python3.8/site-packages/torch/functional.py:504: UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the indexing argument. (Triggered internally at ../aten/src/ATen/native/TensorShape.cpp:3526.)
  return _VF.meshgrid(tensors, **kwargs)  # type: ignore[attr-defined]
Traceback (most recent call last):
  File "/home/asus/Desktop/wjl-project/Mask_RCNN/mask_rcnn/train.py", line 240, in <module>
    main(args)
  File "/home/asus/Desktop/wjl-project/Mask_RCNN/mask_rcnn/train.py", line 139, in main
    mean_loss, lr = utils.train_one_epoch(model, optimizer, train_data_loader,
  File "/home/asus/Desktop/wjl-project/Mask_RCNN/mask_rcnn/train_utils/train_eval_utils.py", line 32, in train_one_epoch
    loss_dict = model(images, targets)
  File "/home/asus/miniconda3/envs/rcnn/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "/home/asus/miniconda3/envs/rcnn/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl
    return forward_call(*args, **kwargs)
  File "/home/asus/Desktop/wjl-project/Mask_RCNN/mask_rcnn/network_files/faster_rcnn_framework.py", line 94, in forward
    detections, detector_losses = self.roi_heads(features, proposals, images.image_sizes, targets)
  File "/home/asus/miniconda3/envs/rcnn/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "/home/asus/miniconda3/envs/rcnn/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1527, in _call_impl
    return forward_call(*args, **kwargs)
  File "/home/asus/Desktop/wjl-project/Mask_RCNN/mask_rcnn/network_files/roi_head.py", line 548, in forward
    gt_masks = [t["masks"] for t in targets]
  File "/home/asus/Desktop/wjl-project/Mask_RCNN/mask_rcnn/network_files/roi_head.py", line 548, in <listcomp>
    gt_masks = [t["masks"] for t in targets]
KeyError: 'masks'

恳求各位解决!

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2023-11-22 13:17
    关注

    【以下回答由 GPT 生成】

    **解决方案:**
    
    问题出在在训练Mask Rcnn时,系统在处理数据集的时候无法找到'mask'这个字段,从而导致了KeyError: 'mask'的报错。
    
    要解决这个问题,可以按照以下步骤进行:
    
    1. 检查数据集的格式是否和Mask Rcnn期望的格式一致。确保每个样本都包含正确的'mask'字段。
    
    2. 检查训练代码,确保在使用数据集时正确地引用了'mask'字段,可以查看数据集加载的部分代码。
    
    3. 确保数据预处理和加载的代码正确处理了'mask'字段。在数据预处理和加载的部分代码中,可以使用print或者logging等方法输出相关信息,查看'mask'字段的存在情况。
    
    4. 如果你的数据集格式和Mask Rcnn期望的格式有所不同,可以考虑编写自定义的数据集加载器,来适配你的数据集格式。
    
    5. 最后,确保你的训练代码和模型配置正确,特别是在涉及到'mask'字段的部分。
    
    通过逐步检查和调试,应该能够找到问题所在并解决该报错问题。
    
    如果以上方案不能解决问题,请提供更多相关的代码和数据集信息,以便更好地帮助你解决该问题。
    

    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 12月1日
  • 已采纳回答 11月23日
  • 创建了问题 11月22日

悬赏问题

  • ¥15 远程访问linux主机超时
  • ¥15 odoo17存货管理优势于中国国内该行业传统ERP或MES的详细解读和举例
  • ¥15 CPU卡指令整合指令数据都在图片上
  • ¥15 odoo17处理受托加工产品
  • ¥15 如何用MATLAB编码图三的积分
  • ¥15 圆孔衍射光强随孔径变化
  • ¥15 MacBook pro m3max上用vscode运行c语言没有反应
  • ¥15 ESP-PROG配置错误,ALL ONES
  • ¥15 结构功能耦合指标计算
  • ¥50 AI大模型精调(百度千帆、飞浆)