一问难求 2022-01-20 11:13 采纳率: 60%
浏览 238
已结题

代码运行错误:AttributeError: 'NoneType' object has no attribute 'find'

问题遇到的现象和发生背景

代码运行报错:AttributeError: 'NoneType' object has no attribute 'find'
ssd算法跑自制数据集

问题相关代码,请勿粘贴截图
def __call__(self, target, width, height):
    """
    Arguments:
        target (annotation) : the target annotation to be made usable
            will be an ET.Element
    Returns:
        a list containing lists of bounding boxes  [bbox coords, class name]
    """
    res = []
    for obj in target.iter('object'):
        difficult = int(obj.find('difficult').text) == 1
        if not self.keep_difficult and difficult:
            continue
        name = obj.find('name').text.lower().strip()
        bbox = obj.find('bndbox')

        pts = ['xmin', 'ymin', 'xmax', 'ymax']
        bndbox = []
        for i, pt in enumerate(pts):
            cur_pt = int(bbox.find(pt).text) - 1
            # scale height or width
            cur_pt = cur_pt / width if i % 2 == 0 else cur_pt / height
            bndbox.append(cur_pt)
        label_idx = self.class_to_ind[name]
        bndbox.append(label_idx)
        res += [bndbox]  # [xmin, ymin, xmax, ymax, label_ind]
        # img_id = target.find('filename').text[:-4]

    return res  # [[xmin, ymin, xmax, ymax, label_ind], ... ]
运行结果及报错内容

File "C:\Users\huayuan001.conda\envs\pyqxr\lib\site-packages\torch\utils\data_utils\fetch.py", line 44, in
data = [self.dataset[idx] for idx in possibly_batched_index]
File "D:\SSD\ssd.pytorch\data\voc0712.py", line 120, in getitem
im, gt, h, w = self.pull_item(index)
File "D:\SSD\ssd.pytorch\data\voc0712.py", line 135, in pull_item
target = self.target_transform(target, width, height)
File "D:\SSD\ssd.pytorch\data\voc0712.py", line 73, in call
cur_pt = (bbox.find(pt).text) - 1
AttributeError: 'NoneType' object has no attribute 'find'

我的解答思路和尝试过的方法

更改类型

我想要达到的结果

运行正确

  • 写回答

2条回答 默认 最新

  • 於黾 2022-01-20 11:18
    关注

    你在调用obj.find或者bbox.find之前,应该先判断它们本身是非空的呀

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 1月28日
  • 已采纳回答 1月20日
  • 创建了问题 1月20日

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog