# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/aozturk/data-focus/blob/master/keras_music_genre_classification.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + id="HRREovoRUyeK" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 204} outputId="c1e1410d-d070-40cf-d3a2-7035d90942a2"
# Download to use GTZAN genre collection dataset for classification. 

# !wget "http://opihi.cs.uvic.ca/sound/genres.tar.gz" 

# + id="c1Jl4E5nYtLf" colab_type="code" colab={}
# !tar -xzf genres.tar.gz

# + id="O7honQueTEms" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="6363bd4b-1b24-4935-e0d3-e6ddd20755c7"
# feature extractoring and preprocessing data
import librosa
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# %matplotlib inline
import os
from PIL import Image
import pathlib
import csv

# Preprocessing
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder, StandardScaler

#Keras
import keras

import warnings
warnings.filterwarnings('ignore')

# + id="Y1iyjCFUZ_bl" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="06086ee1-6262-4de3-f554-6aed6e5f60cc"
cmap = plt.get_cmap('inferno')

plt.figure(figsize=(10,10))
# The dataset consists of 10 genres
genres = 'blues classical country disco hiphop jazz metal pop reggae rock'.split()
for g in genres:
    pathlib.Path(f'img_data/{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)
        # Extracting the Spectrogram for every Audio
        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'img_data/{g}/{filename[:-3].replace(".", "")}.png')
        plt.clf()

# + id="z--OTKIMaMyS" colab_type="code" colab={}
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()

# + id="a_g1e4RaaPr1" colab_type="code" colab={}
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()
# Extracting features from Spectrogram
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)
        rmse = librosa.feature.rmse(y=y)
        chroma_stft = librosa.feature.chroma_stft(y=y, sr=sr) # Chroma Frequencies
        spec_cent = librosa.feature.spectral_centroid(y=y, sr=sr) # Spectral Centroid
        spec_bw = librosa.feature.spectral_bandwidth(y=y, sr=sr) # Spectral Bandwidth
        rolloff = librosa.feature.spectral_rolloff(y=y, sr=sr) # Spectral Roll-off
        zcr = librosa.feature.zero_crossing_rate(y) # Zero Crossing Rate
        mfcc = librosa.feature.mfcc(y=y, sr=sr) # Mel-frequency cepstral coefficients (MFCC)
        to_append = f'{filename} {np.mean(chroma_stft)} {np.mean(rmse)} {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='')
        # Write the data to a csv file
        with file:
            writer = csv.writer(file)
            writer.writerow(to_append.split())

# + id="2pPYbjGKbiiO" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 224} outputId="cb0ba213-3645-4fa5-e7df-a856604b40b7"
# Analysing the Data in Pandas
data = pd.read_csv('data.csv')
data.head()

# + id="KYymSXtzbk7B" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="e1e75170-961a-493b-fc9a-f730b0b37e69"
data.shape

# + id="eQDJz4kSbmzl" colab_type="code" colab={}
# Dropping unneccesary columns
data = data.drop(['filename'],axis=1)

# + id="qtde3SJPboZW" colab_type="code" colab={}
# Encoding the Labels
genre_list = data.iloc[:, -1]
encoder = LabelEncoder()
y = encoder.fit_transform(genre_list)

# + id="nByJZ2uhbp2K" colab_type="code" colab={}
# Scaling the Feature columns
scaler = StandardScaler()
X = scaler.fit_transform(np.array(data.iloc[:, :-1], dtype = float))

# + id="T7fmIcPzbrVA" colab_type="code" colab={}
# Dividing data into training and Testing set¶
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

# + id="XhyZmeXubuE-" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 119} outputId="d813083e-f65d-489a-d787-7dcb22cd6f48"
len(y_train)
len(y_test)
X_train[10]

# + id="LSLZFbafbxFY" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 88} outputId="365b597f-4a48-4ec4-d849-8859459d6473"
# Building the deep learning network

from keras import models
from keras import layers

model = models.Sequential()
model.add(layers.Dense(256, activation='relu', input_shape=(X_train.shape[1],)))

model.add(layers.Dense(128, activation='relu'))

model.add(layers.Dense(64, activation='relu'))

model.add(layers.Dense(10, activation='softmax'))

# + id="fj4LkFoIcAIV" colab_type="code" colab={}
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

# + id="FfJurfS8cCN8" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 768} outputId="93fdf9a0-ffd7-41bf-df4b-366689f6e758"
history = model.fit(X_train,
                    y_train,
                    epochs=20,
                    batch_size=128)

# + id="q5zB68vhcD8U" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 51} outputId="53d74843-a758-47c7-eab4-2dcf27f32b92"
test_loss, test_acc = model.evaluate(X_test,y_test)
print('test_acc: ',test_acc)

# + id="uYHvnRfUcGip" colab_type="code" colab={}
# Validate the approach
x_val = X_train[:200]
partial_x_train = X_train[200:]

y_val = y_train[:200]
partial_y_train = y_train[200:]

# + id="ISMlVT6jcIKL" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 1088} outputId="3aff119a-1ae4-4ad1-a764-20be30970fd0"
model = models.Sequential()
model.add(layers.Dense(512, activation='relu', input_shape=(X_train.shape[1],)))
model.add(layers.Dense(256, activation='relu'))
model.add(layers.Dense(128, activation='relu'))
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10, activation='softmax'))

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

model.fit(partial_x_train,
          partial_y_train,
          epochs=30,
          batch_size=512,
          validation_data=(x_val, y_val))
results = model.evaluate(X_test, y_test)
print(results)

# + id="kS3naC6vcMZm" colab_type="code" colab={}
# Predict on the test data
predictions = model.predict(X_test)

# + id="J88PdHukcOTr" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="0d5064ce-eda7-46d1-9c15-64bf2843c8cc"
predictions[0].shape

# + id="bx0LMQdxcTBF" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="113db154-7317-4f21-9690-f0fbeb64baea"
np.sum(predictions[0])

# + id="Cd8P7ndGcUXw" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="2fb84433-91bd-4c20-d9df-a352757751b9"
np.argmax(predictions[0])

StarcoderdataJupyterScriptsDedupFiltered