{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","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"}],"dockerImageVersionId":30787,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# import os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-10-23T20:22:23.718711Z","iopub.execute_input":"2024-10-23T20:22:23.718995Z","iopub.status.idle":"2024-10-23T20:22:24.701815Z","shell.execute_reply.started":"2024-10-23T20:22:23.718963Z","shell.execute_reply":"2024-10-23T20:22:24.701076Z"},"trusted":true},"execution_count":1,"outputs":[]},{"cell_type":"code","source":"import librosa\nimport torch\nimport torchaudio\nfrom transformers import Wav2Vec2Processor, Wav2Vec2Model\n\n# Load a bird call file from BirdCLEF dataset\nfile_path = '/kaggle/input/birdclef-2024/train_audio/litegr/XC717214.ogg'\naudio, sample_rate = librosa.load(file_path, sr=16000)  # Wav2Vec 2.0 works with 16kHz\n\n# If required, adjust the length of the audio or apply transformations like padding or trimming\n","metadata":{"execution":{"iopub.status.busy":"2024-10-23T20:22:28.490335Z","iopub.execute_input":"2024-10-23T20:22:28.491288Z","iopub.status.idle":"2024-10-23T20:22:49.230328Z","shell.execute_reply.started":"2024-10-23T20:22:28.491236Z","shell.execute_reply":"2024-10-23T20:22:49.22954Z"},"trusted":true},"execution_count":2,"outputs":[]},{"cell_type":"code","source":"from transformers import Wav2Vec2Processor, Wav2Vec2Model\n\n# Load Wav2Vec 2.0 processor and model\nprocessor = Wav2Vec2Processor.from_pretrained(\"facebook/wav2vec2-base\")\nmodel = Wav2Vec2Model.from_pretrained(\"facebook/wav2vec2-base\")\n\n# Convert audio to input features (Wav2Vec 2.0 accepts raw waveforms)\ninputs = processor(audio, sampling_rate=16000, return_tensors=\"pt\", padding=True)\n\n# Extract embeddings from the model\nwith torch.no_grad():\n    embeddings = model(**inputs).last_hidden_state\n","metadata":{"execution":{"iopub.status.busy":"2024-10-23T20:23:34.942325Z","iopub.execute_input":"2024-10-23T20:23:34.943168Z","iopub.status.idle":"2024-10-23T20:23:54.173202Z","shell.execute_reply.started":"2024-10-23T20:23:34.943129Z","shell.execute_reply":"2024-10-23T20:23:54.172275Z"},"trusted":true},"execution_count":3,"outputs":[{"output_type":"display_data","data":{"text/plain":"preprocessor_config.json:   0%|          | 0.00/159 [00:00<?, ?B/s]","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"8001b08e6f824ce1a0090a83b19c8bb0"}},"metadata":{}},{"output_type":"display_data","data":{"text/plain":"tokenizer_config.json:   0%|          | 0.00/163 [00:00<?, ?B/s]","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"52743907cb7c4a82813ce402bd8ace5e"}},"metadata":{}},{"output_type":"display_data","data":{"text/plain":"config.json:   0%|          | 0.00/1.84k [00:00<?, ?B/s]","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"f2053e0d61594c2db5c51cf3ab949da7"}},"metadata":{}},{"name":"stderr","text":"/opt/conda/lib/python3.10/site-packages/transformers/configuration_utils.py:302: UserWarning: Passing `gradient_checkpointing` to a config initialization is deprecated and will be removed in v5 Transformers. Using `model.gradient_checkpointing_enable()` instead, or if you are using the `Trainer` API, pass `gradient_checkpointing=True` in your `TrainingArguments`.\n  warnings.warn(\n","output_type":"stream"},{"output_type":"display_data","data":{"text/plain":"vocab.json:   0%|          | 0.00/291 [00:00<?, ?B/s]","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"5394ec4f83624645bb6261948a38f4a5"}},"metadata":{}},{"output_type":"display_data","data":{"text/plain":"special_tokens_map.json:   0%|          | 0.00/85.0 [00:00<?, ?B/s]","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"4d7818e7360c495ca0bc54a3f7764f77"}},"metadata":{}},{"name":"stderr","text":"/opt/conda/lib/python3.10/site-packages/transformers/tokenization_utils_base.py:1617: FutureWarning: `clean_up_tokenization_spaces` was not set. It will be set to `True` by default. This behavior will be deprecated in transformers v4.45, and will be then set to `False` by default. For more details check this issue: https://github.com/huggingface/transformers/issues/31884\n  warnings.warn(\n","output_type":"stream"},{"output_type":"display_data","data":{"text/plain":"pytorch_model.bin:   0%|          | 0.00/380M [00:00<?, ?B/s]","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"ac0f996550ae4a9eab14e091864f55f7"}},"metadata":{}}]},{"cell_type":"code","source":"print(embeddings)","metadata":{"execution":{"iopub.status.busy":"2024-10-23T20:23:54.175266Z","iopub.execute_input":"2024-10-23T20:23:54.176236Z","iopub.status.idle":"2024-10-23T20:23:54.202721Z","shell.execute_reply.started":"2024-10-23T20:23:54.17619Z","shell.execute_reply":"2024-10-23T20:23:54.201844Z"},"trusted":true},"execution_count":4,"outputs":[{"name":"stdout","text":"tensor([[[ 0.5543, -0.2871,  0.3216,  ...,  0.4398,  0.2234,  0.1359],\n         [ 0.5226, -0.3532,  0.1885,  ...,  0.3382,  0.1059,  0.1969],\n         [ 0.5657, -0.3184,  0.0413,  ...,  0.3740,  0.0838,  0.1221],\n         ...,\n         [ 0.5921, -0.3863,  0.2412,  ...,  0.4432,  0.1068, -0.0556],\n         [ 0.5565, -0.3721,  0.2890,  ...,  0.4398,  0.0267, -0.0023],\n         [ 0.6150, -0.3244,  0.3757,  ...,  0.3425,  0.1494,  0.0295]]])\n","output_type":"stream"}]},{"cell_type":"code","source":"df = pd.read_csv('../input/birdclef-2024/train_metadata.csv')\ndf.shape","metadata":{"execution":{"iopub.status.busy":"2024-10-23T20:23:54.20402Z","iopub.execute_input":"2024-10-23T20:23:54.205062Z","iopub.status.idle":"2024-10-23T20:23:55.76961Z","shell.execute_reply.started":"2024-10-23T20:23:54.204996Z","shell.execute_reply":"2024-10-23T20:23:55.768695Z"},"trusted":true},"execution_count":5,"outputs":[{"execution_count":5,"output_type":"execute_result","data":{"text/plain":"(24459, 12)"},"metadata":{}}]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-10-23T20:23:55.771352Z","iopub.execute_input":"2024-10-23T20:23:55.771663Z","iopub.status.idle":"2024-10-23T20:23:55.795709Z","shell.execute_reply.started":"2024-10-23T20:23:55.771629Z","shell.execute_reply":"2024-10-23T20:23:55.794807Z"},"trusted":true},"execution_count":6,"outputs":[{"execution_count":6,"output_type":"execute_result","data":{"text/plain":"  primary_label secondary_labels      type  latitude  longitude  \\\n0        asbfly               []  ['call']   39.2297   118.1987   \n1        asbfly               []  ['song']   51.4030   104.6401   \n2        asbfly               []  ['song']   36.3319   127.3555   \n3        asbfly               []  ['call']   21.1697    70.6005   \n4        asbfly               []  ['call']   15.5442    73.7733   \n\n      scientific_name             common_name  \\\n0  Muscicapa dauurica  Asian Brown Flycatcher   \n1  Muscicapa dauurica  Asian Brown Flycatcher   \n2  Muscicapa dauurica  Asian Brown Flycatcher   \n3  Muscicapa dauurica  Asian Brown Flycatcher   \n4  Muscicapa dauurica  Asian Brown Flycatcher   \n\n                              author  \\\n0                     Matt Slaymaker   \n1                   Magnus Hellström   \n2                      Stuart Fisher   \n3                          vir joshi   \n4  Albert Lastukhin & Sergei Karpeev   \n\n                                             license  rating  \\\n0  Creative Commons Attribution-NonCommercial-Sha...     5.0   \n1  Creative Commons Attribution-NonCommercial-Sha...     2.5   \n2  Creative Commons Attribution-NonCommercial-Sha...     2.5   \n3  Creative Commons Attribution-NonCommercial-Sha...     4.0   \n4  Creative Commons Attribution-NonCommercial-Sha...     4.0   \n\n                                 url             filename  \n0  https://www.xeno-canto.org/134896  asbfly/XC134896.ogg  \n1  https://www.xeno-canto.org/164848  asbfly/XC164848.ogg  \n2  https://www.xeno-canto.org/175797  asbfly/XC175797.ogg  \n3  https://www.xeno-canto.org/207738  asbfly/XC207738.ogg  \n4  https://www.xeno-canto.org/209218  asbfly/XC209218.ogg  ","text/html":"<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>primary_label</th>\n      <th>secondary_labels</th>\n      <th>type</th>\n      <th>latitude</th>\n      <th>longitude</th>\n      <th>scientific_name</th>\n      <th>common_name</th>\n      <th>author</th>\n      <th>license</th>\n      <th>rating</th>\n      <th>url</th>\n      <th>filename</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>asbfly</td>\n      <td>[]</td>\n      <td>['call']</td>\n      <td>39.2297</td>\n      <td>118.1987</td>\n      <td>Muscicapa dauurica</td>\n      <td>Asian Brown Flycatcher</td>\n      <td>Matt Slaymaker</td>\n      <td>Creative Commons Attribution-NonCommercial-Sha...</td>\n      <td>5.0</td>\n      <td>https://www.xeno-canto.org/134896</td>\n      <td>asbfly/XC134896.ogg</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>asbfly</td>\n      <td>[]</td>\n      <td>['song']</td>\n      <td>51.4030</td>\n      <td>104.6401</td>\n      <td>Muscicapa dauurica</td>\n      <td>Asian Brown Flycatcher</td>\n      <td>Magnus Hellström</td>\n      <td>Creative Commons Attribution-NonCommercial-Sha...</td>\n      <td>2.5</td>\n      <td>https://www.xeno-canto.org/164848</td>\n      <td>asbfly/XC164848.ogg</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>asbfly</td>\n      <td>[]</td>\n      <td>['song']</td>\n      <td>36.3319</td>\n      <td>127.3555</td>\n      <td>Muscicapa dauurica</td>\n      <td>Asian Brown Flycatcher</td>\n      <td>Stuart Fisher</td>\n      <td>Creative Commons Attribution-NonCommercial-Sha...</td>\n      <td>2.5</td>\n      <td>https://www.xeno-canto.org/175797</td>\n      <td>asbfly/XC175797.ogg</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>asbfly</td>\n      <td>[]</td>\n      <td>['call']</td>\n      <td>21.1697</td>\n      <td>70.6005</td>\n      <td>Muscicapa dauurica</td>\n      <td>Asian Brown Flycatcher</td>\n      <td>vir joshi</td>\n      <td>Creative Commons Attribution-NonCommercial-Sha...</td>\n      <td>4.0</td>\n      <td>https://www.xeno-canto.org/207738</td>\n      <td>asbfly/XC207738.ogg</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>asbfly</td>\n      <td>[]</td>\n      <td>['call']</td>\n      <td>15.5442</td>\n      <td>73.7733</td>\n      <td>Muscicapa dauurica</td>\n      <td>Asian Brown Flycatcher</td>\n      <td>Albert Lastukhin &amp; Sergei Karpeev</td>\n      <td>Creative Commons Attribution-NonCommercial-Sha...</td>\n      <td>4.0</td>\n      <td>https://www.xeno-canto.org/209218</td>\n      <td>asbfly/XC209218.ogg</td>\n    </tr>\n  </tbody>\n</table>\n</div>"},"metadata":{}}]},{"cell_type":"code","source":"# Step 1: Extract the primary label column (species codes)\nprimary_labels = df['primary_label']\n\n# Step 2: Assign a unique integer to each unique species code\nunique_species = primary_labels.unique()\nspecies_to_label = {species: idx for idx, species in enumerate(unique_species)}\n\n# Step 3: Create a new column with the numerical labels\ndf['label'] = df['primary_label'].map(species_to_label)\n\n# Display the DataFrame with the new labels\nprint(df[['filename', 'primary_label', 'label']])","metadata":{"execution":{"iopub.status.busy":"2024-10-23T20:23:55.796721Z","iopub.execute_input":"2024-10-23T20:23:55.796992Z","iopub.status.idle":"2024-10-23T20:23:55.820599Z","shell.execute_reply.started":"2024-10-23T20:23:55.796962Z","shell.execute_reply":"2024-10-23T20:23:55.819731Z"},"trusted":true},"execution_count":7,"outputs":[{"name":"stdout","text":"                   filename primary_label  label\n0       asbfly/XC134896.ogg        asbfly      0\n1       asbfly/XC164848.ogg        asbfly      0\n2       asbfly/XC175797.ogg        asbfly      0\n3       asbfly/XC207738.ogg        asbfly      0\n4       asbfly/XC209218.ogg        asbfly      0\n...                     ...           ...    ...\n24454  zitcis1/XC845747.ogg       zitcis1    181\n24455  zitcis1/XC845817.ogg       zitcis1    181\n24456  zitcis1/XC856176.ogg       zitcis1    181\n24457  zitcis1/XC856723.ogg       zitcis1    181\n24458  zitcis1/XC858550.ogg       zitcis1    181\n\n[24459 rows x 3 columns]\n","output_type":"stream"}]},{"cell_type":"code","source":"from pathlib import Path\n\nclass Config:\n    sampling_rate = 32000\n    duration = 5\n    fmin = 0\n    fmax = None\n    audios_path = Path(\"../input/birdclef-2024/train_audio\")\n    out_dir_train = Path(\"specs/train\")\n    out_dir_valid = Path(\"specs/valid\")","metadata":{"execution":{"iopub.status.busy":"2024-10-21T18:34:46.49248Z","iopub.execute_input":"2024-10-21T18:34:46.493129Z","iopub.status.idle":"2024-10-21T18:34:46.498184Z","shell.execute_reply.started":"2024-10-21T18:34:46.493089Z","shell.execute_reply":"2024-10-21T18:34:46.497166Z"},"trusted":true},"execution_count":11,"outputs":[]},{"cell_type":"code","source":"Config.out_dir_train.mkdir(exist_ok=True, parents=True)\nConfig.out_dir_valid.mkdir(exist_ok=True, parents=True)\n\nimport soundfile as sf\nfrom  soundfile import SoundFile\ndef get_audio_info(filepath):\n    \"\"\"Get some properties from  an audio file\"\"\"\n    with SoundFile(filepath) as f:\n        sr = f.samplerate\n        frames = f.frames\n        duration = float(frames)/sr\n    return {\"frames\": frames, \"sr\": sr, \"duration\": duration}","metadata":{"execution":{"iopub.status.busy":"2024-10-21T18:34:49.666948Z","iopub.execute_input":"2024-10-21T18:34:49.667328Z","iopub.status.idle":"2024-10-21T18:34:49.674401Z","shell.execute_reply.started":"2024-10-21T18:34:49.667293Z","shell.execute_reply":"2024-10-21T18:34:49.673191Z"},"trusted":true},"execution_count":12,"outputs":[]},{"cell_type":"code","source":"import re\n\ndf['secondary_labels'] = df['secondary_labels'].apply(lambda x: re.findall(r\"'(\\w+)'\", x))\ndf['len_sec_labels'] = df['secondary_labels'].map(len)\ndf[df.len_sec_labels>0].sample(3)","metadata":{"execution":{"iopub.status.busy":"2024-10-21T18:35:01.255145Z","iopub.execute_input":"2024-10-21T18:35:01.255994Z","iopub.status.idle":"2024-10-21T18:35:01.329616Z","shell.execute_reply.started":"2024-10-21T18:35:01.25595Z","shell.execute_reply":"2024-10-21T18:35:01.328405Z"},"trusted":true},"execution_count":13,"outputs":[{"execution_count":13,"output_type":"execute_result","data":{"text/plain":"      primary_label    secondary_labels               type  latitude  \\\n23346       whtkin2  [blhori1, comtai1]           ['call']   22.6920   \n9392         eucdov            [houspa]  ['adult', 'song']   51.9140   \n6852         comros           [eaywag1]           ['call']   45.5953   \n\n       longitude        scientific_name                common_name  \\\n23346    88.2970     Halcyon smyrnensis  White-throated Kingfisher   \n9392     16.8448  Streptopelia decaocto     Eurasian Collared-Dove   \n6852     64.3647  Carpodacus erythrinus           Common Rosefinch   \n\n                 author                                            license  \\\n23346        Sandip das  Creative Commons Attribution-NonCommercial-Sha...   \n9392   Ireneusz Oleksik  Creative Commons Attribution-NonCommercial-Sha...   \n6852   Albert Lastukhin  Creative Commons Attribution-NonCommercial-Sha...   \n\n       rating                                url              filename  label  \\\n23346     3.5  https://www.xeno-canto.org/633074  whtkin2/XC633074.ogg    176   \n9392      4.5       http://xeno-canto.org/669770   eucdov/XC669770.ogg     55   \n6852      2.5  https://www.xeno-canto.org/182872   comros/XC182872.ogg     43   \n\n       len_sec_labels  \n23346               2  \n9392                1  \n6852                1  ","text/html":"<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>primary_label</th>\n      <th>secondary_labels</th>\n      <th>type</th>\n      <th>latitude</th>\n      <th>longitude</th>\n      <th>scientific_name</th>\n      <th>common_name</th>\n      <th>author</th>\n      <th>license</th>\n      <th>rating</th>\n      <th>url</th>\n      <th>filename</th>\n      <th>label</th>\n      <th>len_sec_labels</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>23346</th>\n      <td>whtkin2</td>\n      <td>[blhori1, comtai1]</td>\n      <td>['call']</td>\n      <td>22.6920</td>\n      <td>88.2970</td>\n      <td>Halcyon smyrnensis</td>\n      <td>White-throated Kingfisher</td>\n      <td>Sandip das</td>\n      <td>Creative Commons Attribution-NonCommercial-Sha...</td>\n      <td>3.5</td>\n      <td>https://www.xeno-canto.org/633074</td>\n      <td>whtkin2/XC633074.ogg</td>\n      <td>176</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>9392</th>\n      <td>eucdov</td>\n      <td>[houspa]</td>\n      <td>['adult', 'song']</td>\n      <td>51.9140</td>\n      <td>16.8448</td>\n      <td>Streptopelia decaocto</td>\n      <td>Eurasian Collared-Dove</td>\n      <td>Ireneusz Oleksik</td>\n      <td>Creative Commons Attribution-NonCommercial-Sha...</td>\n      <td>4.5</td>\n      <td>http://xeno-canto.org/669770</td>\n      <td>eucdov/XC669770.ogg</td>\n      <td>55</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>6852</th>\n      <td>comros</td>\n      <td>[eaywag1]</td>\n      <td>['call']</td>\n      <td>45.5953</td>\n      <td>64.3647</td>\n      <td>Carpodacus erythrinus</td>\n      <td>Common Rosefinch</td>\n      <td>Albert Lastukhin</td>\n      <td>Creative Commons Attribution-NonCommercial-Sha...</td>\n      <td>2.5</td>\n      <td>https://www.xeno-canto.org/182872</td>\n      <td>comros/XC182872.ogg</td>\n      <td>43</td>\n      <td>1</td>\n    </tr>\n  </tbody>\n</table>\n</div>"},"metadata":{}}]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nimport pandas as pd\n\ndef birds_stratified_split(df, target_col, test_size=0.2):\n    class_counts = df[target_col].value_counts()\n    low_count_classes = class_counts[class_counts < 2].index.tolist() ### Birds with single counts\n\n    df['train'] = df[target_col].isin(low_count_classes)\n\n    train_df, val_df = train_test_split(df[~df['train']], test_size=test_size, stratify=df[~df['train']][target_col], random_state=42)\n\n    train_df = pd.concat([train_df, df[df['train']]], axis=0).reset_index(drop=True)\n\n    # Remove the 'valid' column\n    train_df.drop('train', axis=1, inplace=True)\n    val_df.drop('train', axis=1, inplace=True)\n\n    return train_df, val_df","metadata":{"execution":{"iopub.status.busy":"2024-10-21T18:36:43.283838Z","iopub.execute_input":"2024-10-21T18:36:43.284263Z","iopub.status.idle":"2024-10-21T18:36:43.291798Z","shell.execute_reply.started":"2024-10-21T18:36:43.284227Z","shell.execute_reply":"2024-10-21T18:36:43.290801Z"},"trusted":true},"execution_count":14,"outputs":[]},{"cell_type":"code","source":"train_df, valid_df = birds_stratified_split(df, 'primary_label', 0.2)\n","metadata":{"execution":{"iopub.status.busy":"2024-10-21T18:36:46.029044Z","iopub.execute_input":"2024-10-21T18:36:46.029718Z","iopub.status.idle":"2024-10-21T18:36:46.111662Z","shell.execute_reply.started":"2024-10-21T18:36:46.029677Z","shell.execute_reply":"2024-10-21T18:36:46.110897Z"},"trusted":true},"execution_count":15,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-10-21T18:36:48.468557Z","iopub.execute_input":"2024-10-21T18:36:48.469386Z","iopub.status.idle":"2024-10-21T18:36:48.489543Z","shell.execute_reply.started":"2024-10-21T18:36:48.469343Z","shell.execute_reply":"2024-10-21T18:36:48.488671Z"},"trusted":true},"execution_count":16,"outputs":[{"execution_count":16,"output_type":"execute_result","data":{"text/plain":"  primary_label  secondary_labels  \\\n0        litegr                []   \n1        spodov  [houspa, rewbul]   \n2        comsan                []   \n3       eaywag1                []   \n4        greegr                []   \n\n                                                type  latitude  longitude  \\\n0                      ['alarm call', 'flight call']   43.8559     1.7856   \n1                                           ['song']  -20.4338    57.4578   \n2                                               ['']   43.5652     4.5811   \n3                                    ['flight call']   53.5337    -1.7832   \n4  ['flight call', 'life stage uncertain', 'sex u...   51.8056    16.6515   \n\n          scientific_name             common_name            author  \\\n0        Egretta garzetta            Little Egret    Cedric Mroczko   \n1  Streptopelia chinensis            Spotted Dove    Cedric Mroczko   \n2      Actitis hypoleucos        Common Sandpiper    Manceau Lionel   \n3         Motacilla flava  Western Yellow Wagtail  David Pennington   \n4              Ardea alba             Great Egret    Grzegorz Lorek   \n\n                                             license  rating  \\\n0  Creative Commons Attribution-NonCommercial-Sha...     4.0   \n1  Creative Commons Attribution-NonCommercial-Sha...     3.5   \n2  Creative Commons Attribution-NonCommercial-Sha...     5.0   \n3  Creative Commons Attribution-NonCommercial-Sha...     4.0   \n4  Creative Commons Attribution-NonCommercial-Sha...     3.0   \n\n                                 url              filename  label  \\\n0  https://www.xeno-canto.org/576994   litegr/XC576994.ogg    106   \n1  https://www.xeno-canto.org/463317   spodov/XC463317.ogg    155   \n2      https://xeno-canto.org/764652   comsan/XC764652.ogg     44   \n3       http://xeno-canto.org/675728  eaywag1/XC675728.ogg     53   \n4       http://xeno-canto.org/678629   greegr/XC678629.ogg     65   \n\n   len_sec_labels  \n0               0  \n1               2  \n2               0  \n3               0  \n4               0  ","text/html":"<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>primary_label</th>\n      <th>secondary_labels</th>\n      <th>type</th>\n      <th>latitude</th>\n      <th>longitude</th>\n      <th>scientific_name</th>\n      <th>common_name</th>\n      <th>author</th>\n      <th>license</th>\n      <th>rating</th>\n      <th>url</th>\n      <th>filename</th>\n      <th>label</th>\n      <th>len_sec_labels</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>litegr</td>\n      <td>[]</td>\n      <td>['alarm call', 'flight call']</td>\n      <td>43.8559</td>\n      <td>1.7856</td>\n      <td>Egretta garzetta</td>\n      <td>Little Egret</td>\n      <td>Cedric Mroczko</td>\n      <td>Creative Commons Attribution-NonCommercial-Sha...</td>\n      <td>4.0</td>\n      <td>https://www.xeno-canto.org/576994</td>\n      <td>litegr/XC576994.ogg</td>\n      <td>106</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>spodov</td>\n      <td>[houspa, rewbul]</td>\n      <td>['song']</td>\n      <td>-20.4338</td>\n      <td>57.4578</td>\n      <td>Streptopelia chinensis</td>\n      <td>Spotted Dove</td>\n      <td>Cedric Mroczko</td>\n      <td>Creative Commons Attribution-NonCommercial-Sha...</td>\n      <td>3.5</td>\n      <td>https://www.xeno-canto.org/463317</td>\n      <td>spodov/XC463317.ogg</td>\n      <td>155</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>comsan</td>\n      <td>[]</td>\n      <td>['']</td>\n      <td>43.5652</td>\n      <td>4.5811</td>\n      <td>Actitis hypoleucos</td>\n      <td>Common Sandpiper</td>\n      <td>Manceau Lionel</td>\n      <td>Creative Commons Attribution-NonCommercial-Sha...</td>\n      <td>5.0</td>\n      <td>https://xeno-canto.org/764652</td>\n      <td>comsan/XC764652.ogg</td>\n      <td>44</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>eaywag1</td>\n      <td>[]</td>\n      <td>['flight call']</td>\n      <td>53.5337</td>\n      <td>-1.7832</td>\n      <td>Motacilla flava</td>\n      <td>Western Yellow Wagtail</td>\n      <td>David Pennington</td>\n      <td>Creative Commons Attribution-NonCommercial-Sha...</td>\n      <td>4.0</td>\n      <td>http://xeno-canto.org/675728</td>\n      <td>eaywag1/XC675728.ogg</td>\n      <td>53</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>greegr</td>\n      <td>[]</td>\n      <td>['flight call', 'life stage uncertain', 'sex u...</td>\n      <td>51.8056</td>\n      <td>16.6515</td>\n      <td>Ardea alba</td>\n      <td>Great Egret</td>\n      <td>Grzegorz Lorek</td>\n      <td>Creative Commons Attribution-NonCommercial-Sha...</td>\n      <td>3.0</td>\n      <td>http://xeno-canto.org/678629</td>\n      <td>greegr/XC678629.ogg</td>\n      <td>65</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"},"metadata":{}}]},{"cell_type":"code","source":"import os\n\n# Create a new directory\nos.makedirs(\"../working/specs/train\", exist_ok=True)\nos.makedirs(\"../working/specs/valid\", exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2024-10-21T18:36:58.348979Z","iopub.execute_input":"2024-10-21T18:36:58.349927Z","iopub.status.idle":"2024-10-21T18:36:58.354871Z","shell.execute_reply.started":"2024-10-21T18:36:58.349873Z","shell.execute_reply":"2024-10-21T18:36:58.353846Z"},"trusted":true},"execution_count":17,"outputs":[]},{"cell_type":"code","source":"import joblib\nfrom tqdm.notebook import tqdm\n\ndef add_path_df(df):\n    \n    df[\"path\"] = [str(Config.audios_path/filename) for filename in df.filename]\n    df = df.reset_index(drop=True)\n    pool = joblib.Parallel(2)\n    mapper = joblib.delayed(get_audio_info)\n    tasks = [mapper(filepath) for filepath in df.path]\n    df2 =  pd.DataFrame(pool(tqdm(tasks))).reset_index(drop=True)\n    df = pd.concat([df,df2], axis=1).reset_index(drop=True)\n\n    return df","metadata":{"execution":{"iopub.status.busy":"2024-10-21T18:37:00.133595Z","iopub.execute_input":"2024-10-21T18:37:00.134005Z","iopub.status.idle":"2024-10-21T18:37:00.141337Z","shell.execute_reply.started":"2024-10-21T18:37:00.133964Z","shell.execute_reply":"2024-10-21T18:37:00.14024Z"},"trusted":true},"execution_count":18,"outputs":[]},{"cell_type":"code","source":"tqdm.pandas()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = add_path_df(train_df)","metadata":{"execution":{"iopub.status.busy":"2024-10-21T18:37:03.81578Z","iopub.execute_input":"2024-10-21T18:37:03.816744Z","iopub.status.idle":"2024-10-21T18:39:49.866414Z","shell.execute_reply.started":"2024-10-21T18:37:03.816696Z","shell.execute_reply":"2024-10-21T18:39:49.865494Z"},"trusted":true},"execution_count":19,"outputs":[{"output_type":"display_data","data":{"text/plain":"  0%|          | 0/19567 [00:00<?, ?it/s]","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"52c649d2c8264b259bcb2cb158bd5b03"}},"metadata":{}},{"name":"stderr","text":"/opt/conda/lib/python3.10/site-packages/joblib/externals/loky/backend/fork_exec.py:38: RuntimeWarning: os.fork() was called. os.fork() is incompatible with multithreaded code, and JAX is multithreaded, so this will likely lead to a deadlock.\n  pid = os.fork()\n","output_type":"stream"}]},{"cell_type":"code","source":"pip install pandas librosa torch torchvision torchaudio","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport numpy as np\nimport librosa\nimport torch\nimport torch.nn as nn\nimport torchaudio\nfrom torch.utils.data import Dataset, DataLoader\nfrom sklearn.model_selection import train_test_split\nfrom tqdm import tqdm\n\n# Set the audio directory and metadata path\nAUDIO_DIR = '../input/birdclef-2024/train_audio/'\nMETADATA_PATH = '../input/birdclef-2024/train_metadata.csv'\n\n# Load the metadata\nmetadata = pd.read_csv(METADATA_PATH)\n\n# Define a function to preprocess audio\ndef preprocess_audio(file_path, target_sr=32000):\n    y, sr = librosa.load(file_path, sr=target_sr)\n    y = librosa.util.normalize(y)  # Normalize\n    return y\n\n# Custom Dataset for Bird Calls\nclass BirdCallDataset(Dataset):\n    def __init__(self, metadata):\n        self.metadata = metadata\n\n    def __len__(self):\n        return len(self.metadata)\n\n    def __getitem__(self, idx):\n        row = self.metadata.iloc[idx]\n        audio_path = os.path.join(AUDIO_DIR, row['filename'])\n        audio_data = preprocess_audio(audio_path)\n        label = row['primary_label']\n        return audio_data, label\n\n# Create DataLoader for training\ndataset = BirdCallDataset(metadata)\ntrain_dataset, val_dataset = train_test_split(dataset, test_size=0.2)\ntrain_loader = DataLoader(train_dataset, batch_size=16, shuffle=True)\nval_loader = DataLoader(val_dataset, batch_size=16)\n\n# Define a simple CNN model for audio classification\nclass SimpleCNN(nn.Module):\n    def __init__(self):\n        super(SimpleCNN, self).__init__()\n        self.conv1 = nn.Conv1d(1, 16, kernel_size=3, stride=1, padding=1)\n        self.pool = nn.MaxPool1d(2)\n        self.fc1 = nn.Linear(16 * (target_length // 2), 128)\n        self.fc2 = nn.Linear(128, len(metadata['primary_label'].unique()))  # Number of classes\n\n    def forward(self, x):\n        x = self.pool(F.relu(self.conv1(x)))\n        x = x.view(-1, 16 * (target_length // 2))\n        x = F.relu(self.fc1(x))\n        x = self.fc2(x)\n        return x\n\n# Instantiate the model, define loss and optimizer\nmodel = SimpleCNN()\ncriterion = nn.CrossEntropyLoss()\noptimizer = torch.optim.Adam(model.parameters(), lr=0.001)\n\n# Training loop\ndef train_model(model, train_loader, criterion, optimizer, num_epochs=10):\n    model.train()\n    for epoch in range(num_epochs):\n        running_loss = 0.0\n        for inputs, labels in tqdm(train_loader):\n            optimizer.zero_grad()\n            outputs = model(inputs.unsqueeze(1))  # Add channel dimension\n            loss = criterion(outputs, labels)\n            loss.backward()\n            optimizer.step()\n            running_loss += loss.item()\n        print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {running_loss / len(train_loader):.4f}')\n\n# Train the model\ntrain_model(model, train_loader, criterion, optimizer)\n\n# Sample inference code for test_soundscapes\ndef predict_on_test(model, test_loader):\n    model.eval()\n    predictions = []\n    with torch.no_grad():\n        for inputs in test_loader:\n            outputs = model(inputs.unsqueeze(1))\n            _, predicted = torch.max(outputs.data, 1)\n            predictions.extend(predicted.numpy())\n    return predictions\n\n# Assume you have a DataLoader for test data in the same structure\n# predictions = predict_on_test(model, test_loader)\n\n# To prepare your submission, save predictions in the required format\n# sample_submission = pd.read_csv('sample_submission.csv')\n# sample_submission['predictions'] = predictions\n# sample_submission.to_csv('submission.csv', index=False)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport torch\nimport torch.nn as nn\nfrom torch.utils.data import Dataset, DataLoader\nfrom sklearn.model_selection import train_test_split\nfrom transformers import Wav2Vec2ForCTC, Wav2Vec2Processor\nimport librosa\nfrom tqdm import tqdm\n\n# Check if GPU is available and set device\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nprint(device)\n\n# Set the audio directory and metadata path\nAUDIO_DIR = '../input/birdclef-2024/train_audio/'\nMETADATA_PATH = '../input/birdclef-2024/train_metadata.csv'\n\n# Load the metadata\nmetadata = pd.read_csv(METADATA_PATH)\nselected_birds = ['asbfly']\nmetadata = metadata[metadata['primary_label'].isin(selected_birds)]\n\nmetadata = metadata[:20]\n\nprint(metadata.shape)\nprint(metadata.head())\n\n# Load Wav2Vec2 processor and model\nprocessor = Wav2Vec2Processor.from_pretrained(\"facebook/wav2vec2-base-960h\")\nmodel = Wav2Vec2ForCTC.from_pretrained(\"facebook/wav2vec2-base-960h\").to(device)\n\n# Define a function to preprocess audio\ndef preprocess_audio(file_path):\n    audio_input, _ = librosa.load(file_path, sr= 32000)\n    return audio_input\n\n# Custom Dataset for Bird Calls\nclass BirdCallDataset(Dataset):\n    def __init__(self, metadata):\n        self.metadata = metadata\n        self.label_to_index = {label: idx for idx, label in enumerate(metadata['primary_label'].unique())}\n\n    def __len__(self):\n        return len(self.metadata)\n\n    def __getitem__(self, idx):\n        row = self.metadata.iloc[idx]\n        audio_path = os.path.join(AUDIO_DIR, row['filename'])\n        audio_data = preprocess_audio(audio_path)\n        inputs = processor(audio_data, sampling_rate=32000, return_tensors=\"pt\", padding=True)\n        inputs = {key: val.squeeze(0).to(device) for key, val in inputs.items()}  # Move inputs to GPU\n        label = self.label_to_index[row['primary_label']]\n        return inputs, label\n\n# Collate function to pad sequences\ndef collate_fn(batch):\n    inputs, labels = zip(*batch)\n    inputs = {key: torch.nn.utils.rnn.pad_sequence([item[key] for item in inputs], batch_first=True) for key in inputs[0]}\n    labels = torch.tensor(labels).to(device)\n    return inputs, labels\n\n# Create DataLoader for training\ndataset = BirdCallDataset(metadata)\n\nprint(dataset)\ntrain_dataset, val_dataset = train_test_split(dataset, test_size=0.2, random_state=42)\ntrain_loader = DataLoader(train_dataset, batch_size=4, shuffle=True, collate_fn=collate_fn)\nval_loader = DataLoader(val_dataset, batch_size=4, collate_fn=collate_fn)\n\n# Define a simple model for audio classification based on Wav2Vec2 features\nclass BirdClassifier(nn.Module):\n    def __init__(self):\n        super(BirdClassifier, self).__init__()\n        self.wav2vec = model\n        self.fc = nn.Linear(self.wav2vec.config.hidden_size, len(dataset.label_to_index))  # Number of classes\n\n    def forward(self, inputs):\n        outputs = self.wav2vec(inputs[\"input_values\"]).logits  # Outputs shape (batch_size, seq_length, num_classes)\n        logits = self.fc(outputs.mean(dim=1))  # Global average pooling\n        return logits\n\n# Instantiate the model, define loss and optimizer\nclassifier = BirdClassifier().to(device)\ncriterion = nn.CrossEntropyLoss()\noptimizer = torch.optim.Adam(classifier.parameters(), lr=1e-4)\n\n# Training loop\ndef train_model(model, train_loader, criterion, optimizer, num_epochs=5):\n    model.train()\n    for epoch in range(num_epochs):\n        running_loss = 0.0\n        for inputs, labels in tqdm(train_loader):\n            optimizer.zero_grad()\n            outputs = model(inputs)\n            loss = criterion(outputs, labels)\n            loss.backward()\n            optimizer.step()\n            running_loss += loss.item()\n        print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {running_loss / len(train_loader):.4f}')\n\n# Train the model\ntrain_model(classifier, train_loader, criterion, optimizer)\n\n# Sample inference code for test_soundscapes\ndef predict_on_test(model, test_loader):\n    model.eval()\n    predictions = []\n    with torch.no_grad():\n        for inputs in test_loader:\n            inputs = {key: val.to(device) for key, val in inputs.items()}  # Move inputs to GPU\n            outputs = model(inputs)\n            _, predicted = torch.max(outputs.data, 1)\n            predictions.extend(predicted.cpu().numpy())  # Move predictions back to CPU\n    return predictions\n","metadata":{"execution":{"iopub.status.busy":"2024-10-23T20:46:32.964307Z","iopub.execute_input":"2024-10-23T20:46:32.964672Z","iopub.status.idle":"2024-10-23T20:46:34.734005Z","shell.execute_reply.started":"2024-10-23T20:46:32.964637Z","shell.execute_reply":"2024-10-23T20:46:34.732751Z"},"trusted":true},"execution_count":15,"outputs":[{"name":"stdout","text":"cuda\n(20, 12)\n  primary_label secondary_labels      type  latitude  longitude  \\\n0        asbfly               []  ['call']   39.2297   118.1987   \n1        asbfly               []  ['song']   51.4030   104.6401   \n2        asbfly               []  ['song']   36.3319   127.3555   \n3        asbfly               []  ['call']   21.1697    70.6005   \n4        asbfly               []  ['call']   15.5442    73.7733   \n\n      scientific_name             common_name  \\\n0  Muscicapa dauurica  Asian Brown Flycatcher   \n1  Muscicapa dauurica  Asian Brown Flycatcher   \n2  Muscicapa dauurica  Asian Brown Flycatcher   \n3  Muscicapa dauurica  Asian Brown Flycatcher   \n4  Muscicapa dauurica  Asian Brown Flycatcher   \n\n                              author  \\\n0                     Matt Slaymaker   \n1                   Magnus Hellström   \n2                      Stuart Fisher   \n3                          vir joshi   \n4  Albert Lastukhin & Sergei Karpeev   \n\n                                             license  rating  \\\n0  Creative Commons Attribution-NonCommercial-Sha...     5.0   \n1  Creative Commons Attribution-NonCommercial-Sha...     2.5   \n2  Creative Commons Attribution-NonCommercial-Sha...     2.5   \n3  Creative Commons Attribution-NonCommercial-Sha...     4.0   \n4  Creative Commons Attribution-NonCommercial-Sha...     4.0   \n\n                                 url             filename  \n0  https://www.xeno-canto.org/134896  asbfly/XC134896.ogg  \n1  https://www.xeno-canto.org/164848  asbfly/XC164848.ogg  \n2  https://www.xeno-canto.org/175797  asbfly/XC175797.ogg  \n3  https://www.xeno-canto.org/207738  asbfly/XC207738.ogg  \n4  https://www.xeno-canto.org/209218  asbfly/XC209218.ogg  \n","output_type":"stream"},{"name":"stderr","text":"/opt/conda/lib/python3.10/site-packages/transformers/tokenization_utils_base.py:1617: FutureWarning: `clean_up_tokenization_spaces` was not set. It will be set to `True` by default. This behavior will be deprecated in transformers v4.45, and will be then set to `False` by default. For more details check this issue: https://github.com/huggingface/transformers/issues/31884\n  warnings.warn(\nSome weights of Wav2Vec2ForCTC were not initialized from the model checkpoint at facebook/wav2vec2-base-960h and are newly initialized: ['wav2vec2.masked_spec_embed']\nYou should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.\n","output_type":"stream"},{"traceback":["\u001b[0;31m---------------------------------------------------------------------------\u001b[0m","\u001b[0;31mOutOfMemoryError\u001b[0m                          Traceback (most recent call last)","Cell \u001b[0;32mIn[15], line 31\u001b[0m\n\u001b[1;32m     29\u001b[0m \u001b[38;5;66;03m# Load Wav2Vec2 processor and model\u001b[39;00m\n\u001b[1;32m     30\u001b[0m processor \u001b[38;5;241m=\u001b[39m Wav2Vec2Processor\u001b[38;5;241m.\u001b[39mfrom_pretrained(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mfacebook/wav2vec2-base-960h\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m---> 31\u001b[0m model \u001b[38;5;241m=\u001b[39m \u001b[43mWav2Vec2ForCTC\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfrom_pretrained\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mfacebook/wav2vec2-base-960h\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mto\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdevice\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m     33\u001b[0m \u001b[38;5;66;03m# Define a function to preprocess audio\u001b[39;00m\n\u001b[1;32m     34\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mpreprocess_audio\u001b[39m(file_path):\n","File \u001b[0;32m/opt/conda/lib/python3.10/site-packages/transformers/modeling_utils.py:2958\u001b[0m, in \u001b[0;36mPreTrainedModel.to\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m   2953\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m dtype_present_in_args:\n\u001b[1;32m   2954\u001b[0m         \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[1;32m   2955\u001b[0m             \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mYou cannot cast a GPTQ model in a new `dtype`. Make sure to load the model using `from_pretrained` using the desired\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m   2956\u001b[0m             \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m `dtype` by passing the correct `torch_dtype` argument.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m   2957\u001b[0m         )\n\u001b[0;32m-> 2958\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43msuper\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mto\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n","File \u001b[0;32m/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py:1174\u001b[0m, in \u001b[0;36mModule.to\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m   1171\u001b[0m         \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m   1172\u001b[0m             \u001b[38;5;28;01mraise\u001b[39;00m\n\u001b[0;32m-> 1174\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_apply\u001b[49m\u001b[43m(\u001b[49m\u001b[43mconvert\u001b[49m\u001b[43m)\u001b[49m\n","File \u001b[0;32m/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py:780\u001b[0m, in \u001b[0;36mModule._apply\u001b[0;34m(self, fn, recurse)\u001b[0m\n\u001b[1;32m    778\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m recurse:\n\u001b[1;32m    779\u001b[0m     \u001b[38;5;28;01mfor\u001b[39;00m module \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mchildren():\n\u001b[0;32m--> 780\u001b[0m         \u001b[43mmodule\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_apply\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfn\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    782\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mcompute_should_use_set_data\u001b[39m(tensor, tensor_applied):\n\u001b[1;32m    783\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m torch\u001b[38;5;241m.\u001b[39m_has_compatible_shallow_copy_type(tensor, tensor_applied):\n\u001b[1;32m    784\u001b[0m         \u001b[38;5;66;03m# If the new tensor has compatible tensor type as the existing tensor,\u001b[39;00m\n\u001b[1;32m    785\u001b[0m         \u001b[38;5;66;03m# the current behavior is to change the tensor in-place using `.data =`,\u001b[39;00m\n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m    790\u001b[0m         \u001b[38;5;66;03m# global flag to let the user control whether they want the future\u001b[39;00m\n\u001b[1;32m    791\u001b[0m         \u001b[38;5;66;03m# behavior of overwriting the existing tensor or not.\u001b[39;00m\n","File \u001b[0;32m/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py:780\u001b[0m, in \u001b[0;36mModule._apply\u001b[0;34m(self, fn, recurse)\u001b[0m\n\u001b[1;32m    778\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m recurse:\n\u001b[1;32m    779\u001b[0m     \u001b[38;5;28;01mfor\u001b[39;00m module \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mchildren():\n\u001b[0;32m--> 780\u001b[0m         \u001b[43mmodule\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_apply\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfn\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    782\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mcompute_should_use_set_data\u001b[39m(tensor, tensor_applied):\n\u001b[1;32m    783\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m torch\u001b[38;5;241m.\u001b[39m_has_compatible_shallow_copy_type(tensor, tensor_applied):\n\u001b[1;32m    784\u001b[0m         \u001b[38;5;66;03m# If the new tensor has compatible tensor type as the existing tensor,\u001b[39;00m\n\u001b[1;32m    785\u001b[0m         \u001b[38;5;66;03m# the current behavior is to change the tensor in-place using `.data =`,\u001b[39;00m\n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m    790\u001b[0m         \u001b[38;5;66;03m# global flag to let the user control whether they want the future\u001b[39;00m\n\u001b[1;32m    791\u001b[0m         \u001b[38;5;66;03m# behavior of overwriting the existing tensor or not.\u001b[39;00m\n","    \u001b[0;31m[... skipping similar frames: Module._apply at line 780 (2 times)]\u001b[0m\n","File \u001b[0;32m/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py:780\u001b[0m, in \u001b[0;36mModule._apply\u001b[0;34m(self, fn, recurse)\u001b[0m\n\u001b[1;32m    778\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m recurse:\n\u001b[1;32m    779\u001b[0m     \u001b[38;5;28;01mfor\u001b[39;00m module \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mchildren():\n\u001b[0;32m--> 780\u001b[0m         \u001b[43mmodule\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_apply\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfn\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    782\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mcompute_should_use_set_data\u001b[39m(tensor, tensor_applied):\n\u001b[1;32m    783\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m torch\u001b[38;5;241m.\u001b[39m_has_compatible_shallow_copy_type(tensor, tensor_applied):\n\u001b[1;32m    784\u001b[0m         \u001b[38;5;66;03m# If the new tensor has compatible tensor type as the existing tensor,\u001b[39;00m\n\u001b[1;32m    785\u001b[0m         \u001b[38;5;66;03m# the current behavior is to change the tensor in-place using `.data =`,\u001b[39;00m\n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m    790\u001b[0m         \u001b[38;5;66;03m# global flag to let the user control whether they want the future\u001b[39;00m\n\u001b[1;32m    791\u001b[0m         \u001b[38;5;66;03m# behavior of overwriting the existing tensor or not.\u001b[39;00m\n","File \u001b[0;32m/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py:805\u001b[0m, in \u001b[0;36mModule._apply\u001b[0;34m(self, fn, recurse)\u001b[0m\n\u001b[1;32m    801\u001b[0m \u001b[38;5;66;03m# Tensors stored in modules are graph leaves, and we don't want to\u001b[39;00m\n\u001b[1;32m    802\u001b[0m \u001b[38;5;66;03m# track autograd history of `param_applied`, so we have to use\u001b[39;00m\n\u001b[1;32m    803\u001b[0m \u001b[38;5;66;03m# `with torch.no_grad():`\u001b[39;00m\n\u001b[1;32m    804\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m torch\u001b[38;5;241m.\u001b[39mno_grad():\n\u001b[0;32m--> 805\u001b[0m     param_applied \u001b[38;5;241m=\u001b[39m \u001b[43mfn\u001b[49m\u001b[43m(\u001b[49m\u001b[43mparam\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    806\u001b[0m p_should_use_set_data \u001b[38;5;241m=\u001b[39m compute_should_use_set_data(param, param_applied)\n\u001b[1;32m    808\u001b[0m \u001b[38;5;66;03m# subclasses may have multiple child tensors so we need to use swap_tensors\u001b[39;00m\n","File \u001b[0;32m/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py:1160\u001b[0m, in \u001b[0;36mModule.to.<locals>.convert\u001b[0;34m(t)\u001b[0m\n\u001b[1;32m   1153\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m convert_to_format \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mand\u001b[39;00m t\u001b[38;5;241m.\u001b[39mdim() \u001b[38;5;129;01min\u001b[39;00m (\u001b[38;5;241m4\u001b[39m, \u001b[38;5;241m5\u001b[39m):\n\u001b[1;32m   1154\u001b[0m         \u001b[38;5;28;01mreturn\u001b[39;00m t\u001b[38;5;241m.\u001b[39mto(\n\u001b[1;32m   1155\u001b[0m             device,\n\u001b[1;32m   1156\u001b[0m             dtype \u001b[38;5;28;01mif\u001b[39;00m t\u001b[38;5;241m.\u001b[39mis_floating_point() \u001b[38;5;129;01mor\u001b[39;00m t\u001b[38;5;241m.\u001b[39mis_complex() \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[1;32m   1157\u001b[0m             non_blocking,\n\u001b[1;32m   1158\u001b[0m             memory_format\u001b[38;5;241m=\u001b[39mconvert_to_format,\n\u001b[1;32m   1159\u001b[0m         )\n\u001b[0;32m-> 1160\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mt\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mto\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m   1161\u001b[0m \u001b[43m        \u001b[49m\u001b[43mdevice\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m   1162\u001b[0m \u001b[43m        \u001b[49m\u001b[43mdtype\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mif\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mt\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mis_floating_point\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01mor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mt\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mis_complex\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01melse\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[1;32m   1163\u001b[0m \u001b[43m        \u001b[49m\u001b[43mnon_blocking\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m   1164\u001b[0m \u001b[43m    \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m   1165\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mNotImplementedError\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m   1166\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mstr\u001b[39m(e) \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCannot copy out of meta tensor; no data!\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n","\u001b[0;31mOutOfMemoryError\u001b[0m: CUDA out of memory. Tried to allocate 20.00 MiB. GPU 0 has a total capacity of 15.89 GiB of which 37.12 MiB is free. Process 2480 has 15.85 GiB memory in use. Of the allocated memory 15.50 GiB is allocated by PyTorch, and 59.72 MiB is reserved by PyTorch but unallocated. If reserved but unallocated memory is large try setting PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True to avoid fragmentation.  See documentation for Memory Management  (https://pytorch.org/docs/stable/notes/cuda.html#environment-variables)"],"ename":"OutOfMemoryError","evalue":"CUDA out of memory. Tried to allocate 20.00 MiB. GPU 0 has a total capacity of 15.89 GiB of which 37.12 MiB is free. Process 2480 has 15.85 GiB memory in use. Of the allocated memory 15.50 GiB is allocated by PyTorch, and 59.72 MiB is reserved by PyTorch but unallocated. If reserved but unallocated memory is large try setting PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True to avoid fragmentation.  See documentation for Memory Management  (https://pytorch.org/docs/stable/notes/cuda.html#environment-variables)","output_type":"error"}]},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport torch\nimport librosa\nfrom transformers import Wav2Vec2ForCTC, Wav2Vec2Processor\n\n# Check if GPU is available and set device\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nprint(device)\n\n# Set the audio directory and metadata path\nAUDIO_DIR = '../input/birdclef-2024/train_audio/'\nMETADATA_PATH = '../input/birdclef-2024/train_metadata.csv'\n\n# Load the metadata\nmetadata = pd.read_csv(METADATA_PATH)\nselected_birds = ['asbfly']\nmetadata = metadata[metadata['primary_label'].isin(selected_birds)]\n\n# Limit to first 20 samples for embeddings\nmetadata = metadata[:10]\n\nprint(metadata.shape)\nprint(metadata.head())\n\n# Load Wav2Vec2 processor and model\nprocessor = Wav2Vec2Processor.from_pretrained(\"facebook/wav2vec2-base-960h\")\nmodel = Wav2Vec2ForCTC.from_pretrained(\"facebook/wav2vec2-base-960h\").to(device)\nmodel.eval()  # Set the model to evaluation mode\n\n# Define a function to preprocess audio and generate embeddings for a batch\ndef generate_embeddings_batch(file_paths):\n    embeddings_list = []\n    \n    for file_path in file_paths:\n        # Load audio file\n        audio_input, _ = librosa.load(file_path, sr=31000)\n        \n        # Process audio with Wav2Vec2 processor\n        inputs = processor(audio_input, sampling_rate=32000, return_tensors=\"pt\", padding=True)\n        \n        # Move inputs to the device (GPU/CPU)\n        inputs = {key: val.to(device) for key, val in inputs.items()}\n        \n        # Generate embeddings\n        with torch.no_grad():\n            outputs = model(**inputs)\n        \n        # Get the last hidden state\n        embeddings = outputs.last_hidden_state  # Shape: (batch_size, seq_length, hidden_size)\n        embeddings_list.append(embeddings)\n    \n    return torch.cat(embeddings_list, dim=0)  # Concatenate embeddings for the batch\n\n# Generate embeddings for all audio files in batches\nbatch_size = 2  # Adjust the batch size based on your GPU memory\nall_embeddings = []\n\nfor i in range(0, len(metadata), batch_size):\n    batch_files = [os.path.join(AUDIO_DIR, row['filename']) for idx, row in metadata.iloc[i:i+batch_size].iterrows()]\n    embeddings = generate_embeddings_batch(batch_files)\n    all_embeddings.append(embeddings)\n\n# Concatenate all embeddings into a single tensor\nall_embeddings = torch.cat(all_embeddings, dim=0)\n\nprint(f\"Generated embeddings shape: {all_embeddings.shape}\")\n","metadata":{"execution":{"iopub.status.busy":"2024-10-23T20:55:50.433764Z","iopub.execute_input":"2024-10-23T20:55:50.434281Z","iopub.status.idle":"2024-10-23T20:55:52.254131Z","shell.execute_reply.started":"2024-10-23T20:55:50.434242Z","shell.execute_reply":"2024-10-23T20:55:52.252812Z"},"trusted":true},"execution_count":23,"outputs":[{"name":"stdout","text":"cuda\n(10, 12)\n  primary_label secondary_labels      type  latitude  longitude  \\\n0        asbfly               []  ['call']   39.2297   118.1987   \n1        asbfly               []  ['song']   51.4030   104.6401   \n2        asbfly               []  ['song']   36.3319   127.3555   \n3        asbfly               []  ['call']   21.1697    70.6005   \n4        asbfly               []  ['call']   15.5442    73.7733   \n\n      scientific_name             common_name  \\\n0  Muscicapa dauurica  Asian Brown Flycatcher   \n1  Muscicapa dauurica  Asian Brown Flycatcher   \n2  Muscicapa dauurica  Asian Brown Flycatcher   \n3  Muscicapa dauurica  Asian Brown Flycatcher   \n4  Muscicapa dauurica  Asian Brown Flycatcher   \n\n                              author  \\\n0                     Matt Slaymaker   \n1                   Magnus Hellström   \n2                      Stuart Fisher   \n3                          vir joshi   \n4  Albert Lastukhin & Sergei Karpeev   \n\n                                             license  rating  \\\n0  Creative Commons Attribution-NonCommercial-Sha...     5.0   \n1  Creative Commons Attribution-NonCommercial-Sha...     2.5   \n2  Creative Commons Attribution-NonCommercial-Sha...     2.5   \n3  Creative Commons Attribution-NonCommercial-Sha...     4.0   \n4  Creative Commons Attribution-NonCommercial-Sha...     4.0   \n\n                                 url             filename  \n0  https://www.xeno-canto.org/134896  asbfly/XC134896.ogg  \n1  https://www.xeno-canto.org/164848  asbfly/XC164848.ogg  \n2  https://www.xeno-canto.org/175797  asbfly/XC175797.ogg  \n3  https://www.xeno-canto.org/207738  asbfly/XC207738.ogg  \n4  https://www.xeno-canto.org/209218  asbfly/XC209218.ogg  \n","output_type":"stream"},{"name":"stderr","text":"/opt/conda/lib/python3.10/site-packages/transformers/tokenization_utils_base.py:1617: FutureWarning: `clean_up_tokenization_spaces` was not set. It will be set to `True` by default. This behavior will be deprecated in transformers v4.45, and will be then set to `False` by default. For more details check this issue: https://github.com/huggingface/transformers/issues/31884\n  warnings.warn(\nSome weights of Wav2Vec2ForCTC were not initialized from the model checkpoint at facebook/wav2vec2-base-960h and are newly initialized: ['wav2vec2.masked_spec_embed']\nYou should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.\n","output_type":"stream"},{"traceback":["\u001b[0;31m---------------------------------------------------------------------------\u001b[0m","\u001b[0;31mOutOfMemoryError\u001b[0m                          Traceback (most recent call last)","Cell \u001b[0;32mIn[23], line 28\u001b[0m\n\u001b[1;32m     26\u001b[0m \u001b[38;5;66;03m# Load Wav2Vec2 processor and model\u001b[39;00m\n\u001b[1;32m     27\u001b[0m processor \u001b[38;5;241m=\u001b[39m Wav2Vec2Processor\u001b[38;5;241m.\u001b[39mfrom_pretrained(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mfacebook/wav2vec2-base-960h\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m---> 28\u001b[0m model \u001b[38;5;241m=\u001b[39m \u001b[43mWav2Vec2ForCTC\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfrom_pretrained\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mfacebook/wav2vec2-base-960h\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mto\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdevice\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m     29\u001b[0m model\u001b[38;5;241m.\u001b[39meval()  \u001b[38;5;66;03m# Set the model to evaluation mode\u001b[39;00m\n\u001b[1;32m     31\u001b[0m \u001b[38;5;66;03m# Define a function to preprocess audio and generate embeddings for a batch\u001b[39;00m\n","File \u001b[0;32m/opt/conda/lib/python3.10/site-packages/transformers/modeling_utils.py:2958\u001b[0m, in \u001b[0;36mPreTrainedModel.to\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m   2953\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m dtype_present_in_args:\n\u001b[1;32m   2954\u001b[0m         \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[1;32m   2955\u001b[0m             \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mYou cannot cast a GPTQ model in a new `dtype`. Make sure to load the model using `from_pretrained` using the desired\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m   2956\u001b[0m             \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m `dtype` by passing the correct `torch_dtype` argument.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m   2957\u001b[0m         )\n\u001b[0;32m-> 2958\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43msuper\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mto\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n","File \u001b[0;32m/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py:1174\u001b[0m, in \u001b[0;36mModule.to\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m   1171\u001b[0m         \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m   1172\u001b[0m             \u001b[38;5;28;01mraise\u001b[39;00m\n\u001b[0;32m-> 1174\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_apply\u001b[49m\u001b[43m(\u001b[49m\u001b[43mconvert\u001b[49m\u001b[43m)\u001b[49m\n","File \u001b[0;32m/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py:780\u001b[0m, in \u001b[0;36mModule._apply\u001b[0;34m(self, fn, recurse)\u001b[0m\n\u001b[1;32m    778\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m recurse:\n\u001b[1;32m    779\u001b[0m     \u001b[38;5;28;01mfor\u001b[39;00m module \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mchildren():\n\u001b[0;32m--> 780\u001b[0m         \u001b[43mmodule\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_apply\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfn\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    782\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mcompute_should_use_set_data\u001b[39m(tensor, tensor_applied):\n\u001b[1;32m    783\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m torch\u001b[38;5;241m.\u001b[39m_has_compatible_shallow_copy_type(tensor, tensor_applied):\n\u001b[1;32m    784\u001b[0m         \u001b[38;5;66;03m# If the new tensor has compatible tensor type as the existing tensor,\u001b[39;00m\n\u001b[1;32m    785\u001b[0m         \u001b[38;5;66;03m# the current behavior is to change the tensor in-place using `.data =`,\u001b[39;00m\n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m    790\u001b[0m         \u001b[38;5;66;03m# global flag to let the user control whether they want the future\u001b[39;00m\n\u001b[1;32m    791\u001b[0m         \u001b[38;5;66;03m# behavior of overwriting the existing tensor or not.\u001b[39;00m\n","File \u001b[0;32m/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py:780\u001b[0m, in \u001b[0;36mModule._apply\u001b[0;34m(self, fn, recurse)\u001b[0m\n\u001b[1;32m    778\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m recurse:\n\u001b[1;32m    779\u001b[0m     \u001b[38;5;28;01mfor\u001b[39;00m module \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mchildren():\n\u001b[0;32m--> 780\u001b[0m         \u001b[43mmodule\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_apply\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfn\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    782\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mcompute_should_use_set_data\u001b[39m(tensor, tensor_applied):\n\u001b[1;32m    783\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m torch\u001b[38;5;241m.\u001b[39m_has_compatible_shallow_copy_type(tensor, tensor_applied):\n\u001b[1;32m    784\u001b[0m         \u001b[38;5;66;03m# If the new tensor has compatible tensor type as the existing tensor,\u001b[39;00m\n\u001b[1;32m    785\u001b[0m         \u001b[38;5;66;03m# the current behavior is to change the tensor in-place using `.data =`,\u001b[39;00m\n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m    790\u001b[0m         \u001b[38;5;66;03m# global flag to let the user control whether they want the future\u001b[39;00m\n\u001b[1;32m    791\u001b[0m         \u001b[38;5;66;03m# behavior of overwriting the existing tensor or not.\u001b[39;00m\n","    \u001b[0;31m[... skipping similar frames: Module._apply at line 780 (2 times)]\u001b[0m\n","File \u001b[0;32m/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py:780\u001b[0m, in \u001b[0;36mModule._apply\u001b[0;34m(self, fn, recurse)\u001b[0m\n\u001b[1;32m    778\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m recurse:\n\u001b[1;32m    779\u001b[0m     \u001b[38;5;28;01mfor\u001b[39;00m module \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mchildren():\n\u001b[0;32m--> 780\u001b[0m         \u001b[43mmodule\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_apply\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfn\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    782\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mcompute_should_use_set_data\u001b[39m(tensor, tensor_applied):\n\u001b[1;32m    783\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m torch\u001b[38;5;241m.\u001b[39m_has_compatible_shallow_copy_type(tensor, tensor_applied):\n\u001b[1;32m    784\u001b[0m         \u001b[38;5;66;03m# If the new tensor has compatible tensor type as the existing tensor,\u001b[39;00m\n\u001b[1;32m    785\u001b[0m         \u001b[38;5;66;03m# the current behavior is to change the tensor in-place using `.data =`,\u001b[39;00m\n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m    790\u001b[0m         \u001b[38;5;66;03m# global flag to let the user control whether they want the future\u001b[39;00m\n\u001b[1;32m    791\u001b[0m         \u001b[38;5;66;03m# behavior of overwriting the existing tensor or not.\u001b[39;00m\n","File \u001b[0;32m/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py:805\u001b[0m, in \u001b[0;36mModule._apply\u001b[0;34m(self, fn, recurse)\u001b[0m\n\u001b[1;32m    801\u001b[0m \u001b[38;5;66;03m# Tensors stored in modules are graph leaves, and we don't want to\u001b[39;00m\n\u001b[1;32m    802\u001b[0m \u001b[38;5;66;03m# track autograd history of `param_applied`, so we have to use\u001b[39;00m\n\u001b[1;32m    803\u001b[0m \u001b[38;5;66;03m# `with torch.no_grad():`\u001b[39;00m\n\u001b[1;32m    804\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m torch\u001b[38;5;241m.\u001b[39mno_grad():\n\u001b[0;32m--> 805\u001b[0m     param_applied \u001b[38;5;241m=\u001b[39m \u001b[43mfn\u001b[49m\u001b[43m(\u001b[49m\u001b[43mparam\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    806\u001b[0m p_should_use_set_data \u001b[38;5;241m=\u001b[39m compute_should_use_set_data(param, param_applied)\n\u001b[1;32m    808\u001b[0m \u001b[38;5;66;03m# subclasses may have multiple child tensors so we need to use swap_tensors\u001b[39;00m\n","File \u001b[0;32m/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py:1160\u001b[0m, in \u001b[0;36mModule.to.<locals>.convert\u001b[0;34m(t)\u001b[0m\n\u001b[1;32m   1153\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m convert_to_format \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;129;01mand\u001b[39;00m t\u001b[38;5;241m.\u001b[39mdim() \u001b[38;5;129;01min\u001b[39;00m (\u001b[38;5;241m4\u001b[39m, \u001b[38;5;241m5\u001b[39m):\n\u001b[1;32m   1154\u001b[0m         \u001b[38;5;28;01mreturn\u001b[39;00m t\u001b[38;5;241m.\u001b[39mto(\n\u001b[1;32m   1155\u001b[0m             device,\n\u001b[1;32m   1156\u001b[0m             dtype \u001b[38;5;28;01mif\u001b[39;00m t\u001b[38;5;241m.\u001b[39mis_floating_point() \u001b[38;5;129;01mor\u001b[39;00m t\u001b[38;5;241m.\u001b[39mis_complex() \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[1;32m   1157\u001b[0m             non_blocking,\n\u001b[1;32m   1158\u001b[0m             memory_format\u001b[38;5;241m=\u001b[39mconvert_to_format,\n\u001b[1;32m   1159\u001b[0m         )\n\u001b[0;32m-> 1160\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mt\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mto\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m   1161\u001b[0m \u001b[43m        \u001b[49m\u001b[43mdevice\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m   1162\u001b[0m \u001b[43m        \u001b[49m\u001b[43mdtype\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mif\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mt\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mis_floating_point\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01mor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mt\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mis_complex\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01melse\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[1;32m   1163\u001b[0m \u001b[43m        \u001b[49m\u001b[43mnon_blocking\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m   1164\u001b[0m \u001b[43m    \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m   1165\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mNotImplementedError\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m   1166\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mstr\u001b[39m(e) \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCannot copy out of meta tensor; no data!\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n","\u001b[0;31mOutOfMemoryError\u001b[0m: CUDA out of memory. Tried to allocate 20.00 MiB. GPU 0 has a total capacity of 15.89 GiB of which 37.12 MiB is free. Process 2480 has 15.85 GiB memory in use. Of the allocated memory 15.50 GiB is allocated by PyTorch, and 59.58 MiB is reserved by PyTorch but unallocated. If reserved but unallocated memory is large try setting PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True to avoid fragmentation.  See documentation for Memory Management  (https://pytorch.org/docs/stable/notes/cuda.html#environment-variables)"],"ename":"OutOfMemoryError","evalue":"CUDA out of memory. Tried to allocate 20.00 MiB. GPU 0 has a total capacity of 15.89 GiB of which 37.12 MiB is free. Process 2480 has 15.85 GiB memory in use. Of the allocated memory 15.50 GiB is allocated by PyTorch, and 59.58 MiB is reserved by PyTorch but unallocated. If reserved but unallocated memory is large try setting PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True to avoid fragmentation.  See documentation for Memory Management  (https://pytorch.org/docs/stable/notes/cuda.html#environment-variables)","output_type":"error"}]}]}