deep_learning_cookbook icon indicating copy to clipboard operation
deep_learning_cookbook copied to clipboard

15.1 Song Classification -- IndexError: list index out of range

Open mikechen66 opened this issue 4 years ago • 3 comments

Hi Douwe:

I tried the song classification. It has the list index out of range error. Please indicate how how to solve the issue.

IndexError Traceback (most recent call last) in 17 18 song_specs, genres, genre_to_idx, idx_to_genre = load_songs('/home/mike/Documents/datasets/genres') ---> 19 song_specs[0].shape

IndexError: list index out of range

Best regards,

Mike

mikechen66 avatar May 02 '20 12:05 mikechen66

Hi Mike,

It looks like load_songs didn't return anything, which from the source code seems to happen when it can't find any songs. See if the list_dir above does anything or perhaps it is pointing to the wrong place.

Douwe

DOsinga avatar May 02 '20 15:05 DOsinga

Hi Douwe:

I see that the path is too value to be understood. So it is shows the error

IndexError: list index out of range

I see that song_folder is a path but not be expressed in 15.1. Can you make an explicit example in the original code of lines. If the 15.1 Song Classification is to be solved, 15.2 to 15.4 will be easy to be solved.

def load_songs(song_folder):
    song_specs = []
    idx_to_genre = []
    genre_to_idx = {}
    genres = []
    for genre in os.listdir(song_folder):
        genre_to_idx[genre] = len(genre_to_idx)
        idx_to_genre.append(genre)
        genre_folder = os.path.join(song_folder, genre)
        for song in os.listdir(genre_folder):
            if song.endswith('.au'):
                signal, sr = librosa.load(os.path.join(genre_folder, song))
                melspec = librosa.feature.melspectrogram(signal, sr=sr).T[:1280,]
                song_specs.append(melspec)
                genres.append(genre_to_idx[genre])
    return song_specs, genres, genre_to_idx, idx_to_genre

song_specs, genres, genre_to_idx, idx_to_genre = load_songs('/home/mike/Documents/datasets/genres')
song_specs[0].shape

Thanks in advance,

Mike

mikechen66 avatar May 09 '20 16:05 mikechen66

The song classification example is interesting. But the question is the IndexError: list index out of range. I tried many methods and could not correct the issue.

mikechen66 avatar May 12 '20 16:05 mikechen66