谁能告诉我为什么索引器:列表索引超出范围?

2024-10-02 14:25:15 发布

您现在位置:Python中文网/ 问答频道 /正文

正在尝试复制此存储库:https://github.com/sujiongming/UCF-101_video_classification。当我运行CNN\u train\u UCF101.py文件时,出现以下错误

Traceback (most recent call last):
  File "CNN_train_UCF101.py", line 18, in <module>
    data = DataSet()
  File "D:\Clones\UCF-101_video_classification-master\UCFdata.py", line 32, in __init__
    self.classes = self.get_classes()
  File "D:\Clones\UCF-101_video_classification-master\UCFdata.py", line 64, in get_classes
    if item[1] not in classes:
IndexError: list index out of range

引用的部分代码如下所示:

def get_data():
        """Load our data from file."""
        with open('./data/data_file.csv', 'r') as fin:
            reader = csv.reader(fin)
            data = list(reader)   
def clean_data(self):
        """Limit samples to greater than the sequence length and fewer
        than N frames. Also limit it to classes we want to use."""
        data_clean = []
        for item in self.data:
            if int(item[3]) >= self.seq_length and int(item[3]) <= self.max_frames \
                    and item[1] in self.classes:
                data_clean.append(item)

        return data_clean

    def get_classes(self):
        """Extract the classes from our data. If we want to limit them,
        only return the classes we need."""
        classes = []
        for item in self.data:
            if item[1] not in classes:
                classes.append(item[1])

        # Sort them.
        classes = sorted(classes)

        # Return.
        if self.class_limit is not None:
            return classes[:self.class_limit]
        else:
            return classes

我已经更新了问题,以澄清data。 当我做print (self.data)时,我得到如下结果: ['train', 'UnevenBars', 'v_UnevenBars_g22_c04', '126'], []用于数据集中的每个图像

谁能告诉我我做错了什么吗。提前谢谢

Window 10
Python 3.7.6

Tags: toinpyselfcleandatagetreturn
1条回答
网友
1楼 · 发布于 2024-10-02 14:25:15

CSV文件中有一个空行,这将导致self.data末尾出现一个空列表

你应该跳过空项

for item in self.data:
    if len(item) < 2:
        continue
    if item[1] not in classes:
        classes.append(item[1])

相关问题 更多 >