Skip to content

Instantly share code, notes, and snippets.

@parulnith
Last active November 1, 2023 09:12
Show Gist options
  • Save parulnith/7f8c174e6ac099e86f0495d3d9a4c01e to your computer and use it in GitHub Desktop.
Save parulnith/7f8c174e6ac099e86f0495d3d9a4c01e to your computer and use it in GitHub Desktop.
Untitled9.ipynb
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@basriciftci
Copy link

You extracted all spectrograms and saved them as figs, but you don't need them for training? Then, what was the point there?

@parulnith
Copy link
Author

You extracted all spectrograms and saved them as figs, but you don't need them for training? Then, what was the point there?

Hi @basriciftci, good question. Like I have mentioned in the article accompanying the code(https://towardsdatascience.com/music-genre-classification-with-python-c714d032f0d8), the purpose of this whole exercise was to help beginners understand the concept of working and understanding the audio files. Once the data has been extracted, they can use any algorithm of their choice to train it.

@Markjohnyaa
Copy link

hello,
when i test on genres it work fine , when i test on my set of songs it did not work . I am using two type of songs , it read one type but did not read other part for example when print (y) : 000000000000000000000000000000000000 ... is the printed but 111111111111111111111 is missing . would you please solve this issue . thanks

@DylanCarey94
Copy link

Hi @parulnith. I am wondering have you any example where you used the Spectogram images for training the Neural network and classifying?

@michellemul
Copy link

Hi @parulnith. I am wondering have you any example where you used the Spectogram images for training the Neural network and classifying?

Hi @DylanCarey94, did you manage to create a neural network using the spectogram images?

@DylanCarey94
Copy link

DylanCarey94 commented Mar 10, 2020 via email

@specpro30
Copy link

Hi there, I ran through the code and for the code block: np.argmax(predictions[0])

My result was 3. I noticed in the example that it is 8. Does this mean something is wrong with my model?

How can we start using this to pass it new songs to predict?

@ndujar
Copy link

ndujar commented Apr 19, 2020

Hi @specpro30,

Just put replace the data in X_test with whatever features you extract from your new songs and you are good to go :)
Don't forget to scale before using the scaler
X_new = scaler.fit_transform(np.array(new_data, dtype = float))
predictions = model.predict(X_new)

@SonamSangpoLama
Copy link

SonamSangpoLama commented May 2, 2020

For this code, I am getting problem of "keras scratch graph"

history = model.fit(X_train, y_train, epochs=10, batch_size=32)

InvalidArgumentError: Received a label value of 996 which is outside the valid range of [0, 10). Label values: 807 996 153 945 283 178 816 976 923 648 129 22 944 439 34 979 288 994 321 483 810 830 215 736 324 138 308 796 473 824 206 627
[[node loss_7/dense_24_loss/sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits (defined at /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:3009) ]] [Op:__inference_keras_scratch_graph_6597]

Function call stack:
keras_scratch_graph

CAN SOMEONE HELP ME TO SORT OUT THIS PROBLEM

@ndujar
Copy link

ndujar commented May 2, 2020

Hi @SonamSangpoLama, I think you want to check the contents of your y_train vector. There is too many classes in it. It can only be a number between 0 and 10:

The dataset consists of 10 genres i.e

Blues
Classical
Country
Disco
Hiphop
Jazz
Metal
Pop
Reggae
Rock

@SonamSangpoLama
Copy link

Hi @SonamSangpoLama, I think you want to check the contents of your y_train vector. There is too many classes in it. It can only be a number between 0 and 10:

The dataset consists of 10 genres i.e

Blues
Classical
Country
Disco
Hiphop
Jazz
Metal
Pop
Reggae
Rock

Thank you for your kind response..

Can you explain me further. I cannot get it..How can I check y_train content..?

@ndujar
Copy link

ndujar commented May 3, 2020

Hi @SonamSangpoLama,
Can you share what does the 'label' column look like in your dataset?
Maybe you took a dataset with too many classes. Can you share the output of this?:
data = pd.read_csv('data.csv')
display(data.head())
print(data['label'].unique())

@SonamSangpoLama
Copy link

SonamSangpoLama commented May 6, 2020

I have just been trying to use the same code of above but I am getting error. I have just made tiny changes on file directory.

Extracting spec from audios
cmap = plt.get_cmap('inferno')
plt.figure(figsize=(10,10))
genres = 'blues classical country disco hiphop jazz metal pop reggae rock'.split()
for g in genres:
pathlib.Path(f'image_genres/{g}').mkdir(parents=True, exist_ok=True)
for filename in os.listdir(f'./genres/{g}'):
songname = f'./genres/{g}/{filename}'
y, sr = librosa.load(songname, mono=True, duration=5)
plt.specgram(y, NFFT=2048, Fs=2, Fc=0, noverlap=128, cmap=cmap, sides='default', mode='default', scale='dB');
plt.axis('off');
plt.savefig(f'image_genres/{g}/{filename[:-3].replace(".", "")}.png')
plt.clf()

extracting features from spect

header = 'filename chroma_stft rmse spectral_centroid spectral_bandwidth rolloff zero_crossing_rate'
for i in range(1, 21):
header += f' mfcc{i}'
header += ' label'
header = header.split()

writing to csv

file = open('data.csv', 'w', newline='')
with file:
writer = csv.writer(file)
writer.writerow(header)
genres = 'blues classical country disco hiphop jazz metal pop reggae rock'.split()
for g in genres:
for filename in os.listdir(f'./genres/{g}'):
songname = f'./genres/{g}/{filename}'
y, sr = librosa.load(songname, mono=True, duration=30)
chroma_stft = librosa.feature.chroma_stft(y=y, sr=sr)
spec_cent = librosa.feature.spectral_centroid(y=y, sr=sr)
spec_bw = librosa.feature.spectral_bandwidth(y=y, sr=sr)
rolloff = librosa.feature.spectral_rolloff(y=y, sr=sr)
zcr = librosa.feature.zero_crossing_rate(y)
mfcc = librosa.feature.mfcc(y=y, sr=sr)
to_append = f'{filename} {np.mean(chroma_stft)} {np.mean(spec_cent)} {np.mean(spec_bw)} {np.mean(rolloff)} {np.mean(zcr)}'
for e in mfcc:
to_append += f' {np.mean(e)}'
to_append += f' {g}'
file = open('data.csv', 'a', newline='')
with file:
writer = csv.writer(file)
writer.writerow(to_append.split())

reading csv

data = pd.read_csv('data.csv')
data.head()

standard scaler

scaler = StandardScaler()
X = scaler.fit_transform(np.array(data.iloc[:, :1], dtype = float))

@Dhruv28112000
Copy link

What was the use of spectrogram images???

@jkotra
Copy link

jkotra commented Oct 31, 2020

I wrote a easy to understand notebook based on FE ideas in this one:
https://github.com/jkotra/MusicGenreClassification/blob/master/MusicGenreClassification_FeatureEnsemble.ipynb

Take a look if this seems too complicated 😉

@zebrapol
Copy link

zebrapol commented Jan 7, 2022

Hello @ndujar.
Could you please describe in more detail how to me predictions, namely the arrays that are displayed during prediction, turn into genres. I will be very grateful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment