{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":25954,"databundleVersionId":2091745,"sourceType":"competition"}],"dockerImageVersionId":30626,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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\nimport os\nfor 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":"2023-12-21T20:02:53.585161Z","iopub.execute_input":"2023-12-21T20:02:53.585562Z","iopub.status.idle":"2023-12-21T20:02:54.400445Z","shell.execute_reply.started":"2023-12-21T20:02:53.58553Z","shell.execute_reply":"2023-12-21T20:02:54.399428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install --upgrade pip setuptools","metadata":{"execution":{"iopub.status.busy":"2023-12-21T20:02:54.402269Z","iopub.execute_input":"2023-12-21T20:02:54.402576Z","iopub.status.idle":"2023-12-21T20:03:04.461331Z","shell.execute_reply.started":"2023-12-21T20:02:54.402549Z","shell.execute_reply":"2023-12-21T20:03:04.460104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport matplotlib.pyplot as plt\nimport soundfile as sf\nimport librosa\nimport librosa.display\nimport IPython.display as display\n\nfrom sklearn.model_selection import train_test_split\n\nfrom keras.utils import Sequence\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten, Conv1D, MaxPool1D, BatchNormalization\nfrom keras.optimizers import RMSprop,Adam\nfrom keras.applications import VGG19, VGG16, ResNet50\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"execution":{"iopub.status.busy":"2023-12-21T20:03:04.463511Z","iopub.execute_input":"2023-12-21T20:03:04.463927Z","iopub.status.idle":"2023-12-21T20:03:04.471352Z","shell.execute_reply.started":"2023-12-21T20:03:04.463894Z","shell.execute_reply":"2023-12-21T20:03:04.469777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path_input = '../input/birdclef-2021/'","metadata":{"execution":{"iopub.status.busy":"2023-12-21T20:03:04.474753Z","iopub.execute_input":"2023-12-21T20:03:04.475103Z","iopub.status.idle":"2023-12-21T20:03:04.483629Z","shell.execute_reply.started":"2023-12-21T20:03:04.475078Z","shell.execute_reply":"2023-12-21T20:03:04.482739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Support functions","metadata":{}},{"cell_type":"code","source":"def read_audio_file(path, file_name):\n    data, samplerate = sf.read(path + file_name)\n    return data, samplerate\n\ndef plot_audio_file(data, samplerate):\n    fig = plt.figure(figsize=(10, 5))\n    plt.plot(range(len(data)), data, color='r')\n    plt.legend(loc='upper right')\n    plt.grid()\n    \ndef plot_spectrogram(data, samplerate):\n    spectrogram = librosa.feature.melspectrogram(y=data, sr=samplerate)\n    log_spectrogram = librosa.power_to_db(spectrogram, ref=np.max)\n    librosa.display.specshow(log_spectrogram, sr=samplerate, x_axis='time', y_axis='mel')","metadata":{"execution":{"iopub.status.busy":"2023-12-21T20:03:04.484838Z","iopub.execute_input":"2023-12-21T20:03:04.485221Z","iopub.status.idle":"2023-12-21T20:03:04.496109Z","shell.execute_reply.started":"2023-12-21T20:03:04.485188Z","shell.execute_reply":"2023-12-21T20:03:04.495376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Overview Data","metadata":{}},{"cell_type":"code","source":"train_labels = pd.read_csv(path_input + 'train_soundscape_labels.csv')\ntrain_meta = pd.read_csv(path_input + 'train_metadata.csv')\ntest_data = pd.read_csv(path_input + 'test.csv')\nsamp_subm = pd.read_csv(path_input + 'sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-12-21T20:03:04.497331Z","iopub.execute_input":"2023-12-21T20:03:04.497626Z","iopub.status.idle":"2023-12-21T20:03:04.720831Z","shell.execute_reply.started":"2023-12-21T20:03:04.497602Z","shell.execute_reply":"2023-12-21T20:03:04.7192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.head()\n","metadata":{"execution":{"iopub.status.busy":"2023-12-21T20:03:04.723342Z","iopub.execute_input":"2023-12-21T20:03:04.723842Z","iopub.status.idle":"2023-12-21T20:03:04.736293Z","shell.execute_reply.started":"2023-12-21T20:03:04.723801Z","shell.execute_reply":"2023-12-21T20:03:04.73515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Number train label samples:', len(train_labels))\nprint('Number train meta samples:', len(train_meta))\nprint('Number train short folder:', len(os.listdir(path_input + 'train_short_audio')))\nprint('Number train audios:', len(os.listdir(path_input + 'train_soundscapes')))\nprint('Number test samples:', len(test_data))","metadata":{"execution":{"iopub.status.busy":"2023-12-21T20:03:04.737969Z","iopub.execute_input":"2023-12-21T20:03:04.73827Z","iopub.status.idle":"2023-12-21T20:03:04.749741Z","shell.execute_reply.started":"2023-12-21T20:03:04.738239Z","shell.execute_reply":"2023-12-21T20:03:04.748457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print sample\nos.listdir(path_input + 'train_short_audio/caltow')[:5]","metadata":{"execution":{"iopub.status.busy":"2023-12-21T20:03:04.751465Z","iopub.execute_input":"2023-12-21T20:03:04.751904Z","iopub.status.idle":"2023-12-21T20:03:04.759317Z","shell.execute_reply.started":"2023-12-21T20:03:04.751871Z","shell.execute_reply":"2023-12-21T20:03:04.758118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-21T20:03:04.763054Z","iopub.execute_input":"2023-12-21T20:03:04.763331Z","iopub.status.idle":"2023-12-21T20:03:04.774521Z","shell.execute_reply.started":"2023-12-21T20:03:04.763305Z","shell.execute_reply":"2023-12-21T20:03:04.773159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# number of labels\nprint(len(train_labels['birds'].unique()))\n\ntrain_labels['birds'].unique()","metadata":{"execution":{"iopub.status.busy":"2023-12-21T20:03:04.775444Z","iopub.execute_input":"2023-12-21T20:03:04.775862Z","iopub.status.idle":"2023-12-21T20:03:04.788798Z","shell.execute_reply.started":"2023-12-21T20:03:04.775833Z","shell.execute_reply":"2023-12-21T20:03:04.787945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_meta.head()","metadata":{"execution":{"iopub.status.busy":"2023-12-21T20:03:04.789874Z","iopub.execute_input":"2023-12-21T20:03:04.790275Z","iopub.status.idle":"2023-12-21T20:03:04.813462Z","shell.execute_reply.started":"2023-12-21T20:03:04.790249Z","shell.execute_reply":"2023-12-21T20:03:04.811947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_meta['common_name'].unique())","metadata":{"execution":{"iopub.status.busy":"2023-12-21T20:03:04.814916Z","iopub.execute_input":"2023-12-21T20:03:04.815224Z","iopub.status.idle":"2023-12-21T20:03:04.826275Z","shell.execute_reply.started":"2023-12-21T20:03:04.815195Z","shell.execute_reply":"2023-12-21T20:03:04.825016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.scatter(train_meta['latitude'], train_meta['longitude'])","metadata":{"execution":{"iopub.status.busy":"2023-12-21T20:03:04.828138Z","iopub.execute_input":"2023-12-21T20:03:04.828547Z","iopub.status.idle":"2023-12-21T20:03:05.258552Z","shell.execute_reply.started":"2023-12-21T20:03:04.82851Z","shell.execute_reply":"2023-12-21T20:03:05.256963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### sample of first row","metadata":{}},{"cell_type":"code","source":"row = 0\n# check filename in os.listdir\nlabel = train_meta.loc[row, 'primary_label']\nfilename = train_meta.loc[row, 'filename']\n\n# Check if the file is in the folder\nfilename in os.listdir(path_input + 'train_short_audio/' + label)","metadata":{"execution":{"iopub.status.busy":"2023-12-21T20:03:05.260022Z","iopub.execute_input":"2023-12-21T20:03:05.261057Z","iopub.status.idle":"2023-12-21T20:03:05.269709Z","shell.execute_reply.started":"2023-12-21T20:03:05.261011Z","shell.execute_reply":"2023-12-21T20:03:05.268656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data, samplerate = sf.read(path_input + 'train_short_audio/' + label +'/' + filename)\nprint(data[:8])\nprint(samplerate)","metadata":{"execution":{"iopub.status.busy":"2023-12-21T20:03:05.271213Z","iopub.execute_input":"2023-12-21T20:03:05.271579Z","iopub.status.idle":"2023-12-21T20:03:05.341041Z","shell.execute_reply.started":"2023-12-21T20:03:05.271546Z","shell.execute_reply":"2023-12-21T20:03:05.339763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_audio_file(data, samplerate)","metadata":{"execution":{"iopub.status.busy":"2023-12-21T20:03:05.343007Z","iopub.execute_input":"2023-12-21T20:03:05.343779Z","iopub.status.idle":"2023-12-21T20:03:06.322944Z","shell.execute_reply.started":"2023-12-21T20:03:05.34374Z","shell.execute_reply":"2023-12-21T20:03:06.321528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_spectrogram(data, samplerate)","metadata":{"execution":{"iopub.status.busy":"2023-12-21T20:03:06.324803Z","iopub.execute_input":"2023-12-21T20:03:06.325163Z","iopub.status.idle":"2023-12-21T20:03:07.210508Z","shell.execute_reply.started":"2023-12-21T20:03:06.325133Z","shell.execute_reply":"2023-12-21T20:03:07.209549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# display audio\ndisplay.Audio(path_input + 'train_short_audio/' + label + '/' + filename)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-21T20:03:07.211576Z","iopub.execute_input":"2023-12-21T20:03:07.212703Z","iopub.status.idle":"2023-12-21T20:03:07.229988Z","shell.execute_reply.started":"2023-12-21T20:03:07.21258Z","shell.execute_reply":"2023-12-21T20:03:07.228685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# overview train labels","metadata":{}},{"cell_type":"code","source":"train_labels['audio_id'].unique()","metadata":{"execution":{"iopub.status.busy":"2023-12-21T20:03:07.23125Z","iopub.execute_input":"2023-12-21T20:03:07.231558Z","iopub.status.idle":"2023-12-21T20:03:07.238639Z","shell.execute_reply.started":"2023-12-21T20:03:07.231532Z","shell.execute_reply":"2023-12-21T20:03:07.237484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels.groupby(by=['audio_id']).count()['birds']","metadata":{"execution":{"iopub.status.busy":"2023-12-21T20:03:07.240631Z","iopub.execute_input":"2023-12-21T20:03:07.241208Z","iopub.status.idle":"2023-12-21T20:03:07.259869Z","shell.execute_reply.started":"2023-12-21T20:03:07.241173Z","shell.execute_reply":"2023-12-21T20:03:07.258862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('original label:', train_labels.loc[458, 'birds'])\nprint('split into list:', train_labels.loc[458, 'birds'].split(' '))","metadata":{"execution":{"iopub.status.busy":"2023-12-21T20:03:07.26119Z","iopub.execute_input":"2023-12-21T20:03:07.261661Z","iopub.status.idle":"2023-12-21T20:03:07.269205Z","shell.execute_reply.started":"2023-12-21T20:03:07.26163Z","shell.execute_reply":"2023-12-21T20:03:07.267694Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = []\nfor row in train_labels.index:\n    labels.extend(train_labels.loc[row, 'birds'].split(' '))\nlabels = list(set(labels))\n\nprint('Number of unique bird labels:', len(labels))","metadata":{"execution":{"iopub.status.busy":"2023-12-21T20:03:07.270347Z","iopub.execute_input":"2023-12-21T20:03:07.270679Z","iopub.status.idle":"2023-12-21T20:03:07.306831Z","shell.execute_reply.started":"2023-12-21T20:03:07.270648Z","shell.execute_reply":"2023-12-21T20:03:07.30542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_labels_train = pd.DataFrame(index=train_labels.index, columns=labels)\nfor row in train_labels.index:\n    birds = train_labels.loc[row, 'birds'].split(' ')\n    for bird in birds:\n        df_labels_train.loc[row, bird] = 1\ndf_labels_train.fillna(0, inplace=True)\n\n# We set a dummy value for the target label in the test data because we will need for the Data Generator\ntest_data['birds'] = 'nocall'\n\ndf_labels_test = pd.DataFrame(index=test_data.index, columns=labels)\nfor row in test_data.index:\n    birds = test_data.loc[row, 'birds'].split(' ')\n    for bird in birds:\n        df_labels_test.loc[row, bird] = 1\ndf_labels_test.fillna(0, inplace=True)","metadata":{"execution":{"iopub.status.busy":"2023-12-21T20:03:07.308857Z","iopub.execute_input":"2023-12-21T20:03:07.309175Z","iopub.status.idle":"2023-12-21T20:03:07.52711Z","shell.execute_reply.started":"2023-12-21T20:03:07.309145Z","shell.execute_reply":"2023-12-21T20:03:07.525968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_labels_train.sum().sort_values(ascending=False)","metadata":{"execution":{"iopub.status.busy":"2023-12-21T20:03:07.5306Z","iopub.execute_input":"2023-12-21T20:03:07.5309Z","iopub.status.idle":"2023-12-21T20:03:07.543817Z","shell.execute_reply.started":"2023-12-21T20:03:07.530875Z","shell.execute_reply":"2023-12-21T20:03:07.542532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels = pd.concat([train_labels, df_labels_train], axis=1)\ntest_data = pd.concat([test_data, df_labels_test], axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-12-21T20:03:07.54516Z","iopub.execute_input":"2023-12-21T20:03:07.54545Z","iopub.status.idle":"2023-12-21T20:03:07.555581Z","shell.execute_reply.started":"2023-12-21T20:03:07.545424Z","shell.execute_reply":"2023-12-21T20:03:07.554507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file = os.listdir(path_input + 'train_soundscapes')[0]\n\ndata, samplerate = read_audio_file(path_input + 'train_soundscapes/', file)","metadata":{"execution":{"iopub.status.busy":"2023-12-21T20:03:07.557827Z","iopub.execute_input":"2023-12-21T20:03:07.558687Z","iopub.status.idle":"2023-12-21T20:03:08.096894Z","shell.execute_reply.started":"2023-12-21T20:03:07.558652Z","shell.execute_reply":"2023-12-21T20:03:08.095735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"audio_id = file.split('_')[0]\nsite = file.split('_')[1]\nprint('audio_id:', audio_id, ', site:', site)","metadata":{"execution":{"iopub.status.busy":"2023-12-21T20:03:08.103125Z","iopub.execute_input":"2023-12-21T20:03:08.103782Z","iopub.status.idle":"2023-12-21T20:03:08.110686Z","shell.execute_reply.started":"2023-12-21T20:03:08.103748Z","shell.execute_reply":"2023-12-21T20:03:08.109206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels[(train_labels['audio_id']==int(audio_id)) & (train_labels['site']==site) & (train_labels['birds']!='nocall')]\n","metadata":{"execution":{"iopub.status.busy":"2023-12-21T20:03:08.111697Z","iopub.execute_input":"2023-12-21T20:03:08.112005Z","iopub.status.idle":"2023-12-21T20:03:08.143046Z","shell.execute_reply.started":"2023-12-21T20:03:08.111975Z","shell.execute_reply":"2023-12-21T20:03:08.141795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_data = data[int(455/5)*160000:int(460/5)*160000]\n\nplt.figure(figsize=(10, 5))\nlibrosa.display.waveshow(sub_data, sr=samplerate)\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-21T20:03:08.144419Z","iopub.execute_input":"2023-12-21T20:03:08.145384Z","iopub.status.idle":"2023-12-21T20:03:08.659096Z","shell.execute_reply.started":"2023-12-21T20:03:08.145336Z","shell.execute_reply":"2023-12-21T20:03:08.657855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display.Audio(sub_data, rate=samplerate)","metadata":{"execution":{"iopub.status.busy":"2023-12-21T20:03:08.660607Z","iopub.execute_input":"2023-12-21T20:03:08.661119Z","iopub.status.idle":"2023-12-21T20:03:08.675399Z","shell.execute_reply.started":"2023-12-21T20:03:08.66106Z","shell.execute_reply":"2023-12-21T20:03:08.673691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_lenght = 160000\naudio_lenght = 5\nnum_labels = len(labels)","metadata":{"execution":{"iopub.status.busy":"2023-12-21T20:03:08.676953Z","iopub.execute_input":"2023-12-21T20:03:08.677329Z","iopub.status.idle":"2023-12-21T20:03:08.681655Z","shell.execute_reply.started":"2023-12-21T20:03:08.677296Z","shell.execute_reply":"2023-12-21T20:03:08.680954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 16*2\nlist_IDs_train, list_IDs_val = train_test_split(list(train_labels.index), test_size=0.33, random_state=2021)\nlist_IDs_test = list(samp_subm.index)","metadata":{"execution":{"iopub.status.busy":"2023-12-21T20:03:08.682424Z","iopub.execute_input":"2023-12-21T20:03:08.682724Z","iopub.status.idle":"2023-12-21T20:03:08.696402Z","shell.execute_reply.started":"2023-12-21T20:03:08.682698Z","shell.execute_reply":"2023-12-21T20:03:08.694934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"list_IDs_train[:10], list_IDs_test","metadata":{"execution":{"iopub.status.busy":"2023-12-21T20:03:08.698493Z","iopub.execute_input":"2023-12-21T20:03:08.698832Z","iopub.status.idle":"2023-12-21T20:03:08.714615Z","shell.execute_reply.started":"2023-12-21T20:03:08.698802Z","shell.execute_reply":"2023-12-21T20:03:08.71281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class DataGenerator(Sequence):\n    def __init__(self, path, list_IDs, data, batch_size):\n        self.path = path\n        self.list_IDs = list_IDs\n        self.data = data\n        self.batch_size = batch_size\n        self.indexes = np.arange(len(self.list_IDs))\n        \n    def __len__(self):\n        len_ = int(len(self.list_IDs)/self.batch_size)\n        if len_*self.batch_size < len(self.list_IDs):\n            len_ += 1\n        return len_\n    \n    def __getitem__(self, index):\n        indexes = self.indexes[index*self.batch_size:(index+1)*self.batch_size]\n        list_IDs_temp = [self.list_IDs[k] for k in indexes]\n        X, y = self.__data_generation(list_IDs_temp)\n        X = X.reshape((self.batch_size, 100, 1600//2))\n        return X, y\n    \n    def __data_generation(self, list_IDs_temp):\n        X = np.zeros((self.batch_size, data_lenght//2))\n        y = np.zeros((self.batch_size, num_labels))\n        for i, ID in enumerate(list_IDs_temp):\n            prefix = str(self.data.loc[ID, 'audio_id'])+'_'+self.data.loc[ID, 'site']\n            file = [s for s in os.listdir(self.path) if prefix in s][0]\n            audio_file, audio_sr = read_audio_file(self.path, file)\n            audio_file = audio_file[int((self.data.loc[ID, 'seconds']-5)/audio_lenght)*data_lenght:int(self.data.loc[ID, 'seconds']/audio_lenght)*data_lenght]\n            audio_file_fft = data_fft = np.abs(np.fft.fft(audio_file)[: len(audio_file)//2])\n            # scale data\n            audio_file_fft = (audio_file_fft-audio_file_fft.mean())/audio_file_fft.std()\n            X[i, ] = audio_file_fft\n            y[i, ] = self.data.loc[ID, self.data.columns[5:]].values\n        return X, y\n    \n    def data_generate(self, list_IDs_temp):\n        X = np.zeros((self.batch_size, data_lenght//2))\n        y = np.zeros((self.batch_size, num_labels))\n        print(X.shape)\n        for i, ID in enumerate(list_IDs_temp):\n            prefix = str(self.data.loc[ID, 'audio_id'])+'_'+self.data.loc[ID, 'site']\n            file = [s for s in os.listdir(self.path) if prefix in s][0]\n            audio_file, audio_sr = read_audio_file(self.path, file)\n            audio_file = audio_file[int((self.data.loc[ID, 'seconds']-5)/audio_lenght)*data_lenght:int(self.data.loc[ID, 'seconds']/audio_lenght)*data_lenght]\n            audio_file_fft = data_fft = np.abs(np.fft.fft(audio_file)[: len(audio_file)//2])\n            # scale data\n            audio_file_fft = (audio_file_fft-audio_file_fft.mean())/audio_file_fft.std()\n            X[i, ] = audio_file_fft\n            y[i, ] = self.data.loc[ID, self.data.columns[5:]].values\n#             print(audio_file_fft[:5])\n        return X, y","metadata":{"execution":{"iopub.status.busy":"2023-12-21T20:03:08.715978Z","iopub.execute_input":"2023-12-21T20:03:08.717226Z","iopub.status.idle":"2023-12-21T20:03:08.736296Z","shell.execute_reply.started":"2023-12-21T20:03:08.71719Z","shell.execute_reply":"2023-12-21T20:03:08.735103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = DataGenerator(path_input + 'train_soundscapes/', list_IDs_train, train_labels, batch_size)\nval_generator = DataGenerator(path_input + 'train_soundscapes/', list_IDs_val, train_labels, batch_size)","metadata":{"execution":{"iopub.status.busy":"2023-12-21T20:03:08.737815Z","iopub.execute_input":"2023-12-21T20:03:08.738112Z","iopub.status.idle":"2023-12-21T20:03:08.754978Z","shell.execute_reply.started":"2023-12-21T20:03:08.738085Z","shell.execute_reply":"2023-12-21T20:03:08.753333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_generator = DataGenerator(path_input + 'test_soundscapes/', list_IDs_val, train_labels, batch_size)","metadata":{"execution":{"iopub.status.busy":"2023-12-21T20:03:08.756528Z","iopub.execute_input":"2023-12-21T20:03:08.756908Z","iopub.status.idle":"2023-12-21T20:03:08.766032Z","shell.execute_reply.started":"2023-12-21T20:03:08.75688Z","shell.execute_reply":"2023-12-21T20:03:08.764913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# X, y = val_generator.data_generate(list_IDs_val)\n# ","metadata":{"execution":{"iopub.status.busy":"2023-12-21T20:03:08.767556Z","iopub.execute_input":"2023-12-21T20:03:08.768859Z","iopub.status.idle":"2023-12-21T20:03:08.776985Z","shell.execute_reply.started":"2023-12-21T20:03:08.768821Z","shell.execute_reply":"2023-12-21T20:03:08.775428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 2\nlernrate = 1e-3","metadata":{"execution":{"iopub.status.busy":"2023-12-21T20:03:08.778101Z","iopub.execute_input":"2023-12-21T20:03:08.778437Z","iopub.status.idle":"2023-12-21T20:03:08.788411Z","shell.execute_reply.started":"2023-12-21T20:03:08.778411Z","shell.execute_reply":"2023-12-21T20:03:08.787278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import LSTM, Dense, BatchNormalization, Flatten","metadata":{"execution":{"iopub.status.busy":"2023-12-21T20:04:00.108228Z","iopub.execute_input":"2023-12-21T20:04:00.108621Z","iopub.status.idle":"2023-12-21T20:04:00.113545Z","shell.execute_reply.started":"2023-12-21T20:04:00.108586Z","shell.execute_reply":"2023-12-21T20:04:00.11251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(LSTM(128, input_shape=(100, 1600//2), return_sequences=True))\nmodel.add(BatchNormalization())\nmodel.add(LSTM(128, return_sequences=False))\nmodel.add(BatchNormalization())\nmodel.add(Flatten())\nmodel.add(Dense(512, activation='relu'))\nmodel.add(Dense(num_labels, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2023-12-21T20:04:03.237742Z","iopub.execute_input":"2023-12-21T20:04:03.238118Z","iopub.status.idle":"2023-12-21T20:04:03.839137Z","shell.execute_reply.started":"2023-12-21T20:04:03.238082Z","shell.execute_reply":"2023-12-21T20:04:03.837897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer = Adam(lr=lernrate),\n              loss='binary_crossentropy',\n              metrics=['binary_accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-12-21T20:04:11.74771Z","iopub.execute_input":"2023-12-21T20:04:11.74811Z","iopub.status.idle":"2023-12-21T20:04:11.767989Z","shell.execute_reply.started":"2023-12-21T20:04:11.748077Z","shell.execute_reply":"2023-12-21T20:04:11.76707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit_generator(generator=train_generator, validation_data=val_generator, epochs = epochs, workers=4)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-21T20:04:15.960027Z","iopub.execute_input":"2023-12-21T20:04:15.960393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}