{"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":6127,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":4598},{"sourceId":15853,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":2739}],"dockerImageVersionId":30699,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\nimport pandas as pd\nimport seaborn as sns\nimport torch\nimport tqdm\nimport librosa as lbr\nimport IPython.display as ipd\nimport soundfile as sf\nimport os\nimport librosa.display\nfrom glob import glob","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-05-06T12:32:24.129289Z","iopub.execute_input":"2024-05-06T12:32:24.130082Z","iopub.status.idle":"2024-05-06T12:32:24.135496Z","shell.execute_reply.started":"2024-05-06T12:32:24.130045Z","shell.execute_reply":"2024-05-06T12:32:24.134369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Paths","metadata":{}},{"cell_type":"code","source":"test_soundscapes=\"/kaggle/input/birdclef-2024/test_soundscapes\"\ntrain_audio = \"/kaggle/input/birdclef-2024/train_audio\"\nunlabeled_soundscapes = \"/kaggle/input/birdclef-2024/unlabeled_soundscapes\"","metadata":{"execution":{"iopub.status.busy":"2024-05-06T11:40:36.071492Z","iopub.execute_input":"2024-05-06T11:40:36.071955Z","iopub.status.idle":"2024-05-06T11:40:36.076158Z","shell.execute_reply.started":"2024-05-06T11:40:36.071924Z","shell.execute_reply":"2024-05-06T11:40:36.075186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ebird_txn = \"/kaggle/input/birdclef-2024/eBird_Taxonomy_v2021.csv\"\ntrain_metadata = \"/kaggle/input/birdclef-2024/train_metadata.csv\"\nss = \"/kaggle/input/birdclef-2024/sample_submission.csv\"","metadata":{"execution":{"iopub.status.busy":"2024-05-06T11:40:36.206742Z","iopub.execute_input":"2024-05-06T11:40:36.207071Z","iopub.status.idle":"2024-05-06T11:40:36.213398Z","shell.execute_reply.started":"2024-05-06T11:40:36.207044Z","shell.execute_reply":"2024-05-06T11:40:36.212538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ebird_txn_df = pd.read_csv(ebird_txn)","metadata":{"execution":{"iopub.status.busy":"2024-05-06T11:40:36.377637Z","iopub.execute_input":"2024-05-06T11:40:36.378516Z","iopub.status.idle":"2024-05-06T11:40:36.431037Z","shell.execute_reply.started":"2024-05-06T11:40:36.378479Z","shell.execute_reply":"2024-05-06T11:40:36.430211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ebird_txn_df.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-06T11:40:36.514575Z","iopub.execute_input":"2024-05-06T11:40:36.514934Z","iopub.status.idle":"2024-05-06T11:40:36.520626Z","shell.execute_reply.started":"2024-05-06T11:40:36.514904Z","shell.execute_reply":"2024-05-06T11:40:36.519722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ebird_txn_df.head(10)","metadata":{"execution":{"iopub.status.busy":"2024-05-06T11:40:36.655437Z","iopub.execute_input":"2024-05-06T11:40:36.655776Z","iopub.status.idle":"2024-05-06T11:40:36.669883Z","shell.execute_reply.started":"2024-05-06T11:40:36.655751Z","shell.execute_reply":"2024-05-06T11:40:36.668982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ebird_txn_keys = ebird_txn_df.keys()\nebird_txn_keys","metadata":{"execution":{"iopub.status.busy":"2024-05-06T12:18:00.964056Z","iopub.execute_input":"2024-05-06T12:18:00.964431Z","iopub.status.idle":"2024-05-06T12:18:00.970812Z","shell.execute_reply.started":"2024-05-06T12:18:00.964400Z","shell.execute_reply":"2024-05-06T12:18:00.969892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ebird_txn_df.nunique()","metadata":{"execution":{"iopub.status.busy":"2024-05-06T11:40:37.739645Z","iopub.execute_input":"2024-05-06T11:40:37.740501Z","iopub.status.idle":"2024-05-06T11:40:37.767382Z","shell.execute_reply.started":"2024-05-06T11:40:37.740467Z","shell.execute_reply":"2024-05-06T11:40:37.766529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_metadata_df = pd.read_csv(train_metadata)","metadata":{"execution":{"iopub.status.busy":"2024-05-06T11:40:38.135006Z","iopub.execute_input":"2024-05-06T11:40:38.135351Z","iopub.status.idle":"2024-05-06T11:40:38.236094Z","shell.execute_reply.started":"2024-05-06T11:40:38.135324Z","shell.execute_reply":"2024-05-06T11:40:38.235291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_metadata_df.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-06T11:40:38.694586Z","iopub.execute_input":"2024-05-06T11:40:38.695409Z","iopub.status.idle":"2024-05-06T11:40:38.700871Z","shell.execute_reply.started":"2024-05-06T11:40:38.695370Z","shell.execute_reply":"2024-05-06T11:40:38.699969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_metadata_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-06T11:40:39.302596Z","iopub.execute_input":"2024-05-06T11:40:39.303282Z","iopub.status.idle":"2024-05-06T11:40:39.318575Z","shell.execute_reply.started":"2024-05-06T11:40:39.303253Z","shell.execute_reply":"2024-05-06T11:40:39.317741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_metadata_df.keys()","metadata":{"execution":{"iopub.status.busy":"2024-05-06T11:40:39.517523Z","iopub.execute_input":"2024-05-06T11:40:39.518180Z","iopub.status.idle":"2024-05-06T11:40:39.524423Z","shell.execute_reply.started":"2024-05-06T11:40:39.518148Z","shell.execute_reply":"2024-05-06T11:40:39.523523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_metadata_df.nunique()","metadata":{"execution":{"iopub.status.busy":"2024-05-06T11:40:39.845284Z","iopub.execute_input":"2024-05-06T11:40:39.845981Z","iopub.status.idle":"2024-05-06T11:40:39.897792Z","shell.execute_reply.started":"2024-05-06T11:40:39.845951Z","shell.execute_reply":"2024-05-06T11:40:39.896511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_metadata_df['secondary_labels'].unique()","metadata":{"execution":{"iopub.status.busy":"2024-05-06T12:25:43.231602Z","iopub.execute_input":"2024-05-06T12:25:43.232002Z","iopub.status.idle":"2024-05-06T12:25:43.236071Z","shell.execute_reply.started":"2024-05-06T12:25:43.231971Z","shell.execute_reply":"2024-05-06T12:25:43.235117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ss_df = pd.read_csv(ss)\nss_df","metadata":{"execution":{"iopub.status.busy":"2024-05-06T12:04:29.032747Z","iopub.execute_input":"2024-05-06T12:04:29.033647Z","iopub.status.idle":"2024-05-06T12:04:29.067101Z","shell.execute_reply.started":"2024-05-06T12:04:29.033613Z","shell.execute_reply":"2024-05-06T12:04:29.066304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_audio_files = os.listdir(train_audio)","metadata":{"execution":{"iopub.status.busy":"2024-05-06T11:42:30.343701Z","iopub.execute_input":"2024-05-06T11:42:30.344088Z","iopub.status.idle":"2024-05-06T11:42:30.370353Z","shell.execute_reply.started":"2024-05-06T11:42:30.344059Z","shell.execute_reply":"2024-05-06T11:42:30.369592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_train_audio_files= len(train_audio_files)\nn_train_audio_files","metadata":{"execution":{"iopub.status.busy":"2024-05-06T11:43:45.619353Z","iopub.execute_input":"2024-05-06T11:43:45.619963Z","iopub.status.idle":"2024-05-06T11:43:45.625612Z","shell.execute_reply.started":"2024-05-06T11:43:45.619931Z","shell.execute_reply":"2024-05-06T11:43:45.624753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for file in train_audio_files[:5]:\n    file_ = train_audio+\"/\"+file\n    file_s = os.listdir(file_)\n    audio_file = file_+\"/\"+file_s[0]\n    y,sr = lbr.load(audio_file)\n    ipd.Audio(y,rate=sr)\n","metadata":{"execution":{"iopub.status.busy":"2024-05-06T11:59:23.507878Z","iopub.execute_input":"2024-05-06T11:59:23.508591Z","iopub.status.idle":"2024-05-06T11:59:24.069872Z","shell.execute_reply.started":"2024-05-06T11:59:23.508557Z","shell.execute_reply":"2024-05-06T11:59:24.068946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Play Sound Using Librosa","metadata":{}},{"cell_type":"code","source":"audio_file_1 = \"/kaggle/input/birdclef-2024/train_audio/asbfly/XC134896.ogg\"","metadata":{"execution":{"iopub.status.busy":"2024-05-06T13:10:48.317848Z","iopub.execute_input":"2024-05-06T13:10:48.318212Z","iopub.status.idle":"2024-05-06T13:10:48.322330Z","shell.execute_reply.started":"2024-05-06T13:10:48.318181Z","shell.execute_reply":"2024-05-06T13:10:48.321391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y,sr = lbr.load(audio_file_1)","metadata":{"execution":{"iopub.status.busy":"2024-05-06T13:10:50.512051Z","iopub.execute_input":"2024-05-06T13:10:50.512512Z","iopub.status.idle":"2024-05-06T13:10:50.563718Z","shell.execute_reply.started":"2024-05-06T13:10:50.512473Z","shell.execute_reply":"2024-05-06T13:10:50.562979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ipd.Audio(y,rate=sr)","metadata":{"execution":{"iopub.status.busy":"2024-05-06T13:10:55.122294Z","iopub.execute_input":"2024-05-06T13:10:55.122652Z","iopub.status.idle":"2024-05-06T13:10:55.151291Z","shell.execute_reply.started":"2024-05-06T13:10:55.122624Z","shell.execute_reply":"2024-05-06T13:10:55.150419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Frequency vs Amplitude Plot","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(10, 3))\nplt.plot(y, label=\"Audio Waveform\")\nplt.xlabel(\"Time (seconds)\")\nplt.ylabel(\"Amplitude\")\nplt.title(\"Waveform Plot of Bird Call\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-06T13:11:39.857604Z","iopub.execute_input":"2024-05-06T13:11:39.858274Z","iopub.status.idle":"2024-05-06T13:11:40.706979Z","shell.execute_reply.started":"2024-05-06T13:11:39.858244Z","shell.execute_reply":"2024-05-06T13:11:40.705326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Spectogram","metadata":{}},{"cell_type":"code","source":"S = librosa.stft(y)\ndb = librosa.power_to_db(S, ref=np.max(abs(S)))","metadata":{"execution":{"iopub.status.busy":"2024-05-06T13:11:43.830498Z","iopub.execute_input":"2024-05-06T13:11:43.831212Z","iopub.status.idle":"2024-05-06T13:11:43.865046Z","shell.execute_reply.started":"2024-05-06T13:11:43.831177Z","shell.execute_reply":"2024-05-06T13:11:43.864024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10, 5))\nlibrosa.display.specshow(db, sr=sr, x_axis=\"time\", y_axis=\"log\", cmap=\"inferno\")\nplt.colorbar(format=\"%+2.0f dB\")\nplt.title(\"Spectrogram of Bird Call\")\nplt.xlabel(\"Time (seconds)\")\nplt.ylabel(\"Frequency (Hz)\")\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-06T13:11:44.561032Z","iopub.execute_input":"2024-05-06T13:11:44.561617Z","iopub.status.idle":"2024-05-06T13:11:45.741441Z","shell.execute_reply.started":"2024-05-06T13:11:44.561584Z","shell.execute_reply":"2024-05-06T13:11:45.740512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# MFCCs","metadata":{}},{"cell_type":"code","source":"def extract_mfccs(audio_file):\n  y, sr = librosa.load(audio_file)\n  mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=20)\n  return mfccs","metadata":{"execution":{"iopub.status.busy":"2024-05-06T13:30:26.018416Z","iopub.execute_input":"2024-05-06T13:30:26.019221Z","iopub.status.idle":"2024-05-06T13:30:26.023722Z","shell.execute_reply.started":"2024-05-06T13:30:26.019189Z","shell.execute_reply":"2024-05-06T13:30:26.022742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mfccs = extract_mfccs(audio_file_1)","metadata":{"execution":{"iopub.status.busy":"2024-05-06T13:37:28.674628Z","iopub.execute_input":"2024-05-06T13:37:28.675015Z","iopub.status.idle":"2024-05-06T13:37:28.760125Z","shell.execute_reply.started":"2024-05-06T13:37:28.674985Z","shell.execute_reply":"2024-05-06T13:37:28.758677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mfccs ","metadata":{"execution":{"iopub.status.busy":"2024-05-06T13:37:39.906549Z","iopub.execute_input":"2024-05-06T13:37:39.906989Z","iopub.status.idle":"2024-05-06T13:37:39.914375Z","shell.execute_reply.started":"2024-05-06T13:37:39.906960Z","shell.execute_reply":"2024-05-06T13:37:39.913484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Spectral Features","metadata":{}},{"cell_type":"code","source":"def extract_spectral_features(audio_file):\n  # Load audio data\n  y, sr = librosa.load(audio_file)\n\n  # Extract spectral features\n  spectral_centroid = librosa.feature.spectral_centroid(y=y, sr=sr)\n  spectral_flux = librosa.feature.spectral_flux(y=y, sr=sr)\n  spectral_rolloff = librosa.feature.spectral_rolloff(y=y, sr=sr)\n  return spectral_centroid, spectral_flux, spectral_rolloff","metadata":{"execution":{"iopub.status.busy":"2024-05-06T13:32:13.960657Z","iopub.execute_input":"2024-05-06T13:32:13.961017Z","iopub.status.idle":"2024-05-06T13:32:13.966674Z","shell.execute_reply.started":"2024-05-06T13:32:13.960990Z","shell.execute_reply":"2024-05-06T13:32:13.965743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# mfccs","metadata":{"execution":{"iopub.status.busy":"2024-05-06T13:32:49.785208Z","iopub.execute_input":"2024-05-06T13:32:49.785557Z","iopub.status.idle":"2024-05-06T13:32:49.789741Z","shell.execute_reply.started":"2024-05-06T13:32:49.785531Z","shell.execute_reply":"2024-05-06T13:32:49.788769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plt.figure(figsize=(10, 3))\n# for i, mfcc in enumerate(mfccs):\n#     plt.plot(mfccs, label=f\"MFCC {i+1}\")\n# plt.xlabel(\"Time (frames)\")\n# plt.ylabel(\"MFCC Coefficient\")\n# plt.title(\"MFCCs of Bird Call\")\n# plt.legend()\n# plt.tight_layout()\n# plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-05-06T13:38:21.904587Z","iopub.execute_input":"2024-05-06T13:38:21.905246Z","iopub.status.idle":"2024-05-06T13:38:21.909596Z","shell.execute_reply.started":"2024-05-06T13:38:21.905210Z","shell.execute_reply":"2024-05-06T13:38:21.908541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Distribution of Audio Lengths ","metadata":{}},{"cell_type":"code","source":"audio_files = glob(\"/kaggle/input/birdclef-2024/train_audio/**/*.ogg\", recursive=True)","metadata":{"execution":{"iopub.status.busy":"2024-05-06T13:38:23.159374Z","iopub.execute_input":"2024-05-06T13:38:23.159718Z","iopub.status.idle":"2024-05-06T13:38:23.450210Z","shell.execute_reply.started":"2024-05-06T13:38:23.159678Z","shell.execute_reply":"2024-05-06T13:38:23.449478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"audio_lengths = [os.path.getsize(f) / sr for f in audio_files]\n","metadata":{"execution":{"iopub.status.busy":"2024-05-06T13:38:23.468694Z","iopub.execute_input":"2024-05-06T13:38:23.468999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# audio_lengths","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(audio_lengths)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8, 5))\nplt.hist(audio_lengths[:100], bins=20, edgecolor=\"black\") # for 100 files. \nplt.xlabel(\"Audio Length (seconds)\")\nplt.ylabel(\"Number of Audio Files\")\nplt.title(\"Distribution of Audio Lengths in Training Data\")\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}