{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n# Read all the data in teh environment \nimport os\n#for dirname, _, filenames in os.walk('/kaggle/input'):\n #   for filename in filenames:\n        #print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-01-22T21:31:48.992747Z","iopub.execute_input":"2022-01-22T21:31:48.993018Z","iopub.status.idle":"2022-01-22T21:31:48.997421Z","shell.execute_reply.started":"2022-01-22T21:31:48.99299Z","shell.execute_reply":"2022-01-22T21:31:48.996638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## For memory usage \n\nimport tracemalloc\n\ntracemalloc.start()","metadata":{"execution":{"iopub.status.busy":"2022-01-22T21:31:49.433776Z","iopub.execute_input":"2022-01-22T21:31:49.434042Z","iopub.status.idle":"2022-01-22T21:31:49.440104Z","shell.execute_reply.started":"2022-01-22T21:31:49.434013Z","shell.execute_reply":"2022-01-22T21:31:49.439252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Les imports \n\nfrom __future__ import print_function\nimport tensorflow as tf\n\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D, Activation, concatenate , Average, Concatenate\nfrom tensorflow.keras import layers, Model , Input\n#from tensorflow.keras.layers.core import Reshape\nfrom tensorflow import reshape\n\n\n#from pyimagesearch import models\n\n\n#from keras import backend as K\n\nimport random\nimport numpy as np\nimport pandas as pd\n\nimport scipy.io\nfrom scipy.signal import spectrogram\nimport matplotlib.pyplot as plt\n\nfrom numpy import zeros, newaxis\nfrom scipy import stats","metadata":{"execution":{"iopub.status.busy":"2022-01-22T21:31:53.24518Z","iopub.execute_input":"2022-01-22T21:31:53.245764Z","iopub.status.idle":"2022-01-22T21:32:03.536948Z","shell.execute_reply.started":"2022-01-22T21:31:53.245724Z","shell.execute_reply":"2022-01-22T21:32:03.535739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"############# Another draft #############\n#######  I really can't explain what is done here !   #######\n\n#### Try to understand ##### \n#Each .mat file contains a data structure with fields as follow:\n#data: a matrix of EEG sample values arranged row x column as electrode x time.\n#data_length_sec: the time duration of each data row\n#sampling_frequency: the number of data samples representing 1 second of EEG data.\n#channels: a list of electrode names corresponding to the rows in the data field\n#sequence: the index of the data segment within the one hour series of clips. For example, preictal_segment_6.mat has a sequence number of 6, and represents the iEEG data from 50 to 60 minutes into the preictal data.\n\n######## Load one data : a preictal segment and an interictal segment #######\n\ninterictal_tst = '/kaggle/input/seizure-prediction/Patient_1/Patient_1/Patient_1_interictal_segment_0001.mat'\npreictal_tst = '/kaggle/input/seizure-prediction/Patient_1/Patient_1/Patient_1_preictal_segment_0001.mat'\ninterictal_data = scipy.io.loadmat(interictal_tst)\npreictal_data = scipy.io.loadmat(preictal_tst)\n    \ninterictal_array = interictal_data['interictal_segment_1'][0][0][0]\npreictal_array = preictal_data['preictal_segment_1'][0][0][0]\n\n###### Well \n# I finally undestood. It is to get access on the data \n","metadata":{"execution":{"iopub.status.busy":"2022-01-22T21:32:03.539102Z","iopub.execute_input":"2022-01-22T21:32:03.53942Z","iopub.status.idle":"2022-01-22T21:32:07.415145Z","shell.execute_reply.started":"2022-01-22T21:32:03.539381Z","shell.execute_reply":"2022-01-22T21:32:07.414204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#### Create spectograms\n\nl = list(range(10000)) # cree une seq de nombre de 0 a 9999\nfor i in l[::5000]:\n    print('Interictal')\n    i_secs = interictal_array[0][i:i+5000]\n    print(interictal_array[0][i:i+5000])\n    i_f, i_t, i_Sxx = spectrogram(i_secs, fs=5000, return_onesided=False)\n    i_SS = np.log1p(i_Sxx)\n    plt.imshow(i_SS[:] / np.max(i_SS), cmap='gray')\n    plt.show()\n    print('Preictal')\n    p_secs = preictal_array[0][i:i+5000]\n    p_f, p_t, p_Sxx = spectrogram(p_secs, fs=5000, return_onesided=False)\n    p_SS = np.log1p(p_Sxx)\n    plt.imshow(p_SS[:] / np.max(p_SS), cmap='gray')\n    plt.show()\n\n# p_f = array de freq\n# p_t = array de times series\n# p_Sxx = Spectogram de x en sortie \n# P.S : Les spectrogrammes peuvent être utilisés pour visualiser \n#l'évolution du contenu fréquentiel d'un signal non stationnaire dans le temps.","metadata":{"execution":{"iopub.status.busy":"2022-01-22T21:32:07.41659Z","iopub.execute_input":"2022-01-22T21:32:07.416989Z","iopub.status.idle":"2022-01-22T21:32:08.266608Z","shell.execute_reply.started":"2022-01-22T21:32:07.416939Z","shell.execute_reply":"2022-01-22T21:32:08.26546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Le jeu de donnees a 7 patients. donc 5 sont des chiens et les deux autres des humains (Patient1 et patient 2)\n## Le dossier du Patient 2 est vide. Du coup on travaille juste avec le patient 1.  \n## Le patient 1 a 50 enregs en phase interictale contre 18 enregs en phase preictale.\n\n## On va donc lire 18 fichiers en phase interictale pour un jeu de donnes equilibre\n\ndef generate_spect():\n    All_X_spect = [] # Spectral data \n    #All_X_raw = [] # iEEG data row  \n    All_Y = []   # target data \n    #All_d_array = []\n\n    types = ['Patient_1_interictal_segment', 'Patient_1_preictal_segment']\n    for i,typ in enumerate(types):\n        for j in range(9):\n            ## Read the file\n            fl = '/kaggle/input/seizure-prediction/Patient_1/Patient_1/{}_{}.mat'.format(typ, str(j + 1).zfill(4))\n            ## Load the mat file into a matrix\n            data = scipy.io.loadmat(fl)\n            k = typ.replace('Patient_1_', '') + '_'\n            #print(k)\n            d_array = data[k + str(j + 1)][0][0][0]  #d_array.shape = (15, 3000000)\n            \n            #All_d_array.append(d_array)\n\n            ## Transform spectoram data \n            lst = list(range(3000000))  # 10 minutes\n            for m in lst[::5000]:\n                # Create a spectrogram every 1 second\n                p_secs = d_array[0][m:m+5000]\n                p_f, p_t, arr = spectrogram(p_secs, fs=5000, return_onesided=False)\n                #p_SS = np.log1p(p_Sxx) #bof \n                #arr = stats.zscore(arr) # normalisation z-score\n\n                ## Transform iEEG raw data \n                \n                ## How to use a generator here?: \n                #X_2 = d_array[:, m:m+5000]\n                #X_2 = np.array(X_2)\n                #X_2 = np.reshape(X_2,  (250 , 300))\n                #X_2 = stats.zscore(X_2)\n\n                All_X_spect.append(arr)\n               # All_X_raw.append(X_2)\n                All_Y.append(i)\n    return All_X_spect,All_Y","metadata":{"execution":{"iopub.status.busy":"2022-01-22T21:32:08.268927Z","iopub.execute_input":"2022-01-22T21:32:08.269211Z","iopub.status.idle":"2022-01-22T21:32:08.284146Z","shell.execute_reply.started":"2022-01-22T21:32:08.269177Z","shell.execute_reply":"2022-01-22T21:32:08.282837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def read_file_matrix():\n    types = ['Patient_1_interictal_segment', 'Patient_1_preictal_segment']\n    for i,typ in enumerate(types):\n        for j in range(9):\n            ## Read the file\n            fl = '/kaggle/input/seizure-prediction/Patient_1/Patient_1/{}_{}.mat'.format(typ, str(j + 1).zfill(4))\n            ## Load the mat file into a matrix\n            data = scipy.io.loadmat(fl)\n            k = typ.replace('Patient_1_', '') + '_'\n            #print(k)\n            d_array = data[k + str(j + 1)][0][0][0]\n            #d_array.shape = (15, 3000000)\n            \n            yield d_array\n    ","metadata":{"execution":{"iopub.status.busy":"2022-01-22T21:32:08.286641Z","iopub.execute_input":"2022-01-22T21:32:08.287548Z","iopub.status.idle":"2022-01-22T21:32:08.300547Z","shell.execute_reply.started":"2022-01-22T21:32:08.287475Z","shell.execute_reply":"2022-01-22T21:32:08.299498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def generate_matrix(d_array):\n    #for d_array in list_d_array:\n    lst = list(range(3000000))  # 10 minutes\n    for m in lst[::5000]:\n        X_2 = d_array[:, m:m+5000]\n        #X_2 = np.array(X_2)\n        X_2 = np.reshape(X_2,  (250 , 300))\n        #X_2 = stats.zscore(X_2)    \n        yield X_2","metadata":{"execution":{"iopub.status.busy":"2022-01-22T21:32:08.303358Z","iopub.execute_input":"2022-01-22T21:32:08.303979Z","iopub.status.idle":"2022-01-22T21:32:08.315735Z","shell.execute_reply.started":"2022-01-22T21:32:08.303905Z","shell.execute_reply":"2022-01-22T21:32:08.314376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"All_X_spect, All_Y  = generate_spect()","metadata":{"execution":{"iopub.status.busy":"2022-01-22T21:32:08.319146Z","iopub.execute_input":"2022-01-22T21:32:08.320087Z","iopub.status.idle":"2022-01-22T21:32:53.743959Z","shell.execute_reply.started":"2022-01-22T21:32:08.320035Z","shell.execute_reply":"2022-01-22T21:32:53.741826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(All_X_spect)","metadata":{"execution":{"iopub.status.busy":"2022-01-22T21:33:48.407561Z","iopub.execute_input":"2022-01-22T21:33:48.407987Z","iopub.status.idle":"2022-01-22T21:33:48.416119Z","shell.execute_reply.started":"2022-01-22T21:33:48.407942Z","shell.execute_reply":"2022-01-22T21:33:48.414841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"All_X_raw = []\nfor x in read_file_matrix():\n   # print(x.shape)\n    for ar in generate_matrix(x):\n        All_X_raw.append(ar)\n        ","metadata":{"execution":{"iopub.status.busy":"2022-01-22T21:33:50.272919Z","iopub.execute_input":"2022-01-22T21:33:50.274089Z","iopub.status.idle":"2022-01-22T21:34:16.65287Z","shell.execute_reply.started":"2022-01-22T21:33:50.274009Z","shell.execute_reply":"2022-01-22T21:34:16.651949Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(All_X_raw))","metadata":{"execution":{"iopub.status.busy":"2022-01-22T21:34:16.65454Z","iopub.execute_input":"2022-01-22T21:34:16.655265Z","iopub.status.idle":"2022-01-22T21:34:16.660611Z","shell.execute_reply.started":"2022-01-22T21:34:16.655221Z","shell.execute_reply":"2022-01-22T21:34:16.659734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### Fonction pour shuffle les data \n\ndef shuffle(X, Y): \n    ## Zip permet de creer des tuples du genre (data, label)\n    dataset = list(zip(X, Y))\n\n    ## Shuffle du nouveu jeu crée \n    random.shuffle(dataset)\n    # Dezippage. Ainsi les labels reviennent dans leu tableau à part, et pareil pour les données. \n    X,Y = zip(*dataset)\n    \n    return X,Y ","metadata":{"execution":{"iopub.status.busy":"2022-01-22T21:34:16.661744Z","iopub.execute_input":"2022-01-22T21:34:16.662043Z","iopub.status.idle":"2022-01-22T21:34:16.676326Z","shell.execute_reply.started":"2022-01-22T21:34:16.662009Z","shell.execute_reply":"2022-01-22T21:34:16.675429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Zip the datas \ndata = list(zip(All_X_spect, All_X_raw))\n\n## Shufle des datas  \n(data , All_Y) = shuffle(data, All_Y)\n\n## Unzip \nx_spect, x_raw = zip(*data)","metadata":{"execution":{"iopub.status.busy":"2022-01-22T21:34:16.679203Z","iopub.execute_input":"2022-01-22T21:34:16.680129Z","iopub.status.idle":"2022-01-22T21:34:16.732107Z","shell.execute_reply.started":"2022-01-22T21:34:16.680079Z","shell.execute_reply":"2022-01-22T21:34:16.73092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"All_Y[100:200]","metadata":{"execution":{"iopub.status.busy":"2022-01-22T21:34:16.734514Z","iopub.execute_input":"2022-01-22T21:34:16.735123Z","iopub.status.idle":"2022-01-22T21:34:16.745353Z","shell.execute_reply.started":"2022-01-22T21:34:16.735068Z","shell.execute_reply":"2022-01-22T21:34:16.744202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Quelques paramètres de base pour notre model \nbatch_size = 10\nnum_classes = 2\n#epochs = 30\nrows_spect_data, cols_spect_data = 256, 22\nrows_raw_data , cols_raw_data = 250 , 300","metadata":{"execution":{"iopub.status.busy":"2022-01-22T21:34:16.747167Z","iopub.execute_input":"2022-01-22T21:34:16.74828Z","iopub.status.idle":"2022-01-22T21:34:16.758046Z","shell.execute_reply.started":"2022-01-22T21:34:16.748226Z","shell.execute_reply":"2022-01-22T21:34:16.756644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### Division en trainset, valset et testset  \n\n\n#1. Le trainset a 80% de la taille originale du jeu de donnees \n#1. Le valset a 10% de la taille originale du jeu de donnees \n#1. Le testset a 10% de la taille originale du jeu de donnees \n\n\n## Pour les donnes spectrales \nx_spect =  np.array(x_spect, dtype=np.uint16)\n\nratio = int( 0.8 * len(x_spect))\nrate = int (ratio + 0.1 * len(x_spect))\nrate2 = int (rate + 0.1 * len(x_spect))\n\n\n\nx_train_1 = x_spect[:ratio]\nx_val_1 = x_spect[ratio:rate]\nx_test_1 = x_spect[rate:rate2]\n\n\n\n## For raw iEEG data \nx_raw =  np.array(x_raw, dtype=np.uint16)\n\n\n#ratio_raw = int( 0.8 * len(x_raw))\n#rate_raw = int( ratio + 0.1 * len(x_raw))\n#rate2_raw = int (rate + 0.1 * len(x_raw))\n\n\nx_train_2 = x_raw[:ratio]\nx_val_2 = x_raw[ratio:rate]\nx_test_2 = x_raw[rate:rate2]","metadata":{"execution":{"iopub.status.busy":"2022-01-22T21:34:16.759996Z","iopub.execute_input":"2022-01-22T21:34:16.76035Z","iopub.status.idle":"2022-01-22T21:34:18.401218Z","shell.execute_reply.started":"2022-01-22T21:34:16.760311Z","shell.execute_reply":"2022-01-22T21:34:18.399914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x_spect.nbytes)\n#print(x_spect2.nbytes)","metadata":{"execution":{"iopub.status.busy":"2022-01-22T21:34:18.403128Z","iopub.execute_input":"2022-01-22T21:34:18.403593Z","iopub.status.idle":"2022-01-22T21:34:18.410724Z","shell.execute_reply.started":"2022-01-22T21:34:18.403532Z","shell.execute_reply":"2022-01-22T21:34:18.409352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Decoupage des targets \n\nAll_Y =  np.array(All_Y)\ny_train = All_Y[:ratio]\ny_val = All_Y[ratio:rate]\ny_test = All_Y[rate:rate2]\nprint(len(y_train))\nprint(len(y_test))\nprint(len(y_val))","metadata":{"execution":{"iopub.status.busy":"2022-01-22T21:34:18.412697Z","iopub.execute_input":"2022-01-22T21:34:18.413003Z","iopub.status.idle":"2022-01-22T21:34:18.426113Z","shell.execute_reply.started":"2022-01-22T21:34:18.412967Z","shell.execute_reply":"2022-01-22T21:34:18.425151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#### Reshape pour mettre sous la forme (#exemples, rows, cols, niveau_de_gris)\n#x_train_1 = np.array(x_spect)\nx_train_1 = x_train_1.reshape(x_train_1.shape[0], rows_spect_data, cols_spect_data, 1)\nx_val_1 = x_val_1.reshape(x_val_1.shape[0], rows_spect_data,cols_spect_data, 1)\nx_test_1 = x_test_1.reshape(x_test_1.shape[0], rows_spect_data,cols_spect_data, 1)\n\n\n\nx_train_2 = x_train_2.reshape(x_train_2.shape[0], rows_raw_data, cols_raw_data, 1)\nx_val_2 = x_val_2.reshape(x_val_2.shape[0], rows_raw_data, cols_raw_data, 1)\nx_test_2 = x_test_2.reshape(x_test_2.shape[0], rows_raw_data, cols_raw_data, 1)\n\n\n### Taille de l'input du model \ninput_shape_1 = (rows_spect_data, cols_spect_data, 1)\ninput_shape_2 = (rows_raw_data, cols_raw_data, 1)\n\n## On convertie les données en flottant au cas où elles ne l'étaient pas déjà.\nx_train_1 = x_train_1.astype('float32')\nx_val_1 = x_val_1.astype('float32')\nx_test_1 = x_test_1.astype('float32')\n\nx_train_2 = x_train_2.astype('float32')\nx_val_2 = x_val_2.astype('float32')\nx_test_2 = x_test_2.astype('float32')","metadata":{"execution":{"iopub.status.busy":"2022-01-22T21:34:18.429814Z","iopub.execute_input":"2022-01-22T21:34:18.430206Z","iopub.status.idle":"2022-01-22T21:34:20.974473Z","shell.execute_reply.started":"2022-01-22T21:34:18.430163Z","shell.execute_reply":"2022-01-22T21:34:20.973505Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x_train_1.shape)\nprint(x_train_2.shape)","metadata":{"execution":{"iopub.status.busy":"2022-01-22T21:34:20.975709Z","iopub.execute_input":"2022-01-22T21:34:20.97597Z","iopub.status.idle":"2022-01-22T21:34:20.982529Z","shell.execute_reply.started":"2022-01-22T21:34:20.97594Z","shell.execute_reply":"2022-01-22T21:34:20.981489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"All_Y[6000:6200]","metadata":{"execution":{"iopub.status.busy":"2022-01-22T21:34:20.984243Z","iopub.execute_input":"2022-01-22T21:34:20.98515Z","iopub.status.idle":"2022-01-22T21:34:20.99939Z","shell.execute_reply.started":"2022-01-22T21:34:20.98511Z","shell.execute_reply":"2022-01-22T21:34:20.998608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x_train_1.nbytes)","metadata":{"execution":{"iopub.status.busy":"2022-01-22T21:34:21.000815Z","iopub.execute_input":"2022-01-22T21:34:21.001134Z","iopub.status.idle":"2022-01-22T21:34:21.01027Z","shell.execute_reply.started":"2022-01-22T21:34:21.001087Z","shell.execute_reply":"2022-01-22T21:34:21.009135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_classes","metadata":{"execution":{"iopub.status.busy":"2022-01-22T21:34:21.01161Z","iopub.execute_input":"2022-01-22T21:34:21.011917Z","iopub.status.idle":"2022-01-22T21:34:21.022264Z","shell.execute_reply.started":"2022-01-22T21:34:21.011881Z","shell.execute_reply":"2022-01-22T21:34:21.021497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = tf.keras.utils.to_categorical(y_train, num_classes) \ny_val = tf.keras.utils.to_categorical(y_val, num_classes)\ny_test = tf.keras.utils.to_categorical(y_test, num_classes)","metadata":{"execution":{"iopub.status.busy":"2022-01-22T21:34:21.023734Z","iopub.execute_input":"2022-01-22T21:34:21.024652Z","iopub.status.idle":"2022-01-22T21:34:21.034486Z","shell.execute_reply.started":"2022-01-22T21:34:21.024604Z","shell.execute_reply":"2022-01-22T21:34:21.033529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reshape y \ny_test = y_test[:, newaxis, :]\ny_train = y_train[:, newaxis, :]\ny_val = y_val[:, newaxis, :]\n\n\n### Reshape \n#y_test = y_test.reshape(y_test.shape[0], 1, y_test.shape[2])\n\ny_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-01-22T21:34:21.035954Z","iopub.execute_input":"2022-01-22T21:34:21.036917Z","iopub.status.idle":"2022-01-22T21:34:21.050623Z","shell.execute_reply.started":"2022-01-22T21:34:21.036868Z","shell.execute_reply":"2022-01-22T21:34:21.049509Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def define_cnn(input_shape, filters=(16, 64, 128)):\n    \n    # Define the model input \n    \n    inputs = Input(shape = input_shape)\n    for (i,f) in enumerate (filters):\n        \n        if i == 0 : \n            x = inputs \n            \n        x = layers.Conv2D(filters = f, kernel_size=(5, 5),strides=(2, 2), activation='relu', padding=\"same\")(x)\n        x = layers.BatchNormalization()(x)\n\n    x = layers.Dense(9)(x)\n    x = Activation('relu')(x)\n    x = layers.Dropout(0.25)(x)\n    outputs = layers.Reshape((-1, x.shape[1] * x.shape[2], x.shape[3]))(x)\n    #print(x.shape)\n    \n    model = Model(inputs, outputs)\n    \n    return model ","metadata":{"execution":{"iopub.status.busy":"2022-01-22T21:34:21.052709Z","iopub.execute_input":"2022-01-22T21:34:21.053603Z","iopub.status.idle":"2022-01-22T21:34:21.074261Z","shell.execute_reply.started":"2022-01-22T21:34:21.053541Z","shell.execute_reply":"2022-01-22T21:34:21.073257Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Appel sur les inputs \n\n## Define the inputs variable \ninput_shape_spect = (256, 22, 1 )\ninput_shape_raw_data = (250, 300, 1 )\n\n\n## Call the function to build our model \nmodel1 = define_cnn(input_shape_spect)\nmodel2 = define_cnn(input_shape_raw_data)\n\n\n#model2.output.shape = (None, 1, 60, 9)\n#model1.output.shape = [None, 1, 96, 9]\n\n# Then combine the outputs shape \ncombinedInput = Concatenate(axis=2)([model1.output, model2.output])","metadata":{"execution":{"iopub.status.busy":"2022-01-22T21:34:21.075996Z","iopub.execute_input":"2022-01-22T21:34:21.07834Z","iopub.status.idle":"2022-01-22T21:34:21.543706Z","shell.execute_reply.started":"2022-01-22T21:34:21.078254Z","shell.execute_reply":"2022-01-22T21:34:21.542686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"combinedInput.shape","metadata":{"execution":{"iopub.status.busy":"2022-01-22T21:34:21.545061Z","iopub.execute_input":"2022-01-22T21:34:21.545327Z","iopub.status.idle":"2022-01-22T21:34:21.552887Z","shell.execute_reply.started":"2022-01-22T21:34:21.545294Z","shell.execute_reply":"2022-01-22T21:34:21.551496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"### Last parameters \n\nx = layers.Dense(16, activation = 'softmax')(combinedInput)\n\n# Reshape \nx = layers.Reshape((-1, x.shape[1] * x.shape[2]* x.shape[3]))(x)\noutput = layers.Dropout(0.1)(x)","metadata":{"execution":{"iopub.status.busy":"2022-01-22T21:34:21.554892Z","iopub.execute_input":"2022-01-22T21:34:21.55535Z","iopub.status.idle":"2022-01-22T21:34:21.607421Z","shell.execute_reply.started":"2022-01-22T21:34:21.555284Z","shell.execute_reply":"2022-01-22T21:34:21.606545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output.shape","metadata":{"execution":{"iopub.status.busy":"2022-01-22T21:34:21.609264Z","iopub.execute_input":"2022-01-22T21:34:21.61036Z","iopub.status.idle":"2022-01-22T21:34:21.618677Z","shell.execute_reply.started":"2022-01-22T21:34:21.610302Z","shell.execute_reply":"2022-01-22T21:34:21.617706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"###### Add lstm \nlstm = layers.LSTM(20, return_sequences = True)\n\nintermediaire_outputs = lstm(output)\n\ndense_layer = layers.Dense(2, activation= \"sigmoid\")\n\nfinal_outputs = dense_layer(intermediaire_outputs)","metadata":{"execution":{"iopub.status.busy":"2022-01-22T21:34:21.620279Z","iopub.execute_input":"2022-01-22T21:34:21.621017Z","iopub.status.idle":"2022-01-22T21:34:22.030577Z","shell.execute_reply.started":"2022-01-22T21:34:21.620973Z","shell.execute_reply":"2022-01-22T21:34:22.029662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_outputs.shape","metadata":{"execution":{"iopub.status.busy":"2022-01-22T21:34:22.03226Z","iopub.execute_input":"2022-01-22T21:34:22.032582Z","iopub.status.idle":"2022-01-22T21:34:22.03986Z","shell.execute_reply.started":"2022-01-22T21:34:22.032545Z","shell.execute_reply":"2022-01-22T21:34:22.03872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_final = Model (inputs = [model1.input, model2.input], outputs = final_outputs )","metadata":{"execution":{"iopub.status.busy":"2022-01-22T21:34:22.041594Z","iopub.execute_input":"2022-01-22T21:34:22.042147Z","iopub.status.idle":"2022-01-22T21:34:22.058442Z","shell.execute_reply.started":"2022-01-22T21:34:22.042098Z","shell.execute_reply":"2022-01-22T21:34:22.057283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_final.compile(loss=\"binary_crossentropy\",\n              optimizer=tf.keras.optimizers.Adam(learning_rate = 0.0001),\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-01-22T21:34:22.060161Z","iopub.execute_input":"2022-01-22T21:34:22.060534Z","iopub.status.idle":"2022-01-22T21:34:22.081013Z","shell.execute_reply.started":"2022-01-22T21:34:22.060491Z","shell.execute_reply":"2022-01-22T21:34:22.079993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_final.summary()","metadata":{"execution":{"iopub.status.busy":"2022-01-22T21:34:22.082679Z","iopub.execute_input":"2022-01-22T21:34:22.083568Z","iopub.status.idle":"2022-01-22T21:34:22.108778Z","shell.execute_reply.started":"2022-01-22T21:34:22.083506Z","shell.execute_reply":"2022-01-22T21:34:22.107735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_outputs.shape","metadata":{"execution":{"iopub.status.busy":"2022-01-22T21:34:22.110312Z","iopub.execute_input":"2022-01-22T21:34:22.110733Z","iopub.status.idle":"2022-01-22T21:34:22.118482Z","shell.execute_reply.started":"2022-01-22T21:34:22.11069Z","shell.execute_reply":"2022-01-22T21:34:22.117353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_final.fit([x_train_1, x_train_2], y_train, \n          batch_size=batch_size,\n          epochs=20 ,\n          #verbose=1,\n          validation_data=([x_val_1, x_val_2],  [y_val]))\n    \n#score = model.evaluate(x = [x_test_1, x_test_2], y = [y_test_1], verbose=0)\n#print('Test loss:', score[0])\n#print('Test accuracy:', score[1])","metadata":{"execution":{"iopub.status.busy":"2022-01-22T21:34:22.123196Z","iopub.execute_input":"2022-01-22T21:34:22.123664Z","iopub.status.idle":"2022-01-22T23:05:51.518484Z","shell.execute_reply.started":"2022-01-22T21:34:22.123615Z","shell.execute_reply":"2022-01-22T23:05:51.517103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"    \nscore = model_final.evaluate(x = [x_test_1, x_test_2, y = [y_test])\nprint('Test loss:', score[0])\nprint('Test accuracy:', score[1])","metadata":{"execution":{"iopub.status.busy":"2021-12-10T14:20:46.453591Z","iopub.execute_input":"2021-12-10T14:20:46.454671Z","iopub.status.idle":"2021-12-10T14:20:46.852029Z","shell.execute_reply.started":"2021-12-10T14:20:46.454608Z","shell.execute_reply":"2021-12-10T14:20:46.851112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}