YOLOX训练时出现'IndexError: list index out of range'错误解决方法
在使用YOLOX进行训练时,可能会遇到以下错误:
Exception ignored in: <function CacheDataset.__del__ at 0x7fde1cf77940>
Traceback (most recent call last):
File "/home/yqc/learning/YOLO_SSD/YOLOX/YOLOX-main/YOLOX_main_s_2/yolox/data/datasets/datasets_wrapper.py", line 177, in __del__
if self.cache and self.cache_type == "ram":
AttributeError: 'VOCDetection' object has no attribute 'cache'
2023-10-08 22:48:10 | ERROR | yolox.core.launch:98 - An error has been caught in function 'launch', process 'MainProcess' (9741), thread 'MainThread' (140594455536000):
Traceback (most recent call last):
File "tools/train.py", line 145, in <module>
launch(
└ <function launch at 0x7fde2b097280>
> File "/home/yqc/learning/YOLO_SSD/YOLOX/YOLOX-main/YOLOX_main_s_2/yolox/core/launch.py", line 98, in launch
main_func(*args)
│ └ (╒═══════════════════╤═══════════════════════════════════════════════════════════════════════════════════════════════════════...
└ <function main at 0x7fde2b090e50>
File "tools/train.py", line 125, in main
trainer.train()
│ └ <function Trainer.train at 0x7fde1d001040>
└ <yolox.core.trainer.Trainer object at 0x7fde1cf4f9d0>
File "/home/yqc/learning/YOLO_SSD/YOLOX/YOLOX-main/YOLOX_main_s_2/yolox/core/trainer.py", line 74, in train
self.before_train()
│ └ <function Trainer.before_train at 0x7fde1cf4b5e0>
└ <yolox.core.trainer.Trainer object at 0x7fde1cf4f9d0>
File "/home/yqc/learning/YOLO_SSD/YOLOX/YOLOX-main/YOLOX_main_s_2/yolox/core/trainer.py", line 149, in before_train
self.train_loader = self.exp.get_data_loader(
│ │ │ └ <function Exp.get_data_loader at 0x7fde1cf44ee0>
│ │ └ ╒═══════════════════╤════════════════════════════════════════════════════════════════════════════════════════════════════════...
│ └ <yolox.core.trainer.Trainer object at 0x7fde1cf4f9d0>
└ <yolox.core.trainer.Trainer object at 0x7fde1cf4f9d0>
File "/home/yqc/learning/YOLO_SSD/YOLOX/YOLOX-main/YOLOX_main_s_2/yolox/exp/yolox_base.py", line 181, in get_data_loader
self.dataset = self.get_dataset(cache=False, cache_type=cache_img)
│ │ │ │ └ None
│ │ │ └ <function Exp.get_dataset at 0x7fde1cf540d0>
│ │ └ ╒═══════════════════╤════════════════════════════════════════════════════════════════════════════════════════════════════════...
│ └ None
└ ╒═══════════════════╤════════════════════════════════════════════════════════════════════════════════════════════════════════...
File "/home/yqc/learning/YOLO_SSD/YOLOX/YOLOX-main/YOLOX_main_s_2/exps/example/yolox_voc/yolox_voc_s.py", line 27, in get_dataset
return VOCDetection(
└ <class 'yolox.data.datasets.voc.VOCDetection'>
File "/home/yqc/learning/YOLO_SSD/YOLOX/YOLOX-main/YOLOX_main_s_2/yolox/data/datasets/voc.py", line 147, in __init__
path_filename = [
File "/home/yqc/learning/YOLO_SSD/YOLOX/YOLOX-main/YOLOX_main_s_2/yolox/data/datasets/voc.py", line 148, in <listcomp>
str(self._imgpath % i).split(self.root + "\" )[1]
│ │ │ │ └ '/home/yqc/learning/YOLO_SSD/YOLOX/YOLOX-main/YOLOX_main_s_2/datasets/VOCdevkit'
│ │ │ └ <yolox.data.datasets.voc.VOCDetection object at 0x7fddfc0f0ee0>
│ │ └ ('/home/yqc/learning/YOLO_SSD/YOLOX/YOLOX-main/YOLOX_main_s_2/datasets/VOCdevkit/VOC2007', 'inclusion_34')
│ └ '%s/JPEGImages/%s.jpg'
└ <yolox.data.datasets.voc.VOCDetection object at 0x7fddfc0f0ee0>
IndexError: list index out of range
这个错误的主要原因是在VOCDetection类的构造函数中,尝试访问一个列表的索引超出了范围。具体来说,在VOCDetection类的构造函数中的以下代码导致了错误:
path_filename = [
str(self._imgpath % i).split(self.root + "\" )[1]
for i in self.ids
]
这段代码在处理图像文件路径时出错,导致索引超出范围。可能的原因是数据集中的图像文件路径设置不正确或缺失某些文件。
为了解决这个问题,你可以检查以下几点:
- 检查数据集文件路径是否正确设置。确保
self._imgpath变量正确指向图像文件的路径。 - 确保数据集中的每个图像文件都存在,并且路径正确。
- 检查数据集的文件名列表
self.ids是否正确设置,确保它包含了所有的图像文件名。
如果你仍然无法解决问题,可以尝试打印相关变量的值,以便更好地理解错误的原因。
原文地址: https://www.cveoy.top/t/topic/paC4 著作权归作者所有。请勿转载和采集!