<jupyter_start><jupyter_text>Load Test Data<jupyter_code>#x = np.zeros((1600,64000))
x = []
for i, file in enumerate(tqdm_notebook(filenames_all)):
    wave,_ = librosa.load(os.path.join(path_train,file),sr=sr,dtype = np.float32) 
    wave,_ = librosa.effects.trim(wave)
    
    if len(wave)<=length:

        amount = length-len(wave)
        wave = np.pad(wave,(int(np.ceil(amount/2)),int(np.floor(amount/2))),'constant')

    #elif len(wave) > length:
        #max_start = len(wave)-length
        
        #start = np.random.randint(0,max_start)
        #end = start + length
        
        #wave = wave[start:end]
        
    wave = wave/np.max(np.absolute(wave+1e-10))
    x.append(wave)    
import pickle

#with open('./wf16k_train', 'wb') as fp:
#    pickle.dump(x,fp)
with open('./preprocessed_test/WF-16k-64k','rb') as fp:
    x = pickle.load(fp)
from utilities import create_quick_test_wave
x_2 = create_quick_test_wave(x,64000)<jupyter_output><empty_output><jupyter_text>Import Libraries for Models<jupyter_code>import keras
import os 
import random
import numpy as np
import kapre

from keras.layers import Input
from keras.layers.convolutional import Conv1D
from keras.layers.core import Dense, Reshape, Permute
from keras.models import Model
from keras.optimizers import Adam
from keras.layers.pooling import GlobalAveragePooling1D
from keras import metrics
from utils import util
from keras.layers.pooling import MaxPooling1D, GlobalMaxPooling1D, GlobalAveragePooling1D
from keras.layers.core import Dropout 
from keras import backend as K
from keras.layers.convolutional import Conv2D, Conv2DTranspose
from keras.layers.pooling import GlobalAveragePooling2D, GlobalMaxPooling2D, MaxPooling2D
from keras.layers import Add, Average, Concatenate, Multiply, Lambda, BatchNormalization, Activation, TimeDistributed
from keras import regularizers
from kapre.time_frequency import Melspectrogram, Spectrogram
def ccategorical_crossentropy(y_true, y_pred):
    
    y_true_v = K.greater(K.sum(y_true, axis=-1),1.1)
    y_true = y_true%1
    
    y_pred = K.clip(y_pred, K.epsilon(), 1)
    loss = -K.sum(y_true*K.log(y_pred),axis=-1)

    m = K.max(loss)*0.8
    
    loss = loss 
    
    el = 1-(K.cast(K.greater(loss,m),'float32')*K.cast(y_true_v,'float32'))
    loss = loss*el
    return loss<jupyter_output><empty_output><jupyter_text>Waveform Model 16k<jupyter_code>model_filename = './COCAI_Models/dcase2018_task2_cochlearai_saved_models_16000_wav-1-fold_wav_model_1fold_0.8537611783271962.h5'
cocai_wf = keras.models.load_model(model_filename,
                                   custom_objects={'Melspectrogram':kapre.time_frequency.Melspectrogram,
                                                   'Spectrogram':kapre.time_frequency.Spectrogram,
                                                   'ccategorical_crossentropy':ccategorical_crossentropy})
#for layer in cocai_wf_headless.layers:
    #print(layer.name)
new_output_name = 'reshape_16'
new_output_layer = cocai_wf.get_layer(new_output_name).output
cocai_wf_headless = keras.Model(inputs = cocai_wf.input, outputs = new_output_layer)
cocai_wf_headless.summary()
y_hat = cocai_wf_headless.predict(x_2)
y_hat = np.reshape(y_hat,(1600,512))

y_hat.shape<jupyter_output><empty_output><jupyter_text>Agglomerative Clusters<jupyter_code>from sklearn.cluster import AgglomerativeClustering
import scipy.cluster.hierarchy as sch
from collections import Counter
plt.figure(figsize = (15,5))
dendogram = sch.dendrogram(sch.linkage(y_hat, method = 'ward'))
plt.tick_params(
    axis='x',          # changes apply to the x-axis
    which='both',      # both major and minor ticks are affected
    bottom=False,      # ticks along the bottom edge are off
    top=False,         # ticks along the top edge are off
    labelbottom=False) # labels along the bottom edge are off
#plt.title('Dendogram for High Level Representation of FSDKaggle 2018 Test Dataset')
plt.tight_layout()
plt.savefig('./dendogram.png')
clusterer_1 = AgglomerativeClustering(n_clusters=6, affinity='euclidean', linkage = 'ward')
clusterer_1.fit(y_hat)
meta_labels = clusterer_1.labels_
meta_class1 = []
meta_class2 = []
meta_class3 = []
meta_class4 = []
meta_class5 = []
meta_class6 = []
meta_class7 = []


for i, this_ml in enumerate(meta_labels):
    if this_ml == 0:
        meta_class1.append(i)
    elif this_ml == 1:
        meta_class2.append(i)
    elif this_ml == 2:
        meta_class3.append(i)
    elif this_ml == 3:
        meta_class4.append(i)
    elif this_ml == 4:
        meta_class5.append(i)
    elif this_ml == 5:
        meta_class6.append(i)
    elif this_ml == 6:
        meta_class7.append(i)
mc_1_orig = [num_to_label[labels_test[f]] for f in meta_class1]
mc_2_orig = [num_to_label[labels_test[f]] for f in meta_class2]
mc_3_orig = [num_to_label[labels_test[f]] for f in meta_class3]
mc_4_orig = [num_to_label[labels_test[f]] for f in meta_class4]
mc_5_orig = [num_to_label[labels_test[f]] for f in meta_class5]
mc_6_orig = [num_to_label[labels_test[f]] for f in meta_class6]
mc_7_orig = [num_to_label[labels_test[f]] for f in meta_class7]
counts_mc1 = dict(Counter(mc_1_orig).most_common())
counts_mc2 = dict(Counter(mc_2_orig).most_common())
counts_mc3 = dict(Counter(mc_3_orig).most_common())
counts_mc4 = dict(Counter(mc_4_orig).most_common())
counts_mc5 = dict(Counter(mc_5_orig).most_common())
counts_mc6 = dict(Counter(mc_6_orig).most_common())
counts_mc7 = dict(Counter(mc_7_orig).most_common())
all_counts = [counts_mc1,counts_mc2,counts_mc3,counts_mc4,counts_mc5,counts_mc6,counts_mc7]
fig, axs = plt.subplots(7,1,figsize = (16,32))

for i,ax in enumerate(axs):
    
    length = len(all_counts[i])
    x_ticks = np.arange(length)
    values = all_counts[i].values()
    x_ticklabels = all_counts[i].keys()
    
    fig = ax.bar(x_ticks,values)
    fig = ax.set_xticks(x_ticks)
    fig = ax.set_xticklabels(x_ticklabels,rotation = 90)
plt.tight_layout()
classes_meta = dict([[f,[]] for f in classes])
for this_class in classes:
    if this_class in mc_1_orig:
        classes_meta[this_class].append(0)
    if this_class in mc_2_orig:
        classes_meta[this_class].append(1)
    if this_class in mc_3_orig:
        classes_meta[this_class].append(2)
    if this_class in mc_4_orig:
        classes_meta[this_class].append(3)
    if this_class in mc_5_orig:
        classes_meta[this_class].append(4)
    if this_class in mc_6_orig:
        classes_meta[this_class].append(5)
    if this_class in mc_7_orig:
        classes_meta[this_class].append(6)
classes_meta.items()
label_to_meta = dict([[k,1000] for k in classes])
for my_class, clusters in classes_meta.items():
    
    print(my_class)
    print('Present in {}'.format(clusters))
    if len(clusters) > 1:
        
        times_in_cluster = []
        for i in clusters:
            times_in_cluster.append(all_counts[i][my_class])
            print('\tCluster: {}\n\tCounts: {}'.format(i,all_counts[i][my_class]))
            
        this_max = np.argmax(times_in_cluster)
        print('\tWinner: {}\n\n'.format(clusters[this_max]))
        label_to_meta[my_class] = clusters[this_max]
        
    elif len(clusters) == 1:
        print('\tOnly one class')
        print('\tWinner: {}\n\n'.format(clusters))
        
        label_to_meta[my_class] = clusters[0]
        
    elif len(clusters) < 1:
        print('whaaaaaa?')
label_to_meta
with open('./label_to_meta_v1.json','w') as fp:
    json.dump(label_to_meta,fp)
m1 = []
m2 = []
m3 = []
m4 = []
m5 = []
m6 = []
m7 = []

for label, meta in label_to_meta.items():
    if meta == 0:
        m1.append(label)
    elif meta == 1:
        m2.append(label)
    elif meta == 2:
        m3.append(label)
    elif meta == 3:
        m4.append(label)
    elif meta == 4:
        m5.append(label)
    elif meta == 5:
        m6.append(label)
    elif meta == 6:
        m7.append(label)
    else:
        print('askjdna')
    
print(m1,'\n')
print(m2,'\n')
print(m3,'\n')
print(m4,'\n')
print(m5,'\n')
print(m6,'\n')<jupyter_output>['Clarinet', 'Flute', 'Harmonica', 'Oboe', 'Saxophone', 'Telephone', 'Trumpet'] 

['Cello', 'Double_bass', 'Violin_or_fiddle'] 

['Bass_drum', 'Burping_or_eructation', 'Computer_keyboard', 'Cowbell', 'Drawer_open_or_close', 'Fart', 'Finger_snapping', 'Fireworks', 'Gunshot_or_gunfire', 'Hi-hat', 'Keys_jangling', 'Knock', 'Microwave_oven', 'Scissors', 'Shatter', 'Snare_drum', 'Squeak', 'Tambourine', 'Tearing', 'Writing'] 

['Acoustic_guitar', 'Bus', 'Chime', 'Electric_piano', 'Glockenspiel', 'Gong'] 

['Applause'] 

['Bark', 'Cough', 'Laughter', 'Meow']<jupyter_text>Mel Model 16k<jupyter_code>model2_filename = './COCAI_Models/dcase2018_task2_cochlearai_saved_models_16000_mel-1-fold_mel_model_1fold_0.8858495528669121.h5'
cocai_mel = keras.models.load_model(model2_filename,
                                    custom_objects={'Melspectrogram':kapre.time_frequency.Melspectrogram,
                                                    'Spectrogram':kapre.time_frequency.Spectrogram,
                                                    'ccategorical_crossentropy':ccategorical_crossentropy})
#for layer in cocai_mel.layers:
    #print(layer.name)
new_output_name2 = 'reshape_10'
new_output_layer2 = cocai_mel.get_layer(new_output_name2).output
cocai_mel_headless = keras.Model(inputs = cocai_mel.input, outputs = new_output_layer2)
cocai_mel_headless.summary()
y_hat2 = cocai_mel_headless.predict(x)
y_hat2 = np.reshape(y_hat2,(1600,512))<jupyter_output><empty_output><jupyter_text>Agglomerative Clusters<jupyter_code>plt.figure(figsize = (15,5))
dendogram = sch.dendrogram(sch.linkage(y_hat2, method = 'ward'))
cluster_model2 = AgglomerativeClustering(n_clusters=6, affinity='euclidean', linkage = 'ward')
cluster_model2.fit(y_hat2)
meta_labels = cluster_model2.labels_
meta_class1 = []
meta_class2 = []
meta_class3 = []
meta_class4 = []
meta_class5 = []
meta_class6 = []


for i, this_ml in enumerate(meta_labels):
    if this_ml == 0:
        meta_class1.append(i)
    elif this_ml == 1:
        meta_class2.append(i)
    elif this_ml == 2:
        meta_class3.append(i)
    elif this_ml == 3:
        meta_class4.append(i)
    elif this_ml == 4:
        meta_class5.append(i)
    elif this_ml == 5:
        meta_class6.append(i)
mc_1_orig = [num_to_label[labels_test[f]] for f in meta_class1]
mc_2_orig = [num_to_label[labels_test[f]] for f in meta_class2]
mc_3_orig = [num_to_label[labels_test[f]] for f in meta_class3]
mc_4_orig = [num_to_label[labels_test[f]] for f in meta_class4]
mc_5_orig = [num_to_label[labels_test[f]] for f in meta_class5]
mc_6_orig = [num_to_label[labels_test[f]] for f in meta_class6]
counts_mc1 = dict(Counter(mc_1_orig).most_common())
counts_mc2 = dict(Counter(mc_2_orig).most_common())
counts_mc3 = dict(Counter(mc_3_orig).most_common())
counts_mc4 = dict(Counter(mc_4_orig).most_common())
counts_mc5 = dict(Counter(mc_5_orig).most_common())
counts_mc6 = dict(Counter(mc_6_orig).most_common())
all_counts = [counts_mc1,counts_mc2,counts_mc3,counts_mc4,counts_mc5,counts_mc6]
fig, axs = plt.subplots(6,1,figsize = (16,32))

for i,ax in enumerate(axs):
    
    length = len(all_counts[i])
    x_ticks = np.arange(length)
    values = all_counts[i].values()
    x_ticklabels = all_counts[i].keys()
    
    fig = ax.bar(x_ticks,values)
    fig = ax.set_xticks(x_ticks)
    fig = ax.set_xticklabels(x_ticklabels,rotation = 90)
plt.tight_layout()
fig, ax = plt.subplots(1,1,figsize = (16,4))

fig = ax.bar(np.arange(len(counts_mc1)),counts_mc1.values())
fig = ax.set_xticks(np.arange(len(counts_mc1)))
fig = ax.set_xticklabels(counts_mc1.keys(),rotation = 90)<jupyter_output><empty_output><jupyter_text>Cluster from MS-40<jupyter_code>import pickle
with open('./preprocessed_test/MS-80-HL512-WF16k-64k','rb') as fp:
    data = pickle.load(fp)
x_mels = np.zeros((1600,80,126))

for i, spect in enumerate(data):
    
    if spect.shape[1] > 126:
        max_start = spect.shape[1]-126
        start = np.random.randint(0,max_start)
        end = start+126
        spect = spect[:,start:end]
    elif spect.shape[1] == 126:
        pass
    elif spect.shape[1] < 126:
        print("Hol\'up a minute'")
        print("Problem at  {}".format(i))
        
    x_mels[i] = spect
x_mels2 = np.reshape(x_mels,(1600,10080,))
plt.figure(figsize = (15,5))
dendogram = sch.dendrogram(sch.linkage(x_mels2, method = 'ward'))
cluster_model3 = AgglomerativeClustering(n_clusters=6, affinity='euclidean', linkage = 'ward')
cluster_model3.fit(x_mels2)
meta_labels = cluster_model3.labels_
meta_class1 = []
meta_class2 = []
meta_class3 = []
meta_class4 = []
meta_class5 = []
meta_class6 = []


for i, this_ml in enumerate(meta_labels):
    if this_ml == 0:
        meta_class1.append(i)
    elif this_ml == 1:
        meta_class2.append(i)
    elif this_ml == 2:
        meta_class3.append(i)
    elif this_ml == 3:
        meta_class4.append(i)
    elif this_ml == 4:
        meta_class5.append(i)
    elif this_ml == 5:
        meta_class6.append(i)
mc_1_orig = [num_to_label[labels_test[f]] for f in meta_class1]
mc_2_orig = [num_to_label[labels_test[f]] for f in meta_class2]
mc_3_orig = [num_to_label[labels_test[f]] for f in meta_class3]
mc_4_orig = [num_to_label[labels_test[f]] for f in meta_class4]
mc_5_orig = [num_to_label[labels_test[f]] for f in meta_class5]
mc_6_orig = [num_to_label[labels_test[f]] for f in meta_class6]
counts_mc1 = dict(Counter(mc_1_orig).most_common())
counts_mc2 = dict(Counter(mc_2_orig).most_common())
counts_mc3 = dict(Counter(mc_3_orig).most_common())
counts_mc4 = dict(Counter(mc_4_orig).most_common())
counts_mc5 = dict(Counter(mc_5_orig).most_common())
counts_mc6 = dict(Counter(mc_6_orig).most_common())
all_counts = [counts_mc1,counts_mc2,counts_mc3,counts_mc4,counts_mc5,counts_mc6]
fig, axs = plt.subplots(6,1,figsize = (16,32))

for i,ax in enumerate(axs):
    
    length = len(all_counts[i])
    x_ticks = np.arange(length)
    values = all_counts[i].values()
    x_ticklabels = all_counts[i].keys()
    
    fig = ax.bar(x_ticks,values)
    fig = ax.set_xticks(x_ticks)
    fig = ax.set_xticklabels(x_ticklabels,rotation = 90)
plt.tight_layout()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12843324154487043, 'fraction_numerical': 0.03247899456329874, 'mean_word_length': 4.13746826260428, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '443771', 'license': 'MIT', 'n_tokens_mistral': 5675, 'n_tokens_neox': 5424, 'n_words': 917, 'path': 'Meta_Classes_from_Clustering.ipynb', 'repo_name': 'ebucheli/FreeSound_GPAT', 'size': '14163'}