<jupyter_start><jupyter_text>create features<jupyter_code>import librosa
def get_mfcc(wav_name, max_len=1200): # 2895):
#     rate, sig = wav.read(wav_name)
#     mfcc_feat = mfcc(sig, rate, winlen=0.020, appendEnergy = False)
    rate = 22050
    sig, rate = librosa.load(wav_name, sr=rate)
    mfcc_feat = librosa.feature.mfcc(sig, rate, n_mfcc=13, n_fft=512, hop_length=512).T
#     covariance = np.cov(np.matrix.transpose(mfcc_feat))
#     mean_matrix = mfcc_feat.mean(0)
#     feature = (mean_matrix , covariance , 0)
    return mfcc_feat[:max_len]
genre_folders = [path for path in os.listdir(DATASET_PATH) if '.' not in path]
for genre_name in tqdm(genre_folders[4:5]):
    wavs_list = os.listdir(f'{DATASET_PATH}/{genre_name}')
    for w in wavs_list[:1]:
        wav_path = f'{DATASET_PATH}/{genre_name}/{w}'
        mfcc_feat = get_mfcc(wav_path)
mfcc_feat.mean(axis=0).shape
import librosa.display
mfcc_feat.T.shape
librosa.display.specshow(mfcc_feat.T, x_axis='time')
plt.colorbar()
plt.title('MFCC')
plt.tight_layout()
librosa.display.specshow(mfcc_feat.T, x_axis='time')
plt.colorbar()
plt.title('MFCC')
plt.tight_layout()
librosa.display.specshow(mfcc_feat.T, x_axis='time')
plt.colorbar()
plt.title('MFCC')
plt.tight_layout()
def create_save_features(dataset_path: str, out_dir: str):
    data = {}
    data['mfcc'] = []
    data['label'] = []
    """ gets list of folders cotaining wavs and returns created mfcc features and label's csv"""
    genre_folders = [path for path in os.listdir(dataset_path) if '.' not in path]
    for genre_name in tqdm(genre_folders):
        wavs_list = os.listdir(f'{dataset_path}/{genre_name}')
        for wav in tqdm(wavs_list):
            wav_path = f'{dataset_path}/{genre_name}/{wav}'
            data['mfcc'].append(get_mfcc(wav_path).T)
            data['label'].append(genre_to_num[genre_name])
            
    with open(out_dir, 'wb') as f:
        pickle.dump(data, f)
create_save_features(DATASET_PATH, FEATURES_PATH)<jupyter_output><empty_output><jupyter_text>loading data<jupyter_code>#  with open(FEATURES_PATH, "rb") as f:
#     data = pickle.load(f)
# set([it.shape for it in data['mfcc']])
def load_data(data_path):
    with open(data_path, "rb") as f:
        data = pickle.load(f)

    X = np.stack(data["mfcc"], axis=0)
    y = np.array(data["label"])
    return X, y
def ttf_split(data, test_size, val_size, seed=4321):
    X_train, X_test, y_train, y_test = train_test_split(*data, test_size=test_size, random_state=seed)
    X_train, X_validation, y_train, y_validation = train_test_split(X_train, y_train, test_size=val_size, random_state=seed)

    return X_train, X_validation, X_test, y_train, y_validation, y_test
X, y = load_data(FEATURES_PATH)
X.shape, y.shape


X, y =  more_samples(X, y)
# create one hot encoding
y_hot = np.eye(y.max()+1)[y]
y_hot.shape<jupyter_output><empty_output><jupyter_text>Try LSTM<jupyter_code>import tensorflow.keras as keras
import matplotlib.pyplot as plt
from tensorflow.keras.layers import *
def build_model(input_shape):

    # build network topology
    model = keras.Sequential()
    
    model.add(keras.layers.LayerNormalization(axis=1, input_shape=input_shape,))

    # 2 LSTM layers
    model.add(keras.layers.LSTM(128, return_sequences=True))
    model.add(keras.layers.LSTM(128))

    # dense layer
    model.add(keras.layers.Dense(128, activation='relu'))
    model.add(keras.layers.Dropout(0.7))
#     model.add(keras.layers.BatchNormalization())
    
#     model.add(keras.layers.Dense(32, activation='relu'))
#     model.add(keras.layers.Dropout(0.5))

    # output layer
    model.add(keras.layers.Dense(10, activation='softmax'))

    return model


# def build_model(input_shape):

#     # build network topology
#     model = keras.Sequential()
#     model.add(keras.layers.LayerNormalization(axis=1, input_shape=input_shape,))

#     # New model
#     model.add(Conv2D(128, 9, padding='same'))  # X_train.shape[1] = No. of Columns
#     model.add(Activation('relu'))
#     model.add(Conv2D(128, 9, padding='same'))
#     model.add(BatchNormalization())
#     model.add(Activation('relu'))
#     model.add(Dropout(0.25))
#     model.add(MaxPooling2D(pool_size=(7)))
#     model.add(Conv2D(64, 5, padding='same'))
#     model.add(Activation('relu'))
#     model.add(Conv2D(64, 5, padding='same'))
#     model.add(Activation('relu'))
#     model.add(Conv2D(64, 3, padding='same'))
#     model.add(Activation('relu'))
#     model.add(Conv2D(64, 3, padding='same'))
#     model.add(BatchNormalization())
#     model.add(Activation('relu'))
#     model.add(Dropout(0.25))
# #     model.add(MaxPooling2D(pool_size=(7)))
# #     model.add(Conv2D(32, 7, padding='same'))
# #     model.add(Activation('relu'))
# #     model.add(Conv2D(32, 7, padding='same'))
# #     model.add(Activation('relu'))
#     model.add(Flatten())
#     model.add(Dense(256)) 
#     model.add(Activation('selu'))
#     model.add(Dense(10)) # Target class number
#     model.add(Activation('softmax'))

#     return model

def more_samples(x, y, div=150):
    length = 1200 // div
    xs = np.concatenate([x[:,:,i*div:(i+1)*div] for i in range(length)], axis=0)
    ys = np.concatenate([y] * length, axis=0)
    return xs, ys
X = X.reshape([*X.shape, 1])
test_size = 0.25 
val_size = 0.2 
X_train, X_val, X_test, y_train, y_val, y_test = ttf_split((X, y_hot), test_size, val_size)

# X_train, y_train =  more_samples(X_train, y_train)
# X_val, y_val =  more_samples(X_val, y_val)
# X_test, y_test =  more_samples(X_test, y_test)
X_train.shape
# input_shape = (X_train.shape[1], X_train.shape[2], 1) # 1200, 13
input_shape = (X_train.shape[1], X_train.shape[2]) # 1200, 13
model = build_model(input_shape)

optimiser = keras.optimizers.Adam(learning_rate=0.001)
# optimiser = keras.optimizers.SGD(learning_rate=0.001)
model.compile(optimizer=optimiser,
              loss='categorical_crossentropy',
              metrics=['accuracy'])

model.summary()
y_val.sum(axis=0)
y_train.sum(axis=0)
# # train model
history = model.fit(X_train, y_train,
                    validation_data=(X_val, y_val),
                    batch_size=64,
                    epochs=100)
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
from sklearn.metrics import confusion_matrix, precision_score, recall_score, classification_report
print(classification_report(y_test.argmax(axis=1), model.predict(X_test).argmax(axis=1)))
print(classification_report(y_train.argmax(axis=1), model.predict(X_train).argmax(axis=1)))
y_test.sum(axis=0)
test_loss, test_acc = model.evaluate(X_test, y_test, verbose=2)
print('\nTest accuracy:', test_acc)<jupyter_output>63/63 - 0s - loss: 4.3761 - accuracy: 0.5715

Test accuracy: 0.5715000033378601
{'fraction_non_alphanumeric': 0.15394893111638955, 'fraction_numerical': 0.028652019002375295, 'mean_word_length': 4.351072279586973, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '611857', 'license': 'Unlicense', 'n_tokens_mistral': 2740, 'n_tokens_neox': 2595, 'n_words': 426, 'path': 'genre_calssification-LSTM.ipynb', 'repo_name': 'mr-Hades/genre_classification', 'size': '6736'}