{"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":"markdown","source":"# Install and dependencies","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5"}},{"cell_type":"code","source":"import os\nimport pandas as pd\nfrom matplotlib import pyplot as plt\nimport tensorflow as tf \nimport tensorflow_addons as tfa\nimport librosa\nimport numpy as np\nimport math\nimport json","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.347836Z","iopub.status.idle":"2022-05-22T07:21:01.348477Z","shell.execute_reply.started":"2022-05-22T07:21:01.348248Z","shell.execute_reply":"2022-05-22T07:21:01.348273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load data","metadata":{}},{"cell_type":"code","source":"root_folder = \"../input/birdclef2022fftdataset/clean_ffts\"\n\nbird_types = os.listdir(root_folder)\nbird_types_df = []\nfile_paths = []\n\nfor bird_type in bird_types:\n    bird_folder = os.path.join(root_folder, bird_type)\n    for file in os.listdir(bird_folder):\n        file_path = os.path.join(root_folder, bird_type, file)\n        file_paths.append(file_path)\n        bird_types_df.append(bird_type)","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.349651Z","iopub.status.idle":"2022-05-22T07:21:01.350281Z","shell.execute_reply.started":"2022-05-22T07:21:01.350020Z","shell.execute_reply":"2022-05-22T07:21:01.350044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame(\n    {\"file_path\" : file_paths,\n    \"bird_type\": bird_types_df\n})\ndf","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.351462Z","iopub.status.idle":"2022-05-22T07:21:01.352078Z","shell.execute_reply.started":"2022-05-22T07:21:01.351851Z","shell.execute_reply":"2022-05-22T07:21:01.351875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_stat = df.groupby(\"bird_type\").count()\ndf_stat = df_stat.reset_index()\ndf_stat = df_stat.rename(columns={\"file_path\" : \"count\"})\ndf_stat","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.353353Z","iopub.status.idle":"2022-05-22T07:21:01.354030Z","shell.execute_reply.started":"2022-05-22T07:21:01.353795Z","shell.execute_reply":"2022-05-22T07:21:01.353820Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_stat[df_stat[\"bird_type\"]==\"nocall\"]\n","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.355194Z","iopub.status.idle":"2022-05-22T07:21:01.355830Z","shell.execute_reply.started":"2022-05-22T07:21:01.355592Z","shell.execute_reply":"2022-05-22T07:21:01.355628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_nocall = df[df[\"bird_type\"] == \"nocall\"][0:9433]\ndf_nocall = df_nocall.reset_index(drop=True)\ndf_nocall","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.357006Z","iopub.status.idle":"2022-05-22T07:21:01.357646Z","shell.execute_reply.started":"2022-05-22T07:21:01.357409Z","shell.execute_reply":"2022-05-22T07:21:01.357433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_only_birds = df[df[\"bird_type\"] != \"nocall\"]\ndf_only_birds = df_only_birds.reset_index(drop=True)\ndf_only_birds","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.358828Z","iopub.status.idle":"2022-05-22T07:21:01.359432Z","shell.execute_reply.started":"2022-05-22T07:21:01.359193Z","shell.execute_reply":"2022-05-22T07:21:01.359217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.concat([df_nocall, df_only_birds])\ndf = df.sample(frac=1).reset_index(drop=True)\ndf","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.360636Z","iopub.status.idle":"2022-05-22T07:21:01.361264Z","shell.execute_reply.started":"2022-05-22T07:21:01.361021Z","shell.execute_reply":"2022-05-22T07:21:01.361045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(df)","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.362638Z","iopub.status.idle":"2022-05-22T07:21:01.363294Z","shell.execute_reply.started":"2022-05-22T07:21:01.363052Z","shell.execute_reply":"2022-05-22T07:21:01.363076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = df[0:90000]\ndf_test = df[90000:]\ndf_test = df_test.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.364488Z","iopub.status.idle":"2022-05-22T07:21:01.365111Z","shell.execute_reply.started":"2022-05-22T07:21:01.364883Z","shell.execute_reply":"2022-05-22T07:21:01.364907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_stat = df_train.groupby(\"bird_type\").count().reset_index()\ndf_stat = df_stat.rename(columns={\"file_path\": \"n_files\"})\ndf_stat","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.366299Z","iopub.status.idle":"2022-05-22T07:21:01.366935Z","shell.execute_reply.started":"2022-05-22T07:21:01.366696Z","shell.execute_reply":"2022-05-22T07:21:01.366720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndf_stat = df_train.groupby(\"bird_type\").count()\ndf_stat = df_stat.reset_index()\ndf_stat = df_stat.rename(columns={\"file_path\" : \"count\"})\ndf_stat","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.368121Z","iopub.status.idle":"2022-05-22T07:21:01.368788Z","shell.execute_reply.started":"2022-05-22T07:21:01.368547Z","shell.execute_reply":"2022-05-22T07:21:01.368570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Oversample the train dataset","metadata":{}},{"cell_type":"code","source":"target_duration = 9433\ndf_train_oversampled = df_train.copy(deep=True)\nbird_types = list(df_stat[\"bird_type\"])\nfor i, bird_type in enumerate(bird_types):\n    # print(f\"Appending bird type {bird_type} ... {i}/152\")\n    # Get the data frame with the specific bird_type\n    df_bird = df_train[df_train[\"bird_type\"]==bird_type]\n    songs = list(df_bird[\"file_path\"]) * 15000\n    # Get the current total duration in seconds\n    n_files = int(df_stat[df_stat[\"bird_type\"]==bird_type][\"count\"])\n    \n    take_n_files = target_duration - n_files\n    \n    song_appended = songs[:take_n_files]\n    bird_type_appended = [bird_type] * take_n_files\n        \n    df_tmp = pd.DataFrame({\"bird_type\": bird_type_appended,\n                           \"file_path\": song_appended})\n    df_train_oversampled = pd.concat([df_train_oversampled, df_tmp])\ndf_train_oversampled","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.369969Z","iopub.status.idle":"2022-05-22T07:21:01.370594Z","shell.execute_reply.started":"2022-05-22T07:21:01.370367Z","shell.execute_reply":"2022-05-22T07:21:01.370390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_oversampled = df_train_oversampled.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.371755Z","iopub.status.idle":"2022-05-22T07:21:01.372365Z","shell.execute_reply.started":"2022-05-22T07:21:01.372125Z","shell.execute_reply":"2022-05-22T07:21:01.372149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ndf_stat = df_train_oversampled.groupby(\"bird_type\").count()\ndf_stat = df_stat.reset_index()\ndf_stat = df_stat.rename(columns={\"file_path\" : \"count\"})\ndf_stat","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.373519Z","iopub.status.idle":"2022-05-22T07:21:01.374137Z","shell.execute_reply.started":"2022-05-22T07:21:01.373911Z","shell.execute_reply":"2022-05-22T07:21:01.373936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_oversampled = df_train_oversampled.sample(frac=1)\ndf_train_oversampled = df_train_oversampled.reset_index(drop=True)\ndf_train_oversampled","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.375353Z","iopub.status.idle":"2022-05-22T07:21:01.375963Z","shell.execute_reply.started":"2022-05-22T07:21:01.375734Z","shell.execute_reply":"2022-05-22T07:21:01.375758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Asign index to each bird class","metadata":{}},{"cell_type":"code","source":"bird_types = set(list(df[\"bird_type\"])) \nbird_type_dict = dict(zip(bird_types, np.arange(len(bird_types))))\ndf_train_oversampled[\"bird_type_index\"] = df_train_oversampled.apply(lambda x: bird_type_dict[x[\"bird_type\"]], axis=1)\ndf_test[\"bird_type_index\"] = df_test.apply(lambda x: bird_type_dict[x[\"bird_type\"]], axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.377127Z","iopub.status.idle":"2022-05-22T07:21:01.377765Z","shell.execute_reply.started":"2022-05-22T07:21:01.377522Z","shell.execute_reply":"2022-05-22T07:21:01.377545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Build Dataloading Function","metadata":{}},{"cell_type":"code","source":"def preprocess(file_path, label): \n    npy = np.load(file_path.decode('utf-8'))\n    spectrogram = tf.convert_to_tensor(npy)\n    spectrogram = tf.expand_dims(spectrogram, axis=1)\n    #spectrogram = tf.io.read_file(file_path)\n    # label = tf.one_hot(label, depth=152)\n    label = tf.convert_to_tensor(label, dtype=tf.int32)\n    return spectrogram, label","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.378924Z","iopub.status.idle":"2022-05-22T07:21:01.379585Z","shell.execute_reply.started":"2022-05-22T07:21:01.379341Z","shell.execute_reply":"2022-05-22T07:21:01.379367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfiles = list(df_train_oversampled[\"file_path\"])\nlabels = list(df_train_oversampled[\"bird_type_index\"])\ntrain = tf.data.Dataset.from_tensor_slices((files, labels))\n\ntrain = train.map(lambda item1, item2: tf.numpy_function(\n        preprocess, [item1, item2], [tf.float64, tf.int32]),\n        num_parallel_calls=tf.data.AUTOTUNE)\n\n# data_timesum = data_timesum.cache()\ntrain = train.shuffle(buffer_size=1000)\ntrain = train.batch(16)\ntrain = train.prefetch(8)\n\n# data_timesum.as_numpy_iterator().next() \n","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.380784Z","iopub.status.idle":"2022-05-22T07:21:01.381392Z","shell.execute_reply.started":"2022-05-22T07:21:01.381151Z","shell.execute_reply":"2022-05-22T07:21:01.381174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfiles = list(df_test[\"file_path\"])\nlabels = list(df_test[\"bird_type_index\"])\ntest = tf.data.Dataset.from_tensor_slices((files, labels))\n\ntest = test.map(lambda item1, item2: tf.numpy_function(\n        preprocess, [item1, item2], [tf.float64, tf.int32]),\n        num_parallel_calls=tf.data.AUTOTUNE)\n\n# data_timesum = data_timesum.cache()\ntest = test.shuffle(buffer_size=1000)\ntest = test.batch(16)\ntest = test.prefetch(8).repeat()\n\n# data_timesum.as_numpy_iterator().next() \n","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.382727Z","iopub.status.idle":"2022-05-22T07:21:01.383389Z","shell.execute_reply.started":"2022-05-22T07:21:01.383149Z","shell.execute_reply":"2022-05-22T07:21:01.383173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"s, l = train.as_numpy_iterator().next()\ns.shape, l.shape","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.384546Z","iopub.status.idle":"2022-05-22T07:21:01.385180Z","shell.execute_reply.started":"2022-05-22T07:21:01.384952Z","shell.execute_reply":"2022-05-22T07:21:01.384975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"s, l = test.as_numpy_iterator().next()\ns.shape, l.shape","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.386338Z","iopub.status.idle":"2022-05-22T07:21:01.386966Z","shell.execute_reply.started":"2022-05-22T07:21:01.386728Z","shell.execute_reply":"2022-05-22T07:21:01.386752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Build Deep Learning Model","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv1D, Conv2D, Dense, Flatten, MaxPool1D, MaxPool2D, Dropout, BatchNormalization, LSTM, GlobalAveragePooling1D, Dropout, SpatialDropout2D, AveragePooling1D","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:28:31.200141Z","iopub.execute_input":"2022-05-22T07:28:31.200695Z","iopub.status.idle":"2022-05-22T07:28:31.225601Z","shell.execute_reply.started":"2022-05-22T07:28:31.200657Z","shell.execute_reply":"2022-05-22T07:28:31.224280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.load(df_train_oversampled[\"file_path\"][0]).shape","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.389930Z","iopub.status.idle":"2022-05-22T07:21:01.390543Z","shell.execute_reply.started":"2022-05-22T07:21:01.390311Z","shell.execute_reply":"2022-05-22T07:21:01.390335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(set(list(df_train_oversampled[\"bird_type_index\"])))","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.391727Z","iopub.status.idle":"2022-05-22T07:21:01.392371Z","shell.execute_reply.started":"2022-05-22T07:21:01.392127Z","shell.execute_reply":"2022-05-22T07:21:01.392152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential([\n    \n    Conv1D(filters=32, kernel_size=20,strides=1, padding=\"same\",activation=\"relu\",input_shape=[397, 1]),\n    BatchNormalization(),\n    \n    Conv1D(filters=32, kernel_size=20, strides=1, padding=\"valid\", activation=\"relu\", kernel_regularizer='l2'),\n    BatchNormalization(),\n    Dropout(0.2),\n    \n    Conv1D(filters=32, kernel_size=6, strides=1, padding=\"valid\", activation=\"relu\", kernel_regularizer='l2'),\n    AveragePooling1D(),\n    \n    Conv1D(filters=32, kernel_size=6, strides=1, padding=\"valid\", activation=\"relu\", kernel_regularizer='l2'),\n    Dropout(0.2),\n    \n    \n    \n    Flatten(),\n    Dense(1024, activation='relu', kernel_regularizer='l2'), # You have 152 bird types, check this with len(bird_types)\n    Dropout(0.3),\n    Dense(512, activation='relu', kernel_regularizer='l2'), # You have 152 bird types, check this with len(bird_types)\n    Dropout(0.3),\n    Dense(256, activation='relu', kernel_regularizer='l2'), # You have 152 bird types, check this with len(bird_types)\n    #Dense(1024, activation='relu'), # You have 152 bird types, check this with len(bird_types)\n    Dense(153, activation='softmax', kernel_regularizer='l2'), # You have 152 bird types, check this with len(bird_types)\n])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:29:14.649869Z","iopub.execute_input":"2022-05-22T07:29:14.650117Z","iopub.status.idle":"2022-05-22T07:29:14.769265Z","shell.execute_reply.started":"2022-05-22T07:29:14.650088Z","shell.execute_reply":"2022-05-22T07:29:14.768252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = tf.ones((16, 397, 1))\ny=model(x)\ny.shape","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.491086Z","iopub.status.idle":"2022-05-22T07:21:01.491866Z","shell.execute_reply.started":"2022-05-22T07:21:01.491598Z","shell.execute_reply":"2022-05-22T07:21:01.491637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer=\"Adam\", \n              loss= \"sparse_categorical_crossentropy\",\n              metrics=[\"sparse_categorical_accuracy\" ]\n              )\nmodel.evaluate(test, steps=64)","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.493138Z","iopub.status.idle":"2022-05-22T07:21:01.493757Z","shell.execute_reply.started":"2022-05-22T07:21:01.493520Z","shell.execute_reply":"2022-05-22T07:21:01.493544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Learning Rate scheduler","metadata":{}},{"cell_type":"code","source":"class DetectHighAccuracyCallback(tf.keras.callbacks.Callback):\n    def __init__(self, threshold=0.98):\n        super(DetectHighAccuracyCallback, self).__init__()\n        self.threshold = threshold\n\n    def on_epoch_end(self, epoch, logs=None):\n        if logs[\"val_sparse_categorical_accuracy\"] > self.threshold:\n            print(f\"Stopping training, reached {self.threshold} accuracy. \")\n            self.model.stop_training = True","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.494981Z","iopub.status.idle":"2022-05-22T07:21:01.495625Z","shell.execute_reply.started":"2022-05-22T07:21:01.495389Z","shell.execute_reply":"2022-05-22T07:21:01.495412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir_ckpt = \"./training_version_18\"\nif os.path.exists(train_dir_ckpt) == False:\n    os.mkdir(train_dir_ckpt)","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.496766Z","iopub.status.idle":"2022-05-22T07:21:01.497377Z","shell.execute_reply.started":"2022-05-22T07:21:01.497133Z","shell.execute_reply":"2022-05-22T07:21:01.497157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"checkpoint_path = f\"{train_dir_ckpt}/cp.ckpt\"\ncheckpoint_dir = os.path.dirname(checkpoint_path)\n\n# Create a callback that saves the model's weights\ncp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_path,\n                                                 save_weights_only=True,\n                                                 verbose=1)\n#https://www.tensorflow.org/tutorials/keras/save_and_load\nlr_schedule = tf.keras.callbacks.LearningRateScheduler(\n    lambda epoch: 1e-5 if epoch < 5 else 1.E-04)\n\nhist = model.fit(train, \n                 epochs=200, \n                 validation_data=test, \n                 steps_per_epoch=512,\n                 validation_steps=64,\n#                 callbacks=[lr_schedule, \n#                           DetectHighAccuracyCallback(),\n#                           cp_callback\n#                           ],\n                 verbose=1)","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.498543Z","iopub.status.idle":"2022-05-22T07:21:01.499142Z","shell.execute_reply.started":"2022-05-22T07:21:01.498916Z","shell.execute_reply":"2022-05-22T07:21:01.498939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.clf()\nplt.plot(hist.history[\"loss\"], label=\"train\")\nplt.plot(hist.history[\"val_loss\"], label=\"validation\")\nplt.savefig(f\"./{train_dir_ckpt}/loss.png\")\nplt.savefig(f\"./{train_dir_ckpt}/loss.pdf\")","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.500326Z","iopub.status.idle":"2022-05-22T07:21:01.500972Z","shell.execute_reply.started":"2022-05-22T07:21:01.500731Z","shell.execute_reply":"2022-05-22T07:21:01.500756Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.clf()\nplt.plot(hist.history[\"sparse_categorical_accuracy\"], label=\"test\")\nplt.plot(hist.history[\"val_sparse_categorical_accuracy\"], label=\"validation\")\nplt.savefig(f\"./{train_dir_ckpt}/sparse_categorical_accuracy.png\")\nplt.savefig(f\"./{train_dir_ckpt}/sparse_categorical_accuracy.pdf\")\n","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.502298Z","iopub.status.idle":"2022-05-22T07:21:01.502962Z","shell.execute_reply.started":"2022-05-22T07:21:01.502709Z","shell.execute_reply":"2022-05-22T07:21:01.502735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# hist.history","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.504094Z","iopub.status.idle":"2022-05-22T07:21:01.504713Z","shell.execute_reply.started":"2022-05-22T07:21:01.504472Z","shell.execute_reply":"2022-05-22T07:21:01.504496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"max(hist.history[\"val_sparse_categorical_accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.506036Z","iopub.status.idle":"2022-05-22T07:21:01.506686Z","shell.execute_reply.started":"2022-05-22T07:21:01.506445Z","shell.execute_reply":"2022-05-22T07:21:01.506471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submit the solution","metadata":{}},{"cell_type":"code","source":"bird_type_dict_inverse = {v: k for k, v in bird_type_dict.items()}\n# bird_type_dict_inverse","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.507823Z","iopub.status.idle":"2022-05-22T07:21:01.508421Z","shell.execute_reply.started":"2022-05-22T07:21:01.508178Z","shell.execute_reply":"2022-05-22T07:21:01.508201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# \"aniani\" in list(bird_type_dict_inverse.values())","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.509561Z","iopub.status.idle":"2022-05-22T07:21:01.510211Z","shell.execute_reply.started":"2022-05-22T07:21:01.509951Z","shell.execute_reply":"2022-05-22T07:21:01.509975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def moving_average(x, w):\n    arr =  np.convolve(x, np.ones(w), 'valid') / w\n    return arr / np.max(arr)\n\ndef load_spectrogram(file_input, offset):\n    wav, sample_rate = librosa.load(file_input, duration=5, sr=16000, offset=0 )\n    wav = wav - np.average(wav)\n    spectrogram = np.abs(np.fft.fftn(wav))\n    spectrogram *= 1. / np.max(spectrogram)\n    spectrogram = spectrogram[0:int(len(spectrogram)/2)]\n    spectrogram = moving_average(spectrogram, 400)\n    x_shape = np.shape(spectrogram)[0]\n    x = np.arange(1, x_shape+1)\n    spectrogram = spectrogram * np.exp(-(2000 / x)**5)\n    spectrogram = spectrogram[::100]\n    return spectrogram\n    \n\ndef preprocess(file_input, offset):\n    spectrogram = load_spectrogram(file_input, offset)\n    spectrogram = np.expand_dims(spectrogram, axis=1)\n    return spectrogram\n    \n\n\n\ndef predict_label(model, file_id, offset):\n    #file_input = os.path.join(\"../input/birdclef-2022/test_soundscapes/\", f\"{file_id}\")\n    file_input = file_id\n    x = preprocess(file_input, offset)\n    x = tf.expand_dims(x, axis=0)\n    prediction = model(x)\n    prediction_label = tf.argmax(prediction, axis=1).numpy()[0]\n    return prediction_label\n\ndef predict_label_npy(model, file_id, offset):\n    #file_input = os.path.join(\"../input/birdclef-2022/test_soundscapes/\", f\"{file_id}\")\n    x = np.load(file_id)\n    x = tf.expand_dims(x, axis=0)\n    x = tf.expand_dims(x, axis=2)\n    prediction = model(x)\n    prediction_label = tf.argmax(prediction, axis=1).numpy()[0]\n    predicted_bird = bird_type_dict_inverse[prediction_label]\n    return predicted_bird\n    \ndef predict(model, file_id, offset):\n    prediction_label = predict_label(model, file_id, offset)\n    predicted_bird = bird_type_dict_inverse[prediction_label]\n    return predicted_bird\n    ","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.511562Z","iopub.status.idle":"2022-05-22T07:21:01.512202Z","shell.execute_reply.started":"2022-05-22T07:21:01.511976Z","shell.execute_reply":"2022-05-22T07:21:01.512000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train[\"file_path\"][2]","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.513372Z","iopub.status.idle":"2022-05-22T07:21:01.513970Z","shell.execute_reply.started":"2022-05-22T07:21:01.513742Z","shell.execute_reply":"2022-05-22T07:21:01.513766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file_input = \"../input/birdclef-2022/train_audio/arcter/XC432747.ogg\"\nx = preprocess(file_input, offset=0)\nx = tf.expand_dims(x, axis=0)\nprediction = predict(model, file_id=file_input, offset=9*5)\nprediction","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.515108Z","iopub.status.idle":"2022-05-22T07:21:01.515718Z","shell.execute_reply.started":"2022-05-22T07:21:01.515481Z","shell.execute_reply":"2022-05-22T07:21:01.515504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file_input = \"../input/birdclef-2022/train_audio/bcnher/XC636465.ogg\"\nx = preprocess(file_input, offset=0)\nx = tf.expand_dims(x, axis=0)\nprediction = predict(model, file_id=file_input, offset=3*5)\nprediction","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.516870Z","iopub.status.idle":"2022-05-22T07:21:01.517473Z","shell.execute_reply.started":"2022-05-22T07:21:01.517253Z","shell.execute_reply":"2022-05-22T07:21:01.517276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.518602Z","iopub.status.idle":"2022-05-22T07:21:01.519191Z","shell.execute_reply.started":"2022-05-22T07:21:01.518965Z","shell.execute_reply":"2022-05-22T07:21:01.518988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_true = list(df_test[\"bird_type\"])[0:100]\nfiles = list(df_test[\"file_path\"])[0:100]\ny_pred = [predict_label_npy(model, file, offset=0) for file in files]","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.520354Z","iopub.status.idle":"2022-05-22T07:21:01.520951Z","shell.execute_reply.started":"2022-05-22T07:21:01.520724Z","shell.execute_reply":"2022-05-22T07:21:01.520748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sklearn\nf1_score = sklearn.metrics.f1_score(y_true, y_pred, average=\"macro\")\nf1_score","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.522219Z","iopub.status.idle":"2022-05-22T07:21:01.522904Z","shell.execute_reply.started":"2022-05-22T07:21:01.522644Z","shell.execute_reply":"2022-05-22T07:21:01.522671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\n#Get the confusion matrix\ncf_matrix = confusion_matrix(y_true, y_pred)","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.524046Z","iopub.status.idle":"2022-05-22T07:21:01.524659Z","shell.execute_reply.started":"2022-05-22T07:21:01.524419Z","shell.execute_reply":"2022-05-22T07:21:01.524442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt\n\nplt.figure(figsize=(16,16))\n\n\nax = sns.heatmap(cf_matrix, annot=True, cmap='Blues')\n\nax.set_xlabel('\\nPredicted')\nax.set_ylabel('Actual ');\n\n## Ticket labels - List must be in alphabetical order\n#ax.xaxis.set_ticklabels(bird_names, rotation=45)\n#ax.yaxis.set_ticklabels(bird_names, rotation=45)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.525827Z","iopub.status.idle":"2022-05-22T07:21:01.526434Z","shell.execute_reply.started":"2022-05-22T07:21:01.526199Z","shell.execute_reply":"2022-05-22T07:21:01.526223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_dir=\"/kaggle/input/birdclef-2022\"\n\npred={\n  'row_id':[],\n  'target':[]\n}\n\ntest_path=input_dir+\"/test_soundscapes/\"\n# files= list(df_test[\"file_id\"]) # [f.split('.')[0] for f in sorted(os.listdir(test_path))]\nfiles= [f.split('.')[0] for f in sorted(os.listdir(test_path))]\nprint(files)\n\nbirds_path=input_dir+\"/scored_birds.json\"\nwith open(birds_path) as bf:\n    birds = json.load(bf)\n\nfor f in files:\n    p=test_path+f+'.ogg'\n    p = os.path.join(\"../input/birdclef-2022/test_soundscapes/\", f\"{p}\")\n    \n    d=librosa.get_duration(filename=p)\n    pcs=round(d/5)\n    segments = [[] for i in range(pcs)]\n      \n    for i in range(len(segments)):\n        offset = int(i * 5)\n        segment_end=(i+1)*5  \n        predicted_bird = predict(model, p, offset)\n        for b in birds:\n            print(f'Predicted bird: {predicted_bird} [{bird_type_dict[predicted_bird]}]. Real Bird: {b}')\n            row_id=f+'_'+b+'_'+str(segment_end)\n            pred['row_id'].append(row_id)\n            is_prediction_true = (predicted_bird == b)\n            pred['target'].append(is_prediction_true)","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.527626Z","iopub.status.idle":"2022-05-22T07:21:01.528213Z","shell.execute_reply.started":"2022-05-22T07:21:01.527985Z","shell.execute_reply":"2022-05-22T07:21:01.528008Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cols=['row_id','target']\ndf_sub=pd.DataFrame({'row_id': pred['row_id'],\n                   'target': pred['target']})\ndf_sub","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.529380Z","iopub.status.idle":"2022-05-22T07:21:01.529983Z","shell.execute_reply.started":"2022-05-22T07:21:01.529757Z","shell.execute_reply":"2022-05-22T07:21:01.529781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"list(df_sub[\"row_id\"])","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.531133Z","iopub.status.idle":"2022-05-22T07:21:01.531750Z","shell.execute_reply.started":"2022-05-22T07:21:01.531510Z","shell.execute_reply":"2022-05-22T07:21:01.531534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"True in list(df_sub[\"target\"])","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.532910Z","iopub.status.idle":"2022-05-22T07:21:01.533552Z","shell.execute_reply.started":"2022-05-22T07:21:01.533310Z","shell.execute_reply":"2022-05-22T07:21:01.533334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test_df","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.534733Z","iopub.status.idle":"2022-05-22T07:21:01.535339Z","shell.execute_reply.started":"2022-05-22T07:21:01.535100Z","shell.execute_reply":"2022-05-22T07:21:01.535125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"work_dir=\"/kaggle/working\"       \ndf_sub.to_csv(work_dir+\"/submission.csv\", index=False)\ndf_sub.to_csv(\"submission.csv\", index=False)\n","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.536529Z","iopub.status.idle":"2022-05-22T07:21:01.537139Z","shell.execute_reply.started":"2022-05-22T07:21:01.536903Z","shell.execute_reply":"2022-05-22T07:21:01.536933Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"work_dir+\"/submission.csv\"","metadata":{"execution":{"iopub.status.busy":"2022-05-22T07:21:01.538325Z","iopub.status.idle":"2022-05-22T07:21:01.538932Z","shell.execute_reply.started":"2022-05-22T07:21:01.538704Z","shell.execute_reply":"2022-05-22T07:21:01.538728Z"},"trusted":true},"execution_count":null,"outputs":[]}]}