{"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":30684,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-output":true,"_kg_hide-input":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow_io as tfio\nimport tensorflow as tf\nimport pandas as pd\nfrom pathlib import Path","metadata":{"execution":{"iopub.status.busy":"2024-04-12T07:00:42.999038Z","iopub.execute_input":"2024-04-12T07:00:42.999461Z","iopub.status.idle":"2024-04-12T07:00:57.708821Z","shell.execute_reply.started":"2024-04-12T07:00:42.999428Z","shell.execute_reply":"2024-04-12T07:00:57.707766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#training audio files\n\naudio_dict = dict()\n\naudio_path = '/kaggle/input/birdclef-2024/train_audio'\nbird_folders= Path(audio_path).glob(\"*\")\n\n# Iterate over files in directory\nfor bird_path in bird_folders:\n    \n    #bird_list is a generator\n    bird_list = Path(bird_path).glob('**/*.ogg')\n    audio_dict[bird_path.name] = [f for f in bird_list]\n    ","metadata":{"execution":{"iopub.status.busy":"2024-04-12T07:00:57.710672Z","iopub.execute_input":"2024-04-12T07:00:57.711224Z","iopub.status.idle":"2024-04-12T07:01:02.884684Z","shell.execute_reply.started":"2024-04-12T07:00:57.711197Z","shell.execute_reply":"2024-04-12T07:01:02.883133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\naudio = tfio.audio.AudioIOTensor(str(audio_dict['brakit1'][1]))\n#print(type(audio[:]))\naudio_tensor = tf.squeeze(audio[:], axis=[-1])\n\nposition = tfio.audio.trim(audio_tensor, axis=0, epsilon=0.1)\n\nstart = position[0]\nstop = position[1]\n\ntrimmed_tensor = audio_tensor[start:stop]\n#original window/nfft size 512\nspectrogram = tfio.audio.spectrogram(\n    trimmed_tensor, nfft=4096, window=4096, stride=512)\n\nplt.imshow(tf.math.log(spectrogram).numpy())","metadata":{"execution":{"iopub.status.busy":"2024-04-12T07:54:02.522184Z","iopub.execute_input":"2024-04-12T07:54:02.523805Z","iopub.status.idle":"2024-04-12T07:54:04.252502Z","shell.execute_reply.started":"2024-04-12T07:54:02.523763Z","shell.execute_reply":"2024-04-12T07:54:04.251357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Convert to mel-spectrogram\nmel_spectrogram = tfio.audio.melscale(\n    spectrogram, rate=32000, mels=128, fmin=0, fmax=2000)\n\n\nplt.figure(figsize = [3,100])\nplt.imshow(tf.math.log(mel_spectrogram).numpy())\n\n# Convert to db scale mel-spectrogram\ndbscale_mel_spectrogram = tfio.audio.dbscale(\n    mel_spectrogram, top_db=80)\n\nplt.figure()\nplt.imshow(dbscale_mel_spectrogram.numpy())","metadata":{"execution":{"iopub.status.busy":"2024-04-12T07:54:56.406163Z","iopub.execute_input":"2024-04-12T07:54:56.406561Z","iopub.status.idle":"2024-04-12T07:54:57.649984Z","shell.execute_reply.started":"2024-04-12T07:54:56.406530Z","shell.execute_reply":"2024-04-12T07:54:57.649110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\naudio = tfio.audio.AudioIOTensor(str(audio_dict['asbfly'][6]))\n#print(type(audio[:]))\naudio_tensor = tf.squeeze(audio[:], axis=[-1])\n\nposition = tfio.audio.trim(audio_tensor, axis=0, epsilon=0.1)\n\nstart = position[0]\nstop = position[1]\n\ntrimmed_tensor = audio_tensor[start:stop]\n#original window/nfft size 512\nspectrogram = tfio.audio.spectrogram(\n    trimmed_tensor, nfft=4096, window=4096, stride=512)\n\nplt.imshow(tf.math.log(spectrogram).numpy())","metadata":{"execution":{"iopub.status.busy":"2024-04-12T07:08:53.543580Z","iopub.execute_input":"2024-04-12T07:08:53.543975Z","iopub.status.idle":"2024-04-12T07:08:54.166434Z","shell.execute_reply.started":"2024-04-12T07:08:53.543946Z","shell.execute_reply":"2024-04-12T07:08:54.165274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import Audio\naudio = tfio.audio.AudioIOTensor(str(audio_dict['asbfly'][6]))\n#print(type(audio[:]))\naudio_tensor = tf.squeeze(audio[:], axis=[-1])\n\nAudio(audio_tensor.numpy(), rate=audio.rate.numpy())","metadata":{"execution":{"iopub.status.busy":"2024-04-12T07:22:16.667371Z","iopub.execute_input":"2024-04-12T07:22:16.668457Z","iopub.status.idle":"2024-04-12T07:22:16.825025Z","shell.execute_reply.started":"2024-04-12T07:22:16.668416Z","shell.execute_reply":"2024-04-12T07:22:16.823593Z"},"trusted":true},"execution_count":null,"outputs":[]}]}