{"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":"import os  #provides functions for interacting with the operating system\nimport json  #used to work with JSON data\nimport librosa #librosa is a python package for music and audio analysis\nimport numpy as np #NumPy is a Python library used for working with arrays\nimport pandas as pd #data analysis toolkit\nfrom sklearn.model_selection import train_test_split #Split arrays or matrices into random train and test subsets\nimport tensorflow as tf #TensorFlow is a Python library for fast numerical computing\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import Dense, GlobalAveragePooling2D\nfrom tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras import optimizers\nfrom tensorflow.keras.utils import plot_model, Sequence\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.applications import MobileNetV2\nfrom tensorflow.keras.layers import Input, Concatenate","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-05-09T10:50:49.437831Z","iopub.execute_input":"2022-05-09T10:50:49.438478Z","iopub.status.idle":"2022-05-09T10:50:56.220155Z","shell.execute_reply.started":"2022-05-09T10:50:49.438368Z","shell.execute_reply":"2022-05-09T10:50:56.219331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Trained Data Loading\ntrain_meta = pd.read_csv(\"/kaggle/input/birdclef-2022/train_metadata.csv\")\ntrain_meta#species","metadata":{"execution":{"iopub.status.busy":"2022-05-09T10:50:56.221684Z","iopub.execute_input":"2022-05-09T10:50:56.221919Z","iopub.status.idle":"2022-05-09T10:50:56.34744Z","shell.execute_reply.started":"2022-05-09T10:50:56.221875Z","shell.execute_reply":"2022-05-09T10:50:56.346681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Split audio into 5 seconds chunks","metadata":{}},{"cell_type":"code","source":"train_meta","metadata":{"execution":{"iopub.status.busy":"2022-05-09T10:50:56.348581Z","iopub.execute_input":"2022-05-09T10:50:56.349296Z","iopub.status.idle":"2022-05-09T10:50:56.372536Z","shell.execute_reply.started":"2022-05-09T10:50:56.349258Z","shell.execute_reply":"2022-05-09T10:50:56.371662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_meta2 = pd.read_csv(\"../input/xenocantoazcsv/train_extended_A-Z_ogg.csv\")\ntrain_meta2","metadata":{"execution":{"iopub.status.busy":"2022-05-09T10:50:56.374957Z","iopub.execute_input":"2022-05-09T10:50:56.375304Z","iopub.status.idle":"2022-05-09T10:50:56.776418Z","shell.execute_reply.started":"2022-05-09T10:50:56.375268Z","shell.execute_reply":"2022-05-09T10:50:56.77574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_meta2","metadata":{"execution":{"iopub.status.busy":"2022-05-09T10:50:56.777654Z","iopub.execute_input":"2022-05-09T10:50:56.778071Z","iopub.status.idle":"2022-05-09T10:50:56.821926Z","shell.execute_reply.started":"2022-05-09T10:50:56.778032Z","shell.execute_reply":"2022-05-09T10:50:56.821289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_meta2 = pd.read_csv(\"../input/xenocantoazcsv/train_extended_A-Z_ogg.csv\")\n\n\n#train_meta2 = train_meta2.rename(columns={\"species\": \"primary_label\"})\n\n#train_meta2 = train_meta2[['species','filename']]\n\n\ntrain_meta2 = train_meta2[['ebird_code', 'species', 'filename']]\ntrain_meta2\n\npathAll = []\nfor each, path in zip(train_meta2['ebird_code'].values, train_meta2['filename'].values):\n    if each[0] < \"n\":\n        temp = '../input/xenocantoamogg/A-M-ogg/' + each + '/' + path\n        pathAll.append(temp)\n    else:\n        temp = '../input/xenocantonzogg/N-Z-ogg/' + each + '/' + path\n        pathAll.append(temp)\ntrain_meta2['filename'] = pathAll\ntrain_meta2 = train_meta2[['species', 'filename']]\n                          \ntrain_meta2 = train_meta2.rename(columns={\"species\": \"primary_label\"})\n\n\n\n\ntrain_meta = pd.read_csv(\"/kaggle/input/birdclef-2022/train_metadata.csv\")\ntrain_meta = train_meta[['scientific_name', 'filename']]\ntrain_meta = train_meta.rename(columns={\"scientific_name\": \"primary_label\"})\n\n\nfilePath = '../input/birdclef-2022/train_audio/'\n\narr = []\nfor each in train_meta['filename'].values:\n    arr.append(filePath+each)\ntrain_meta['filename'] = arr\n\ntrain_meta #.head().iloc[1]\n\n\ntrain_meta['source'] = 0\ntrain_meta2['source'] = 1","metadata":{"execution":{"iopub.status.busy":"2022-05-09T10:50:56.823071Z","iopub.execute_input":"2022-05-09T10:50:56.824148Z","iopub.status.idle":"2022-05-09T10:50:57.137689Z","shell.execute_reply.started":"2022-05-09T10:50:56.824109Z","shell.execute_reply":"2022-05-09T10:50:57.136951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_df = pd.concat([train_meta, train_meta2])\ntrain_meta = data_df","metadata":{"execution":{"iopub.status.busy":"2022-05-09T10:50:57.139213Z","iopub.execute_input":"2022-05-09T10:50:57.139464Z","iopub.status.idle":"2022-05-09T10:50:57.145925Z","shell.execute_reply.started":"2022-05-09T10:50:57.13943Z","shell.execute_reply":"2022-05-09T10:50:57.145225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_meta.iloc[3]['filename']","metadata":{"execution":{"iopub.status.busy":"2022-05-09T10:50:57.147175Z","iopub.execute_input":"2022-05-09T10:50:57.147599Z","iopub.status.idle":"2022-05-09T10:50:57.15576Z","shell.execute_reply.started":"2022-05-09T10:50:57.147561Z","shell.execute_reply":"2022-05-09T10:50:57.154899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = list(train_meta['primary_label'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-05-09T10:50:57.157435Z","iopub.execute_input":"2022-05-09T10:50:57.157701Z","iopub.status.idle":"2022-05-09T10:50:57.1678Z","shell.execute_reply.started":"2022-05-09T10:50:57.157665Z","shell.execute_reply":"2022-05-09T10:50:57.167021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Function for Cutting Audio If There is any extra Noise.\nimport soundfile as sf\nimport os\ndef cutAudio(file_path, is_save):\n    # First load the file\n    filename = file_path.split('/')[-1]\n    file_path =  file_path\n    audio, sr = librosa.load(file_path)\n\n    # Get number of samples for 5 seconds; replace 5 by any number\n    buffer = 5 * sr\n\n    samples_total = len(audio)\n    samples_wrote = 0\n    counter = 1\n\n    audio_split = []\n    audio_filenames = []\n    while samples_wrote < samples_total:\n        #check if the buffer is not exceeding total samples \n        if buffer > (samples_total - samples_wrote):\n            buffer = samples_total - samples_wrote\n\n        block = audio[samples_wrote : (samples_wrote + buffer)]\n        audio_split.append(block)\n\n        # Write 5 second segment\n        if is_save == True:\n            out_filename = \"/kaggle/working/each5s/split_\" + str(counter) + \"_\" + filename\n            audio_filenames.append(out_filename)\n            sf.write(out_filename, block, sr)\n        counter += 1\n        samples_wrote += buffer\n    return audio_split, sr, audio_filenames","metadata":{"execution":{"iopub.status.busy":"2022-05-09T10:50:57.172816Z","iopub.execute_input":"2022-05-09T10:50:57.173034Z","iopub.status.idle":"2022-05-09T10:50:57.181654Z","shell.execute_reply.started":"2022-05-09T10:50:57.172992Z","shell.execute_reply":"2022-05-09T10:50:57.180949Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Function for splitting Trained audio\ndef splitTrainAudio(_df):\n    data = []\n    for index, row in _df.iterrows():\n        cutAudio(row[\"filename\"], True)\n        audio_lst, sr, filenames = cutAudio(row[\"filename\"], True)\n        for idx, y in enumerate(audio_lst):\n            data.append([row[\"primary_label\"], row[\"filename\"], filenames[idx]])\n\n    data_df = pd.DataFrame(data, columns=['primary_label', 'original_filename', 'filename'])\n    data_df.to_csv(\"/kaggle/working/data_df.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-05-09T10:50:57.183121Z","iopub.execute_input":"2022-05-09T10:50:57.183409Z","iopub.status.idle":"2022-05-09T10:50:57.193151Z","shell.execute_reply.started":"2022-05-09T10:50:57.183348Z","shell.execute_reply":"2022-05-09T10:50:57.192065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Input Sampleling Data to data_frame List\ndata_frames = []\nfor label in labels:\n    tmp_df = train_meta[train_meta[\"primary_label\"] == label].sample(n=1, replace=True).reset_index(drop=True)\n    data_frames.append(tmp_df)\nsample_df = pd.concat(data_frames).reset_index(drop=True)\nsample_df","metadata":{"execution":{"iopub.status.busy":"2022-05-09T10:50:57.19604Z","iopub.execute_input":"2022-05-09T10:50:57.196305Z","iopub.status.idle":"2022-05-09T10:50:59.45753Z","shell.execute_reply.started":"2022-05-09T10:50:57.19628Z","shell.execute_reply":"2022-05-09T10:50:59.456855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train_meta2['filename'][0].split('/')[-1]","metadata":{"execution":{"iopub.status.busy":"2022-05-09T10:50:59.458696Z","iopub.execute_input":"2022-05-09T10:50:59.458933Z","iopub.status.idle":"2022-05-09T10:50:59.462221Z","shell.execute_reply.started":"2022-05-09T10:50:59.458908Z","shell.execute_reply":"2022-05-09T10:50:59.461539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#creating a directory named path\n!mkdir -p \"/kaggle/working/each5s\"\nsplitTrainAudio(sample_df)","metadata":{"execution":{"iopub.status.busy":"2022-05-09T10:50:59.46348Z","iopub.execute_input":"2022-05-09T10:50:59.463952Z","iopub.status.idle":"2022-05-09T11:16:05.909652Z","shell.execute_reply.started":"2022-05-09T10:50:59.463917Z","shell.execute_reply":"2022-05-09T11:16:05.908808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_meta","metadata":{"execution":{"iopub.status.busy":"2022-05-09T11:16:05.911121Z","iopub.execute_input":"2022-05-09T11:16:05.91153Z","iopub.status.idle":"2022-05-09T11:16:05.927578Z","shell.execute_reply.started":"2022-05-09T11:16:05.911491Z","shell.execute_reply":"2022-05-09T11:16:05.926863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#After processing the data we store it in DataFrame\ndata_df = pd.read_csv(\"/kaggle/working/data_df.csv\")\ndata_df","metadata":{"execution":{"iopub.status.busy":"2022-05-09T11:16:05.92882Z","iopub.execute_input":"2022-05-09T11:16:05.929105Z","iopub.status.idle":"2022-05-09T11:16:05.95148Z","shell.execute_reply.started":"2022-05-09T11:16:05.929072Z","shell.execute_reply":"2022-05-09T11:16:05.950783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### The full data can be found [here](https://www.kaggle.com/duythanhng/birdclef-2022-audio-per-5-second)","metadata":{}},{"cell_type":"markdown","source":"## Training","metadata":{}},{"cell_type":"code","source":"#From this function we extarting features of Sample data/audio file\nnum_rows = 216\nnum_columns = 216\nnum_channels = 1\nn_mels = 512\n\ndef extractFeatures(y, sr):\n    feat = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=num_rows, n_mels=n_mels)\n    if feat.shape[1] <= num_columns:\n        pad_width = num_columns - feat.shape[1]\n        feat = np.pad(feat, pad_width=((0,0),(0,pad_width)), mode='constant')\n    return feat","metadata":{"execution":{"iopub.status.busy":"2022-05-09T11:16:05.952935Z","iopub.execute_input":"2022-05-09T11:16:05.953424Z","iopub.status.idle":"2022-05-09T11:16:05.960195Z","shell.execute_reply.started":"2022-05-09T11:16:05.953388Z","shell.execute_reply":"2022-05-09T11:16:05.959518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Here we are setting how many data sample/audio file will enter in each time in the network.#Data Managing\nclass DataGenerator(Sequence):\n    def __init__(self,\n                _X,\n                batch_size=32,\n                n_channels=1,\n                n_columns=470,\n                n_rows=120,\n                shuffle=True):\n        self.batch_size = batch_size\n        self.X = _X\n        self.n_channels = n_channels\n        self.n_columns = n_columns\n        self.n_rows = n_rows\n        self.shuffle = shuffle\n        self.img_indexes = np.arange(len(self.X))\n        self.on_epoch_end()\n        \n    def __len__(self):\n        'Denotes the number of batches per epoch'\n        return int(np.floor(len(self.img_indexes) / self.batch_size))\n\n    def __getitem__(self, index):\n        'Generate one batch of data'\n        # Generate indexes of the batch\n        indexes = self.indexes[index*self.batch_size:(index+1)*self.batch_size]\n        # Find list of IDs\n        list_IDs_temps = [self.img_indexes[k] for k in indexes]\n\n        # Generate data\n        X, y = self.__data_generation(list_IDs_temps)\n        return X, y\n     \n        \n    #Function for Updates indexes after each epoch\n    def on_epoch_end(self):\n        'Updates indexes after each epoch'\n        self.indexes = np.arange(len(self.X))\n        if self.shuffle == True:\n            np.random.shuffle(self.indexes)\n\n    def __data_generation(self, list_IDs_temps):\n        X = np.empty((self.batch_size, self.n_rows, self.n_columns))\n        y = np.empty((self.batch_size), dtype=int)\n        for i, ID in enumerate(list_IDs_temps):\n            file_path = self.X.iloc[ID][\"filename\"]\n            audio, sr = librosa.load(file_path)\n            feat = extractFeatures(audio, sr)\n            x_features = feat.tolist()\n            label = self.X.iloc[ID][\"target\"]\n            X[i] = np.array(x_features)\n            y[i] = label\n        X = X.reshape(X.shape[0], self.n_rows, self.n_columns, self.n_channels)\n        \n        return X, to_categorical(y, num_classes=len(labels))","metadata":{"execution":{"iopub.status.busy":"2022-05-09T11:16:05.963071Z","iopub.execute_input":"2022-05-09T11:16:05.963302Z","iopub.status.idle":"2022-05-09T11:16:05.978069Z","shell.execute_reply.started":"2022-05-09T11:16:05.963262Z","shell.execute_reply":"2022-05-09T11:16:05.976437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"params = dict(\n    batch_size=128,\n    n_rows=num_rows,\n    n_columns=num_columns,\n    n_channels=num_channels,\n)\nparams_train = dict(\n    shuffle=True,\n    **params\n)\nparams_valid = dict(\n    shuffle=False,\n    **params\n)","metadata":{"execution":{"iopub.status.busy":"2022-05-09T11:16:05.980295Z","iopub.execute_input":"2022-05-09T11:16:05.98109Z","iopub.status.idle":"2022-05-09T11:16:05.988766Z","shell.execute_reply.started":"2022-05-09T11:16:05.981051Z","shell.execute_reply":"2022-05-09T11:16:05.988064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Function for data visualization of audio sample\nimport matplotlib.pyplot as plt\n\ndef plot_his(history):\n    plt.figure(1, figsize = (15,8))\n    plt.subplot(221)\n    plt.plot(history.history['accuracy'])\n    plt.plot(history.history['val_accuracy'])\n    plt.title('model accuracy')\n    plt.ylabel('accuracy')\n    plt.xlabel('epoch')\n    plt.legend(['train', 'valid'])\n    plt.subplot(222)\n    plt.plot(history.history['loss'])\n    plt.plot(history.history['val_loss'])\n    plt.title('model loss')\n    plt.ylabel('loss')\n    plt.xlabel('epoch')\n    plt.legend(['train', 'valid'])\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-09T11:16:05.991608Z","iopub.execute_input":"2022-05-09T11:16:05.992147Z","iopub.status.idle":"2022-05-09T11:16:06.000041Z","shell.execute_reply.started":"2022-05-09T11:16:05.99211Z","shell.execute_reply":"2022-05-09T11:16:05.999224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Function for creating Convolutional neural network\ndef create_cnn():\n    img_input = Input(shape=(num_rows, num_columns, num_channels))\n    img_conc = Concatenate()([img_input, img_input, img_input])\n    base_model = MobileNetV2(\n        include_top=False,\n        weights='/kaggle/input/keras-pretrain-model-weights/mobilenet_v2_weights_tf_dim_ordering_tf_kernels_1.0_224_no_top.h5',\n        input_tensor=img_conc\n    )\n    # base_model.trainable = False\n    avgpool = GlobalAveragePooling2D()(base_model.output)\n    outputs = Dense(len(labels), activation='softmax')(avgpool)\n\n    model = Model(inputs=base_model.input, outputs=outputs)\n    return model","metadata":{"execution":{"iopub.status.busy":"2022-05-09T11:16:06.001282Z","iopub.execute_input":"2022-05-09T11:16:06.001645Z","iopub.status.idle":"2022-05-09T11:16:06.010755Z","shell.execute_reply.started":"2022-05-09T11:16:06.001596Z","shell.execute_reply":"2022-05-09T11:16:06.010019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_df['target'] = data_df['primary_label'].apply(lambda x: labels.index(x))\ndata_df","metadata":{"execution":{"iopub.status.busy":"2022-05-09T11:16:06.012083Z","iopub.execute_input":"2022-05-09T11:16:06.012451Z","iopub.status.idle":"2022-05-09T11:16:06.046445Z","shell.execute_reply.started":"2022-05-09T11:16:06.012417Z","shell.execute_reply":"2022-05-09T11:16:06.045724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Function for Input Data Training  for Model\ndef train_model(model, train_gen, val_gen):\n    checkpoint_model_path = \"/kaggle/working/mobilnetv2.h5\"\n    metric = \"val_accuracy\"\n    model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001), loss='categorical_crossentropy', metrics=[\"accuracy\"])\n    num_epochs = 50\n    checkpointer = ModelCheckpoint(\n        filepath=checkpoint_model_path,\n        monitor=metric, verbose=1, save_best_only=True)\n    es_callback = EarlyStopping(monitor=metric, patience=5, verbose=1)\n    reduce_lr = ReduceLROnPlateau(monitor=metric, factor=0.3, patience=1, verbose=1, min_delta=0.0001, cooldown=1, min_lr=0.00001)\n\n    history = model.fit(\n        train_gen,\n        epochs=num_epochs,\n        validation_data=val_gen,\n        callbacks=[checkpointer,es_callback,reduce_lr],\n        verbose=1\n    )\n\n    plot_his(history)","metadata":{"execution":{"iopub.status.busy":"2022-05-09T11:16:06.047767Z","iopub.execute_input":"2022-05-09T11:16:06.048149Z","iopub.status.idle":"2022-05-09T11:16:06.055867Z","shell.execute_reply.started":"2022-05-09T11:16:06.048116Z","shell.execute_reply":"2022-05-09T11:16:06.055211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Function Calling\nX_train, X_valid, _, _ = train_test_split(data_df, data_df[\"target\"], test_size=0.1, random_state=42)\ntrain_generator = DataGenerator(X_train, **params_train)\nvalid_generator = DataGenerator(X_valid, **params_valid)\ncnn_model = create_cnn()\ntrain_model(cnn_model, train_generator, valid_generator)","metadata":{"execution":{"iopub.status.busy":"2022-05-09T11:16:06.058271Z","iopub.execute_input":"2022-05-09T11:16:06.059045Z","iopub.status.idle":"2022-05-09T11:48:30.482502Z","shell.execute_reply.started":"2022-05-09T11:16:06.058991Z","shell.execute_reply":"2022-05-09T11:48:30.481851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Submission","metadata":{}},{"cell_type":"code","source":"#CNN Function Calling\ncnn_model = create_cnn()\n# cnn_model.load_weights(\"/kaggle/input/birdclef-2022-keras-model/mobilenetv20_0.8545.h5\")\ncnn_model.load_weights(\"/kaggle/working/mobilnetv2.h5\")","metadata":{"execution":{"iopub.status.busy":"2022-05-09T11:48:30.483925Z","iopub.execute_input":"2022-05-09T11:48:30.48439Z","iopub.status.idle":"2022-05-09T11:48:31.892684Z","shell.execute_reply.started":"2022-05-09T11:48:30.484353Z","shell.execute_reply":"2022-05-09T11:48:31.89194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#converting an entire data table into a NumPy matrix array\n# data_df = pd.read_csv(\"/kaggle/input/birdclef-2022-keras-model/data_df.csv\")\ndata_df = pd.read_csv(\"/kaggle/working/data_df.csv\")\nlabels = list(data_df['primary_label'].unique())","metadata":{"execution":{"iopub.status.busy":"2022-05-09T11:48:31.894099Z","iopub.execute_input":"2022-05-09T11:48:31.894339Z","iopub.status.idle":"2022-05-09T11:48:31.908948Z","shell.execute_reply.started":"2022-05-09T11:48:31.894306Z","shell.execute_reply":"2022-05-09T11:48:31.908346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_path = \"/kaggle/input/birdclef-2022/test_soundscapes/\"\nfiles = [f.split('.')[0] for f in sorted(os.listdir(test_path))]\n\nbirds_path = \"/kaggle/input/birdclef-2022/scored_birds.json\"\nwith open(birds_path) as bf:\n    birds = json.load(bf)\n\ndata = []\nfor f in files:\n    file_path = test_path + f + '.ogg'\n    audio, sr = librosa.load(file_path)\n    # Get number of samples for 5 seconds; replace 5 by any number\n    buffer = 5 * sr\n    samples_total = len(audio)\n    samples_wrote = 0\n    counter = 1\n\n    while samples_wrote < samples_total:\n        #check if the buffer is not exceeding total samples \n        if buffer > (samples_total - samples_wrote):\n            buffer = samples_total - samples_wrote\n\n        block = audio[samples_wrote : (samples_wrote + buffer)]\n        feat = extractFeatures(block, sr)\n        x = feat.reshape(1, num_rows, num_columns, num_channels)\n        pred = cnn_model.predict(x)\n        label_index = np.argmax(pred,axis=1)[0]\n        \n        for b in birds:\n            segment_end = counter * 5   \n            row_id = f + '_' + b + '_' + str(segment_end)\n            target = False\n            if labels[label_index] == b:\n                target = True\n            data.append([row_id, target])\n        counter += 1\n        samples_wrote += buffer\n        \nsubmission_df = pd.DataFrame(data, columns=['row_id', 'target'])\nsubmission_df","metadata":{"execution":{"iopub.status.busy":"2022-05-09T11:48:31.910103Z","iopub.execute_input":"2022-05-09T11:48:31.910422Z","iopub.status.idle":"2022-05-09T11:48:35.533706Z","shell.execute_reply.started":"2022-05-09T11:48:31.910381Z","shell.execute_reply":"2022-05-09T11:48:35.532716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.to_csv(\"/kaggle/working/submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-05-09T11:48:35.538067Z","iopub.execute_input":"2022-05-09T11:48:35.538434Z","iopub.status.idle":"2022-05-09T11:48:35.550891Z","shell.execute_reply.started":"2022-05-09T11:48:35.538393Z","shell.execute_reply":"2022-05-09T11:48:35.54987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hello","metadata":{"execution":{"iopub.status.busy":"2022-05-09T11:48:35.556702Z","iopub.execute_input":"2022-05-09T11:48:35.55932Z","iopub.status.idle":"2022-05-09T11:48:35.909761Z","shell.execute_reply.started":"2022-05-09T11:48:35.55927Z","shell.execute_reply":"2022-05-09T11:48:35.908886Z"},"trusted":true},"execution_count":null,"outputs":[]}]}