{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceType":"competition","sourceId":70203,"databundleVersionId":8068726}],"dockerImageVersionId":31328,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install librosa","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-17T20:01:51.870790Z","iopub.execute_input":"2026-05-17T20:01:51.871515Z","iopub.status.idle":"2026-05-17T20:01:56.402801Z","shell.execute_reply.started":"2026-05-17T20:01:51.871479Z","shell.execute_reply":"2026-05-17T20:01:56.401921Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nimport librosa\nimport os","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-17T20:02:15.026527Z","iopub.execute_input":"2026-05-17T20:02:15.026870Z","iopub.status.idle":"2026-05-17T20:02:15.031459Z","shell.execute_reply.started":"2026-05-17T20:02:15.026838Z","shell.execute_reply":"2026-05-17T20:02:15.030537Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_csv = pd.read_csv('/kaggle/input/competitions/birdclef-2024/train_metadata.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-17T20:21:31.691226Z","iopub.execute_input":"2026-05-17T20:21:31.691556Z","iopub.status.idle":"2026-05-17T20:21:31.802700Z","shell.execute_reply.started":"2026-05-17T20:21:31.691525Z","shell.execute_reply":"2026-05-17T20:21:31.802063Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_csv.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-17T20:27:11.658902Z","iopub.execute_input":"2026-05-17T20:27:11.659598Z","iopub.status.idle":"2026-05-17T20:27:11.688235Z","shell.execute_reply.started":"2026-05-17T20:27:11.659562Z","shell.execute_reply":"2026-05-17T20:27:11.687494Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"labels = train_csv['primary_label'].unique()\n\nlabel_to_index = {label: i for i, label in enumerate(labels)}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-17T20:27:41.267661Z","iopub.execute_input":"2026-05-17T20:27:41.268022Z","iopub.status.idle":"2026-05-17T20:27:41.274109Z","shell.execute_reply.started":"2026-05-17T20:27:41.267990Z","shell.execute_reply":"2026-05-17T20:27:41.273223Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = []\ny = []","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-17T20:28:50.634740Z","iopub.execute_input":"2026-05-17T20:28:50.635557Z","iopub.status.idle":"2026-05-17T20:28:50.639309Z","shell.execute_reply.started":"2026-05-17T20:28:50.635520Z","shell.execute_reply":"2026-05-17T20:28:50.638381Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for i in range(100):\n\n    row = train_csv.iloc[i]\n\n    file_name = row['filename']\n    label = row['primary_label']\n\n    path = '/kaggle/input/competitions/birdclef-2024/train_audio/' + file_name\n\n    audio, sr = librosa.load(path, duration=5)\n\n    mel = librosa.feature.melspectrogram(y=audio, sr=sr)\n\n    mel_db = librosa.power_to_db(mel, ref=np.max)\n\n    mel_db = tf.image.resize(mel_db[..., np.newaxis], [128, 128])\n\n    X.append(mel_db)\n    y.append(label_to_index[label])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-17T20:34:11.978105Z","iopub.execute_input":"2026-05-17T20:34:11.978754Z","iopub.status.idle":"2026-05-17T20:34:27.955872Z","shell.execute_reply.started":"2026-05-17T20:34:11.978720Z","shell.execute_reply":"2026-05-17T20:34:27.955154Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = np.array(X)\ny = np.array(y)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-17T20:54:33.939696Z","iopub.execute_input":"2026-05-17T20:54:33.940280Z","iopub.status.idle":"2026-05-17T20:54:33.947955Z","shell.execute_reply.started":"2026-05-17T20:54:33.940248Z","shell.execute_reply":"2026-05-17T20:54:33.947147Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX_train, X_test, y_train, y_test = train_test_split(\n    X, y, test_size=0.2, random_state=42\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-17T20:54:38.850757Z","iopub.execute_input":"2026-05-17T20:54:38.851490Z","iopub.status.idle":"2026-05-17T20:54:38.860246Z","shell.execute_reply.started":"2026-05-17T20:54:38.851452Z","shell.execute_reply":"2026-05-17T20:54:38.859378Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = keras.Sequential([\n    layers.Conv2D(32, (3,3), activation='relu', input_shape=(128,128,1)),\n    layers.MaxPooling2D((2,2)),\n\n    layers.Conv2D(64, (3,3), activation='relu'),\n    layers.MaxPooling2D((2,2)),\n\n    layers.Flatten(),\n\n    layers.Dense(64, activation='relu'),\n\n    layers.Dense(len(labels), activation='softmax')\n])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-17T20:54:46.223251Z","iopub.execute_input":"2026-05-17T20:54:46.223581Z","iopub.status.idle":"2026-05-17T20:54:46.292234Z","shell.execute_reply.started":"2026-05-17T20:54:46.223550Z","shell.execute_reply":"2026-05-17T20:54:46.291525Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(\n    optimizer='adam',\n    loss='sparse_categorical_crossentropy',\n    metrics=['accuracy']\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-17T20:54:50.935856Z","iopub.execute_input":"2026-05-17T20:54:50.936487Z","iopub.status.idle":"2026-05-17T20:54:50.945873Z","shell.execute_reply.started":"2026-05-17T20:54:50.936447Z","shell.execute_reply":"2026-05-17T20:54:50.945103Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(\n    X_train,\n    y_train,\n    epochs=5,\n    batch_size=16,\n    validation_split=0.2\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-17T20:54:55.720944Z","iopub.execute_input":"2026-05-17T20:54:55.721621Z","iopub.status.idle":"2026-05-17T20:55:01.583514Z","shell.execute_reply.started":"2026-05-17T20:54:55.721584Z","shell.execute_reply":"2026-05-17T20:55:01.582600Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"loss, accuracy = model.evaluate(X_test, y_test)\n\nprint('Loss:', loss)\nprint('Accuracy:', accuracy)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-17T20:55:45.670582Z","iopub.execute_input":"2026-05-17T20:55:45.671284Z","iopub.status.idle":"2026-05-17T20:55:45.986568Z","shell.execute_reply.started":"2026-05-17T20:55:45.671249Z","shell.execute_reply":"2026-05-17T20:55:45.985719Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"prediction = model.predict(X_test[:1])\n\npredicted_class = np.argmax(prediction)\n\nprint(predicted_class)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-05-17T20:55:51.705292Z","iopub.execute_input":"2026-05-17T20:55:51.705599Z","iopub.status.idle":"2026-05-17T20:55:51.854272Z","shell.execute_reply.started":"2026-05-17T20:55:51.705571Z","shell.execute_reply":"2026-05-17T20:55:51.853471Z"}},"outputs":[],"execution_count":null}]}