<jupyter_start><jupyter_text>Importing the required libraries<jupyter_code># TRAINING ON TESTS OF INCREASING COMPLEXITY




import librosa
import librosa.display
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf 
from matplotlib.pyplot import specgram
import keras
from keras.preprocessing import sequence
from keras.models import Sequential
from keras.layers import Dense, Embedding
#from keras.layers import LSTM
#from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.utils import to_categorical
from keras.layers import Input, Flatten, Dropout, Activation
from keras.layers import Conv1D, MaxPooling1D, AveragePooling1D
from keras.models import Model
from keras.callbacks import ModelCheckpoint
from sklearn.metrics import confusion_matrix
import pandas as pd
from keras_tqdm import TQDMNotebookCallback
import pickle
import os<jupyter_output>/home/stjepan/anaconda3/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
Using TensorFlow backend.<jupyter_text>Data<jupyter_code># Preprocessed data (feature extraction + labels)
# config file only for info on preprocessing details

# https://drive.google.com/open?id=1ARx2M2OnHjUDXFb1Z33BZ7lBhrn0HIpn
features = np.load('/home/stjepan/Documents/soloact/data/processed/training_X_power.npy')
features.shape
#reading labels from csv
labels = pd.read_csv("/home/stjepan/Documents/soloact/data/processed/training_Y_power.csv", delimiter=",")
#labels[:9]
#our labels (2 classes)
labels[["overdrive.gain_db", "reverb.reverberance"]].head(10)<jupyter_output><empty_output><jupyter_text>Train/test split + Shuffle<jupyter_code>from sklearn.model_selection import train_test_split

X_train, X_val, Y_train, Y_val = train_test_split(features,labels, shuffle = True, test_size = 0.2, random_state = 44)
Y_train.shape<jupyter_output><empty_output><jupyter_text>Filtering labels from dataframe<jupyter_code>Y_train_l = Y_train.filter(regex=r'reverb.rev|gain_db')
Y_val_l = Y_val.filter(regex=r'reverb.rev|gain_db')

# Y_train_l = Y_train.filter(regex=r'chorus.del|phaser.del|reverb.rev|gain_db')
# Y_val_l = Y_val.filter(regex=r'chorus.del|phaser.del|reverb.rev|gain_db')

Y_train_l[:9]<jupyter_output><empty_output><jupyter_text>Setting up labels<jupyter_code>Y_train_l2 = Y_train_l.fillna(0)
Y_val_l2 = Y_val_l.fillna(0)

Y_train_l2.columns = ["O","R"] 
Y_val_l2.columns = ["O","R"]

Y_train_l2 = Y_train_l2.where(Y_train_l2 == 0).replace(np.nan,1)
Y_val_l2 = Y_val_l2.where(Y_val_l2 == 0).replace(np.nan,1)

Y_val_l2[list("OR")] = Y_val_l2[list("OR")].astype(int)
Y_train_l2[list("OR")] = Y_train_l2[list("OR")].astype(int)


# Getting binary representation of label states (on - off)
Y_val_l2[:9]<jupyter_output><empty_output><jupyter_text>Concatonating effect name to its state for later comparisons of actual/predicted labels<jupyter_code>labels_train = Y_train_l2.assign(labels = 'Ovd__' + Y_train_l2["O"].apply(str) + '_Rev__' + Y_train_l2["R"].apply(str))
labels_val   = Y_val_l2.assign(  labels = 'Ovd__'+ Y_val_l2["O"].map(str) + '_Rev__' + Y_val_l2["R"].map(str))

labels_TR = labels_train["labels"]
labels_VAL = labels_val["labels"]

labels_VAL[:5]

from keras.utils import np_utils
from sklearn.preprocessing import LabelEncoder

lb = LabelEncoder()

Y_train_OCPR = np_utils.to_categorical(lb.fit_transform(labels_TR))
Y_val_OCPR = np_utils.to_categorical(lb.fit_transform(labels_VAL))
Y_train_OCPR[0]
Y_val_OCPR<jupyter_output><empty_output><jupyter_text>CNN model<jupyter_code>from keras import layers
model = Sequential()

model.add(Conv1D(128, 5,padding='same',
                 input_shape=(205,1)))
model.add(Activation('relu'))
model.add(Conv1D(128, 5,padding='same'))
model.add(Activation('relu'))
model.add(Dropout(0.1))
model.add(MaxPooling1D(pool_size=(8)))
model.add(Conv1D(128, 5,padding='same',))
model.add(Activation('relu'))
model.add(Conv1D(128, 5,padding='same',))
model.add(Activation('relu'))
model.add(Conv1D(128, 5,padding='same',))
model.add(Activation('relu'))
model.add(Dropout(0.3))
model.add(Conv1D(128, 5,padding='same',))
model.add(Activation('relu'))
model.add(Flatten())

model2 = Sequential()
model2.add(model)
model2.add(Dense(4, activation = "softmax"))

model1 = Sequential()
model1.add(model)
model1.add(Dense(1, activation = "relu"))

opt = opt = keras.optimizers.Adam(lr=0.00001)

model1.compile(optimizer= opt, loss=['mae'], metrics =["mae"])
model2.compile(optimizer = opt, loss = "categorical_crossentropy", metrics = ["accuracy"])<jupyter_output>WARNING:tensorflow:From /home/stjepan/anaconda3/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py:497: calling conv1d (from tensorflow.python.ops.nn_ops) with data_format=NHWC is deprecated and will be removed in a future version.
Instructions for updating:
`NHWC` for data_format is deprecated, use `NWC` instead<jupyter_text>Fitting the model<jupyter_code>CLASS_history = model2.fit(X_train, Y_train_OCPR, 
                            batch_size=32, 
                            epochs=10, 
                            validation_data=(X_val, Y_val_OCPR),
                            verbose=0, 
                            callbacks=[TQDMNotebookCallback()])
model_name = 'guitar_Overdrive_Reverb_on-off_20-70_lr_0.0001_10ep_dropout_0.3_PWC_2_CLASSES_5.9.2018.h5'
save_dir = os.path.join(os.getcwd(), 'saved_models')

# Save model, and history

if not os.path.isdir(save_dir):
    os.makedirs(save_dir)
model_path = os.path.join(save_dir, model_name)
model2.save(model_path)
print('Saved trained model at %s ' % model_path)

with open('guitar_Overdrive_Reverb_on-off_20-70_lr_0.0001_10ep_dropout_0.3_PWC_2_CLASSES_5.9.2018', 'wb') as file_pi:
    pickle.dump(CLASS_history.history, file_pi)
    
# Plot history

plt.plot(CLASS_history.history['acc'])
plt.plot(CLASS_history.history['val_acc'])
plt.title('model ACC')
plt.ylabel('ACC')
plt.xlabel('epoch')
plt.legend(['train', 'val'], loc='upper left')

plt.ylim(ymax=1)
plt.ylim(ymin=0)
plt.show()<jupyter_output><empty_output><jupyter_text>Predictions<jupyter_code>preds = model2.predict(X_val, 
                         batch_size=32, 
                         verbose=1)

type(preds)
preds1=preds.argmax(axis=1)
preds1
abc = preds1.astype(int).flatten()
predictions = (lb.inverse_transform((abc)))
preddf = pd.DataFrame({'predictedvalues': predictions})
preddf[:10]
actual=Y_val_OCPR.argmax(axis=1)
abc123 = actual.astype(int).flatten()
actualvalues = (lb.inverse_transform((abc123)))
actualdf = pd.DataFrame({'actualvalues': actualvalues})
finaldf = actualdf.join(preddf)
np.sum(finaldf.actualvalues != finaldf.predictedvalues)
finaldf[:9]
cnnhistory=model.fit(X_train, Y_train, batch_size=32, epochs=50, validation_data=(X_val, Y_val))<jupyter_output><empty_output><jupyter_text>Plotting results<jupyter_code>from pylab import rcParams
rcParams['figure.figsize'] = 15, 9 #setting figure size


plt.plot(cnnhistory.history['mean_absolute_error'])
plt.plot(cnnhistory.history['val_mean_absolute_error'])
plt.title('model MAE')
plt.ylabel('MAE')
plt.xlabel('epoch')
plt.legend(['train', 'val'], loc='upper left')

plt.ylim(ymax=20)
plt.ylim(ymin=0)
plt.show()<jupyter_output><empty_output><jupyter_text>Saving model<jupyter_code>import os
model_name = 'guitar_dist_gain_0-80_reverberance_0-100_other_on-off_200ep_lr_0.0001_dropout_0.3_PWC.h5'
save_dir = os.path.join(os.getcwd(), 'saved_models')
# Save model and weights
if not os.path.isdir(save_dir):
    os.makedirs(save_dir)
model_path = os.path.join(save_dir, model_name)
model.save(model_path)
print('Saved trained model at %s ' % model_path)<jupyter_output>Saved trained model at /home/stjepan/Documents/_Krish_Suchitra_Tristan_PORTFOLIO/DL_audio/saved_models/guitar_dist_gain_0-80_reverberance_0-100_other_on-off_200ep_lr_0.0001_dropout_0.3_PWC.h5<jupyter_text>Predctions<jupyter_code>preds = model.predict(X_val, 
                         batch_size=32, 
                         verbose=1)


preds=list(preds[:,0])
#actual=list(Y_val[:,0])


results = pd.DataFrame({'predicted' : preds, 'actual' : Y_val})    


results['diff'] = abs(results['predicted'] - results['actual'])
print(results['diff'].mean()) 
results.head(15)

results.sort_values(by='actual', ascending=False, inplace = True)

results_p=results[["predicted", "actual"]]

results_p.reset_index(inplace=True, drop = "index")
results_p.plot()<jupyter_output>210/210 [==============================] - 0s 1ms/step
3.3403684885728926<jupyter_text>Baseline model predicts gain parameter value of guitar distorstion effect with average error of 4.5 (out of range 0-65)<jupyter_code>results['diff'].describe()<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup