{"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"},{"sourceId":37123,"sourceType":"modelInstanceVersion","modelInstanceId":31246}],"dockerImageVersionId":30684,"isInternetEnabled":false,"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\nfrom keras.models import Model,Sequential, load_model\n#for 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","execution":{"iopub.status.busy":"2024-04-28T22:59:43.595300Z","iopub.execute_input":"2024-04-28T22:59:43.595699Z","iopub.status.idle":"2024-04-28T22:59:59.352840Z","shell.execute_reply.started":"2024-04-28T22:59:43.595667Z","shell.execute_reply":"2024-04-28T22:59:59.351464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_metadata = pd.read_csv('/kaggle/input/birdclef-2024/train_metadata.csv')\ntrain_metadata['file_ID'] = train_metadata['filename'].str.split('/',expand=True)[1]\ntrain_metadata.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-28T23:00:02.029027Z","iopub.execute_input":"2024-04-28T23:00:02.030436Z","iopub.status.idle":"2024-04-28T23:00:02.513929Z","shell.execute_reply.started":"2024-04-28T23:00:02.030389Z","shell.execute_reply":"2024-04-28T23:00:02.512760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create Class for Handling Audio Files","metadata":{}},{"cell_type":"code","source":"import librosa\nimport random\n\nclass AudioUtil():\n    # ----------------------------\n    # Load an audio file. Return the signal as a tensor and the sample rate\n    # ----------------------------\n    @staticmethod\n    def open(filepath):\n        data, sample_rate = librosa.load(filepath)\n        return (data, sample_rate)\n    \n    # ----------------------------\n    # Resample the audio file to the specified rate\n    # ----------------------------\n    @staticmethod\n    def resample(aud, new_sample_rate):\n        data, sample_rate = aud\n    \n        if (sample_rate == new_sample_rate):\n          # Nothing to do\n          return aud\n\n        resampled = librosa.resample(data, orig_sr=sample_rate, target_sr=new_sample_rate)\n    \n        return ((resampled, new_sample_rate))\n\n    # ----------------------------\n    # Pad (or truncate) the signal to a fixed length 'max_ms' in milliseconds\n    # ----------------------------\n    @staticmethod\n    def pad_trunc(aud, start, max_ms):\n        data, sample_rate = aud\n        data = data[start*duration:(start+1)*duration]\n        sig_len = data.shape[0]\n        max_len = sample_rate // 1000 * max_ms\n\n        if (sig_len > max_len):\n            # Truncate the signal to the given length\n            data = data[:max_len]\n\n        elif (sig_len < max_len):\n            # Length of padding to add at the beginning and end of the signal\n            pad_begin_len = random.randint(0, max_len - sig_len)\n            pad_end_len = max_len - sig_len - pad_begin_len\n\n            # Pad with 0s\n            pad_begin = np.zeros(pad_begin_len)\n            pad_end = np.zeros(pad_end_len)\n\n            data = np.concatenate((pad_begin, data, pad_end))\n        \n        return (data, sample_rate)\n    \n    # ----------------------------\n    # Generate a mel spectrogram\n    # ----------------------------\n    @staticmethod\n    def spectro_gram(aud, n_mels=128, n_fft=1024, hop_len=None):\n        data,sample_rate = aud\n        \n        S = librosa.feature.melspectrogram(y=data, sr=sample_rate, n_fft=n_fft, hop_length=hop_len, n_mels=n_mels)\n        S_dB = librosa.power_to_db(S, ref=np.max)\n\n        return (S_dB)\n    \n    # ----------------------------\n    # Normalize the spectrogram\n    # ----------------------------\n    @staticmethod\n    def normalize_sgram(spec):        \n        # Normalize 0-min\n        spec = spec - spec.min()\n        # Normalize 0-255\n        spec = (spec / spec.max() * 255).astype(np.uint8)\n        \n        #inputs_max, inputs_min = aud.max(), aud.min()\n        #norm_sgram = (aud - inputs_min) / (inputs_max - inputs_min)        \n        \n        return (np.reshape(spec,(128,626,1)))","metadata":{"execution":{"iopub.status.busy":"2024-04-28T23:00:05.986593Z","iopub.execute_input":"2024-04-28T23:00:05.987054Z","iopub.status.idle":"2024-04-28T23:00:06.013236Z","shell.execute_reply.started":"2024-04-28T23:00:05.987014Z","shell.execute_reply":"2024-04-28T23:00:06.012051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model","metadata":{}},{"cell_type":"code","source":"import shutil\n\nsource_dir = '/kaggle/input/birdclef-2024-model1/tensorflow2/model1/1'\n\n# Destination directory\ndestination_dir = '/kaggle/working/model/'\n\nshutil.copytree(source_dir, destination_dir)\n\n# load saved model\nmodel = load_model('/kaggle/working/model/best_model.keras',compile=True)","metadata":{"execution":{"iopub.status.busy":"2024-04-28T23:00:10.638824Z","iopub.execute_input":"2024-04-28T23:00:10.639254Z","iopub.status.idle":"2024-04-28T23:00:11.267056Z","shell.execute_reply.started":"2024-04-28T23:00:10.639219Z","shell.execute_reply":"2024-04-28T23:00:11.265898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-04-28T23:00:14.487119Z","iopub.execute_input":"2024-04-28T23:00:14.488030Z","iopub.status.idle":"2024-04-28T23:00:14.526133Z","shell.execute_reply.started":"2024-04-28T23:00:14.487990Z","shell.execute_reply":"2024-04-28T23:00:14.524946Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#from tqdm.notebook import tqdm\nimport glob\nimport re\nimport cv2\nimport matplotlib.pyplot as plt\n\nsample_rate = 32000      # standardize the sampling rate\nduration = 5000          # duration of snapshot to keep in milliseconds\n\n# dictionary to save inference clips in \nINFERENCE_ROWS = {}\n#specs = {}\n\n# Hidden test files\n#if len(glob.glob(f'/kaggle/input/birdclef-2024/test_soundscapes/*.ogg')) > 0:\nogg_file_paths = glob.glob(f'/kaggle/input/birdclef-2024/test_soundscapes/*.ogg')\n   # print('Number of test soundscapes:', len(ogg_file_paths))\n#else:\n#ogg_file_paths = sorted(glob.glob(f'/kaggle/input/birdclef-2024/unlabeled_soundscapes/*.ogg'))[:10]\n\n# Iterate over OGG files\nfor i, file_path in enumerate(ogg_file_paths):\n    # Extract filename\n    row_id = re.search(r'/([^/]+)\\.ogg$', file_path).group(1)\n    \n    #print(file_path)\n    \n    # Read OGG file and convert to melspectrogram\n    aud = AudioUtil.open(file_path)\n    reaud = AudioUtil.resample(aud, sample_rate)\n    \n    for i in range(int(240/5)):\n        dur_aud = AudioUtil.pad_trunc(reaud, i, duration)\n        sgram = AudioUtil.spectro_gram(dur_aud, n_mels=128, n_fft=1024, hop_len=None)            \n\n        #data, sample_rate = librosa.load(file_path)\n        #plt.figure(figsize=(12, 5))\n        #librosa.display.waveshow(data, sr=sample_rate)\n\n        # normalize spectrogram\n        norm_sgram = AudioUtil.normalize_sgram(sgram)\n\n        #print(norm_sgram.shape)\n        # Predict\n        #print(model.predict(np.reshape(norm_sgram,(1,128,626,1))))    \n\n        INFERENCE_ROWS[f'{row_id}_{(i+1)*5}']= model.predict(np.reshape(norm_sgram,(1,128,626,1)))\n        #specs[f'soundscape_{row_id}_{(i+1)*5}'] = norm_sgram\n        \n        \n        # Add to inference rows and limit to 4 minutes\n        #for t, o in zip(range(CONFIG.N_TEST_CHUNKS), outputs):\n            # Predictions for each bird\n        #    predictions = dict([ (l,p) for l, p in zip(CONFIG.LABELS, o) ])\n        #    # Append to inference rows\n        #    INFERENCE_ROWS.append(\n        #        { 'row_id': f'{row_id}_{(t+1)*5}' } | predictions\n        #    )","metadata":{"execution":{"iopub.status.busy":"2024-04-28T23:35:31.321680Z","iopub.execute_input":"2024-04-28T23:35:31.322097Z","iopub.status.idle":"2024-04-28T23:37:33.221418Z","shell.execute_reply.started":"2024-04-28T23:35:31.322065Z","shell.execute_reply":"2024-04-28T23:37:33.220253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission = pd.read_csv('/kaggle/input/birdclef-2024/sample_submission.csv')\n# Set labels\nLABELS = sample_submission.columns[1:]\n\npredictions = []\nfor j in INFERENCE_ROWS:\n    predictions.append(INFERENCE_ROWS[j].flatten())\n    \nsubmission_df = pd.DataFrame(predictions, columns=LABELS)\nsubmission_df.insert(0,'row_id',INFERENCE_ROWS.keys())","metadata":{"execution":{"iopub.status.busy":"2024-04-28T23:39:31.836725Z","iopub.execute_input":"2024-04-28T23:39:31.837125Z","iopub.status.idle":"2024-04-28T23:39:31.913041Z","shell.execute_reply.started":"2024-04-28T23:39:31.837094Z","shell.execute_reply":"2024-04-28T23:39:31.911855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-28T23:39:34.531717Z","iopub.execute_input":"2024-04-28T23:39:34.532155Z","iopub.status.idle":"2024-04-28T23:39:34.572300Z","shell.execute_reply.started":"2024-04-28T23:39:34.532116Z","shell.execute_reply":"2024-04-28T23:39:34.571098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission","metadata":{}},{"cell_type":"code","source":"# Saving the updated subs DataFrame to a CSV file\nsubmission_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-04-28T23:31:21.209869Z","iopub.execute_input":"2024-04-28T23:31:21.210630Z","iopub.status.idle":"2024-04-28T23:31:21.339627Z","shell.execute_reply.started":"2024-04-28T23:31:21.210577Z","shell.execute_reply":"2024-04-28T23:31:21.338430Z"},"trusted":true},"execution_count":null,"outputs":[]}]}