<jupyter_start><jupyter_text>Load Data<jupyter_code>pickle_train = './preprocessed_train/MS-80-HL512-WF16k-64k'
pickle_test = './preprocessed_test/MS-80-HL512-WF16k-64k'
with open(pickle_train,'rb') as fp:
    x_train = pickle.load(fp)
with open(pickle_test, 'rb') as fp:
    x_test = pickle.load(fp)
sr = 16000
file_length = sr*4
hop_length = 512
n_mels = 80

frames = int(np.ceil(file_length/hop_length))
from keras.utils import Sequence

class DataGenerator(Sequence):
    def __init__(self,x_set,y_set,
                 batch_size = 128,input_shape = (80,frames),
                 n_classes=80, sr = 44100, seconds = 4):
        
        self.x, self.y = x_set,y_set
        self.batch_size = batch_size
        self.input_shape = input_shape
        self.n_classes = n_classes
        self.sr = sr
        self.file_length = sr*seconds
        #self.on_epoch_end()
    
    def __len__(self):
        return int(np.ceil(len(self.x)/self.batch_size))
    
    def __getitem__(self,idx):
        
        spects = self.x[idx*self.batch_size:(idx+1)*self.batch_size]
        batch_y = self.y[idx*self.batch_size:(idx+1)*self.batch_size]
        
        batch_x = self.__generate_data(spects)
        
        #print(batch_x.shape)
        #print(batch_y.shape)
        
        return batch_x,batch_y
    
    def __generate_data(self,spects):
        n_mels,frames = self.input_shape
        x_batch = np.zeros((len(spects),n_mels,frames))
        
        for i, spect in enumerate(spects):
            freq_res,time_res = spect.shape
            
            max_start = time_res-frames
            if max_start == 0:
                start = 0
            else:
                start = np.random.randint(0,max_start)
            end = start+frames
            
            x_batch[i] = spect[:,start:end]
        
        return x_batch<jupyter_output>Using TensorFlow backend.<jupyter_text>Malley CNN<jupyter_code>from keras.layers import Conv2D, MaxPool2D, Dropout, Activation
from keras.layers import GlobalMaxPool2D, Dense, Input
from keras.layers import Lambda
from keras.models import Model
from keras.activations import relu, softmax
from keras.backend import expand_dims
from keras.optimizers import Adam
from kapre.time_frequency import Melspectrogram
from kapre.utils import Normalization2D
def malley_cnn_80(input_shape, n_classes):

    X_input = Input(input_shape)
    
    X = Lambda(lambda q: expand_dims(q, -1), name='expand_dims') (X_input)
    
    X = Conv2D(64,[3,7],padding = 'same')(X)
    X = Activation(relu)(X)
    X = MaxPool2D([2,1])(X)
    
    X = Conv2D(128,[7,1],padding = 'same')(X)
    X = Activation('relu')(X)
    X = MaxPool2D([4,1])(X)
    
    X = Conv2D(256,[10,1],padding = 'valid')(X)
    X = Activation('relu')(X)
    
    X = Conv2D(512,[1,7],padding = 'same')(X)
    X = Activation('relu')(X)
    
    X = GlobalMaxPool2D()(X)
    
    X = Dense(512,activation = 'relu')(X)
    X = Dropout(0.5)(X)
    
    X = Dense(n_classes, activation = 'softmax')(X)

    model = Model(inputs = X_input, outputs = X)
    
    return model
input_shape = ([n_mels,frames])
lr = 0.001
malley = malley_cnn_80(input_shape,n_meta_classes)
malley.summary()
malley.compile(optimizer=Adam(lr),loss = 'sparse_categorical_crossentropy',metrics = ['accuracy'])<jupyter_output><empty_output><jupyter_text>Split Dataset and make Generators<jupyter_code>from sklearn.model_selection import train_test_split
#X_train_n,X_val_n,y_train_n,y_val_n = train_test_split(X_noisy,y_noisy,test_size=0.1, random_state=7)
X_train_a,X_val_a,y_train_a,y_val_a = train_test_split(x_train,meta_labels_all,test_size=0.1, random_state=7)
#train_generator_n = DataGenerator(X_train_n,y_train_n)
#val_generator_n = DataGenerator(X_val_n,y_val_n)
train_generator_a = DataGenerator(X_train_a,y_train_a)
val_generator_a = DataGenerator(X_val_a,y_val_a)<jupyter_output><empty_output><jupyter_text>Train the Model<jupyter_code>from tensorflow.keras.callbacks import ModelCheckpoint
best_filepath = './weights.best.hdf5'
checkpoint = ModelCheckpoint(best_filepath,monitor='val_acc',save_best_only=True, mode='max')
callbacks_list = [checkpoint]
malley.fit_generator(train_generator_a,epochs = 50,validation_data=val_generator_a, callbacks = callbacks_list)
malley.load_weights('./weights.best.hdf5')
x_test_2 = np.zeros((1600,80,125))

for i, this_x in enumerate(x_test):
    
    this_frames = this_x.shape[1]
    
    if this_frames > 125:
        max_start = this_frames - 125
        start = np.random.randint(0,max_start)
        end = start+125

        this_x = this_x[:,start:end]
        
    x_test_2[i] = this_x
malley.evaluate(x_test_2,meta_labels_test)
malley.save_weights('./weights_test1_malley_mc_v3.h5')
y_scores = malley.predict(x_test_2)
y_hat = np.argmax(y_scores, axis = 1)
y_hat<jupyter_output><empty_output><jupyter_text>Plot Confussion Matrix<jupyter_code>from sklearn.metrics import confusion_matrix
from mpl_toolkits.axes_grid1 import make_axes_locatable
cm = confusion_matrix(meta_labels_test,y_hat)
normalize = True


fig, ax = plt.subplots(1,1,figsize = (15,15))
#plt.colorbar(im1,ax = ax)

if normalize:
    ax.set_title('Normalized Confussion Matrix for Meta Classes')
    cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]

im1 = ax.imshow(cm, cmap = plt.cm.Blues)

divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)

plt.colorbar(im1, cax=cax)

fig = ax.set(xlabel = 'Predicted', ylabel = 'True')
fig = ax.set_xticks(np.arange(len(np.unique(y_hat))))
#fig = ax.set_xticklabels(c1_to_name.values())

fig = ax.set_yticks(np.arange(len(np.unique(y_hat))))
#fig = ax.set_yticklabels(c1_to_name.values())

fmt = '.2f' if normalize else 'd'

#fmt = 'd'
thresh = cm.max() / 2.

for i in range(cm.shape[0]):
        for j in range(cm.shape[1]):
            ax.text(j, i, format(cm[i, j], fmt),
                    ha="center", va="center",
                    color="white" if cm[i, j] > thresh else "black")
#malley.save_weights('./weights_mc_malley_v2.h5')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1273538156590684, 'fraction_numerical': 0.025933267261314834, 'mean_word_length': 3.7378716744913927, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 22, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '446415', 'license': 'MIT', 'n_tokens_mistral': 2414, 'n_tokens_neox': 2299, 'n_words': 371, 'path': 'test_notebooks/FSDKaggle2018_MC_Classification_Malley.ipynb', 'repo_name': 'ebucheli/FreeSound_GPAT', 'size': '6054'}