{"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":"none","dataSources":[{"sourceId":70203,"databundleVersionId":8068726,"sourceType":"competition"}],"dockerImageVersionId":30673,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# THIS NOTEBOOK LOADS THE AUDIO AND CREATES THE DATALOADER FOR TRAINING","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-04-04T07:23:42.544220Z","iopub.execute_input":"2024-04-04T07:23:42.544667Z","iopub.status.idle":"2024-04-04T07:23:46.338805Z","shell.execute_reply.started":"2024-04-04T07:23:42.544627Z","shell.execute_reply":"2024-04-04T07:23:46.337401Z"}}},{"cell_type":"code","source":"import os\nimport librosa\nimport numpy as np\nfrom torch.utils.data import Dataset, DataLoader","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-04-04T07:25:54.375174Z","iopub.execute_input":"2024-04-04T07:25:54.375887Z","iopub.status.idle":"2024-04-04T07:25:54.381396Z","shell.execute_reply.started":"2024-04-04T07:25:54.375854Z","shell.execute_reply":"2024-04-04T07:25:54.380536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_path = '/kaggle/input/birdclef-2024/train_audio'\noutput_path = '/kaggle/working/train_audio_features'  # Output directory to save processed audio features\n\n# Function to load and process audio files\ndef load_and_process_audio(file_path, duration=240, sr=32000):\n    audio, _ = librosa.load(file_path, sr=sr, duration=duration)\n    mfccs = librosa.feature.mfcc(y=audio, sr=sr, n_mfcc=40)  #n_mfcc=40 is the number of features we need from the auido\n    return mfccs\n\n# Process and save audio features\nfor bird_class in os.listdir(dataset_path):\n    class_path = os.path.join(dataset_path, bird_class)\n    output_class_path = os.path.join(output_path, bird_class)\n    os.makedirs(output_class_path, exist_ok=True)\n    for file_name in os.listdir(class_path):\n        file_path = os.path.join(class_path, file_name)\n        output_file_path = os.path.join(output_class_path, file_name.replace('.ogg', '.npy'))\n        mfccs = load_and_process_audio(file_path)\n        np.save(output_file_path, mfccs)\n\nprint(\"Audio features saved successfully.\")","metadata":{"execution":{"iopub.status.busy":"2024-04-04T07:34:08.620944Z","iopub.execute_input":"2024-04-04T07:34:08.621586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# class AudioDataset(Dataset):\n#     def __init__(self, dataset_path, duration=240, sr=32000):\n#         self.dataset_path = dataset_path\n#         self.duration = duration\n#         self.sr = sr\n#         self.classes = os.listdir(dataset_path)\n#         self.class_to_index = {c: i for i, c in enumerate(self.classes)}\n#         self.index_to_class = {i: c for i, c in enumerate(self.classes)}\n#         self.data = self._load_data()\n\n#     def _load_data(self):\n#         data = []\n#         for bird_class in self.classes:\n#             class_path = os.path.join(self.dataset_path, bird_class)\n#             for file_name in os.listdir(class_path):\n#                 file_path = os.path.join(class_path, file_name)\n#                 mfccs = self._load_and_process_audio(file_path)\n#                 label = self.class_to_index[bird_class]\n#                 data.append((mfccs, label))\n#         return data\n\n#     def _load_and_process_audio(self, file_path):\n#         audio, _ = librosa.load(file_path, sr=self.sr, duration=self.duration)\n#         mfccs = librosa.feature.mfcc(y=audio, sr=self.sr, n_mfcc=40)\n#         return mfccs\n\n#     def __len__(self):\n#         return len(self.data)\n\n#     def __getitem__(self, idx):\n#         mfccs, label = self.data[idx]\n#         return mfccs, label","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-04-04T07:31:54.632309Z","iopub.execute_input":"2024-04-04T07:31:54.633434Z","iopub.status.idle":"2024-04-04T07:31:54.639647Z","shell.execute_reply.started":"2024-04-04T07:31:54.633395Z","shell.execute_reply":"2024-04-04T07:31:54.638522Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# dataset = AudioDataset(dataset_path='/kaggle/input/birdclef-2024/train_audio')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-04-04T07:31:45.552275Z","iopub.execute_input":"2024-04-04T07:31:45.552673Z","iopub.status.idle":"2024-04-04T07:31:45.557675Z","shell.execute_reply.started":"2024-04-04T07:31:45.552641Z","shell.execute_reply":"2024-04-04T07:31:45.556486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# next(iter(dataset))","metadata":{"execution":{"iopub.status.busy":"2024-04-04T07:31:47.499490Z","iopub.execute_input":"2024-04-04T07:31:47.500259Z","iopub.status.idle":"2024-04-04T07:31:47.504928Z","shell.execute_reply.started":"2024-04-04T07:31:47.500219Z","shell.execute_reply":"2024-04-04T07:31:47.503943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# dataloader = DataLoader(dataset, batch_size=32, shuffle=True)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-04-04T07:31:51.011398Z","iopub.execute_input":"2024-04-04T07:31:51.011805Z","iopub.status.idle":"2024-04-04T07:31:51.016588Z","shell.execute_reply.started":"2024-04-04T07:31:51.011777Z","shell.execute_reply":"2024-04-04T07:31:51.015322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}