代码运行报错: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'
更改类型
运行正确
你在调用obj.find或者bbox.find之前,应该先判断它们本身是非空的呀
你的代码, 当 obj.find('bndbox')没有找到时
bbox 的值是 None , 这样会导致后续的代码出错。
可以考虑当 bbox 的值是 None , 不做处理。
bbox = obj.find('bndbox')
#改成
bbox = obj.find('bndbox')
if bbox == None:
continue