{"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-15T20:33:32.038531Z","iopub.execute_input":"2022-05-15T20:33:32.038793Z","iopub.status.idle":"2022-05-15T20:33:38.567674Z","shell.execute_reply.started":"2022-05-15T20:33:32.038765Z","shell.execute_reply":"2022-05-15T20:33:38.566594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load data","metadata":{}},{"cell_type":"code","source":"# df = pd.read_csv(\"../input/alldatapicklebirdclef2022/5_second_clips.csv\") \ndf = pd.read_csv(\"../input/alldatapicklebirdclef2022/5_second_clips.csv\") \ndf = df.drop(columns=[\"Unnamed: 0\"])\ndf = df.sample(frac=1).reset_index(drop=True)\n\ndf","metadata":{"execution":{"iopub.status.busy":"2022-05-15T20:33:38.573695Z","iopub.execute_input":"2022-05-15T20:33:38.576088Z","iopub.status.idle":"2022-05-15T20:33:38.888267Z","shell.execute_reply.started":"2022-05-15T20:33:38.576014Z","shell.execute_reply":"2022-05-15T20:33:38.887506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = df[0:130000]\ndf_test = df[130000:]\ndf_test = df_test.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2022-05-15T20:33:38.892861Z","iopub.execute_input":"2022-05-15T20:33:38.895181Z","iopub.status.idle":"2022-05-15T20:33:38.902838Z","shell.execute_reply.started":"2022-05-15T20:33:38.895142Z","shell.execute_reply":"2022-05-15T20:33:38.902075Z"},"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-15T20:33:38.908886Z","iopub.execute_input":"2022-05-15T20:33:38.911437Z","iopub.status.idle":"2022-05-15T20:33:38.988945Z","shell.execute_reply.started":"2022-05-15T20:33:38.911371Z","shell.execute_reply":"2022-05-15T20:33:38.988149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Oversample the train dataset","metadata":{}},{"cell_type":"code","source":"target_duration = 10623\ndf_train_new = 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][\"n_files\"])\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_new = pd.concat([df_train_new, df_tmp])\ndf_train_new","metadata":{"execution":{"iopub.status.busy":"2022-05-15T20:33:38.993462Z","iopub.execute_input":"2022-05-15T20:33:38.995856Z","iopub.status.idle":"2022-05-15T20:33:59.484353Z","shell.execute_reply.started":"2022-05-15T20:33:38.995815Z","shell.execute_reply":"2022-05-15T20:33:59.483463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# root_file = os.path.abspath(\"../input/alldatapicklebirdclef2022/timesum/timesum\")\nroot_file = os.path.abspath(\"../input/alldatapicklebirdclef2022/timesum/timesum\")\ndf_train_new[\"files_timesum\"] = df_train_new.apply(lambda x: os.path.join(root_file, x[\"file_path\"]), axis=1)\n# root_file = os.path.abspath(\"../input/alldatapicklebirdclef2022/freqsum/freqsum\")\nroot_file = os.path.abspath(\"../input/alldatapicklebirdclef2022/freqsum/freqsum\")\ndf_train_new[\"files_freqsum\"] = df_train_new.apply(lambda x: os.path.join(root_file, x[\"file_path\"]), axis=1)\ndf_train_new","metadata":{"execution":{"iopub.status.busy":"2022-05-15T20:33:59.485693Z","iopub.execute_input":"2022-05-15T20:33:59.486005Z","iopub.status.idle":"2022-05-15T20:34:44.376838Z","shell.execute_reply.started":"2022-05-15T20:33:59.485968Z","shell.execute_reply":"2022-05-15T20:34:44.376064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# root_file = os.path.abspath(\"../input/alldatapicklebirdclef2022/timesum/timesum\")\nroot_file = os.path.abspath(\"../input/alldatapicklebirdclef2022/timesum/timesum\")\ndf_test[\"files_timesum\"] = df_test.apply(lambda x: os.path.join(root_file, x[\"file_path\"]), axis=1)\n# root_file = os.path.abspath(\"../input/alldatapicklebirdclef2022/freqsum/freqsum\")\nroot_file = os.path.abspath(\"../input/alldatapicklebirdclef2022/freqsum/freqsum\")\ndf_test[\"files_freqsum\"] = df_test.apply(lambda x: os.path.join(root_file, x[\"file_path\"]), axis=1)\ndf_test","metadata":{"execution":{"iopub.status.busy":"2022-05-15T20:34:44.378263Z","iopub.execute_input":"2022-05-15T20:34:44.379068Z","iopub.status.idle":"2022-05-15T20:34:44.825809Z","shell.execute_reply.started":"2022-05-15T20:34:44.379029Z","shell.execute_reply":"2022-05-15T20:34:44.825132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_new = df_train_new.sample(frac=1)","metadata":{"execution":{"iopub.status.busy":"2022-05-15T20:34:44.82724Z","iopub.execute_input":"2022-05-15T20:34:44.827506Z","iopub.status.idle":"2022-05-15T20:34:45.987697Z","shell.execute_reply.started":"2022-05-15T20:34:44.82747Z","shell.execute_reply":"2022-05-15T20:34:45.98697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_new[\"files_timesum\"][0], df_train_new[\"files_freqsum\"][0]","metadata":{"execution":{"iopub.status.busy":"2022-05-15T20:34:45.989078Z","iopub.execute_input":"2022-05-15T20:34:45.989341Z","iopub.status.idle":"2022-05-15T20:34:46.027783Z","shell.execute_reply.started":"2022-05-15T20:34:45.989298Z","shell.execute_reply":"2022-05-15T20:34:46.027046Z"},"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_new[\"bird_type_index\"] = df_train_new.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-15T20:34:46.031251Z","iopub.execute_input":"2022-05-15T20:34:46.031504Z","iopub.status.idle":"2022-05-15T20:35:05.516901Z","shell.execute_reply.started":"2022-05-15T20:34:46.031477Z","shell.execute_reply":"2022-05-15T20:35:05.516179Z"},"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-15T20:35:05.518022Z","iopub.execute_input":"2022-05-15T20:35:05.518283Z","iopub.status.idle":"2022-05-15T20:35:05.523781Z","shell.execute_reply.started":"2022-05-15T20:35:05.518249Z","shell.execute_reply":"2022-05-15T20:35:05.523111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfiles = list(df_train_new[\"files_timesum\"])\nlabels = list(df_train_new[\"bird_type_index\"])\ndata_timesum = tf.data.Dataset.from_tensor_slices((files, labels))\n\ndata_timesum = data_timesum.map(lambda item1, item2: tf.numpy_function(\n        preprocess, [item1, item2], [tf.float32, tf.int32]),\n        num_parallel_calls=tf.data.AUTOTUNE)\n\n# data_timesum = data_timesum.cache()\ndata_timesum = data_timesum.shuffle(buffer_size=1000)\ndata_timesum = data_timesum.batch(16)\ndata_timesum_train = data_timesum.prefetch(8)\n\n# data_timesum.as_numpy_iterator().next() \n","metadata":{"execution":{"iopub.status.busy":"2022-05-15T20:35:05.525117Z","iopub.execute_input":"2022-05-15T20:35:05.525606Z","iopub.status.idle":"2022-05-15T20:35:20.024454Z","shell.execute_reply.started":"2022-05-15T20:35:05.525569Z","shell.execute_reply":"2022-05-15T20:35:20.023744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfiles = list(df_test[\"files_timesum\"])\nlabels = list(df_test[\"bird_type_index\"])\ndata_timesum = tf.data.Dataset.from_tensor_slices((files, labels))\n\ndata_timesum = data_timesum.map(lambda item1, item2: tf.numpy_function(\n        preprocess, [item1, item2], [tf.float32, tf.int32]),\n        num_parallel_calls=tf.data.AUTOTUNE)\n\n# data_timesum = data_timesum.cache()\ndata_timesum = data_timesum.shuffle(buffer_size=1000)\ndata_timesum = data_timesum.batch(16)\ndata_timesum_test = data_timesum.prefetch(8).repeat()\n\n# data_timesum.as_numpy_iterator().next() \n","metadata":{"execution":{"iopub.status.busy":"2022-05-15T20:35:20.025552Z","iopub.execute_input":"2022-05-15T20:35:20.025779Z","iopub.status.idle":"2022-05-15T20:35:20.168784Z","shell.execute_reply.started":"2022-05-15T20:35:20.025747Z","shell.execute_reply":"2022-05-15T20:35:20.16812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"s, l = data_timesum_train.as_numpy_iterator().next()\ns.shape, l.shape","metadata":{"execution":{"iopub.status.busy":"2022-05-15T20:35:20.169902Z","iopub.execute_input":"2022-05-15T20:35:20.170281Z","iopub.status.idle":"2022-05-15T20:35:23.924564Z","shell.execute_reply.started":"2022-05-15T20:35:20.170245Z","shell.execute_reply":"2022-05-15T20:35:23.923626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Merge two datasets","metadata":{}},{"cell_type":"code","source":"train = data_timesum_train\ntest = data_timesum_test","metadata":{"execution":{"iopub.status.busy":"2022-05-15T20:35:23.926198Z","iopub.execute_input":"2022-05-15T20:35:23.9265Z","iopub.status.idle":"2022-05-15T20:35:23.93136Z","shell.execute_reply.started":"2022-05-15T20:35:23.926462Z","shell.execute_reply":"2022-05-15T20:35:23.930071Z"},"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-15T20:35:23.933437Z","iopub.execute_input":"2022-05-15T20:35:23.933892Z","iopub.status.idle":"2022-05-15T20:35:25.12992Z","shell.execute_reply.started":"2022-05-15T20:35:23.933855Z","shell.execute_reply":"2022-05-15T20:35:25.129268Z"},"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","metadata":{"execution":{"iopub.status.busy":"2022-05-15T20:35:25.131006Z","iopub.execute_input":"2022-05-15T20:35:25.131264Z","iopub.status.idle":"2022-05-15T20:35:25.138952Z","shell.execute_reply.started":"2022-05-15T20:35:25.131206Z","shell.execute_reply":"2022-05-15T20:35:25.138268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential([\n    \n    Conv1D(64, 3, activation='relu', input_shape=(513,1)),\n    Dropout(0.5),\n    Conv1D(64, 3, activation='relu'),\n    Dropout(0.5),\n    Conv1D(64, 3, activation='relu'),\n    Dropout(0.5),\n    MaxPool1D(2),\n    Flatten(),\n    Dense(1024, activation='relu'), # You have 152 bird types, check this with len(bird_types)\n    Dense(152, activation='softmax'), # You have 152 bird types, check this with len(bird_types)\n])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-05-15T20:35:25.142078Z","iopub.execute_input":"2022-05-15T20:35:25.142279Z","iopub.status.idle":"2022-05-15T20:35:25.249606Z","shell.execute_reply.started":"2022-05-15T20:35:25.142255Z","shell.execute_reply":"2022-05-15T20:35:25.248939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = tf.ones((16, 513, 1))\ny=model(x)\ny.shape","metadata":{"execution":{"iopub.status.busy":"2022-05-15T20:35:25.250835Z","iopub.execute_input":"2022-05-15T20:35:25.251076Z","iopub.status.idle":"2022-05-15T20:35:31.25479Z","shell.execute_reply.started":"2022-05-15T20:35:25.251042Z","shell.execute_reply":"2022-05-15T20:35:31.254071Z"},"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-15T20:35:31.256079Z","iopub.execute_input":"2022-05-15T20:35:31.256352Z","iopub.status.idle":"2022-05-15T20:35:38.285726Z","shell.execute_reply.started":"2022-05-15T20:35:31.256307Z","shell.execute_reply":"2022-05-15T20:35:38.284959Z"},"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-15T20:35:38.287998Z","iopub.execute_input":"2022-05-15T20:35:38.289319Z","iopub.status.idle":"2022-05-15T20:35:38.294846Z","shell.execute_reply.started":"2022-05-15T20:35:38.28927Z","shell.execute_reply":"2022-05-15T20:35:38.294087Z"},"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-15T20:35:38.296784Z","iopub.execute_input":"2022-05-15T20:35:38.297196Z","iopub.status.idle":"2022-05-15T20:35:38.305485Z","shell.execute_reply.started":"2022-05-15T20:35:38.29716Z","shell.execute_reply":"2022-05-15T20:35:38.304734Z"},"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 4.E-04)\n\nhist = model.fit(train, \n                 epochs=100, \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-15T20:35:38.307085Z","iopub.execute_input":"2022-05-15T20:35:38.307797Z","iopub.status.idle":"2022-05-15T20:40:54.072485Z","shell.execute_reply.started":"2022-05-15T20:35:38.307741Z","shell.execute_reply":"2022-05-15T20:40:54.0692Z"},"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-15T20:40:54.075614Z","iopub.status.idle":"2022-05-15T20:40:54.07773Z","shell.execute_reply.started":"2022-05-15T20:40:54.077486Z","shell.execute_reply":"2022-05-15T20:40:54.077513Z"},"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-15T20:40:54.081347Z","iopub.status.idle":"2022-05-15T20:40:54.083437Z","shell.execute_reply.started":"2022-05-15T20:40:54.083187Z","shell.execute_reply":"2022-05-15T20:40:54.083213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# hist.history","metadata":{"execution":{"iopub.status.busy":"2022-05-15T20:40:54.086107Z","iopub.status.idle":"2022-05-15T20:40:54.087212Z","shell.execute_reply.started":"2022-05-15T20:40:54.086967Z","shell.execute_reply":"2022-05-15T20:40:54.086994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"max(hist.history[\"val_sparse_categorical_accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2022-05-15T20:40:54.089261Z","iopub.status.idle":"2022-05-15T20:40:54.089861Z","shell.execute_reply.started":"2022-05-15T20:40:54.08963Z","shell.execute_reply":"2022-05-15T20:40:54.089653Z"},"trusted":true},"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-15T20:41:18.279688Z","iopub.execute_input":"2022-05-15T20:41:18.279941Z","iopub.status.idle":"2022-05-15T20:41:18.284032Z","shell.execute_reply.started":"2022-05-15T20:41:18.279912Z","shell.execute_reply":"2022-05-15T20:41:18.283001Z"},"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-15T20:41:18.619985Z","iopub.execute_input":"2022-05-15T20:41:18.620284Z","iopub.status.idle":"2022-05-15T20:41:18.625629Z","shell.execute_reply.started":"2022-05-15T20:41:18.620246Z","shell.execute_reply":"2022-05-15T20:41:18.624813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_wav_16k_mono(file_input, offset):\n    # Args: \n    # file_name: path to the .ogg file\n    # total_duration: how much the song lasts\n    # Output: you will take only 5 second of each song.\n    \n    wav, sample_rate = librosa.load(file_input, \n                                    sr=16000, \n                                    duration=5,\n                                    offset=offset)\n    return wav\n\n\ndef load_spectrogram(file_input, offset):\n    wav = load_wav_16k_mono(file_input, offset)\n    # 16000 data = 1 seconds\n    num_data = int(16000 * 5)\n    frame_length=1024\n    frame_step=128\n    wav = wav[:num_data]\n    zero_padding = tf.zeros([num_data] - tf.shape(wav), dtype=tf.float32)\n    wav = tf.concat([zero_padding, wav],0)\n    spectrogram = tf.signal.stft(wav, frame_length=frame_length, frame_step=frame_step)\n    spectrogram = tf.abs(spectrogram)\n    spectrogram = spectrogram / tf.reduce_max(spectrogram)\n    return spectrogram.numpy()\n    \n\ndef preprocess(file_input, offset):\n    spectrogram = load_spectrogram(file_input, offset)\n    spectrogram = np.sum(spectrogram, axis=0)\n    spectrogram *= 1. / np.max(spectrogram)\n    spectrogram = np.expand_dims(spectrogram, axis=1)\n    return spectrogram\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    #file_input = os.path.join(\"../input/birdclef-2022/test_soundscapes/soundscape_453028782.ogg\")\n    file_input = file_id\n    \n    \n    x = np.load(file_input)\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    return prediction_label\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-15T20:41:18.986127Z","iopub.execute_input":"2022-05-15T20:41:18.986512Z","iopub.status.idle":"2022-05-15T20:41:19.000846Z","shell.execute_reply.started":"2022-05-15T20:41:18.986457Z","shell.execute_reply":"2022-05-15T20:41:18.999336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train[\"file_path\"][2]","metadata":{"execution":{"iopub.status.busy":"2022-05-15T20:41:19.558002Z","iopub.execute_input":"2022-05-15T20:41:19.559013Z","iopub.status.idle":"2022-05-15T20:41:19.564854Z","shell.execute_reply.started":"2022-05-15T20:41:19.558968Z","shell.execute_reply":"2022-05-15T20:41:19.564063Z"},"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-15T20:41:20.292012Z","iopub.execute_input":"2022-05-15T20:41:20.292293Z","iopub.status.idle":"2022-05-15T20:41:20.625823Z","shell.execute_reply.started":"2022-05-15T20:41:20.292259Z","shell.execute_reply":"2022-05-15T20:41:20.625098Z"},"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-15T20:41:20.832146Z","iopub.execute_input":"2022-05-15T20:41:20.832841Z","iopub.status.idle":"2022-05-15T20:41:21.170697Z","shell.execute_reply.started":"2022-05-15T20:41:20.832806Z","shell.execute_reply":"2022-05-15T20:41:21.170025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_tot = 100\nn_correct = 0\nfor i, file_path in enumerate(list(df_test[\"file_path\"])):\n    if i>n_tot:\n        break\n    offset = int(file_path.split(\"_\")[-1].split(\".\")[0])\n    file_input = file_path.replace(\"_\" + str(offset), \"\").replace(\"npy\", \"ogg\")\n    bird = file_path.split(\"/\")[0]\n    file_input = f\"../input/birdclef-2022/train_audio/{file_input}\"\n    x = preprocess(file_input, offset=0)\n    x = tf.expand_dims(x, axis=0)\n    prediction = predict(model, file_id=file_input, offset=offset*5)\n    n_correct += 1 if prediction == bird else 0\n    print(f\"Real bird: {bird}. Predicted: {prediction}\")\nprint(f\"Correctly guessed: {n_correct}. Percentage of correct: {n_correct / n_tot}\")","metadata":{"execution":{"iopub.status.busy":"2022-05-15T20:41:21.760271Z","iopub.execute_input":"2022-05-15T20:41:21.760817Z","iopub.status.idle":"2022-05-15T20:42:11.460646Z","shell.execute_reply.started":"2022-05-15T20:41:21.760781Z","shell.execute_reply":"2022-05-15T20:42:11.459894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_true = list(df_test[\"bird_type_index\"])[0:1000]\nfiles = list(df_test[\"files_timesum\"])[0:1000]\ny_pred = [predict_label_npy(model, file, offset=0) for file in files]","metadata":{"execution":{"iopub.status.busy":"2022-05-15T20:42:11.46218Z","iopub.execute_input":"2022-05-15T20:42:11.462675Z","iopub.status.idle":"2022-05-15T20:42:16.401093Z","shell.execute_reply.started":"2022-05-15T20:42:11.462635Z","shell.execute_reply":"2022-05-15T20:42:16.400338Z"},"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-15T20:42:16.402408Z","iopub.execute_input":"2022-05-15T20:42:16.402644Z","iopub.status.idle":"2022-05-15T20:42:16.416183Z","shell.execute_reply.started":"2022-05-15T20:42:16.402613Z","shell.execute_reply":"2022-05-15T20:42:16.415548Z"},"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)\n","metadata":{"execution":{"iopub.status.busy":"2022-05-15T20:42:16.418015Z","iopub.execute_input":"2022-05-15T20:42:16.420098Z","iopub.status.idle":"2022-05-15T20:42:16.426736Z","shell.execute_reply.started":"2022-05-15T20:42:16.420071Z","shell.execute_reply":"2022-05-15T20:42:16.426052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt\n\nplt.figure(figsize=(30,20))\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-15T20:40:54.125348Z","iopub.status.idle":"2022-05-15T20:40:54.125961Z","shell.execute_reply.started":"2022-05-15T20:40:54.12572Z","shell.execute_reply":"2022-05-15T20:40:54.125743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.sum(cf_matrix, axis=1) ","metadata":{"execution":{"iopub.status.busy":"2022-05-15T20:44:29.846599Z","iopub.execute_input":"2022-05-15T20:44:29.847155Z","iopub.status.idle":"2022-05-15T20:44:29.853575Z","shell.execute_reply.started":"2022-05-15T20:44:29.847117Z","shell.execute_reply":"2022-05-15T20:44:29.852751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.diag(cf_matrix) # np.shape(cf_matrix)","metadata":{"execution":{"iopub.status.busy":"2022-05-15T20:44:34.385779Z","iopub.execute_input":"2022-05-15T20:44:34.386045Z","iopub.status.idle":"2022-05-15T20:44:34.393008Z","shell.execute_reply.started":"2022-05-15T20:44:34.386015Z","shell.execute_reply":"2022-05-15T20:44:34.392321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.hist(y_pred)","metadata":{"execution":{"iopub.status.busy":"2022-05-15T18:21:16.370472Z","iopub.execute_input":"2022-05-15T18:21:16.370752Z","iopub.status.idle":"2022-05-15T18:21:16.580897Z","shell.execute_reply.started":"2022-05-15T18:21:16.370721Z","shell.execute_reply":"2022-05-15T18:21:16.580249Z"},"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-15T18:21:16.832195Z","iopub.execute_input":"2022-05-15T18:21:16.832397Z","iopub.status.idle":"2022-05-15T18:21:18.936352Z","shell.execute_reply.started":"2022-05-15T18:21:16.832373Z","shell.execute_reply":"2022-05-15T18:21:18.935674Z"},"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-15T08:34:02.841465Z","iopub.execute_input":"2022-05-15T08:34:02.84171Z","iopub.status.idle":"2022-05-15T08:34:02.867055Z","shell.execute_reply.started":"2022-05-15T08:34:02.841677Z","shell.execute_reply":"2022-05-15T08:34:02.866208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"list(df_sub[\"row_id\"])","metadata":{"execution":{"iopub.status.busy":"2022-05-15T08:34:02.868429Z","iopub.execute_input":"2022-05-15T08:34:02.868842Z","iopub.status.idle":"2022-05-15T08:34:02.881722Z","shell.execute_reply.started":"2022-05-15T08:34:02.868806Z","shell.execute_reply":"2022-05-15T08:34:02.880874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"True in list(df_sub[\"target\"])","metadata":{"execution":{"iopub.status.busy":"2022-05-15T08:34:02.88327Z","iopub.execute_input":"2022-05-15T08:34:02.883585Z","iopub.status.idle":"2022-05-15T08:34:02.89121Z","shell.execute_reply.started":"2022-05-15T08:34:02.883515Z","shell.execute_reply":"2022-05-15T08:34:02.890389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test_df","metadata":{"execution":{"iopub.status.busy":"2022-05-15T08:34:02.892568Z","iopub.execute_input":"2022-05-15T08:34:02.892825Z","iopub.status.idle":"2022-05-15T08:34:02.898047Z","shell.execute_reply.started":"2022-05-15T08:34:02.892791Z","shell.execute_reply":"2022-05-15T08:34:02.897282Z"},"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-15T08:34:02.899707Z","iopub.execute_input":"2022-05-15T08:34:02.899964Z","iopub.status.idle":"2022-05-15T08:34:02.913738Z","shell.execute_reply.started":"2022-05-15T08:34:02.899931Z","shell.execute_reply":"2022-05-15T08:34:02.913101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"work_dir+\"/submission.csv\"","metadata":{"execution":{"iopub.status.busy":"2022-05-15T08:34:02.914646Z","iopub.execute_input":"2022-05-15T08:34:02.914821Z","iopub.status.idle":"2022-05-15T08:34:02.920967Z","shell.execute_reply.started":"2022-05-15T08:34:02.914799Z","shell.execute_reply":"2022-05-15T08:34:02.920065Z"},"trusted":true},"execution_count":null,"outputs":[]}]}