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

时间:2020-02-18 06:22:48

标签: python deep-learning data-processing

尝试复制此存储库:https://github.com/sujiongming/UCF-101_video_classification。运行CNN_train_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

1 个答案:

答案 0 :(得分:2)

您在CSV文件中有一个空白行,这导致self.data末尾有一个空列表。

您应该跳过空的项目。

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