object has no attribute ‘show_result‘,mmdetection3.0最新检测方法

之前是使用 show_result 来绘制检测结果,并保存,现在已经无法使用,会报错
‘xxx Model’ object has no attribute ‘show_result’

最新的方法如下:

# -*- coding: UTF-8 -*-

import os
from mmdet.apis import init_detector, inference_detector
from mmdet.registry import VISUALIZERS
import mmcv

# 使用前修改下面的路径
image_path = r'E:\Models\mmdetection-3.0.0\data\coco\test\JPEGImages'
savepath = r'E:\Models_exp\mmdetection\yolact\save_for_clamp'

config_file = r'E:\Models\mmdetection-3.0.0\work_dirs\yolact_r50_1xb8-55e_coco_clamp\yolact_r50_1xb8-55e_coco_clamp.py'
checkpoint_file = r'E:\Models\mmdetection-3.0.0\work_dirs\yolact_r50_1xb8-55e_coco_clamp\epoch_55.pth'

# gpu 不行 会 Out of memory
device = 'cuda:0'

model = init_detector(config_file, checkpoint_file, device=device)

visualizer = VISUALIZERS.build(model.cfg.visualizer)
# the dataset_meta is loaded from the checkpoint and
# then pass to the model in init_detector
visualizer.dataset_meta = model.dataset_meta

for filename in os.listdir(image_path):
    imgpath = os.path.join(image_path, filename)

    print('process ', imgpath)
    img = mmcv.imread(imgpath)
    result = inference_detector(model, img)
    out_file = os.path.join(savepath, filename)

    ### old implementation invalid
    # show_result_pyplot(model, img, result, out_file, score_thr=0.3)
    ###

    # show the results
    visualizer.add_datasample('result',
                              img, data_sample=result,
                              draw_gt=False,
                              wait_time=0,
                              out_file=out_file,
                              pred_score_thr=0.3
                              )
    # vis
    # visualizer.show()
    # break

有问题,后期维护更新,欢迎留言、进群讨论或私聊:【群号:392784757】

### 解决 'DetDataSample' 对象没有 '_gt_sem_seg' 属性的问题 在 MMDetection 中遇到 `'DetDataSample' object has no attribute '_gt_sem_seg'` 的错误通常是因为版本兼容性问题或是配置文件设置不当所致[^1]。 MMDetection 是一个模块化设计的目标检测工具箱,在不同版本之间可能存在 API 变更。如果使用的是较新的 MMDetection 版本,则某些旧版中存在的属性可能已被移除或重命名。对于 `_gt_sem_seg` 这一特定情况,该属性主要用于语义分割任务中的标注数据存储,而在目标检测任务中并不一定存在此字段[^2]。 为了修复这个问题: #### 方法 1: 更新至最新稳定版本 确保所使用的 MMDetection 和其依赖库均为最新的稳定版本,这可以避免由于版本差异引起的各种潜在问题。 ```bash pip install mmcv-full -f https://download.openmmlab.com/mmcv/dist/cu113/torch1.10/index.html pip install mmdet ``` #### 方法 2: 修改配置文件 检查并调整配置文件以匹配当前使用的模型和任务需求。如果是基于实例分割或者全景分割的任务,确认 `data_preprocessor` 配置项已正确设置了相应的参数。 ```yaml model = dict( ... data_preprocessor=dict( type='DetDataPreprocessor', mean=[123.675, 116.28, 103.53], std=[58.395, 57.12, 57.375], bgr_to_rgb=True, pad_size_divisor=32), ... ) ``` #### 方法 3: 自定义 Data Sample 类 当确实需要扩展默认的数据样本类来支持额外的功能时,可以通过继承原有类的方式实现自定义功能而不破坏现有结构。 ```python from mmdet.structures import DetDataSample class CustomDetDataSample(DetDataSample): @property def gt_sem_seg(self): if not hasattr(self, "_gt_sem_seg"): self._gt_sem_seg = None return self._gt_sem_seg @gt_sem_seg.setter def gt_sem_seg(self, value): setattr(self, "_gt_sem_seg", value) # 使用新创建的CustomDetDataSample替代原有的DetDataSample ``` 通过上述方法之一应该能够有效解决 `'DetDataSample' object has no attribute '_gt_sem_seg'` 错误。具体选择哪种方式取决于实际应用场景以及项目的需求。
评论 23
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值