{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":91844,"databundleVersionId":11361821,"sourceType":"competition"},{"sourceId":11663225,"sourceType":"datasetVersion","datasetId":7319585}],"dockerImageVersionId":31040,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import tensorflow_probability as tfp\nimport librosa\nimport numpy as np\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nfrom tf_keras import layers\nfrom tf_keras import Model\nfrom tensorflow import keras\nimport os\nimport pandas as pd\n\ntfd = tfp.distributions\ntfpl = tfp.layers","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-05-15T10:11:37.819640Z","iopub.execute_input":"2025-05-15T10:11:37.819955Z","iopub.status.idle":"2025-05-15T10:11:37.826421Z","shell.execute_reply.started":"2025-05-15T10:11:37.819932Z","shell.execute_reply":"2025-05-15T10:11:37.825203Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class CFG:\n    n_mels = 256\n    sr = 32000\n    lr = 1e-3\n    duration = 5  # seconds\n    samples = sr * duration\n    dir_path = '/kaggle/input/birdclefprocessed/train_audio'\n    label_path = '/kaggle/input/birdclef-2025/train.csv'\n    tax_path = '/kaggle/input/birdclef-2025/taxonomy.csv'\n    train_path = '/kaggle/input/birdclef-2025/train_audio'\n    sample_path = '/kaggle/input/birdclef-2025/sample_submission.csv'\n    test_path = '/kaggle/input/birdclef-2025/test_soundscapes'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-15T10:13:18.888609Z","iopub.execute_input":"2025-05-15T10:13:18.888937Z","iopub.status.idle":"2025-05-15T10:13:18.894264Z","shell.execute_reply.started":"2025-05-15T10:13:18.888912Z","shell.execute_reply":"2025-05-15T10:13:18.893265Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def load_audio(audio_path):\n    y, sr = librosa.load(audio_path, sr=CFG.sr)\n    # Convert to mel spectrogram\n    mel_spec = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=CFG.n_mels)\n    log_mel_spec = librosa.power_to_db(mel_spec)\n    return log_mel_spec.T","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-15T10:10:54.085073Z","iopub.execute_input":"2025-05-15T10:10:54.085393Z","iopub.status.idle":"2025-05-15T10:10:54.091333Z","shell.execute_reply.started":"2025-05-15T10:10:54.085372Z","shell.execute_reply":"2025-05-15T10:10:54.089889Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def load_and_preprocess_audio(filename):\n    # Load audio file\n    filepath = os.path.join(CFG.dir_path, filename)\n    audio, _ = librosa.load(filepath, sr=CFG.sr, duration=CFG.duration)\n    \n    # Pad or truncate to fixed length\n    if len(audio) < CFG.samples:\n        audio = np.pad(audio, (0, CFG.samples - len(audio)))\n    else:\n        audio = audio[:CFG.samples]\n    \n    # Mel spectrogram\n    mel_spec = librosa.feature.melspectrogram(y=audio, sr=CFG.sr, n_mels=CFG.n_mels)\n    log_mel_spec = librosa.power_to_db(mel_spec)\n    \n    # Normalize to 0-1\n    log_mel_spec -= log_mel_spec.min()\n    log_mel_spec /= log_mel_spec.max()\n    \n    return log_mel_spec.T.astype(np.float32)  # shape: (time, n_mels)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-15T10:10:56.509830Z","iopub.execute_input":"2025-05-15T10:10:56.510168Z","iopub.status.idle":"2025-05-15T10:10:56.516436Z","shell.execute_reply.started":"2025-05-15T10:10:56.510143Z","shell.execute_reply":"2025-05-15T10:10:56.515398Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.read_csv(CFG.label_path)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-15T10:13:30.146223Z","iopub.execute_input":"2025-05-15T10:13:30.146582Z","iopub.status.idle":"2025-05-15T10:13:30.349174Z","shell.execute_reply.started":"2025-05-15T10:13:30.146557Z","shell.execute_reply":"2025-05-15T10:13:30.348073Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.head(2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-15T10:13:35.027907Z","iopub.execute_input":"2025-05-15T10:13:35.028283Z","iopub.status.idle":"2025-05-15T10:13:35.042866Z","shell.execute_reply.started":"2025-05-15T10:13:35.028258Z","shell.execute_reply":"2025-05-15T10:13:35.041785Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def gen():\n    for _, row in df.iterrows():\n        mel_spec = load_and_preprocess_audio(row['filename'])\n        label = row['label_idx']\n        yield mel_spec, label\n\noutput_signature = (\n    tf.TensorSpec(shape=(None, CFG.n_mels), dtype=tf.float32),\n    tf.TensorSpec(shape=(), dtype=tf.int32)\n)\n\ndataset = tf.data.Dataset.from_generator(gen, output_signature=output_signature)\n\n# Pad to fixed length (time dimension)\nMAX_TIME = 313  # Depends on mel spec parameters, ~5 seconds\ndef pad_to_max_time(mel_spec, label):\n    mel_spec = mel_spec[:MAX_TIME, :]\n    mel_spec = tf.pad(mel_spec, [[0, MAX_TIME - tf.shape(mel_spec)[0]], [0,0]])\n    return mel_spec, label\n\ndataset = dataset.map(pad_to_max_time)\ndataset = dataset.shuffle(1000).batch(16).prefetch(tf.data.AUTOTUNE)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def nll(y_true, y_pred):\n    return -y_pred.log_prob(y_true)\n\ndef build_model():\n    inputs = layers.Input(shape=(None, CFG.n_mels))\n    x1 = tfp.layers.DenseFlipout(128, activation='relu')(inputs)\n    x2 = tfp.layers.DenseFlipout(64, activation='relu')(x1)\n    x3 = tfp.layers.DenseFlipout(206)(x2) #206 sound classes\n    outputs = tfp.layers.DistributionLambda(lambda t: tfd.Independent(\n        tfd.Bernoulli(logits=t), reinterpreted_batch_ndims=1))(x3)\n    model = Model(inputs=inputs, outputs=outputs)\n    return model","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-15T10:10:56.660126Z","iopub.execute_input":"2025-05-15T10:10:56.660459Z","iopub.status.idle":"2025-05-15T10:10:56.667037Z","shell.execute_reply.started":"2025-05-15T10:10:56.660436Z","shell.execute_reply":"2025-05-15T10:10:56.666115Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#model = build_model()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-15T10:10:56.813222Z","iopub.execute_input":"2025-05-15T10:10:56.813543Z","iopub.status.idle":"2025-05-15T10:10:56.818216Z","shell.execute_reply.started":"2025-05-15T10:10:56.813522Z","shell.execute_reply":"2025-05-15T10:10:56.817218Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"file_path = '/kaggle/input/birdclefprocessed/train_audio/CSA03598.wav'","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-15T10:10:57.035457Z","iopub.execute_input":"2025-05-15T10:10:57.036242Z","iopub.status.idle":"2025-05-15T10:10:57.040481Z","shell.execute_reply.started":"2025-05-15T10:10:57.036166Z","shell.execute_reply":"2025-05-15T10:10:57.039363Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# x = load_audio(file_path)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-15T10:10:58.389516Z","iopub.execute_input":"2025-05-15T10:10:58.389792Z","iopub.status.idle":"2025-05-15T10:10:58.394251Z","shell.execute_reply.started":"2025-05-15T10:10:58.389773Z","shell.execute_reply":"2025-05-15T10:10:58.393121Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"o = load_and_preprocess_audio(file_path)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-15T10:10:58.529541Z","iopub.execute_input":"2025-05-15T10:10:58.529877Z","iopub.status.idle":"2025-05-15T10:10:58.562401Z","shell.execute_reply.started":"2025-05-15T10:10:58.529840Z","shell.execute_reply":"2025-05-15T10:10:58.561522Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"o.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-15T10:10:58.761393Z","iopub.execute_input":"2025-05-15T10:10:58.761703Z","iopub.status.idle":"2025-05-15T10:10:58.768191Z","shell.execute_reply.started":"2025-05-15T10:10:58.761681Z","shell.execute_reply":"2025-05-15T10:10:58.766912Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}