You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

训练SSD模型时遭遇XML解析错误,寻求技术帮助

训练SSD模型时XML解析错误的排查与解决建议

操作流程

  • 克隆jetson-inference项目,将标注完成的数据集放置在jetson-inference/python/training/detection/ssd/路径下
  • 使用ssd/models目录下的预训练模型mobileenet-v1-ssd-mp-0_675.pth执行训练命令

训练命令

!python3 train_ssd.py --dataset-type=voc --data=data/jetbot
--model-dir=models/jetbot --batch-size=2 --workers=1 --epochs=1

错误日志

2022-11-04 20:43:30 - Namespace(balance_data=False, base_net=None, base_net_lr=0.001, batch_size=2, checkpoint_folder='models/jetbot', dataset_type='voc', datasets=['data/jetbot'], debug_steps=10, extra_layers_lr=None, freeze_base_net=False, freeze_net=False, gamma=0.1, log_level='info', lr=0.01, mb2_width_mult=1.0, milestones='80,100', momentum=0.9, net='mb1-ssd', num_epochs=1, num_workers=1, pretrained_ssd='models/mobilenet-v1-ssd-mp-0_675.pth', resolution=300, resume=None, scheduler='cosine', t_max=100, use_cuda=True, validation_epochs=1, validation_mean_ap=False, weight_decay=0.0005)
2022-11-04 20:43:30 - model resolution 300x300
2022-11-04 20:43:30 - SSDSpec(feature_map_size=19, shrinkage=16, box_sizes=SSDBoxSizes(min=60, max=105), aspect_ratios=[2, 3])
2022-11-04 20:43:30 - SSDSpec(feature_map_size=10, shrinkage=32, box_sizes=SSDBoxSizes(min=105, max=150), aspect_ratios=[2, 3])
2022-11-04 20:43:30 - SSDSpec(feature_map_size=5, shrinkage=64, box_sizes=SSDBoxSizes(min=150, max=195), aspect_ratios=[2, 3])
2022-11-04 20:43:30 - SSDSpec(feature_map_size=3, shrinkage=100, box_sizes=SSDBoxSizes(min=195, max=240), aspect_ratios=[2, 3])
2022-11-04 20:43:30 - SSDSpec(feature_map_size=2, shrinkage=150, box_sizes=SSDBoxSizes(min=240, max=285), aspect_ratios=[2, 3])
2022-11-04 20:43:30 - SSDSpec(feature_map_size=1, shrinkage=300, box_sizes=SSDBoxSizes(min=285, max=330), aspect_ratios=[2, 3])
2022-11-04 20:43:30 - Prepare training datasets.
warning - image 20221031-132300 has no box/labels annotations, ignoring from dataset
warning - image 20221031-132304 has no box/labels annotations, ignoring from dataset
Traceback (most recent call last):
  File "train_ssd.py", line 257, in <module>
    target_transform=target_transform)
  File "/content/jetson-inference/python/training/detection/ssd/vision/datasets/voc_dataset.py", line 36, in __init__
    self.ids = self._read_image_ids(image_sets_file) #202200-000000 shape
  File "/content/jetson-inference/python/training/detection/ssd/vision/datasets/voc_dataset.py", line 112, in _read_image_ids
    if self._get_num_annotations(image_id) > 0: #202200-001234 shape
  File "/content/jetson-inference/python/training/detection/ssd/vision/datasets/voc_dataset.py", line 125, in _get_num_annotations
    objects = ET.parse(annotation_file).findall("object") #202200-001234.xml
  File "/usr/lib/python3.7/xml/etree/ElementTree.py", line 1197, in parse
    tree.parse(source, parser)
  File "/usr/lib/python3.7/xml/etree/ElementTree.py", line 598, in parse
    self._root = parser._parse_whole(source)
xml.etree.ElementTree.ParseError: no element found: line 1, column 0

数据集结构

- Annotations/
      - *.xml
- ImageSets/
      - Main
            - test.txt
            - train.txt
            - trainval.txt
            - val.txt
- JPEGImages/
      - *.jpg
- labels.txt

XML标注文件示例

<annotation>
    <filename>20221101-065756.jpg</filename>
    <folder>jetbot</folder>
    <source>
        <database>jetbot</database>
        <annotation>custom</annotation>
        <image>custom</image>
    </source>
    <size>
        <width>1280</width>
        <height>720</height>
        <depth>3</depth>
    </size>
    <segmented>0</segmented>
    <object>
        <name>jetbot</name>
        <pose>unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>100</xmin>
            <ymin>187</ymin>
            <xmax>514</xmax>
            <ymax>678</ymax>
        </bndbox>
    </object>
</annotation>

解决建议

  • 排查空XML文件:错误提示“no element found”说明存在内容为空的XML文件,遍历Annotations目录,找出文件大小为0的标注文件,直接删除或补全标注内容。
  • 匹配图像与标注对应关系:根据日志警告,20221031-132300和20221031-132304两张图像无标注,检查对应XML文件是否存在、是否为空,或者是否标注格式错误。
  • 批量验证XML格式:虽然示例XML格式正确,但不排除个别文件存在语法问题(如标签未闭合、特殊字符未转义),可使用XML校验工具批量检查所有标注文件。
  • 检查ImageSets文件内容:确保train.txt、val.txt等文件中的图像ID与JPEGImages、Annotations中的文件名一致(不要带.jpg或.xml后缀),例如图像20221101-065756.jpg对应的ID应为20221101-065756。
  • 确认文件权限:检查XML文件是否有可读权限,避免因权限不足导致读取内容为空。

内容的提问来源于stack exchange,提问作者YejinPu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 12:40:35