Search code examples
pythondeep-learningdata-processing

Can anyone tell me why I get IndexError: list index out of range?


Trying to replicate this repository: https://github.com/sujiongming/UCF-101_video_classification. I get the following error when I run the CNN_train_UCF101.py file.

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

part of the code referenced is as follows:

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

I have updated the question to give clarity on data. When I do print (self.data) I get something like this: ['train', 'UnevenBars', 'v_UnevenBars_g22_c04', '126'], [] for each image in the dataset.

Can anyone please show me what I'm doing wrong. Thanks in advance.

Window 10
Python 3.7.6

Solution

  • You have a blank line in the CSV file, which is resulting in an empty list at the end of self.data.

    You should skip empty items.

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