{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":70203,"databundleVersionId":8068726,"sourceType":"competition"},{"sourceId":8637188,"sourceType":"datasetVersion","datasetId":5172238}],"dockerImageVersionId":30732,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"#### !pip install resampy","metadata":{"execution":{"iopub.status.busy":"2024-06-11T05:53:17.000622Z","iopub.execute_input":"2024-06-11T05:53:17.001028Z","iopub.status.idle":"2024-06-11T05:53:33.062024Z","shell.execute_reply.started":"2024-06-11T05:53:17.000996Z","shell.execute_reply":"2024-06-11T05:53:33.060933Z"}}},{"cell_type":"code","source":"!pip install audiomentations","metadata":{"execution":{"iopub.status.busy":"2024-06-11T05:53:33.064627Z","iopub.execute_input":"2024-06-11T05:53:33.065057Z","iopub.status.idle":"2024-06-11T05:53:48.180650Z","shell.execute_reply.started":"2024-06-11T05:53:33.065016Z","shell.execute_reply":"2024-06-11T05:53:48.179304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport numpy as np\nimport librosa.display\nimport matplotlib.pyplot as plt\nfrom keras.preprocessing import image\nimport tensorflow as tf\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, Reshape, LSTM, Dropout, Dense, BatchNormalization\nfrom tensorflow.keras.applications import EfficientNetV2B0\nfrom tensorflow.keras.models import Model\nfrom audiomentations import SpecCompose, SpecChannelShuffle, SpecFrequencyMask\nimport resampy\nfrom tensorflow.keras.layers import BatchNormalization, Reshape, LSTM, Dropout, Dense\nfrom tensorflow.keras.models import Model\nfrom sklearn.metrics import roc_auc_score\nimport numpy as np","metadata":{"execution":{"iopub.status.busy":"2024-06-11T05:53:48.182230Z","iopub.execute_input":"2024-06-11T05:53:48.182588Z","iopub.status.idle":"2024-06-11T05:54:04.547851Z","shell.execute_reply.started":"2024-06-11T05:53:48.182557Z","shell.execute_reply":"2024-06-11T05:54:04.546823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_set = tf.keras.utils.image_dataset_from_directory(\n    '/kaggle/input/dataset/birdclef_spectogram',\n    labels=\"inferred\",\n    label_mode=\"categorical\",\n    class_names=None,\n    color_mode=\"rgb\",\n    batch_size=32,\n    image_size=(224, 224),\n    shuffle=True,\n    seed=0,\n    validation_split=0.2,\n    subset=\"training\",\n    interpolation=\"bilinear\",\n    follow_links=False,\n    crop_to_aspect_ratio=False,\n)","metadata":{"execution":{"iopub.status.busy":"2024-06-11T08:07:35.685276Z","iopub.execute_input":"2024-06-11T08:07:35.685640Z","iopub.status.idle":"2024-06-11T08:07:39.689180Z","shell.execute_reply.started":"2024-06-11T08:07:35.685610Z","shell.execute_reply":"2024-06-11T08:07:39.688305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation_set = tf.keras.utils.image_dataset_from_directory(\n    '/kaggle/input/dataset/birdclef_spectogram',\n    labels=\"inferred\",\n    label_mode=\"categorical\",\n    class_names=None,\n    color_mode=\"rgb\",\n    batch_size=32,\n    image_size=(224, 224),\n    shuffle=True,\n    seed=0,\n    validation_split=0.2,\n    subset=\"validation\",\n    interpolation=\"bilinear\",\n    follow_links=False,\n    crop_to_aspect_ratio=False,\n)","metadata":{"execution":{"iopub.status.busy":"2024-06-11T05:54:09.250899Z","iopub.execute_input":"2024-06-11T05:54:09.251222Z","iopub.status.idle":"2024-06-11T05:54:14.107881Z","shell.execute_reply.started":"2024-06-11T05:54:09.251195Z","shell.execute_reply":"2024-06-11T05:54:14.106790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define model parameters\nheight = 224\nwidth = 224\nchannels = 3\nnum_classes = 182","metadata":{"execution":{"iopub.status.busy":"2024-06-11T05:54:14.109146Z","iopub.execute_input":"2024-06-11T05:54:14.109568Z","iopub.status.idle":"2024-06-11T05:54:14.115024Z","shell.execute_reply.started":"2024-06-11T05:54:14.109537Z","shell.execute_reply":"2024-06-11T05:54:14.113553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load EfficientNetV2B0 model with pretrained weights\nbase_model =EfficientNetV2B0(include_top=False, input_shape=(height, width, channels), weights='imagenet')","metadata":{"execution":{"iopub.status.busy":"2024-06-11T05:54:14.116406Z","iopub.execute_input":"2024-06-11T05:54:14.116755Z","iopub.status.idle":"2024-06-11T05:54:16.264352Z","shell.execute_reply.started":"2024-06-11T05:54:14.116727Z","shell.execute_reply":"2024-06-11T05:54:16.263381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Add custom layers on top of base model\nx = BatchNormalization()(base_model.output)\n\n# Flatten the output of EfficientNetV2B0\nx = Reshape((-1, 128))(x)\n\n# Recurrent layers\nx = LSTM(128, return_sequences=True)(x)\nx = Dropout(0.2)(x)\nx = LSTM(128)(x)\nx = Dropout(0.2)(x)\n\n# Fully connected layers\nx = Dense(512, activation='relu')(x)\nx = Dropout(0.2)(x)\n\n# Output layer\npredictions = Dense(num_classes, activation='softmax')(x)","metadata":{"execution":{"iopub.status.busy":"2024-06-11T05:54:16.266114Z","iopub.execute_input":"2024-06-11T05:54:16.266506Z","iopub.status.idle":"2024-06-11T05:54:16.496550Z","shell.execute_reply.started":"2024-06-11T05:54:16.266475Z","shell.execute_reply":"2024-06-11T05:54:16.495634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Create the full model\nmodel = Model(inputs=base_model.input, outputs=predictions)","metadata":{"execution":{"iopub.status.busy":"2024-06-11T05:54:16.497733Z","iopub.execute_input":"2024-06-11T05:54:16.498065Z","iopub.status.idle":"2024-06-11T05:54:16.551030Z","shell.execute_reply.started":"2024-06-11T05:54:16.498037Z","shell.execute_reply":"2024-06-11T05:54:16.549792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Print model summary\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2024-06-11T05:54:16.555050Z","iopub.execute_input":"2024-06-11T05:54:16.555405Z","iopub.status.idle":"2024-06-11T05:54:16.977327Z","shell.execute_reply.started":"2024-06-11T05:54:16.555362Z","shell.execute_reply":"2024-06-11T05:54:16.976415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compile the model\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-06-11T05:54:16.978682Z","iopub.execute_input":"2024-06-11T05:54:16.979057Z","iopub.status.idle":"2024-06-11T05:54:16.999045Z","shell.execute_reply.started":"2024-06-11T05:54:16.979022Z","shell.execute_reply":"2024-06-11T05:54:16.998210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"height = 224\nwidth = 224\nchannels = 3\nnum_classes = 182","metadata":{"execution":{"iopub.status.busy":"2024-06-11T05:54:17.000256Z","iopub.execute_input":"2024-06-11T05:54:17.000672Z","iopub.status.idle":"2024-06-11T05:54:17.005596Z","shell.execute_reply.started":"2024-06-11T05:54:17.000636Z","shell.execute_reply":"2024-06-11T05:54:17.004487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train the model\nhistory = model.fit(\n    training_set,\n    epochs=50,\n    validation_data=validation_set,\n)","metadata":{"execution":{"iopub.status.busy":"2024-06-11T05:54:17.006939Z","iopub.execute_input":"2024-06-11T05:54:17.007316Z","iopub.status.idle":"2024-06-11T08:07:04.294139Z","shell.execute_reply.started":"2024-06-11T05:54:17.007280Z","shell.execute_reply":"2024-06-11T08:07:04.293133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Function to calculate custom ROC-AUC\ndef custom_roc_auc_score(y_true, y_pred):\n    roc_auc_list = []\n    for i in range(num_classes):\n        if np.sum(y_true[:, i]) > 0:  # Skip classes with no true positive labels\n            roc_auc = roc_auc_score(y_true[:, i], y_pred[:, i])\n            roc_auc_list.append(roc_auc)\n    return np.mean(roc_auc_list) if len(roc_auc_list) > 0 else 0.0","metadata":{"execution":{"iopub.status.busy":"2024-06-11T08:07:04.295536Z","iopub.execute_input":"2024-06-11T08:07:04.295836Z","iopub.status.idle":"2024-06-11T08:07:04.302411Z","shell.execute_reply.started":"2024-06-11T08:07:04.295810Z","shell.execute_reply":"2024-06-11T08:07:04.301441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get predictions and true labels for the validation set\ny_true = np.concatenate([y for x, y in validation_set], axis=0)\ny_pred = model.predict(validation_set)\ny_true = y_true[:len(y_pred)]  # Ensure same length","metadata":{"execution":{"iopub.status.busy":"2024-06-11T08:07:04.303791Z","iopub.execute_input":"2024-06-11T08:07:04.304088Z","iopub.status.idle":"2024-06-11T08:07:34.673309Z","shell.execute_reply.started":"2024-06-11T08:07:04.304061Z","shell.execute_reply":"2024-06-11T08:07:34.672175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Calculate custom ROC-AUC\nroc_auc = custom_roc_auc_score(y_true, y_pred)\nprint(f'Macro-Averaged ROC-AUC: {roc_auc:.4f}')","metadata":{"execution":{"iopub.status.busy":"2024-06-11T08:07:34.674706Z","iopub.execute_input":"2024-06-11T08:07:34.675032Z","iopub.status.idle":"2024-06-11T08:07:35.289608Z","shell.execute_reply.started":"2024-06-11T08:07:34.675004Z","shell.execute_reply":"2024-06-11T08:07:35.288581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Extract accuracy and ROC-AUC scores from the history object\nacc =history .history['accuracy']\nval_acc = history.history['val_accuracy']\nepochs = range(1, len(acc) + 1)\n\n# Plot training and validation accuracy\nplt.figure(figsize=(12, 5))\n\nplt.subplot(1, 2, 1)\nplt.plot(epochs, acc, '-', label='Training Accuracy')\nplt.plot(epochs, val_acc, '-', label='Validation Accuracy')\nplt.title('Training and Validation Accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend(loc='lower right')","metadata":{"execution":{"iopub.status.busy":"2024-06-11T08:07:35.290853Z","iopub.execute_input":"2024-06-11T08:07:35.291181Z","iopub.status.idle":"2024-06-11T08:07:35.682220Z","shell.execute_reply.started":"2024-06-11T08:07:35.291152Z","shell.execute_reply":"2024-06-11T08:07:35.681072Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plot ROC-AUC score\nplt.figure(figsize=(6, 4))\nplt.bar(['Custom Macro-Averaged ROC-AUC'], [roc_auc])\nplt.ylim(0, 1)\nplt.title('Custom Macro-Averaged ROC-AUC')\nplt.ylabel('ROC-AUC Score')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-11T08:07:39.690437Z","iopub.execute_input":"2024-06-11T08:07:39.690766Z","iopub.status.idle":"2024-06-11T08:07:39.933936Z","shell.execute_reply.started":"2024-06-11T08:07:39.690718Z","shell.execute_reply":"2024-06-11T08:07:39.932766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}