{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n\n# Taking a look at the classes\nTRAIN_DATA_PATH = '../input/birdclef-2022/train_audio/'\nbirds = os.listdir(TRAIN_DATA_PATH)\nprint(\"# Birds: \",len(birds))\nimport json\nscored_birds = json.loads(open('../input/birdclef-2022/scored_birds.json', 'r').read())\nprint(\"# Scored birds: \", len(scored_birds))\nbird_frequencies = {}\nAUDIO_PATHS = {}\nfor bird_path in birds:\n    AUDIO_PATHS[bird_path] = [os.path.join(TRAIN_DATA_PATH, bird_path, i) for i in os.listdir(os.path.join(TRAIN_DATA_PATH, bird_path))]\n    bird_frequencies[bird_path] = len(AUDIO_PATHS[bird_path])\nprint(\"# of total data points:\", sum(bird_frequencies.values()))\nprint(\"# of data points for scored species: \", sum([bird_frequencies[i] for i in scored_birds]))\nsns.barplot(x=[bird_frequencies[i] for i in scored_birds], y=scored_birds)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-03-09T21:01:15.693090Z","iopub.execute_input":"2022-03-09T21:01:15.693523Z","iopub.status.idle":"2022-03-09T21:01:19.106590Z","shell.execute_reply.started":"2022-03-09T21:01:15.693492Z","shell.execute_reply":"2022-03-09T21:01:19.105744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.histplot(bird_frequencies.values())\nplt.title(\"Song # distribution - all birds\")","metadata":{"execution":{"iopub.status.busy":"2022-03-09T21:01:19.108338Z","iopub.execute_input":"2022-03-09T21:01:19.109061Z","iopub.status.idle":"2022-03-09T21:01:19.410806Z","shell.execute_reply.started":"2022-03-09T21:01:19.109016Z","shell.execute_reply":"2022-03-09T21:01:19.410025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.histplot([bird_frequencies[i] for i in scored_birds])\nplt.title(\"Song # distribution - scored birds\")","metadata":{"execution":{"iopub.status.busy":"2022-03-09T21:01:19.412033Z","iopub.execute_input":"2022-03-09T21:01:19.412314Z","iopub.status.idle":"2022-03-09T21:01:19.671313Z","shell.execute_reply.started":"2022-03-09T21:01:19.412283Z","shell.execute_reply":"2022-03-09T21:01:19.670305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Visualizing songs\n\nimport librosa, random\nfrom librosa import display\nAUDIO_FILE = random.choice(AUDIO_PATHS[\"skylar\"])\nsamples, sample_rate = librosa.load(AUDIO_FILE, sr=None)\nprint(\"Sample rate:\", sample_rate)\nlibrosa.display.waveshow(samples, sr=sample_rate)\nplt.show()\nfrom IPython.display import Audio\nAudio(AUDIO_FILE)","metadata":{"execution":{"iopub.status.busy":"2022-03-09T21:01:19.673116Z","iopub.execute_input":"2022-03-09T21:01:19.673388Z","iopub.status.idle":"2022-03-09T21:01:21.781697Z","shell.execute_reply.started":"2022-03-09T21:01:19.673358Z","shell.execute_reply":"2022-03-09T21:01:21.780922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Fourier transform and mel spectrogram\nimport numpy as np\nn_fft = 2048\nsns.lineplot(data=np.abs(librosa.stft(samples[:n_fft], n_fft=n_fft, hop_length=n_fft+1)))\nplt.show()\nS = librosa.feature.melspectrogram(samples, sr=sample_rate, n_fft=2048, hop_length=512, n_mels=128)\nS_DB = librosa.power_to_db(S, ref=np.max)\nlibrosa.display.specshow(S_DB, sr=sample_rate, hop_length=512, x_axis='time', y_axis='mel')\nplt.colorbar(format='%+2.0f dB')","metadata":{"execution":{"iopub.status.busy":"2022-03-09T21:01:21.783111Z","iopub.execute_input":"2022-03-09T21:01:21.783424Z","iopub.status.idle":"2022-03-09T21:01:22.834477Z","shell.execute_reply.started":"2022-03-09T21:01:21.783388Z","shell.execute_reply":"2022-03-09T21:01:22.833529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Note: There's often lots of background sound! Noise removal / augmentation to increase model\n# noise tolerance may be an avenue to explore.","metadata":{"execution":{"iopub.status.busy":"2022-03-09T21:01:22.835986Z","iopub.execute_input":"2022-03-09T21:01:22.836243Z","iopub.status.idle":"2022-03-09T21:01:22.840416Z","shell.execute_reply.started":"2022-03-09T21:01:22.836214Z","shell.execute_reply":"2022-03-09T21:01:22.839395Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Analyzing song lengths\nimport collections, tqdm\nimport pandas as pd\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\n# Create and store mel spectrograms\ndef scale_minmax(X, min=0.0, max=1.0):\n    X_std = (X - X.min()) / (X.max() - X.min())\n    X_scaled = X_std * (max - min) + min\n    return X_scaled\n    \nbird_song_lengths = collections.defaultdict(list)\nimgs, labels = [], []\nset_scored_birds = set(scored_birds)\nfor bird in tqdm.tqdm(birds):\n    for AUDIO_FILE in AUDIO_PATHS[bird]:\n        samples, sample_rate = librosa.load(AUDIO_FILE, sr=None)\n        S = librosa.feature.melspectrogram(samples, sr=sample_rate, n_fft=2048, hop_length=512, n_mels=128)\n        S_DB = librosa.power_to_db(S, ref=np.max)\n        img = scale_minmax(S_DB, 0, 255).astype(np.uint8)\n        imgs.append(img)\n        \n        #if bird in set_scored_birds: labels.append(bird)\n        #else: labels.append(\"OTHER BIRD\")\n        labels.append(bird)\n            \n        bird_song_lengths[bird].append(len(samples) / sample_rate)","metadata":{"execution":{"iopub.status.busy":"2022-03-09T21:24:54.728513Z","iopub.execute_input":"2022-03-09T21:24:54.729264Z","iopub.status.idle":"2022-03-09T21:24:56.417219Z","shell.execute_reply.started":"2022-03-09T21:24:54.729208Z","shell.execute_reply":"2022-03-09T21:24:56.416013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame(list(zip(list(range(len(imgs))), labels)), columns =['Image','Label'])\ndf.to_csv(\"labels.csv\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir mel_spectrogram_imgs\n%cd mel_spectrogram_imgs\nfor i in range(len(imgs)):\n    with open(\"imgs%d.npy\"%i, \"wb\") as f:\n        np.save(f, imgs[i])","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def mean(li):\n    return sum(li) / len(li)\n\nsns.histplot([mean(i) for i in bird_song_lengths.values()]); plt.title(\"Mean song length distribution across species.\")\nplt.show()\nsns.barplot(y = scored_birds, x = [mean(bird_song_lengths[bird]) for bird in scored_birds])\nplt.title(\"Mean song length across scored birds.\")","metadata":{"execution":{"iopub.status.busy":"2022-03-09T21:24:21.729134Z","iopub.execute_input":"2022-03-09T21:24:21.729417Z","iopub.status.idle":"2022-03-09T21:24:21.988129Z","shell.execute_reply.started":"2022-03-09T21:24:21.729387Z","shell.execute_reply":"2022-03-09T21:24:21.986974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Shortest bird song (s):\",min([min(i) for i in bird_song_lengths.values()]))\nprint(\"Longest bird song (s):\",max([max(i) for i in bird_song_lengths.values()]))","metadata":{"execution":{"iopub.status.busy":"2022-03-09T21:25:01.847121Z","iopub.execute_input":"2022-03-09T21:25:01.847428Z","iopub.status.idle":"2022-03-09T21:25:01.853599Z","shell.execute_reply.started":"2022-03-09T21:25:01.847393Z","shell.execute_reply":"2022-03-09T21:25:01.852948Z"},"trusted":true},"execution_count":null,"outputs":[]}]}